Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,16 @@ jobs:
cache: true
- name: run Go tidy
run: make go-tidy
- name: Run golangci-lint with cgo
- name: Run golangci-lint
env:
CGO_ENABLED: 1
uses: golangci/golangci-lint-action@v8
with:
version: ${{ env.LINT_VERSION }}
# https://github.com/golangci/golangci-lint-action/issues/244
skip-cache: true
- name: Run golangci-lint without cgo
env:
CGO_ENABLED: 0
uses: golangci/golangci-lint-action@v8
with:
version: ${{ env.LINT_VERSION }}
args: --build-tags no_cgo
# https://github.com/golangci/golangci-lint-action/issues/244
skip-cache: true
- name: Run Go Fix
run: make go-fix
- name: Run incorrect builds
run: |
echo "::remove-matcher owner=go::"
make incorrect_builds

c-code:
strategy:
Expand Down
11 changes: 0 additions & 11 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@ linters:
- linters:
- govet
text: "unsafeptr" # disable flagging unsafeptr usage
# ecdsa.go wraps crypto/ecdsa, whose Sign/Verify still consume the raw
# PrivateKey.D and PublicKey.X/Y fields.
# Go 1.26 deprecated direct access to those fields, but the recommended
# replacement API (ecdsa.ParseRawPrivateKey / ParseUncompressedPublicKey /
# (*PrivateKey).Bytes / (*PublicKey).Bytes) supports only the NIST curves and
# rejects secp256k1, which this package supports via btcec's custom
# elliptic.Curve, so the package has to keep using the low-level fields.
- path: (^|/)ecdsa(_test)?\.go$
linters:
- staticcheck
text: "SA1019"
formatters:
exclusions:
paths:
Expand Down
8 changes: 0 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,7 @@ go-lint: go-tidy go-fix
test:
# root package
CGO_ENABLED=1 CGO_CFLAGS=$(ADX_FLAG) go test -coverprofile=$(COVER_PROFILE) $(RACE_FLAG) $(if $(JSON_OUTPUT),-json,) $(if $(VERBOSE),-v,)
#root package without cgo
CGO_ENABLED=0 go test -tags=no_cgo -coverprofile=$(COVER_PROFILE) $(RACE_FLAG) $(if $(JSON_OUTPUT),-json,) $(if $(VERBOSE),-v,)
# sub packages
go test -coverprofile=$(COVER_PROFILE) $(RACE_FLAG) $(if $(JSON_OUTPUT),-json,) $(if $(VERBOSE),-v,) ./hash
go test -coverprofile=$(COVER_PROFILE) $(RACE_FLAG) $(if $(JSON_OUTPUT),-json,) $(if $(VERBOSE),-v,) ./random

