Skip to content

Commit 13349e7

Browse files
committed
TestGetTokensWithWitness
Signed-off-by: Angelo De Caro <[email protected]>
1 parent dfb99cd commit 13349e7

File tree

2 files changed

+87
-12
lines changed

2 files changed

+87
-12
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (t *Token) Deserialize(bytes []byte) error {
6464

6565
// ToClear returns Token in the clear
6666
func (t *Token) ToClear(meta *Metadata, pp *noghv1.PublicParams) (*token.Token, error) {
67-
com, err := commit([]*math.Zr{math.Curves[pp.Curve].HashToZr([]byte(meta.Type)), meta.Value, meta.BlindingFactor}, pp.PedersenGenerators, math.Curves[pp.Curve])
67+
com, err := Commit([]*math.Zr{math.Curves[pp.Curve].HashToZr([]byte(meta.Type)), meta.Value, meta.BlindingFactor}, pp.PedersenGenerators, math.Curves[pp.Curve])
6868
if err != nil {
6969
return nil, errors.Wrap(err, "cannot retrieve token in the clear: failed to check token data")
7070
}
@@ -94,7 +94,7 @@ func computeTokens(tw []*Metadata, pp []*math.G1, c *math.Curve) ([]*math.G1, er
9494
var err error
9595
for i := 0; i < len(tw); i++ {
9696
hash := c.HashToZr([]byte(tw[i].Type))
97-
tokens[i], err = commit([]*math.Zr{hash, tw[i].Value, tw[i].BlindingFactor}, pp, c)
97+
tokens[i], err = Commit([]*math.Zr{hash, tw[i].Value, tw[i].BlindingFactor}, pp, c)
9898
if err != nil {
9999
return nil, errors.WithMessagef(err, "failed to compute token [%d]", i)
100100
}
@@ -195,7 +195,11 @@ func (m *Metadata) Clone() *Metadata {
195195
}
196196
}
197197

198-
func commit(vector []*math.Zr, generators []*math.G1, c *math.Curve) (*math.G1, error) {
198+
// Commit computes the Pedersen commitment of the passed elements using the passed bases
199+
func Commit(vector []*math.Zr, generators []*math.G1, c *math.Curve) (*math.G1, error) {
200+
if len(generators) != len(vector) {
201+
return nil, errors.Errorf("number of generators is not equal to number of vector elements, [%d]!=[%d]", len(generators), len(vector))
202+
}
199203
com := c.NewG1()
200204
for i := range vector {
201205
if vector[i] == nil {

token/core/zkatdlog/nogh/v1/token/token_test.go

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/hyperledger-labs/fabric-token-sdk/token/services/tokens"
1616
"github.com/hyperledger-labs/fabric-token-sdk/token/services/tokens/core/comm"
1717
token3 "github.com/hyperledger-labs/fabric-token-sdk/token/token"
18+
"github.com/pkg/errors"
1819
"github.com/stretchr/testify/assert"
1920
)
2021

@@ -99,24 +100,93 @@ func TestTokenIsRedeem(t *testing.T) {
99100

100101
func TestGetTokensWithWitness(t *testing.T) {
101102
tests := []struct {
102-
name string
103-
values []uint64
104-
tokenType token3.Type
105-
pp []*math.G1
106-
curve *math.Curve
107-
wantErr bool
108-
expectedError string
109-
expectedQuantity uint64
103+
name string
104+
values []uint64
105+
tokenType token3.Type
106+
pp []*math.G1
107+
curve *math.Curve
108+
validate func([]*math.G1, []*token2.TokenDataWitness) error
109+
wantErr bool
110+
expectedError string
110111
}{
111112
{
112113
name: "curve is nil",
113114
wantErr: true,
114115
expectedError: "cannot get tokens with witness: please initialize curve",
115116
},
117+
{
118+
name: "curve is not nil",
119+
curve: math.Curves[math.FP256BN_AMCL],
120+
wantErr: false,
121+
validate: func(tokens []*math.G1, data []*token2.TokenDataWitness) error {
122+
if len(tokens) != 0 {
123+
return errors.New("tokens should be empty")
124+
}
125+
if len(data) != 0 {
126+
return errors.New("tokens should be empty")
127+
}
128+
return nil
129+
},
130+
},
131+
{
132+
name: "number of generators is not equal to number of vector elements",
133+
values: []uint64{10},
134+
tokenType: "token type",
135+
pp: nil,
136+
curve: math.Curves[math.FP256BN_AMCL],
137+
wantErr: true,
138+
expectedError: "cannot get tokens with witness: failed to compute token [0]: number of generators is not equal to number of vector elements, [0]!=[3]",
139+
},
140+
{
141+
name: "success",
142+
values: []uint64{10},
143+
tokenType: "token type",
144+
pp: []*math.G1{
145+
math.Curves[math.FP256BN_AMCL].NewG1(),
146+
math.Curves[math.FP256BN_AMCL].NewG1(),
147+
math.Curves[math.FP256BN_AMCL].NewG1(),
148+
},
149+
curve: math.Curves[math.FP256BN_AMCL],
150+
wantErr: false,
151+
validate: func(toks []*math.G1, data []*token2.TokenDataWitness) error {
152+
if len(toks) != 1 {
153+
return errors.New("one token was expected")
154+
}
155+
if len(data) != 1 {
156+
return errors.New("one data was expected")
157+
}
158+
c := math.Curves[math.FP256BN_AMCL]
159+
pp := []*math.G1{
160+
math.Curves[math.FP256BN_AMCL].NewG1(),
161+
math.Curves[math.FP256BN_AMCL].NewG1(),
162+
math.Curves[math.FP256BN_AMCL].NewG1(),
163+
}
164+
165+
for i, token := range toks {
166+
hash := c.HashToZr([]byte(data[i].Type))
167+
tok, err := token2.Commit(
168+
[]*math.Zr{
169+
hash,
170+
c.NewZrFromUint64(data[i].Value),
171+
data[i].BlindingFactor,
172+
},
173+
pp,
174+
c,
175+
)
176+
if err != nil {
177+
return err
178+
}
179+
if !token.Equals(tok) {
180+
return errors.New("token does not match")
181+
}
182+
}
183+
return nil
184+
},
185+
},
116186
}
117187
for _, tt := range tests {
118188
t.Run(tt.name, func(t *testing.T) {
119-
_, _, err := token2.GetTokensWithWitness(
189+
g1s, witnesses, err := token2.GetTokensWithWitness(
120190
tt.values,
121191
tt.tokenType,
122192
tt.pp,
@@ -127,6 +197,7 @@ func TestGetTokensWithWitness(t *testing.T) {
127197
assert.EqualError(t, err, tt.expectedError)
128198
} else {
129199
assert.NoError(t, err)
200+
assert.NoError(t, tt.validate(g1s, witnesses))
130201
}
131202
})
132203
}

0 commit comments

Comments
 (0)