-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use go version bls implementation (#39)
* replace github.com/herumi/bls-go-binary with github.com/consensys/gnark-crypto for bls
- Loading branch information
Showing
5 changed files
with
137 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,100 @@ | ||
package bls | ||
|
||
import "github.com/herumi/bls-go-binary/bls" | ||
import ( | ||
"encoding/hex" | ||
bls "github.com/consensys/gnark-crypto/ecc/bls12-381" | ||
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr" | ||
"math/big" | ||
) | ||
|
||
const ( | ||
dstG1 = "BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_" | ||
publicKeyGenerator = "1 0x24aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8 0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e 0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801 0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be" | ||
dstG1 = "BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_" | ||
) | ||
|
||
func init() { | ||
if err := bls.Init(bls.BLS12_381); err != nil { | ||
panic(err) | ||
} | ||
|
||
// Set Ethereum serialization format. | ||
bls.SetETHserialization(true) | ||
if err := bls.SetMapToMode(bls.IRTF); err != nil { | ||
panic(err) | ||
} | ||
|
||
// Set the generator of G2. see https://www.ietf.org/archive/id/draft-irtf-cfrg-pairing-friendly-curves-11.html#section-4.2.1 | ||
var gen bls.PublicKey | ||
if err := gen.SetHexString(publicKeyGenerator); err != nil { | ||
panic(err) | ||
} | ||
if err := bls.SetGeneratorOfPublicKey(&gen); err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := bls.SetDstG1(dstG1); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
type PublicKey = bls.PublicKey | ||
type PublicKey bls.G2Affine | ||
|
||
// PublicKeyFromBytes returns a PublicKey from a byte slice. | ||
func PublicKeyFromBytes(b []byte) (*PublicKey, error) { | ||
var pub bls.PublicKey | ||
return &pub, pub.Deserialize(b) | ||
var pub bls.G2Affine | ||
err := pub.Unmarshal(b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res := PublicKey(pub) | ||
return &res, err | ||
} | ||
|
||
// PublicKeyFromHexString returns a PublicKey from a hex string. | ||
func PublicKeyFromHexString(s string) (*PublicKey, error) { | ||
var pub bls.PublicKey | ||
return &pub, pub.DeserializeHexStr(s) | ||
b, err := hex.DecodeString(s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return PublicKeyFromBytes(b) | ||
} | ||
|
||
type SecretKey = bls.SecretKey | ||
type SecretKey fr.Element | ||
|
||
// NewSecretKeyByCSPRNG returns a new SecretKey generated by CSPRNG. | ||
func NewSecretKeyByCSPRNG() *SecretKey { | ||
var sk bls.SecretKey | ||
sk.SetByCSPRNG() | ||
return &sk | ||
var sk fr.Element | ||
_, err := sk.SetRandom() | ||
if err != nil { | ||
return nil | ||
} | ||
res := SecretKey(sk) | ||
return &res | ||
} | ||
|
||
func (sk *SecretKey) PublicKey() *PublicKey { | ||
_, _, _, g2Gen := bls.Generators() | ||
|
||
el := fr.Element(*sk) | ||
v := el.BigInt(big.NewInt(0)) | ||
|
||
pk := g2Gen.ScalarMultiplication(&g2Gen, v) | ||
return (*PublicKey)(pk) | ||
} | ||
|
||
func (sk *SecretKey) Sign(msg []byte) *Signature { | ||
el := fr.Element(*sk) | ||
v := el.BigInt(big.NewInt(0)) | ||
g1, _ := bls.HashToG1(msg, []byte(dstG1)) | ||
sig := g1.ScalarMultiplication(&g1, v) | ||
return (*Signature)(sig) | ||
} | ||
|
||
type Signature = bls.Sign | ||
type Signature bls.G1Affine | ||
|
||
// SignatureFromBytes returns a Signature from a byte slice. | ||
func SignatureFromBytes(b []byte) (*Signature, error) { | ||
var sig bls.Sign | ||
return &sig, sig.Deserialize(b) | ||
var sig bls.G1Affine | ||
_, err := sig.SetBytes(b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res := Signature(sig) | ||
return &res, err | ||
} | ||
|
||
// SignatureFromHexString returns a Signature from a hex string. | ||
func SignatureFromHexString(s string) (*Signature, error) { | ||
var sig bls.Sign | ||
return &sig, sig.DeserializeHexStr(s) | ||
b, err := hex.DecodeString(s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return SignatureFromBytes(b) | ||
} | ||
|
||
func (sig *Signature) Verify(pk *PublicKey, msg []byte) bool { | ||
g1, _ := bls.HashToG1(msg, []byte(dstG1)) | ||
_, _, _, g2 := bls.Generators() | ||
var g2n bls.G2Affine | ||
g2n.Neg(&g2) | ||
|
||
valid, err := bls.PairingCheck([]bls.G1Affine{bls.G1Affine(*sig), g1}, []bls.G2Affine{g2n, bls.G2Affine(*pk)}) | ||
if err != nil { | ||
return false | ||
} | ||
return valid | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters