go-circom-prover-verifier/parsers.go
parsers.goRaw
1package gocircomprover
2
3import (
4 "encoding/hex"
5 "fmt"
6 "math/big"
7 "strings"
8
9 bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
10)
11
12func stringToG1(h []string) (*bn256.G1, error) {
13 if len(h) <= 2 {
14 return nil, fmt.Errorf("not enought data for stringToG1")
15 }
16 h = h[:2]
17 hexa := false
18 if len(h[0]) > 1 {
19 if "0x" == h[0][:2] {
20 hexa = true
21 }
22 }
23 in := ""
24
25 var b []byte
26 var err error
27 if hexa {
28 for i := range h {
29 in += strings.TrimPrefix(h[i], "0x")
30 }
31 b, err = hex.DecodeString(in)
32 if err != nil {
33 return nil, err
34 }
35 } else {
36 // TODO TMP
37 // TODO use stringToBytes()
38 if h[0] == "1" {
39 h[0] = "0"
40 }
41 if h[1] == "1" {
42 h[1] = "0"
43 }
44 bi0, ok := new(big.Int).SetString(h[0], 10)
45 if !ok {
46 return nil, fmt.Errorf("error parsing stringToG1")
47 }
48 bi1, ok := new(big.Int).SetString(h[1], 10)
49 if !ok {
50 return nil, fmt.Errorf("error parsing stringToG1")
51 }
52 b0 := bi0.Bytes()
53 b1 := bi1.Bytes()
54 if len(b0) != 32 {
55 b0 = addZPadding(b0)
56 }
57 if len(b1) != 32 {
58 b1 = addZPadding(b1)
59 }
60
61 b = append(b, b0...)
62 b = append(b, b1...)
63 }
64 p := new(bn256.G1)
65 _, err = p.Unmarshal(b)
66
67 return p, err
68}