arnaucube
arnaucube/go-circom-prover-verifierPublic Self-hosted repository
go-circom-prover-verifier/types/types.go
types/types.goRaw
 1package types
 2
 3import (
 4	"encoding/hex"
 5	"encoding/json"
 6	"math/big"
 7
 8	bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
 9)
10
11// R is the mod of the finite field
12var R, _ = new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
13
14// Proof is the data structure of the Groth16 zkSNARK proof
15type Proof struct {
16	A *bn256.G1
17	B *bn256.G2
18	C *bn256.G1
19}
20
21type proofAux struct {
22	A string `json:"pi_a"`
23	B string `json:"pi_b"`
24	C string `json:"pi_c"`
25}
26
27func (p Proof) MarshalJSON() ([]byte, error) {
28	var pa proofAux
29	pa.A = hex.EncodeToString(p.A.Marshal())
30	pa.B = hex.EncodeToString(p.B.Marshal())
31	pa.C = hex.EncodeToString(p.C.Marshal())
32	return json.Marshal(pa)
33}
34
35func (p *Proof) UnmarshalJSON(data []byte) error {
36	var pa proofAux
37	if err := json.Unmarshal(data, &pa); err != nil {
38		return err
39	}
40	aBytes, err := hex.DecodeString(pa.A)
41	if err != nil {
42		return err
43	}
44	p.A = new(bn256.G1)
45	if _, err := p.A.Unmarshal(aBytes); err != nil {
46		return err
47	}
48	bBytes, err := hex.DecodeString(pa.B)
49	if err != nil {
50		return err
51	}
52	p.B = new(bn256.G2)
53	if _, err := p.B.Unmarshal(bBytes); err != nil {
54		return err
55	}
56	cBytes, err := hex.DecodeString(pa.C)
57	if err != nil {
58		return err
59	}
60	p.C = new(bn256.G1)
61	if _, err := p.C.Unmarshal(cBytes); err != nil {
62		return err
63	}
64	return nil
65}
66
67// Pk holds the data structure of the ProvingKey
68type Pk struct {
69	A          []*bn256.G1
70	B2         []*bn256.G2
71	B1         []*bn256.G1
72	C          []*bn256.G1
73	NVars      int
74	NPublic    int
75	VkAlpha1   *bn256.G1
76	VkDelta1   *bn256.G1
77	VkBeta1    *bn256.G1
78	VkBeta2    *bn256.G2
79	VkDelta2   *bn256.G2
80	HExps      []*bn256.G1
81	DomainSize int
82	PolsA      []map[int]*big.Int
83	PolsB      []map[int]*big.Int
84	PolsC      []map[int]*big.Int
85}
86
87// Witness contains the witness
88type Witness []*big.Int
89
90// Vk is the Verification Key data structure
91type Vk struct {
92	Alpha *bn256.G1
93	Beta  *bn256.G2
94	Gamma *bn256.G2
95	Delta *bn256.G2
96	IC    []*bn256.G1
97}