-
Notifications
You must be signed in to change notification settings - Fork 10
/
additionproof.go
159 lines (137 loc) · 4.89 KB
/
additionproof.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package keyproof
import (
"strings"
"github.com/privacybydesign/gabi/big"
"github.com/privacybydesign/gabi/zkproof"
)
type (
additionProofStructure struct {
a1 string
a2 string
mod string
result string
myname string
addRepresentation zkproof.RepresentationProofStructure
addRange rangeProofStructure
}
AdditionProof struct {
ModAddProof Proof
HiderProof Proof
RangeProof RangeProof
}
additionProofCommit struct {
modAdd secret
hider secret
rangeCommit rangeCommit
}
)
func newAdditionProofStructure(a1, a2, mod, result string, l uint) additionProofStructure {
structure := additionProofStructure{
a1: a1,
a2: a2,
mod: mod,
result: result,
myname: strings.Join([]string{a1, a2, mod, result, "add"}, "_"),
}
structure.addRepresentation = zkproof.RepresentationProofStructure{
Lhs: []zkproof.LhsContribution{
{Base: result, Power: big.NewInt(1)},
{Base: a1, Power: big.NewInt(-1)},
{Base: a2, Power: big.NewInt(-1)},
},
Rhs: []zkproof.RhsContribution{
{Base: mod, Secret: strings.Join([]string{structure.myname, "mod"}, "_"), Power: 1},
{Base: "h", Secret: strings.Join([]string{structure.myname, "hider"}, "_"), Power: 1},
},
}
structure.addRange = rangeProofStructure{
structure.addRepresentation,
strings.Join([]string{structure.myname, "mod"}, "_"),
0,
l,
}
return structure
}
func (s *additionProofStructure) commitmentsFromSecrets(g zkproof.Group, list []*big.Int, bases zkproof.BaseLookup, secretdata zkproof.SecretLookup) ([]*big.Int, additionProofCommit) {
var commit additionProofCommit
// Generate needed commit data
commit.modAdd = newSecret(g, strings.Join([]string{s.myname, "mod"}, "_"),
new(big.Int).Div(
new(big.Int).Sub(
secretdata.Secret(s.result),
new(big.Int).Add(
secretdata.Secret(s.a1),
secretdata.Secret(s.a2))),
secretdata.Secret(s.mod)))
commit.hider = newSecret(g, strings.Join([]string{s.myname, "hider"}, "_"),
new(big.Int).Mod(
new(big.Int).Sub(
secretdata.Secret(strings.Join([]string{s.result, "hider"}, "_")),
new(big.Int).Add(
new(big.Int).Add(
secretdata.Secret(strings.Join([]string{s.a1, "hider"}, "_")),
secretdata.Secret(strings.Join([]string{s.a2, "hider"}, "_"))),
new(big.Int).Mul(
secretdata.Secret(strings.Join([]string{s.mod, "hider"}, "_")),
commit.modAdd.secretv))),
g.Order))
// build inner secrets
secrets := zkproof.NewSecretMerge(&commit.hider, &commit.modAdd, secretdata)
// and build commits
list = s.addRepresentation.CommitmentsFromSecrets(g, list, bases, &secrets)
list, commit.rangeCommit = s.addRange.commitmentsFromSecrets(g, list, bases, &secrets)
return list, commit
}
func (s *additionProofStructure) buildProof(g zkproof.Group, challenge *big.Int, commit additionProofCommit, secretdata zkproof.SecretLookup) AdditionProof {
rangeSecrets := zkproof.NewSecretMerge(&commit.hider, &commit.modAdd, secretdata)
return AdditionProof{
RangeProof: s.addRange.buildProof(g, challenge, commit.rangeCommit, &rangeSecrets),
ModAddProof: commit.modAdd.buildProof(g, challenge),
HiderProof: commit.hider.buildProof(g, challenge),
}
}
func (s *additionProofStructure) fakeProof(g zkproof.Group) AdditionProof {
return AdditionProof{
RangeProof: s.addRange.fakeProof(g),
ModAddProof: fakeProof(g),
HiderProof: fakeProof(g),
}
}
func (s *additionProofStructure) verifyProofStructure(proof AdditionProof) bool {
if !s.addRange.verifyProofStructure(proof.RangeProof) {
return false
}
if !proof.HiderProof.verifyStructure() || !proof.ModAddProof.verifyStructure() {
return false
}
return true
}
func (s *additionProofStructure) commitmentsFromProof(g zkproof.Group, list []*big.Int, challenge *big.Int, bases zkproof.BaseLookup, proofdata zkproof.ProofLookup, proof AdditionProof) []*big.Int {
// build inner proof lookup
proof.ModAddProof.setName(strings.Join([]string{s.myname, "mod"}, "_"))
proof.HiderProof.setName(strings.Join([]string{s.myname, "hider"}, "_"))
proofs := zkproof.NewProofMerge(&proof.HiderProof, &proof.ModAddProof, proofdata)
// build commitments
list = s.addRepresentation.CommitmentsFromProof(g, list, challenge, bases, &proofs)
list = s.addRange.commitmentsFromProof(g, list, challenge, bases, proof.RangeProof)
return list
}
func (s *additionProofStructure) isTrue(secretdata zkproof.SecretLookup) bool {
div := new(big.Int)
mod := new(big.Int)
div.DivMod(
new(big.Int).Sub(
secretdata.Secret(s.result),
new(big.Int).Add(
secretdata.Secret(s.a1),
secretdata.Secret(s.a2))),
secretdata.Secret(s.mod),
mod)
return mod.Cmp(big.NewInt(0)) == 0 && uint(div.BitLen()) <= s.addRange.l2
}
func (s *additionProofStructure) numRangeProofs() int {
return 1
}
func (s *additionProofStructure) numCommitments() int {
return s.addRepresentation.NumCommitments() + s.addRange.numCommitments()
}