# test incorrect builds and make sure they fail
.PHONY: incorrect_builds
incorrect_builds:
# both tests should fail
! CGO_ENABLED=0 go test
! CGO_ENABLED=1 CGO_CFLAGS=$(ADX_FLAG) go test -tags=no_cgo
30 changes: 6 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ import "github.com/onflow/crypto"

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 `CGO_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.
Expand All @@ -53,19 +48,6 @@ GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc CGO_ENABLED=1 go build

When using the `go mod vendor` command in your project, [a known issue](https://github.com/golang/go/issues/26366) with the Go vendoring tool prevents cgo dependencies from being copied into your vendor directory. This results in build errors related to the Flow crypto package. External vendoring tools that do copy the entire package files can be used instead of the Go command to resolve the issue.


### 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](#algorithms) and [protocols](#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 MAC:
Expand All @@ -83,10 +65,10 @@ All signature schemes use the generic interfaces of `PrivateKey` and `PublicKey`

* ECDSA
* public keys are compressed or uncompressed.
* ephemeral key is derived from the private key, hash and the system entropy (based on https://golang.org/pkg/crypto/ecdsa/).
* supports NIST P-256 (secp256r1) and secp256k1 curves.
* For NIST P-256, ephemeral key is derived from the private key, hash and the system entropy (based on https://golang.org/pkg/crypto/ecdsa/). For secp256k1, ephemeral key is deterministically formed following RFC 6979 (based on github.com/ethereum/go-ethereum/crypto/secp256k1)

* BLS (requires cgo)
* BLS
* supports [BLS12-381](https://electriccoin.co/blog/new-snark-curve/) curve.
* is implementing the minimal-signature-size variant:
signatures in G1 and public keys in G2.
Expand Down Expand Up @@ -114,7 +96,7 @@ All signature schemes use the generic interfaces of `PrivateKey` and `PublicKey`

### Threshold Signature

* BLS-based threshold signature (requires cgo)
* BLS-based threshold signature
* [non interactive](https://www.iacr.org/archive/pkc2003/25670031/25670031.pdf) threshold signature reconstruction.
* supports only BLS 12-381 curve with the same features above.
* (t+1) signatures are required to reconstruct the threshold signature.
Expand All @@ -126,16 +108,16 @@ All signature schemes use the generic interfaces of `PrivateKey` and `PublicKey`

All supported Distributed Key Generation protocols are [discrete log based](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.50.2737&rep=rep1&type=pdf) 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.

* Feldman VSS (requires cgo)
* Feldman VSS
* simple verifiable secret sharing with a single dealer.
* the library does not implement the communication channels between participants. The caller should implement the methods `PrivateSend` (1-to-1 messaging) and `Broadcast` (1-to-n messaging)
* 1-to-1 messaging must be a private channel, the caller must make sure the channel preserves confidentialiy and authenticates the sender.
* 1-to-n broadcasting is a reliable broadcast, where honest senders are able to reach all honest receivers, and where all honest receivers end up with the same received messages. The channel should also authenticate the broadcaster.
* It is recommended that both communication channels are unique per protocol instance. This could be achieved by prepending the messages to send/broadcast by a unique protocol instance ID.
* Feldman VSS Qual (requires cgo)
* Feldman VSS Qual
* an extension of the simple Feldman VSS.
* implements a complaint mechanism to qualify/disqualify the dealer.
* Joint Feldman (Pedersen) (requires cgo)
* Joint Feldman (Pedersen)
* distributed generation.
* based on parallel instances of Feldman VSS Qual, each with a different dealer.
* same assumptions about the communication channels as in Feldman VSS.
2 changes: 0 additions & 2 deletions bls.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo && !no_cgo

/*
* Flow Crypto
*
Expand Down
6 changes: 4 additions & 2 deletions bls12381_utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo && !no_cgo

/*
* Flow Crypto
*
Expand Down Expand Up @@ -117,6 +115,10 @@ func initBLS12381() {
// set a global point to infinity
C.E2_set_infty((*C.E2)(&g2PublicKey.point))
g2PublicKey.isIdentity = true

blsInstance = &blsBLS12381Algo{
algo: BLSBLS12381,
}
}

// String returns a hex-encoded representation of the scalar.
Expand Down
2 changes: 0 additions & 2 deletions bls12381_utils_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo && !no_cgo

/*
* Flow Crypto
*
Expand Down
2 changes: 0 additions & 2 deletions bls_crossBLST_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo && !no_cgo

/*
* Flow Crypto
*
Expand Down
2 changes: 0 additions & 2 deletions bls_multisig.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo && !no_cgo

/*
* Flow Crypto
*
Expand Down
2 changes: 0 additions & 2 deletions bls_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo && !no_cgo

/*
* Flow Crypto
*
Expand Down
2 changes: 0 additions & 2 deletions bls_thresholdsign.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo && !no_cgo

/*
* Flow Crypto
*
Expand Down
2 changes: 0 additions & 2 deletions bls_thresholdsign_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo && !no_cgo

/*
* Flow Crypto
*
Expand Down
2 changes: 1 addition & 1 deletion blst_src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The folder contains:
To upgrade the BLST version:
- [ ] audit all BLST updates, with focus on `<blst>/src`: https://github.com/supranational/blst/compare/v0.3.14...<new_version>
- [ ] delete all files in this folder `./blst_src/` but `blst_src.c` and `README.md`.
- [ ] delete all files in `./internal/blst/` but `non_cgo.go`.
- [ ] delete all files in `./internal/blst/`.
- [ ] open BLST repository on the new version.
- [ ] copy all `.c` and `.h` files from `<blst>/src/` into `./blst_src/`.
- [ ] delete newly copied `./blst_src/server.c`.
Expand Down
2 changes: 0 additions & 2 deletions dkg_feldmanvss.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo && !no_cgo

/*
* Flow Crypto
*
Expand Down
2 changes: 0 additions & 2 deletions dkg_feldmanvssq.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo && !no_cgo

/*
* Flow Crypto
*
Expand Down
2 changes: 0 additions & 2 deletions dkg_jointfeldman.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo && !no_cgo

/*
* Flow Crypto
*
Expand Down
2 changes: 0 additions & 2 deletions dkg_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo && !no_cgo

/*
* Flow Crypto
*
Expand Down
Loading
Loading