Skip to content

Commit

Permalink
Use geth's in-tree BLS again after v1.14.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PlasmaPower committed Oct 29, 2024
1 parent 6257261 commit 8f4998c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
22 changes: 12 additions & 10 deletions blsSignatures/blsSignatures.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ import (
cryptorand "crypto/rand"
"encoding/base64"
"errors"
"math/big"

"github.com/ethereum/go-ethereum/crypto"
bls12381 "github.com/kilic/bls12-381"
"github.com/ethereum/go-ethereum/crypto/bls12381"
)

type PublicKey struct {
key *bls12381.PointG2
validityProof *bls12381.PointG1 // if this is nil, key came from a trusted source
}

type PrivateKey *bls12381.Fr
type PrivateKey *big.Int

type Signature *bls12381.PointG1

func GeneratePrivKeyString() (string, error) {
fr := bls12381.NewFr()
privKey, err := fr.Rand(cryptorand.Reader)
g2 := bls12381.NewG2()
privKey, err := cryptorand.Int(cryptorand.Reader, g2.Q())
if err != nil {
return "", err
}
Expand All @@ -33,8 +35,8 @@ func GeneratePrivKeyString() (string, error) {
}

func GenerateKeys() (PublicKey, PrivateKey, error) {
fr := bls12381.NewFr()
privateKey, err := fr.Rand(cryptorand.Reader)
g2 := bls12381.NewG2()
privateKey, err := cryptorand.Int(cryptorand.Reader, g2.Q())
if err != nil {
return PublicKey{}, nil, err
}
Expand Down Expand Up @@ -118,7 +120,7 @@ func verifySignature2(sig Signature, message []byte, publicKey PublicKey, keyVal
return false, err
}

engine := bls12381.NewEngine()
engine := bls12381.NewPairingEngine()
engine.Reset()
engine.AddPair(pointOnCurve, publicKey.key)
leftSide := engine.Result()
Expand Down Expand Up @@ -154,7 +156,7 @@ func VerifyAggregatedSignatureDifferentMessages(sig Signature, messages [][]byte
if len(messages) != len(pubKeys) {
return false, errors.New("len(messages) does not match (len(pub keys) in verification")
}
engine := bls12381.NewEngine()
engine := bls12381.NewPairingEngine()
engine.Reset()
for i, msg := range messages {
pointOnCurve, err := hashToG1Curve(msg, false)
Expand Down Expand Up @@ -240,11 +242,11 @@ func PublicKeyFromBytes(in []byte, trustedSource bool) (PublicKey, error) {
}

func PrivateKeyToBytes(priv PrivateKey) []byte {
return bls12381.NewFr().Set(priv).ToBytes()
return ((*big.Int)(priv)).Bytes()
}

func PrivateKeyFromBytes(in []byte) (PrivateKey, error) {
return bls12381.NewFr().FromBytes(in), nil
return new(big.Int).SetBytes(in), nil
}

func SignatureToBytes(sig Signature) []byte {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ require (
github.com/google/uuid v1.6.0
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/holiman/uint256 v1.2.4
github.com/kilic/bls12-381 v0.1.0
github.com/knadh/koanf v1.4.0
github.com/mailru/easygo v0.0.0-20190618140210-3c14a0dc985f
github.com/mitchellh/mapstructure v1.4.1
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,6 @@ github.com/juju/utils v0.0.0-20180808125547-9dfc6dbfb02b/go.mod h1:6/KLg8Wz/y2KV
github.com/juju/version v0.0.0-20161031051906-1f41e27e54f2/go.mod h1:kE8gK5X0CImdr7qpSKl3xB2PmpySSmfj7zVbkZFs81U=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kilic/bls12-381 v0.1.0 h1:encrdjqKMEvabVQ7qYOKu1OvhqpK4s47wDYtNiPtlp4=
github.com/kilic/bls12-381 v0.1.0/go.mod h1:vDTTHJONJ6G+P2R74EhnyotQDTliQDnFEwhdmfzw1ig=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4=
Expand Down Expand Up @@ -810,7 +808,6 @@ golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201101102859-da207088b7d1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down

0 comments on commit 8f4998c

Please sign in to comment.