Skip to content

Commit 22dd218

Browse files
committed
improvements
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent bd09223 commit 22dd218

File tree

8 files changed

+137
-30
lines changed

8 files changed

+137
-30
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.20251208124832-1dd505f14350
11+
github.com/IBM/mathlib v0.0.3-0.20251209063833-bdbf97a6c3d2
1212
github.com/dgraph-io/ristretto/v2 v2.3.0
1313
github.com/gin-gonic/gin v1.10.0
1414
github.com/hashicorp/go-uuid v1.0.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,8 @@ github.com/IBM/idemix/bccsp/schemes/weak-bb v0.0.0-20250313153527-832db18b9478 h
650650
github.com/IBM/idemix/bccsp/schemes/weak-bb v0.0.0-20250313153527-832db18b9478/go.mod h1:k4Q5EYKRnYC6t80ipSCY3G8H4FdcxRa8jjlsJdGfNCY=
651651
github.com/IBM/idemix/bccsp/types v0.0.0-20250313153527-832db18b9478 h1:Uzmcb4pNb54/fbAjnrZTiJwWV74+twP60N4qBGm4PvU=
652652
github.com/IBM/idemix/bccsp/types v0.0.0-20250313153527-832db18b9478/go.mod h1:Pi1QIuIZ+1OXIbnYe27vNwJOnSq2WvkHRT/sfweTw8E=
653-
github.com/IBM/mathlib v0.0.3-0.20251208124832-1dd505f14350 h1:1DF5XTkBuJSRRW2gk+mFrcc+uqvMK6SH39t3qCbtPD8=
654-
github.com/IBM/mathlib v0.0.3-0.20251208124832-1dd505f14350/go.mod h1:rq67W1H6L1eorrE7DZ/HcSY/pfMDjbPWOx12SeUfQDk=
653+
github.com/IBM/mathlib v0.0.3-0.20251209063833-bdbf97a6c3d2 h1:xxqXQL645JpGvuUqWdNUHCY/6EwxqsmuBuiEUsbswQU=
654+
github.com/IBM/mathlib v0.0.3-0.20251209063833-bdbf97a6c3d2/go.mod h1:rq67W1H6L1eorrE7DZ/HcSY/pfMDjbPWOx12SeUfQDk=
655655
github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk=
656656
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
657657
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=

token/core/zkatdlog/nogh/v1/crypto/common/array.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ SPDX-License-Identifier: Apache-2.0
77
package common
88

