Skip to content

Commit 35aeffb

Browse files
committed
improvements
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent 0b56894 commit 35aeffb

File tree

8 files changed

+48
-49
lines changed

8 files changed

+48
-49
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/DATA-DOG/go-sqlmock v1.5.2
99
github.com/IBM/idemix v0.0.2-0.20250313153527-832db18b9478
1010
github.com/IBM/idemix/bccsp/types v0.0.0-20250313153527-832db18b9478
11-
github.com/IBM/mathlib v0.0.3-0.20251209063833-bdbf97a6c3d2
11+
github.com/IBM/mathlib v0.0.3-0.20251210060400-7e58831c9abe
1212
github.com/dgraph-io/ristretto/v2 v2.3.0
1313
github.com/gin-gonic/gin v1.10.0
1414
github.com/google/pprof v0.0.0-20250403155104-27863c87afa6

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,8 @@ github.com/IBM/idemix/bccsp/schemes/weak-bb v0.0.0-20250313153527-832db18b9478 h
643643
github.com/IBM/idemix/bccsp/schemes/weak-bb v0.0.0-20250313153527-832db18b9478/go.mod h1:k4Q5EYKRnYC6t80ipSCY3G8H4FdcxRa8jjlsJdGfNCY=
644644
github.com/IBM/idemix/bccsp/types v0.0.0-20250313153527-832db18b9478 h1:Uzmcb4pNb54/fbAjnrZTiJwWV74+twP60N4qBGm4PvU=
645645
github.com/IBM/idemix/bccsp/types v0.0.0-20250313153527-832db18b9478/go.mod h1:Pi1QIuIZ+1OXIbnYe27vNwJOnSq2WvkHRT/sfweTw8E=
646-
github.com/IBM/mathlib v0.0.3-0.20251209063833-bdbf97a6c3d2 h1:xxqXQL645JpGvuUqWdNUHCY/6EwxqsmuBuiEUsbswQU=
647-
github.com/IBM/mathlib v0.0.3-0.20251209063833-bdbf97a6c3d2/go.mod h1:rq67W1H6L1eorrE7DZ/HcSY/pfMDjbPWOx12SeUfQDk=
646+
github.com/IBM/mathlib v0.0.3-0.20251210060400-7e58831c9abe h1:EwFXJqfqz0lnsOtqSEswQ0kond3a8waLUfIXbaTgB4A=
647+
github.com/IBM/mathlib v0.0.3-0.20251210060400-7e58831c9abe/go.mod h1:rq67W1H6L1eorrE7DZ/HcSY/pfMDjbPWOx12SeUfQDk=
648648
github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk=
649649
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
650650
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=

