-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzkp.go
121 lines (105 loc) · 2.47 KB
/
zkp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package crypto
import (
"fmt"
"math/big"
)
// nolint
const (
ST_ONE = 1 + iota
ST_OUTPUT
ST_PUBINPUT
ST_PRVINPUT
ST_INTERNAL //除1、输入和输出外的骨干信号
ST_DISCARDED //边缘信号,总是依附于某个骨干信号; 无用的信号
ST_CONSTANT //常量信号,即被赋值为N的信号
)
// zkp algo
const (
Groth16 = 0x00
AlgoTypeOffset = 32
)
// algo name
const (
AlgoGroth16 = "groth16"
AlgoPlonk = "plonk"
)
// VerifyVersion1 version for evm unmarshal
const (
VerifyVersion1 = 0x01 << iota
)
// ProofVersion1 version for proof unmarshal
const (
ProofVersion1 = 0x01 << iota
)
// ChainType chain type
type ChainType string
// chain type enum
const (
VMEvm ChainType = "EVM"
VMChainCode ChainType = "ChainCode"
)
// ProverInfo witness
type ProverInfo map[string][]*big.Int
// R1CSIterator r1cs iterator
type R1CSIterator interface {
ConstraintsNum() uint64
Next(pos *int) (index int, a, b, c map[int]FieldElement)
GetSignalByID(nowid int) (id, reoffset int, prefix string, value FieldElement)
}
// R1CS r1cs
type R1CS interface {
fmt.Stringer
R1CSIterator
SignalNum() int //m
InputNum() (privateInput, publicInput int)
OutputNum() int // l = publicInput + OutputNum() + 1 = m - privateInput
Marshal() []byte
GetPairing() Pairing
}
// ProveKey zk-SNARKs algorithm prove key
type ProveKey interface {
Prove(cs R1CS, witness []FieldElement, mpc MPC) ([]byte, error)
GetSnark() string
GetPairing() string
GetVKTag() string
Marshaller
}
// VerifyProofKey vk
type VerifyProofKey interface {
Verify(in []byte, limit string, mpc MPC) error
GetSnark() string
GetPairing() string
Marshaller
}
// MPC mpc
type MPC interface {
New(curve string, power uint) []byte
Contribute(entropy, name string) error
Verify() (bool, error)
Beacon(string, int) error
GetPower() int
GetG1() []Point
GetG2() []Point
GetPairing() string
GetHash() string
VerifyWithHistory(srsBefore MPC) (bool, error)
Marshaller
}
// Snarks zk-SNARKS
type Snarks interface {
Name() string
Setup(r1cs R1CS) (ProveKey, VerifyProofKey, error)
GenCode(p, v []byte, circuitID [32]byte, t ChainType) []byte
UnmarshalVK(in []byte) (VerifyProofKey, error)
UnmarshalPK(in []byte) (ProveKey, error)
}
// VCMetrics metrics for prove and verify
type VCMetrics interface {
UpdateGetDataTime(s, e int64)
UpdateWaitTime(s, e int64)
UpdateProveTime(s, e int64)
UpdateVerifyTime(s, e int64)
UpdateComputerTime(s, e int64)
UpdateG1TPS(s, e int64, n int)
UpdateG2TPS(s, e int64, n int)
}