99
import (
10+
"hash"
11+
1012
math "github.com/IBM/mathlib"
1113
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1214
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common/crypto"
@@ -52,3 +54,12 @@ func GetG1Array(elements ...[]*math.G1) *G1Array {
5254
a := G1Array(array)
5355
return &a
5456
}
57+
58+
func HashG1Array(h hash.Hash, elements ...*math.G1) []byte {
59+
h.Reset()
60+
61+
for _, e := range elements {
62+
h.Write(e.Bytes())
63+
}
64+
return h.Sum(nil)
65+
}

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

Lines changed: 99 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,42 @@ SPDX-License-Identifier: Apache-2.0
77
package rp_test
88

99
import (
10+
"math/bits"
11+
"math/rand"
1012
"strconv"
1113
"testing"
1214

1315
math "github.com/IBM/mathlib"
1416
"github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/crypto/rp"
1517
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
1619
)
1720

18-
func TestBFProofVerify(t *testing.T) {
19-
curve := math.Curves[1]
20-
nr := uint64(3)
21-
l := uint64(1 << nr)
21+
type bfSetup struct {
22+
com *math.G1
23+
Q *math.G1
24+
P *math.G1
25+
H *math.G1
26+
G *math.G1
27+
bf *math.Zr
28+
leftGens []*math.G1
29+
rightGens []*math.G1
30+
nr uint64
31+
l uint64
32+
curve *math.Curve
33+
}
34+
35+
func NewBfSetup(curveID math.CurveID) (*bfSetup, error) {
36+
curve := math.Curves[curveID]
37+
l := uint64(64)
38+
nr := 63 - uint64(bits.LeadingZeros64(l))
2239
leftGens := make([]*math.G1, l)
2340
rightGens := make([]*math.G1, l)
2441

2542
rand, err := curve.Rand()
26-
assert.NoError(t, err)
43+
if err != nil {
44+
return nil, err
45+
}
2746

2847
Q := curve.GenG1.Mul(curve.NewRandomZr(rand))
2948
P := curve.GenG1.Mul(curve.NewRandomZr(rand))
@@ -36,12 +55,85 @@ func TestBFProofVerify(t *testing.T) {
3655
bf := curve.NewRandomZr(rand)
3756
com := G.Mul(curve.NewZrFromInt(115))
3857
com.Add(H.Mul(bf))
39-
prover := rp.NewRangeProver(com, 115, []*math.G1{G, H}, bf, leftGens, rightGens, P, Q, nr, l, curve)
40-
verifier := rp.NewRangeVerifier(com, []*math.G1{G, H}, leftGens, rightGens, P, Q, nr, l, curve)
4158

59+
return &bfSetup{
60+
com: com,
61+
Q: Q,
62+
P: P,
63+
H: H,
64+
G: G,
65+
bf: bf,
66+
leftGens: leftGens,
67+
rightGens: rightGens,
68+
nr: nr,
69+
l: l,
70+
curve: curve,
71+
}, nil
72+
}
73+
74+
func TestBFProofVerify(t *testing.T) {
75+
setup, err := NewBfSetup(math.BLS12_381_BBS_GURVY)
76+
require.NoError(t, err)
77+
78+
prover := rp.NewRangeProver(
79+
setup.com,
80+
115,
81+
[]*math.G1{setup.G, setup.H},
82+
setup.bf,
83+
setup.leftGens,
84+
setup.rightGens,
85+
setup.P,
86+
setup.Q,
87+
setup.nr,
88+
setup.l,
89+
setup.curve,
90+
)
4291
proof, err := prover.Prove()
4392
assert.NoError(t, err)
4493
assert.NotNil(t, proof)
94+
95+
verifier := rp.NewRangeVerifier(
96+
setup.com,
97+
[]*math.G1{setup.G, setup.H},
98+
setup.leftGens,
99+
setup.rightGens,
100+
setup.P,
101+
setup.Q,
102+
setup.nr,
103+
setup.l,
104+
setup.curve,
105+
)
45106
err = verifier.Verify(proof)
46107
assert.NoError(t, err)
47108
}
109+
110+
func BenchmarkBFProver(b *testing.B) {
111+
envs := make([]*bfSetup, 0, 128)
112+
for i := 0; i < 128; i++ {
113+
setup, err := NewBfSetup(math.BLS12_381_BBS_GURVY)
114+
require.NoError(b, err)
115+
envs = append(envs, setup)
116+
}
117+
118+
b.Run("bench", func(b *testing.B) {
119+
for b.Loop() {
120+
setup := envs[rand.Intn(len(envs))]
121+
prover := rp.NewRangeProver(
122+
setup.com,
123+
115,
124+
[]*math.G1{setup.G, setup.H},
125+
setup.bf,
126+
setup.leftGens,
127+
setup.rightGens,
128+
setup.P,
129+
setup.Q,
130+
setup.nr,
131+
setup.l,
132+
setup.curve,
133+
)
134+
proof, err := prover.Prove()
135+
assert.NoError(b, err)
136+
assert.NotNil(b, proof)
137+
}
138+
})
139+
}

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ func (p *ipaProver) Prove() (*IPA, error) {
154154
}
155155
// compute first challenge
156156
x := p.Curve.HashToZr(raw)
157+
157158
// compute a commitment to inner product value and the vectors
158159
C := p.Q.Mul(p.Curve.ModMul(x, p.InnerProduct, p.Curve.GroupOrder))
159160
C.Add(p.Commitment)
@@ -172,14 +173,13 @@ func (p *ipaProver) Prove() (*IPA, error) {
172173
// of the left vector and right is a function of right vector.
173174
// Both vectors are committed in com which is passed as a parameter to reduce
174175
func (p *ipaProver) reduce(X, com *mathlib.G1) (*mathlib.Zr, *mathlib.Zr, []*mathlib.G1, []*mathlib.G1, error) {
175-
var leftGen, rightGen []*mathlib.G1
176-
var left, right []*mathlib.Zr
177-
178-
leftGen = p.LeftGenerators
179-
rightGen = p.RightGenerators
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)
180180

181-
left = p.leftVector
182-
right = p.rightVector
181+
left := p.leftVector
182+
right := p.rightVector
183183

184184
LArray := make([]*mathlib.G1, p.NumberOfRounds)
185185
RArray := make([]*mathlib.G1, p.NumberOfRounds)
@@ -324,8 +324,7 @@ func (v *ipaVerifier) Verify(proof *IPA) error {
324324
xSquareInv := xSquare.Copy()
325325
xSquareInv.InvModP(v.Curve.GroupOrder)
326326
// compute a commitment to the reduced vectors and their inner product
327-
CPrime := proof.L[i].Mul(xSquare)
328-
CPrime.Add(proof.R[i].Mul(xSquareInv))
327+
CPrime := proof.L[i].Mul2(xSquare, proof.R[i], xSquareInv)
329328
CPrime.Add(C)
330329
C = CPrime.Copy()
331330
// reduce the generators by 1/2, as a function of the old generators and x and 1/x
@@ -360,15 +359,14 @@ func reduceVectors(left, right []*mathlib.Zr, x, xInv *mathlib.Zr, c *mathlib.Cu
360359
// reduceGenerators reduces the number of generators passed in the parameters by 1/2,
361360
// as a function of the old generators, x and 1/x
362361
func reduceGenerators(leftGen, rightGen []*mathlib.G1, x, xInv *mathlib.Zr) ([]*mathlib.G1, []*mathlib.G1) {
363-
leftGenPrime := make([]*mathlib.G1, len(leftGen)/2)
364-
rightGenPrime := make([]*mathlib.G1, len(rightGen)/2)
365-
for i := 0; i < len(leftGenPrime); i++ {
362+
l := len(leftGen) / 2
363+
for i := 0; i < l; i++ {
366364
// G_i = G_i^x*G_{i+len(left)/2}^{1/x}
367-
leftGenPrime[i] = leftGen[i].Mul2(xInv, leftGen[i+len(leftGenPrime)], x)
365+
leftGen[i] = leftGen[i].Mul2(xInv, leftGen[i+l], x)
368366
// H_i = H_i^{1/x}*H_{i+len(right)/2}^{x}
369-
rightGenPrime[i] = rightGen[i].Mul2(x, rightGen[i+len(rightGenPrime)], xInv)
367+
rightGen[i] = rightGen[i].Mul2(x, rightGen[i+l], xInv)
370368
}
371-
return leftGenPrime, rightGenPrime
369+
return leftGen[:l], rightGen[:l]
372370
}
373371

374372
func innerProduct(left []*mathlib.Zr, right []*mathlib.Zr, c *mathlib.Curve) *mathlib.Zr {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ SPDX-License-Identifier: Apache-2.0
77
package rp_test
88

99
import (
10+
"math/bits"
1011
"math/rand"
1112
"strconv"
1213
"testing"
1314

1415
math "github.com/IBM/mathlib"
16+
"github.com/hyperledger-labs/fabric-smart-client/node/start/profile"
1517
"github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/crypto/rp"
1618
"github.com/stretchr/testify/assert"
1719
"github.com/stretchr/testify/require"
@@ -30,8 +32,8 @@ type ipaSetup struct {
3032

3133
func NewIpaSetup(curveID math.CurveID) (*ipaSetup, error) {
3234
curve := math.Curves[curveID]
33-
nr := uint64(6)
34-
l := uint64(1 << nr)
35+
l := uint64(64)
36+
nr := 63 - uint64(bits.LeadingZeros64(l))
3537
leftGens := make([]*math.G1, l)
3638
rightGens := make([]*math.G1, l)
3739
left := make([]*math.Zr, l)
@@ -96,6 +98,10 @@ func TestIPAProofVerify(t *testing.T) {
9698
}
9799

98100
func BenchmarkIPAProver(b *testing.B) {
101+
pp, err := profile.New(profile.WithAll(), profile.WithPath("./profile"))
102+
require.NoError(b, err)
103+
require.NoError(b, pp.Start())
104+
defer pp.Stop()
99105
envs := make([]*ipaSetup, 0, 128)
100106
for i := 0; i < 128; i++ {
101107
setup, err := NewIpaSetup(math.BLS12_381_BBS_GURVY)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/IBM/idemix v0.0.2-0.20250313153527-832db18b9478 // indirect
2222
github.com/IBM/idemix/bccsp/schemes/weak-bb v0.0.0-20250313153527-832db18b9478 // indirect
2323
github.com/IBM/idemix/bccsp/types v0.0.0-20250313153527-832db18b9478 // indirect
24-
github.com/IBM/mathlib v0.0.3-0.20251208124832-1dd505f14350 // indirect
24+
github.com/IBM/mathlib v0.0.3-0.20251209063833-bdbf97a6c3d2 // indirect
2525
github.com/Microsoft/go-winio v0.6.2 // indirect
2626
github.com/beorn7/perks v1.0.1 // indirect
2727
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.20251208124832-1dd505f14350 h1:1DF5XTkBuJSRRW2gk+mFrcc+uqvMK6SH39t3qCbtPD8=
15-
github.com/IBM/mathlib v0.0.3-0.20251208124832-1dd505f14350/go.mod h1:rq67W1H6L1eorrE7DZ/HcSY/pfMDjbPWOx12SeUfQDk=
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=
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)