-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathtransfer.go
212 lines (179 loc) · 6.28 KB
/
transfer.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package transfer
import (
"sync"
math "github.com/IBM/mathlib"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common/encoding/asn1"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/crypto/rp"
v1 "github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/setup"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/token"
"github.com/pkg/errors"
)
// Proof is a zero-knowledge proof that shows that a Action is valid
type Proof struct {
// proof that inputs and outputs in a Transfer Action are well-formed
// inputs and outputs have the same total value
// inputs and outputs have the same type
TypeAndSum *TypeAndSumProof
// Proof that the outputs have value in the authorized range
RangeCorrectness *rp.RangeCorrectness
}
// Serialize marshals Proof
func (p *Proof) Serialize() ([]byte, error) {
return asn1.Marshal[asn1.Serializer](p.TypeAndSum, p.RangeCorrectness)
}
// Deserialize unmarshals Proof
func (p *Proof) Deserialize(bytes []byte) error {
p.TypeAndSum = &TypeAndSumProof{}
p.RangeCorrectness = &rp.RangeCorrectness{}
return asn1.Unmarshal[asn1.Serializer](bytes, p.TypeAndSum, p.RangeCorrectness)
}
// Verifier verifies if a Action is valid
type Verifier struct {
TypeAndSum *TypeAndSumVerifier
RangeCorrectness *rp.RangeCorrectnessVerifier
}
// NewVerifier returns a Action Verifier as a function of the passed parameters
func NewVerifier(inputs, outputs []*math.G1, pp *v1.PublicParams) *Verifier {
v := &Verifier{}
v.TypeAndSum = NewTypeAndSumVerifier(pp.PedersenGenerators, inputs, outputs, math.Curves[pp.Curve])
// check if this is an ownership transfer
// if so, skip range proof, well-formedness proof is enough
if len(inputs) != 1 || len(outputs) != 1 {
v.RangeCorrectness = rp.NewRangeCorrectnessVerifier(pp.PedersenGenerators[1:], pp.RangeProofParams.LeftGenerators, pp.RangeProofParams.RightGenerators, pp.RangeProofParams.P, pp.RangeProofParams.Q, pp.RangeProofParams.BitLength, pp.RangeProofParams.NumberOfRounds, math.Curves[pp.Curve])
}
return v
}
// Prover produces a proof that a Action is valid
type Prover struct {
TypeAndSum *TypeAndSumProver
RangeCorrectness *rp.RangeCorrectnessProver
}
// NewProver returns a Action Prover that corresponds to the passed arguments
func NewProver(inputWitness, outputWitness []*token.Metadata, inputs, outputs []*math.G1, pp *v1.PublicParams) (*Prover, error) {
c := math.Curves[pp.Curve]
p := &Prover{}
inW := make([]*token.Metadata, len(inputWitness))
outW := make([]*token.Metadata, len(outputWitness))
for i := 0; i < len(inputWitness); i++ {
if inputWitness[i] == nil || inputWitness[i].BlindingFactor == nil {
return nil, errors.New("invalid token witness")
}
inW[i] = inputWitness[i].Clone()
}
values := make([]uint64, len(outputWitness))
blindingFactors := make([]*math.Zr, len(outputWitness))
// commit to the type of inputs and outputs
commitmentToType := pp.PedersenGenerators[0].Mul(c.HashToZr([]byte(inputWitness[0].Type)))
rand, err := c.Rand()
if err != nil {
return nil, err
}
typeBF := c.NewRandomZr(rand)
for i := 0; i < len(outputWitness); i++ {
if outputWitness[i] == nil || outputWitness[i].BlindingFactor == nil {
return nil, errors.New("invalid token witness")
}
outW[i] = outputWitness[i].Clone()
values[i], err = outW[i].Value.Uint()
if err != nil {
return nil, errors.Wrapf(err, "invalid token witness values")
}
blindingFactors[i] = c.ModSub(outW[i].BlindingFactor, typeBF, c.GroupOrder)
}
commitmentToType.Add(pp.PedersenGenerators[2].Mul(typeBF))
p.TypeAndSum = NewTypeAndSumProver(NewTypeAndSumWitness(typeBF, inW, outW, c), pp.PedersenGenerators, inputs, outputs, commitmentToType, c)
// check if this is an ownership transfer
// if so, skip range proof, well-formedness proof is enough
if len(inputWitness) != 1 || len(outputWitness) != 1 {
coms := make([]*math.G1, len(outputs))
// The range prover takes as input commitments outputs[i]/commitmentToType
for i := 0; i < len(outputs); i++ {
coms[i] = outputs[i].Copy()
coms[i].Sub(commitmentToType)
}
p.RangeCorrectness = rp.NewRangeCorrectnessProver(
coms,
values,
blindingFactors,
pp.PedersenGenerators[1:],
pp.RangeProofParams.LeftGenerators,
pp.RangeProofParams.RightGenerators,
pp.RangeProofParams.P,
pp.RangeProofParams.Q,
pp.RangeProofParams.BitLength,
pp.RangeProofParams.NumberOfRounds,
math.Curves[pp.Curve],
)
}
return p, nil
}
// Prove produces a serialized Proof
func (p *Prover) Prove() ([]byte, error) {
var wg sync.WaitGroup
wg.Add(1)
var tsProof *TypeAndSumProof
var rangeProof *rp.RangeCorrectness
var tsErr, rangeErr error
go func() {
defer wg.Done()
if p.RangeCorrectness != nil {
rangeProof, rangeErr = p.RangeCorrectness.Prove()
}
}()
tsProof, tsErr = p.TypeAndSum.Prove()
wg.Wait()
if tsErr != nil {
return nil, errors.Wrapf(tsErr, "failed to generate transfer proof")
}
if rangeErr != nil {
return nil, errors.Wrapf(rangeErr, "failed to generate range proof for transfer")
}
proof := &Proof{
TypeAndSum: tsProof,
RangeCorrectness: rangeProof,
}
return proof.Serialize()
}
// Verify checks validity of serialized Proof
func (v *Verifier) Verify(proof []byte) error {
tp := Proof{}
err := tp.Deserialize(proof)
if err != nil {
return errors.Wrap(err, "invalid transfer proof")
}
if tp.TypeAndSum == nil {
return errors.New("invalid transfer proof")
}
var wg sync.WaitGroup
wg.Add(1)
var tspErr, rangeErr error
// verify well-formedness of inputs and outputs
tspErr = v.TypeAndSum.Verify(tp.TypeAndSum)
go func() {
defer wg.Done()
// verify range proof
if v.RangeCorrectness != nil {
if tp.RangeCorrectness == nil {
rangeErr = errors.New("invalid transfer proof")
} else {
commitmentToType := tp.TypeAndSum.CommitmentToType.Copy()
coms := make([]*math.G1, len(v.TypeAndSum.Outputs))
for i := 0; i < len(v.TypeAndSum.Outputs); i++ {
coms[i] = v.TypeAndSum.Outputs[i].Copy()
coms[i].Sub(commitmentToType)
}
v.RangeCorrectness.Commitments = coms
rangeErr = v.RangeCorrectness.Verify(tp.RangeCorrectness)
}
}
}()
wg.Wait()
if tspErr != nil {
return errors.Wrap(tspErr, "invalid transfer proof")
}
return rangeErr
}