Browse Source

stringToBigInt return err instead of panic

master
arnaucube 4 years ago
parent
commit
cd1285876a
1 changed files with 19 additions and 7 deletions
  1. +19
    -7
      verifier.go

+ 19
- 7
verifier.go

@ -11,6 +11,16 @@ import (
bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare" bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
) )
var q *big.Int
func init() {
var err error
q, err = stringToBigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617")
if err != nil {
panic(err)
}
}
// Vk is the Verification Key data structure // Vk is the Verification Key data structure
type Vk struct { type Vk struct {
Alpha *bn256.G1 Alpha *bn256.G1
@ -93,7 +103,7 @@ func hexToG2(h [][]string) (*bn256.G2, error) {
return p, err return p, err
} }
func stringToBigInt(s string) *big.Int {
func stringToBigInt(s string) (*big.Int, error) {
base := 10 base := 10
if bytes.HasPrefix([]byte(s), []byte("0x")) { if bytes.HasPrefix([]byte(s), []byte("0x")) {
base = 16 base = 16
@ -101,9 +111,9 @@ func stringToBigInt(s string) *big.Int {
} }
n, ok := new(big.Int).SetString(s, base) n, ok := new(big.Int).SetString(s, base)
if !ok { if !ok {
panic(fmt.Errorf("Can not parse string to *big.Int: %s", s))
return nil, fmt.Errorf("Can not parse string to *big.Int: %s", s)
} }
return n
return n, nil
} }
// ParsePublicRaw takes a json []byte and outputs the []*big.Int struct // ParsePublicRaw takes a json []byte and outputs the []*big.Int struct
@ -115,7 +125,11 @@ func ParsePublicRaw(pj []byte) ([]*big.Int, error) {
} }
var public []*big.Int var public []*big.Int
for _, s := range pr { for _, s := range pr {
public = append(public, stringToBigInt(s))
sb, err := stringToBigInt(s)
if err != nil {
return nil, err
}
public = append(public, sb)
} }
return public, nil return public, nil
} }
@ -197,15 +211,13 @@ func proofRawToProof(pr ProofRaw) (*Proof, error) {
return &p, nil return &p, nil
} }
var q = stringToBigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617")
// Verify performs the Groth16 zkSnark verification // Verify performs the Groth16 zkSnark verification
func Verify(vk *Vk, proof *Proof, inputs []*big.Int) bool { func Verify(vk *Vk, proof *Proof, inputs []*big.Int) bool {
if len(inputs)+1 != len(vk.GammaABC) { if len(inputs)+1 != len(vk.GammaABC) {
fmt.Println("len(inputs)+1 != len(vk.GammaABC)") fmt.Println("len(inputs)+1 != len(vk.GammaABC)")
return false return false
} }
vkX := new(bn256.G1).ScalarBaseMult(stringToBigInt("0"))
vkX := new(bn256.G1).ScalarBaseMult(big.NewInt(0))
for i := 0; i < len(inputs); i++ { for i := 0; i < len(inputs); i++ {
// check input inside field // check input inside field
if inputs[0].Cmp(q) != -1 { if inputs[0].Cmp(q) != -1 {

Loading…
Cancel
Save