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