Skip to content

Commit 9be19a9

Browse files
committed
adding reciprocal range proof
1 parent 135f7a8 commit 9be19a9

7 files changed

+390
-29
lines changed

circuit.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ func VerifyCircuit(public *ArithmeticCircuitPublic, V []*bn256.G1, fs FiatShamir
2525
fs.AddPoint(proof.CR)
2626
fs.AddPoint(proof.CO)
2727

28+
for i := range V {
29+
fs.AddPoint(V[i])
30+
}
31+
2832
// Generates challenges using Fiat-Shamir heuristic
2933
ro := fs.GetChallenge()
3034
lambda := fs.GetChallenge()
@@ -147,11 +151,19 @@ func VerifyCircuit(public *ArithmeticCircuitPublic, V []*bn256.G1, fs FiatShamir
147151

148152
// ProveCircuit generates zero knowledge proof that witness satisfies BP++ arithmetic circuit.
149153
// Use empty FiatShamirEngine for call.
150-
func ProveCircuit(public *ArithmeticCircuitPublic, fs FiatShamirEngine, private *ArithmeticCircuitPrivate) *ArithmeticCircuitProof {
154+
func ProveCircuit(public *ArithmeticCircuitPublic, V []*bn256.G1, fs FiatShamirEngine, private *ArithmeticCircuitPrivate) *ArithmeticCircuitProof {
151155
ro, rl, no, nl, lo, ll, Co, Cl := commitOL(public, private.Wo, private.Wl)
152156

153157
rr, nr, lr, Cr := commitR(public, private.Wo, private.Wr)
154158

159+
fs.AddPoint(Cl)
160+
fs.AddPoint(Cr)
161+
fs.AddPoint(Co)
162+
163+
for i := range V {
164+
fs.AddPoint(V[i])
165+
}
166+
155167
return innerArithmeticCircuitProve(public, fs, private,
156168
[][]*big.Int{rl, rr, ro},
157169
[][]*big.Int{nl, nr, no},
@@ -186,7 +198,7 @@ func commitOL(public *ArithmeticCircuitPublic, wo, wl []*big.Int) (ro []*big.Int
186198
}
187199

188200
ll = make([]*big.Int, public.Nv) // Nv
189-
for j := range lo {
201+
for j := range ll {
190202
ll[j] = big.NewInt(0)
191203

192204
if i := public.F(PartitionLL, j); i != nil {
@@ -247,10 +259,6 @@ func innerArithmeticCircuitProve(public *ArithmeticCircuitPublic, fs FiatShamirE
247259
CO: Co,
248260
}
249261

250-
fs.AddPoint(Cl)
251-
fs.AddPoint(Cr)
252-
fs.AddPoint(Co)
253-
254262
// Generates challenges using Fiat-Shamir heuristic
255263
rho := fs.GetChallenge()
256264
lambda := fs.GetChallenge()
@@ -260,6 +268,15 @@ func innerArithmeticCircuitProve(public *ArithmeticCircuitPublic, fs FiatShamirE
260268
MlnL, MmnL, MlnR, MmnR := calculateMRL(public)
261269
MlnO, MmnO, MllL, MmlL, MllR, MmlR, MllO, MmlO := calculateMO(public)
262270

271+
// Check M matrix calculated ok
272+
Wlw := vectorAdd(matrixMulOnVector(lo, MllO), matrixMulOnVector(no, MlnO))
273+
Wlw = vectorAdd(Wlw, vectorAdd(matrixMulOnVector(ll, MllL), matrixMulOnVector(nl, MlnL)))
274+
Wlw = vectorAdd(Wlw, vectorAdd(matrixMulOnVector(lr, MllR), matrixMulOnVector(nr, MlnR)))
275+
276+
Wmw := vectorAdd(matrixMulOnVector(lo, MmlO), matrixMulOnVector(no, MmnO))
277+
Wmw = vectorAdd(Wmw, vectorAdd(matrixMulOnVector(ll, MmlL), matrixMulOnVector(nl, MmnL)))
278+
Wmw = vectorAdd(Wmw, vectorAdd(matrixMulOnVector(lr, MmlR), matrixMulOnVector(nr, MmnR)))
279+
263280
mu := mul(rho, rho)
264281

265282
// Calculate lambda vector (nl == nv * k)

circuit_test.go

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestArithmeticCircuit(t *testing.T) {
116116
V[i] = public.CommitCircuit(private.V[i], private.Sv[i])
117117
}
118118

119-
proof := ProveCircuit(public, NewKeccakFS(), private)
119+
proof := ProveCircuit(public, V, NewKeccakFS(), private)
120120
spew.Dump(proof)
121121

122122
if err := VerifyCircuit(public, V, NewKeccakFS(), proof); err != nil {
@@ -209,7 +209,7 @@ func TestArithmeticCircuit2(t *testing.T) {
209209
V[i] = public.CommitCircuit(private.V[i], private.Sv[i])
210210
}
211211

212-
proof := ProveCircuit(public, NewKeccakFS(), private)
212+
proof := ProveCircuit(public, V, NewKeccakFS(), private)
213213
spew.Dump(proof)
214214

215215
if err := VerifyCircuit(public, V, NewKeccakFS(), proof); err != nil {
@@ -261,7 +261,7 @@ func TestArithmeticCircuitBinaryRangeProof(t *testing.T) {
261261
w := append(wl, wr...)
262262
w = append(w, wo...) // w = wl||wl||wo
263263

264-
wv := make([]*big.Int, 0, Nw)
264+
wv := make([]*big.Int, 0, Nv*K)
265265
for i := range v {
266266
wv = append(wv, v[i]...)
267267
}
@@ -337,33 +337,14 @@ func TestArithmeticCircuitBinaryRangeProof(t *testing.T) {
337337
V[i] = public.CommitCircuit(private.V[i], private.Sv[i])
338338
}
339339

340-
proof := ProveCircuit(public, NewKeccakFS(), private)
340+
proof := ProveCircuit(public, V, NewKeccakFS(), private)
341341
spew.Dump(proof)
342342

343343
if err := VerifyCircuit(public, V, NewKeccakFS(), proof); err != nil {
344344
panic(err)
345345
}
346346
}
347347

348-
func matrixMulOnVector(a []*big.Int, m [][]*big.Int) []*big.Int {
349-
var res []*big.Int
350-
351-
for i := 0; i < len(m); i++ {
352-
res = append(res, vectorMul(a, m[i]))
353-
}
354-
355-
return res
356-
}
357-
358-
func hadamardMul(a, b []*big.Int) []*big.Int {
359-
res := make([]*big.Int, len(a))
360-
for i := range res {
361-
res[i] = mul(a[i], b[i])
362-
}
363-
364-
return res
365-
}
366-
367348
func frac(a, b int) *big.Int {
368349
return mul(bint(a), inv(bint(b)))
369350
}

math_matrix.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ package bulletproofs
66

77
import "math/big"
88

9+
func zeroMatrix(n, m int) [][]*big.Int {
10+
res := make([][]*big.Int, n)
11+
for i := range res {
12+
res[i] = make([]*big.Int, m)
13+
for j := range res[i] {
14+
res[i][j] = bint(0)
15+
}
16+
}
17+
return res
18+
}
19+
920
func diagInv(x *big.Int, n int) [][]*big.Int {
1021
var res [][]*big.Int = make([][]*big.Int, n)
1122
inv := inv(x)
@@ -42,3 +53,13 @@ func vectorMulOnMatrix(a []*big.Int, m [][]*big.Int) []*big.Int {
4253

4354
return res
4455
}
56+
57+
func matrixMulOnVector(a []*big.Int, m [][]*big.Int) []*big.Int {
58+
var res []*big.Int
59+
60+
for i := 0; i < len(m); i++ {
61+
res = append(res, vectorMul(a, m[i]))
62+
}
63+
64+
return res
65+
}

math_vectors.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ func zeroVector(n int) []*big.Int {
1919
return res
2020
}
2121

22+
func oneVector(n int) []*big.Int {
23+
res := make([]*big.Int, n)
24+
for i := range res {
25+
res[i] = big.NewInt(1)
26+
}
27+
return res
28+
}
29+
2230
func vectorAdd(a []*big.Int, b []*big.Int) []*big.Int {
2331
for len(a) < len(b) {
2432
a = append(a, bint(0))
@@ -157,3 +165,12 @@ func e(v *big.Int, a int) []*big.Int {
157165

158166
return res
159167
}
168+
169+
func hadamardMul(a, b []*big.Int) []*big.Int {
170+
res := make([]*big.Int, len(a))
171+
for i := range res {
172+
res[i] = mul(a[i], b[i])
173+
}
174+
175+
return res
176+
}

0 commit comments

Comments
 (0)