Skip to content

Commit

Permalink
Merge pull request #27 from ecies/cgo-support
Browse files Browse the repository at this point in the history
CGO secp256k1 support added, more mature version of Go native version…
  • Loading branch information
savely-krasovsky authored Feb 2, 2022
2 parents b8b2562 + e9d6976 commit 11dcd55
Show file tree
Hide file tree
Showing 8 changed files with 599 additions and 23 deletions.
20 changes: 16 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,29 @@ jobs:
with:
go-version: 1.17

- name: Build
- name: Build w/ CGO
run: GOOS=linux go build

- name: Build w/o CGO
run: GOOS=linux CGO_ENABLED=0 go build

- name: WASM Build
run: GOOS=js GOARCH=wasm go build

- name: Test decompression
- name: Test decompression w/ CGO
run: go test -v -run TestPublicKeyDecompression -count 100

- name: Test
- name: Test decompression w/o CGO
run: CGO_ENABLED=0 go test -v -run TestPublicKeyDecompression -count 100

- name: Test w/ CGO
run: go test -v ./...

- name: Test race
- name: Test w/o CGO
run: CGO_ENABLED=0 go test -v ./...

- name: Test race w/ CGO
run: go test -race -v ./...

- name: Test race w/o CGO
run: go test -tags=ecies_test_race -race -v ./...
4 changes: 1 addition & 3 deletions ecies.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"crypto/rand"
"fmt"
"math/big"

"github.com/fomichev/secp256k1"
)

// Encrypt encrypts a passed message with a receiver public key, returns ciphertext or encryption error
Expand Down Expand Up @@ -66,7 +64,7 @@ func Decrypt(privkey *PrivateKey, msg []byte) ([]byte, error) {

// Ephemeral sender public key
ethPubkey := &PublicKey{
Curve: secp256k1.SECP256K1(),
Curve: getCurve(),
X: new(big.Int).SetBytes(msg[1:33]),
Y: new(big.Int).SetBytes(msg[33:65]),
}
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
module github.com/ecies/go/v2

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fomichev/secp256k1 v0.0.0-20180413221153-00116ff8c62f
github.com/kr/pretty v0.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1
github.com/ethereum/go-ethereum v1.10.15
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)

go 1.13
554 changes: 549 additions & 5 deletions go.sum

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions privatekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"encoding/hex"
"fmt"
"math/big"

"github.com/fomichev/secp256k1"
)

// PrivateKey is an instance of secp256k1 private key with nested public key
Expand All @@ -20,7 +18,7 @@ type PrivateKey struct {

// GenerateKey generates secp256k1 key pair
func GenerateKey() (*PrivateKey, error) {
curve := secp256k1.SECP256K1()
curve := getCurve()

p, x, y, err := elliptic.GenerateKey(curve, rand.Reader)
if err != nil {
Expand Down Expand Up @@ -49,7 +47,7 @@ func NewPrivateKeyFromHex(s string) (*PrivateKey, error) {

// NewPrivateKeyFromBytes decodes private key raw bytes, computes public key and returns PrivateKey instance
func NewPrivateKeyFromBytes(priv []byte) *PrivateKey {
curve := secp256k1.SECP256K1()
curve := getCurve()
x, y := curve.ScalarBaseMult(priv)

return &PrivateKey{
Expand Down
4 changes: 1 addition & 3 deletions publickey.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"encoding/hex"
"fmt"
"math/big"

"github.com/fomichev/secp256k1"
)

// PublicKey instance with nested elliptic.Curve interface (secp256k1 instance in our case)
Expand All @@ -30,7 +28,7 @@ func NewPublicKeyFromHex(s string) (*PublicKey, error) {
// NewPublicKeyFromBytes decodes public key raw bytes and returns PublicKey instance;
// Supports both compressed and uncompressed public keys
func NewPublicKeyFromBytes(b []byte) (*PublicKey, error) {
curve := secp256k1.SECP256K1()
curve := getCurve()

switch b[0] {
case 0x02, 0x03:
Expand Down
14 changes: 14 additions & 0 deletions secp256k1_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build cgo && !ecies_test_race
// +build cgo,!ecies_test_race

package eciesgo

import (
"crypto/elliptic"

"github.com/ethereum/go-ethereum/crypto/secp256k1"
)

func getCurve() elliptic.Curve {
return secp256k1.S256()
}
14 changes: 14 additions & 0 deletions secp256k1_nocgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build !cgo || ecies_test_race
// +build !cgo ecies_test_race

package eciesgo

import (
"crypto/elliptic"

"github.com/decred/dcrd/dcrec/secp256k1/v4"
)

func getCurve() elliptic.Curve {
return secp256k1.S256()
}

0 comments on commit 11dcd55

Please sign in to comment.