Home

Awesome

Flow Cryptography

The Flow crypto Go module provides the cryptography tools needed by the Flow blockchain. The primitives and protocols can be used in other projects and are not specific to Flow.

Notes:

Module import

Flow cryptography can be imported as any other Go package and does not require extra setup or pre-build (it used to require a pre-build up to version v0.24.9):

get the package

go get github.com/onflow/crypto

import the package into your Go code

import "github.com/onflow/crypto"

Build

Building your project with Flow crypto and enabling all the supported algorithms requires using cgo to compile the C code underneath. If cgo isn't enabled by default, the GCO_ENABLED environment variable should be set to 1. It is also possible to build without cgo (CGO_ENABLED=0) but this would disable some primitives (the ones related to BLS).

Build with cgo

Building with cgo is required to support all the algorithms of the module, including the algorithms based on the BLS12-381 curve.

If the test or target application crashes with a "Caught SIGILL" exception, rebuild with CGO_CFLAGS set to "-O2 -D__BLST_PORTABLE__" to disable non-portable code. The runtime error can happen if the CPU doesn't support certain instructions. Building with this flag results in a slower performance, it is therefore recommended to not use it when possible for an optimal performance.

CGO_CFLAGS="-O2 -D__BLST_PORTABLE__" go build 

If you're cross-compiling, you need to set the CC environment variable to the target C cross-compiler and set CGO_ENABLED to 1. You also need to set the GOOS and GOARCH variables.For example, to compile the test program for linux arm64:

GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc CGO_ENABLED=1 go build

Build without cgo

It is possible to build without cgo but this requires disabling all primitives based on the BLS12-381 curve (BLS signature, BLS threshold signature, BLS-based DKG, BLS-based SPoCK). Refer to algorithms and protocols to check the supported features. Calling any of the non-supported primitives would panic. In order to avoid accidental builds that result in unwanted crashes, disabling cgo must be confirmed with the no_cgo build tag.

CGO_ENABLED=0 go build -tags=no_cgo

Algorithms

Hashing and Message Authentication Code:

crypto/hash provides the hashing and MAC algorithms required for Flow. All algorithm implement the generic interface Hasher. All digests are of the generic type Hash.

Signature schemes

All signature schemes use the generic interfaces of PrivateKey and PublicKey. All signatures are of the generic type Signature.

PRNG

Protocols

Threshold Signature

Discrete-Log based distributed key generation

All supported Distributed Key Generation protocols are discrete log based and are implemented for the same BLS setup on the BLS 12-381 curve. The protocols generate key sets for the BLS-based threshold signature.