-
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.
Replace BLS w/ non-cgo based module.
- Loading branch information
Showing
6 changed files
with
167 additions
and
105 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,101 @@ | ||
package bls | ||
|
||
import "github.com/herumi/bls-go-binary/bls" | ||
|
||
const ( | ||
dstG1 = "BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_" | ||
publicKeyGenerator = "1 0x24aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8 0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e 0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801 0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be" | ||
import ( | ||
"encoding/hex" | ||
"github.com/consensys/gnark-crypto/ecc/bls12-381" | ||
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr" | ||
"math/big" | ||
) | ||
|
||
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) | ||
} | ||
const dstG1 = "BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_" | ||
|
||
// 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) | ||
} | ||
var ( | ||
g2, g2Gen bls12381.G2Affine | ||
) | ||
|
||
if err := bls.SetDstG1(dstG1); err != nil { | ||
panic(err) | ||
} | ||
func init() { | ||
_, _, _, g2Gen = bls12381.Generators() | ||
g2.Neg(&g2Gen) | ||
} | ||
|
||
type PublicKey = bls.PublicKey | ||
type PublicKey bls12381.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 g2 bls12381.G2Affine | ||
if err := g2.Unmarshal(b); err != nil { | ||
return nil, err | ||
} | ||
return (*PublicKey)(&g2), nil | ||
} | ||
|
||
// PublicKeyFromHexString returns a PublicKey from a hex string. | ||
func PublicKeyFromHexString(s string) (*PublicKey, error) { | ||
var pub bls.PublicKey | ||
return &pub, pub.DeserializeHexStr(s) | ||
raw, err := hex.DecodeString(s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return PublicKeyFromBytes(raw) | ||
} | ||
|
||
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 e fr.Element | ||
if _, err := e.SetRandom(); err != nil { | ||
return nil | ||
} | ||
return (*SecretKey)(&e) | ||
} | ||
|
||
func (sk *SecretKey) GetPublicKey() *PublicKey { | ||
e := fr.Element(*sk) | ||
pk := g2Gen.ScalarMultiplication( | ||
&g2Gen, | ||
e.BigInt(big.NewInt(0)), | ||
) | ||
return (*PublicKey)(pk) | ||
} | ||
|
||
func (sk *SecretKey) Sign(msg []byte) *Signature { | ||
e := fr.Element(*sk) | ||
g1, _ := bls12381.HashToG1(msg, []byte(dstG1)) | ||
sig := g1.ScalarMultiplication( | ||
&g1, | ||
e.BigInt(big.NewInt(0)), | ||
) | ||
return (*Signature)(sig) | ||
} | ||
|
||
type Signature = bls.Sign | ||
type Signature bls12381.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 bls12381.G1Affine | ||
if _, err := sig.SetBytes(b); err != nil { | ||
return nil, err | ||
} | ||
return (*Signature)(&sig), nil | ||
} | ||
|
||
// SignatureFromHexString returns a Signature from a hex string. | ||
func SignatureFromHexString(s string) (*Signature, error) { | ||
var sig bls.Sign | ||
return &sig, sig.DeserializeHexStr(s) | ||
raw, err := hex.DecodeString(s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return SignatureFromBytes(raw) | ||
} | ||
|
||
// Verify returns true if the signature is valid for the message. | ||
func (sig *Signature) Verify(pk *PublicKey, msg []byte) bool { | ||
g1, _ := bls12381.HashToG1(msg, []byte(dstG1)) | ||
valid, err := bls12381.PairingCheck( | ||
[]bls12381.G1Affine{bls12381.G1Affine(*sig), g1}, | ||
[]bls12381.G2Affine{g2, bls12381.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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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