token/core/zkatdlog/nogh/v1/crypto/rp/bulletproof.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func (p *rangeProver) Prove() (*RangeProof, error) {
241241
}
242242
// compute the commitment to left and right
243243
com := commitVector(left, right, p.LeftGenerators, rightGeneratorsPrime, p.Curve)
244-
rp.Data.InnerProduct = innerProduct(left, right, p.Curve)
244+
rp.Data.InnerProduct = InnerProduct(left, right, p.Curve)
245245
// produce the IPA
246246
ipp := NewIPAProver(
247247
rp.Data.InnerProduct,
@@ -337,18 +337,18 @@ func (p *rangeProver) preprocess() ([]*math.Zr, []*math.Zr, *math.Zr, *RangeProo
337337
}
338338

339339
// compute \sum y^iV_i(L_i-z)
340-
t1 := innerProduct(leftPrime, randRightPrime, p.Curve)
340+
t1 := InnerProduct(leftPrime, randRightPrime, p.Curve)
341341
// compute \sum y^i(V_i(L_i-z) + (R_i +z)U_i)
342-
t1 = p.Curve.ModAdd(t1, innerProduct(rightPrime, randomLeft, p.Curve), p.Curve.GroupOrder)
342+
t1 = p.Curve.ModAdd(t1, InnerProduct(rightPrime, randomLeft, p.Curve), p.Curve.GroupOrder)
343343
// compute \sum y^i(V_i(L_i-z) + (R_i+z)U_i) + U_i2^iz^2
344-
t1 = p.Curve.ModAdd(t1, innerProduct(zPrime, randomLeft, p.Curve), p.Curve.GroupOrder)
344+
t1 = p.Curve.ModAdd(t1, InnerProduct(zPrime, randomLeft, p.Curve), p.Curve.GroupOrder)
345345
// commit to t1
346346
tau1 := p.Curve.NewRandomZr(rand)
347347
T1 := p.CommitmentGenerators[0].Mul(t1)
348348
T1.Add(p.CommitmentGenerators[1].Mul(tau1))
349349

350350
// compute = \sum y^iU_iV_i
351-
t2 := innerProduct(randomLeft, randRightPrime, p.Curve)
351+
t2 := InnerProduct(randomLeft, randRightPrime, p.Curve)
352352
// commit to t2
353353
tau2 := p.Curve.NewRandomZr(rand)
354354
T2 := p.CommitmentGenerators[0].Mul(t2)

token/core/zkatdlog/nogh/v1/crypto/rp/ipa.go

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,7 @@ func (p *ipaProver) Prove() (*IPA, error) {
173173
// of the left vector and right is a function of right vector.
174174
// Both vectors are committed in com which is passed as a parameter to reduce
175175
func (p *ipaProver) reduce(X, com *mathlib.G1) (*mathlib.Zr, *mathlib.Zr, []*mathlib.G1, []*mathlib.G1, error) {
176-
leftGen := make([]*mathlib.G1, len(p.LeftGenerators))
177-
copy(leftGen, p.LeftGenerators)
178-
rightGen := make([]*mathlib.G1, len(p.RightGenerators))
179-
copy(rightGen, p.RightGenerators)
176+
leftGen, rightGen := cloneGenerators(p.LeftGenerators, p.RightGenerators)
180177

181178
left := p.leftVector
182179
right := p.rightVector
@@ -186,8 +183,8 @@ func (p *ipaProver) reduce(X, com *mathlib.G1) (*mathlib.Zr, *mathlib.Zr, []*mat
186183
for i := range p.NumberOfRounds {
187184
// in each round the size of the vector is reduced by 2
188185
n := len(leftGen) / 2
189-
leftIP := innerProduct(left[:n], right[n:], p.Curve)
190-
rightIP := innerProduct(left[n:], right[:n], p.Curve)
186+
leftIP := InnerProduct(left[:n], right[n:], p.Curve)
187+
rightIP := InnerProduct(left[n:], right[:n], p.Curve)
191188
// LArray[i] is a commitment to left[:n], right[n:] and their inner product
192189
LArray[i] = commitVector(left[:n], right[n:], leftGen[n:], rightGen[:n], p.Curve)
193190
LArray[i].Add(X.Mul(leftIP))
@@ -298,10 +295,7 @@ func (v *ipaVerifier) Verify(proof *IPA) error {
298295

299296
X := v.Q.Mul(x)
300297

301-
leftGen := make([]*mathlib.G1, len(v.LeftGenerators))
302-
copy(leftGen, v.LeftGenerators)
303-
rightGen := make([]*mathlib.G1, len(v.RightGenerators))
304-
copy(rightGen, v.RightGenerators)
298+
leftGen, rightGen := cloneGenerators(v.LeftGenerators, v.RightGenerators)
305299

306300
for i := range v.NumberOfRounds {
307301
// check well-formedness
@@ -343,17 +337,17 @@ func (v *ipaVerifier) Verify(proof *IPA) error {
343337
// reduceVectors reduces the size of the vectors passed in the parameters by 1/2,
344338
// as a function of the old vectors, x and 1/x
345339
func reduceVectors(left, right []*mathlib.Zr, x, xInv *mathlib.Zr, c *mathlib.Curve) ([]*mathlib.Zr, []*mathlib.Zr) {
346-
leftPrime := make([]*mathlib.Zr, len(left)/2)
347-
rightPrime := make([]*mathlib.Zr, len(right)/2)
348-
for i := 0; i < len(leftPrime); i++ {
340+
l := len(left) / 2
341+
leftPrime := make([]*mathlib.Zr, l)
342+
rightPrime := make([]*mathlib.Zr, l)
343+
for i := 0; i < l; i++ {
349344
// a_i = a_ix + a_{i+len(left)/2}x^{-1}
350-
leftPrime[i] = c.ModMul(left[i], x, c.GroupOrder)
351-
leftPrime[i] = c.ModAdd(leftPrime[i], c.ModMul(left[i+len(leftPrime)], xInv, c.GroupOrder), c.GroupOrder)
345+
leftPrime[i] = c.ModAddMul2(left[i], x, left[i+l], xInv, c.GroupOrder)
352346

353347
// b_i = b_ix^{-1} + b_{i+len(right)/2}x
354-
rightPrime[i] = c.ModMul(right[i], xInv, c.GroupOrder)
355-
rightPrime[i] = c.ModAdd(rightPrime[i], c.ModMul(right[i+len(rightPrime)], x, c.GroupOrder), c.GroupOrder)
348+
rightPrime[i] = c.ModAddMul2(right[i], xInv, right[i+l], x, c.GroupOrder)
356349
}
350+
357351
return leftPrime, rightPrime
358352
}
359353

@@ -363,19 +357,20 @@ func reduceGenerators(leftGen, rightGen []*mathlib.G1, x, xInv *mathlib.Zr) ([]*
363357
l := len(leftGen) / 2
364358
for i := 0; i < l; i++ {
365359
// G_i = G_i^x*G_{i+len(left)/2}^{1/x}
366-
leftGen[i] = leftGen[i].Mul2(xInv, leftGen[i+l], x)
360+
leftGen[i].Mul2InPlace(xInv, leftGen[i+l], x)
367361
// H_i = H_i^{1/x}*H_{i+len(right)/2}^{x}
368-
rightGen[i] = rightGen[i].Mul2(x, rightGen[i+l], xInv)
362+
rightGen[i].Mul2InPlace(x, rightGen[i+l], xInv)
369363
}
370364
return leftGen[:l], rightGen[:l]
371365
}
372366

373-
func innerProduct(left []*mathlib.Zr, right []*mathlib.Zr, c *mathlib.Curve) *mathlib.Zr {
374-
ip := c.NewZrFromInt(0)
375-
for i, l := range left {
376-
ip = c.ModAdd(ip, c.ModMul(l, right[i], c.GroupOrder), c.GroupOrder)
377-
}
378-
return ip
367+
func InnerProduct(left []*mathlib.Zr, right []*mathlib.Zr, c *mathlib.Curve) *mathlib.Zr {
368+
return c.ModAddMul(left, right, c.GroupOrder)
369+
// ip := c.NewZrFromInt(0)
370+
// for i, l := range left {
371+
// ip = c.ModAdd(ip, c.ModMul(l, right[i], c.GroupOrder), c.GroupOrder)
372+
// }
373+
// return ip
379374
}
380375

381376
func commitVector(left []*mathlib.Zr, right []*mathlib.Zr, leftgen []*mathlib.G1, rightgen []*mathlib.G1, c *mathlib.Curve) *mathlib.G1 {
@@ -385,3 +380,15 @@ func commitVector(left []*mathlib.Zr, right []*mathlib.Zr, leftgen []*mathlib.G1
385380
}
386381
return com
387382
}
383+
384+
func cloneGenerators(LeftGenerators, RightGenerators []*mathlib.G1) ([]*mathlib.G1, []*mathlib.G1) {
385+
leftGen := make([]*mathlib.G1, len(LeftGenerators))
386+
for i := 0; i < len(LeftGenerators); i++ {
387+
leftGen[i] = LeftGenerators[i].Copy()
388+
}
389+
rightGen := make([]*mathlib.G1, len(RightGenerators))
390+
for i := 0; i < len(RightGenerators); i++ {
391+
rightGen[i] = RightGenerators[i].Copy()
392+
}
393+
return leftGen, rightGen
394+
}

token/core/zkatdlog/nogh/v1/crypto/rp/ipa_test.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestIPAProofVerify(t *testing.T) {
7070
require.NoError(t, err)
7171

7272
prover := rp.NewIPAProver(
73-
innerProduct(setup.left, setup.right, setup.curve),
73+
rp.InnerProduct(setup.left, setup.right, setup.curve),
7474
setup.left,
7575
setup.right,
7676
setup.Q,
@@ -85,7 +85,7 @@ func TestIPAProofVerify(t *testing.T) {
8585
assert.NotNil(t, proof)
8686

8787
verifier := rp.NewIPAVerifier(
88-
innerProduct(setup.left, setup.right, setup.curve),
88+
rp.InnerProduct(setup.left, setup.right, setup.curve),
8989
setup.Q,
9090
setup.leftGens,
9191
setup.rightGens,
@@ -113,7 +113,7 @@ func BenchmarkIPAProver(b *testing.B) {
113113
for b.Loop() {
114114
setup := envs[rand.Intn(len(envs))]
115115
prover := rp.NewIPAProver(
116-
innerProduct(setup.left, setup.right, setup.curve),
116+
rp.InnerProduct(setup.left, setup.right, setup.curve),
117117
setup.left,
118118
setup.right,
119119
setup.Q,
@@ -129,11 +129,3 @@ func BenchmarkIPAProver(b *testing.B) {
129129
}
130130
})
131131
}
132-
133-
func innerProduct(left []*math.Zr, right []*math.Zr, c *math.Curve) *math.Zr {
134-
ip := c.NewZrFromInt(0)
135-
for i, l := range left {
136-
ip = c.ModAdd(ip, c.ModMul(l, right[i], c.GroupOrder), c.GroupOrder)
137-
}
138-
return ip
139-
}

token/core/zkatdlog/nogh/v1/issue/issuer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
// TestProverVerifier exercises a full prover -> verifier round-trip for
2525
// a generated ZK-issue proof using the given curve and output count.
2626
func TestProverVerifier(t *testing.T) {
27-
prover, verifier := prepareZKIssue(t, 32, math.BN254, 2)
27+
prover, verifier := prepareZKIssue(t, 32, math.BLS12_381_BBS_GURVY, 2)
2828
proof, err := prover.Prove()
2929
assert.NoError(t, err)
3030
assert.NotNil(t, proof)
@@ -35,7 +35,7 @@ func TestProverVerifier(t *testing.T) {
3535
// TestIssuer tests the high-level issuer API: generating a ZK issue
3636
// action and verifying the resulting proof.
3737
func TestIssuer(t *testing.T) {
38-
pp := setup(t, 32, math.BN254)
38+
pp := setup(t, 32, math.BLS12_381_BBS_GURVY)
3939
issuer := issue2.NewIssuer("ABC", &mock.SigningIdentity{}, pp)
4040
action, _, err := issuer.GenerateZKIssue([]uint64{10, 20}, [][]byte{[]byte("alice"), []byte("bob")})
4141
require.NoError(t, err)

token/services/identity/storage/kvs/hashicorp/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ require (
2020
github.com/IBM/idemix v0.0.2-0.20250313153527-832db18b9478 // indirect
2121
github.com/IBM/idemix/bccsp/schemes/weak-bb v0.0.0-20250313153527-832db18b9478 // indirect
2222
github.com/IBM/idemix/bccsp/types v0.0.0-20250313153527-832db18b9478 // indirect
23-
github.com/IBM/mathlib v0.0.3-0.20251209063833-bdbf97a6c3d2 // indirect
23+
github.com/IBM/mathlib v0.0.3-0.20251210060400-7e58831c9abe // indirect
2424
github.com/Microsoft/go-winio v0.6.2 // indirect
2525
github.com/beorn7/perks v1.0.1 // indirect
2626
github.com/bits-and-blooms/bitset v1.20.0 // indirect

token/services/identity/storage/kvs/hashicorp/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ github.com/IBM/idemix/bccsp/schemes/weak-bb v0.0.0-20250313153527-832db18b9478 h
1111
github.com/IBM/idemix/bccsp/schemes/weak-bb v0.0.0-20250313153527-832db18b9478/go.mod h1:k4Q5EYKRnYC6t80ipSCY3G8H4FdcxRa8jjlsJdGfNCY=
1212
github.com/IBM/idemix/bccsp/types v0.0.0-20250313153527-832db18b9478 h1:Uzmcb4pNb54/fbAjnrZTiJwWV74+twP60N4qBGm4PvU=
1313
github.com/IBM/idemix/bccsp/types v0.0.0-20250313153527-832db18b9478/go.mod h1:Pi1QIuIZ+1OXIbnYe27vNwJOnSq2WvkHRT/sfweTw8E=
14-
github.com/IBM/mathlib v0.0.3-0.20251209063833-bdbf97a6c3d2 h1:xxqXQL645JpGvuUqWdNUHCY/6EwxqsmuBuiEUsbswQU=
15-
github.com/IBM/mathlib v0.0.3-0.20251209063833-bdbf97a6c3d2/go.mod h1:rq67W1H6L1eorrE7DZ/HcSY/pfMDjbPWOx12SeUfQDk=
14+
github.com/IBM/mathlib v0.0.3-0.20251210060400-7e58831c9abe h1:EwFXJqfqz0lnsOtqSEswQ0kond3a8waLUfIXbaTgB4A=
15+
github.com/IBM/mathlib v0.0.3-0.20251210060400-7e58831c9abe/go.mod h1:rq67W1H6L1eorrE7DZ/HcSY/pfMDjbPWOx12SeUfQDk=
1616
github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1Xbatp0=
1717
github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
1818
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=

0 commit comments

Comments
 (0)