-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
321 lines (274 loc) · 8.34 KB
/
model.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package trie_kzg_bn256
import (
"bytes"
"io"
"github.com/lunfardo314/unitrie/common"
"go.dedis.ch/kyber/v3"
"go.dedis.ch/kyber/v3/pairing/bn256"
"golang.org/x/crypto/blake2b"
)
type terminalCommitment struct {
kyber.Scalar
}
type vectorCommitment struct {
kyber.Point
}
// *vectorCommitment implements trie_go.VCommitment
var _ common.VCommitment = &vectorCommitment{}
func (v *vectorCommitment) Bytes() []byte {
return common.MustBytes(v)
}
func (v *vectorCommitment) Read(r io.Reader) error {
_, err := v.Point.UnmarshalFrom(r)
return err
}
func (v *vectorCommitment) Write(w io.Writer) error {
_, err := v.Point.MarshalTo(w)
return err
}
func (v *vectorCommitment) AsKey() []byte {
if common.IsNil(v.Point) {
return nil
}
b, err := v.Point.MarshalBinary()
if err != nil {
panic(err)
}
ret := blake2b.Sum256(b)
return ret[:]
}
func (v *vectorCommitment) String() string {
return v.Point.String()
}
func (v *vectorCommitment) Clone() common.VCommitment {
if v == nil {
return nil
}
return &vectorCommitment{Point: v.Point.Clone()}
}
// *terminalCommitment implements trie_go.TCommitment
var _ common.TCommitment = &terminalCommitment{}
func (t *terminalCommitment) Write(w io.Writer) error {
_, err := t.Scalar.MarshalTo(w)
return err
}
func (t *terminalCommitment) Read(r io.Reader) error {
_, err := t.Scalar.UnmarshalFrom(r)
return err
}
func (t *terminalCommitment) Bytes() []byte {
return common.MustBytes(t)
}
func (t *terminalCommitment) String() string {
return t.Scalar.String()
}
func (t *terminalCommitment) Clone() common.TCommitment {
if t == nil {
return nil
}
return &terminalCommitment{Scalar: t.Scalar.Clone()}
}
func (t *terminalCommitment) AsKey() []byte {
return t.Bytes()
}
func (t *terminalCommitment) ExtractValue() ([]byte, bool) {
return nil, false
}
// CommitmentModel implements 256+ trie based on blake2b hashing
type CommitmentModel struct {
TrustedSetup
}
// Model is a singleton
var Model = New()
func New() *CommitmentModel {
ret, err := TrustedSetupFromBytes(bn256.NewSuite(), GetTrustedSetupBin())
if err != nil {
panic(err)
}
return &CommitmentModel{
TrustedSetup: *ret,
}
}
func (m *CommitmentModel) PathArity() common.PathArity {
return common.PathArity256 // only can be used with 256-ary
}
func (m *CommitmentModel) EqualCommitments(c1, c2 common.Serializable) bool {
return equalCommitments(c1, c2)
}
func equalCommitments(c1, c2 common.Serializable) bool {
if equals, conclusive := common.CheckNils(c1, c2); conclusive {
return equals
}
// both not nils
return bytes.Equal(c1.Bytes(), c2.Bytes())
}
func (m *CommitmentModel) Description() string {
return "trie commitment common implementation based on KZG (Kate) polynomial commitments and bn256 curve frm Dedis.Kyber library. 256-ary keys"
}
func (m *CommitmentModel) ShortName() string {
return "kzg-bn256"
}
func (m *CommitmentModel) NewVectorCommitment() common.VCommitment {
return m.newVectorCommitment()
}
func (m *CommitmentModel) ForceStoreTerminalWithNode(_ common.TCommitment) bool {
return true
}
func (m *CommitmentModel) AlwaysStoreTerminalWithNode() bool {
return true
}
func (m *CommitmentModel) ExtractDataFromTCommitment(c common.TCommitment) ([]byte, bool) {
return nil, common.IsNil(c)
}
func (m *CommitmentModel) newVectorCommitment(p ...kyber.Point) *vectorCommitment {
if len(p) == 0 {
return &vectorCommitment{Point: m.Suite.G1().Point()}
}
return &vectorCommitment{Point: p[0]}
}
func (m *CommitmentModel) NewTerminalCommitment() common.TCommitment {
return m.newTerminalCommitment()
}
func (m *CommitmentModel) newTerminalCommitment() *terminalCommitment {
return &terminalCommitment{Scalar: m.Suite.G1().Scalar()}
}
func (m *CommitmentModel) CommitToData(data []byte) common.TCommitment {
return commitToData(data, m.Suite)
}
func (m *CommitmentModel) UpdateVCommitment(c *common.VCommitment, delta common.VCommitment) {
if *c == nil {
*c = m.newVectorCommitment()
}
p := (*c).(*vectorCommitment).Point
p.Add(p, delta.(*vectorCommitment).Point)
}
// UpdateNodeCommitment updates mutated part of node's data and, optionaly, upper
func (m *CommitmentModel) UpdateNodeCommitment(mutate *common.NodeData, childUpdates map[byte]common.VCommitment, terminal common.TCommitment, pathFragment, nodePath []byte, calcDelta bool) {
var deltas map[int]kyber.Scalar
if calcDelta {
deltas = make(map[int]kyber.Scalar)
}
for i, childUpd := range childUpdates {
if calcDelta {
prevC, existsPrevC := mutate.ChildCommitments[i]
if !existsPrevC && childUpd == nil {
// child didn't exist, no need to delete it
continue
}
delta := m.TrustedSetup.Suite.G1().Scalar().Zero()
if childUpd == nil {
// deleting child
common.Assert(prevC != nil, "prevC != nil")
common.Assert(existsPrevC, "par.ChildCommitments[i] != nil")
delta = scalarFromPoint(m.TrustedSetup.Suite.G1().Scalar(), prevC.(*vectorCommitment).Point)
delta.Neg(delta)
} else {
delta = scalarFromPoint(m.TrustedSetup.Suite.G1().Scalar(), childUpd.(*vectorCommitment).Point)
if prevC != nil {
prevS := scalarFromPoint(m.TrustedSetup.Suite.G1().Scalar(), prevC.(*vectorCommitment).Point)
delta.Sub(delta, prevS)
}
}
deltas[int(i)] = delta
}
// update mutated part
if childUpd == nil {
delete(mutate.ChildCommitments, i)
} else {
mutate.ChildCommitments[i] = childUpd
}
}
if calcDelta && !equalCommitments(mutate.Terminal, terminal) {
delta := m.TrustedSetup.Suite.G1().Scalar().Zero()
if terminal == nil {
if mutate.Terminal != nil {
delta = mutate.Terminal.(*terminalCommitment).Scalar
delta.Neg(delta)
}
} else {
delta.Set(terminal.(*terminalCommitment).Scalar)
if mutate.Terminal != nil {
delta.Sub(terminal.(*terminalCommitment).Scalar, mutate.Terminal.(*terminalCommitment).Scalar)
}
}
deltas[256] = delta
}
mutate.Terminal = terminal
mutate.PathFragment = pathFragment
if calcDelta {
var prevP kyber.Point
// update upper commitment by adding calculated delta
if !common.IsNil(mutate.Commitment) {
prevP = mutate.Commitment.(*vectorCommitment).Point.Clone()
} else {
prevP = m.TrustedSetup.Suite.G1().Point().Null()
}
elem := m.TrustedSetup.Suite.G1().Point()
for i, deltaS := range deltas {
elem.Mul(deltaS, m.TrustedSetup.LagrangeBasis[i])
prevP.Add(prevP, elem)
}
mutate.Commitment = m.newVectorCommitment(prevP)
} else {
mutate.Commitment = m.CalcNodeCommitment(mutate, nodePath)
}
}
func (m *CommitmentModel) CalcNodeCommitment(data *common.NodeData, nodePath []byte) common.VCommitment {
return m.calcNodeCommitment(data, nodePath)
}
func (m *CommitmentModel) calcNodeCommitment(data *common.NodeData, nodePath []byte) *vectorCommitment {
var vect [258]kyber.Scalar
makeVector(data, nodePath, &m.TrustedSetup, &vect)
return &vectorCommitment{Point: m.TrustedSetup.commit(vect[:])}
}
func (m *CommitmentModel) calcProof(data *common.NodeData, nodePath []byte, index int) kyber.Point {
var vect [258]kyber.Scalar
makeVector(data, nodePath, &m.TrustedSetup, &vect)
return m.TrustedSetup.prove(vect[:], index)
}
// Vector extracts vector from the node
func makeVector(n *common.NodeData, nodePath []byte, ts *TrustedSetup, ret *[258]kyber.Scalar) {
for i, p := range n.ChildCommitments {
if p == nil {
continue
}
ret[i] = ts.Suite.G1().Scalar()
scalarFromPoint(ret[i], p.(*vectorCommitment).Point)
}
if n.Terminal != nil {
ret[256] = n.Terminal.(*terminalCommitment).Scalar
}
// we concatenate path to the node, '+' and path fragment to distinguish
// between for example 'a'+'bc' and 'ab'+'c'
h := blake2b.Sum256(common.Concat(nodePath, byte('+'), n.PathFragment))
ret[257] = ts.Suite.G1().Scalar()
scalarFromBytes(ret[257], h[:])
}
// scalarFromPoint hashes the point and make a scalar from hash
// Note that zero-point does not result in zero scalar
func scalarFromPoint(ret kyber.Scalar, point kyber.Point) kyber.Scalar {
if point == nil {
ret.Zero()
return ret
}
pBin, err := point.MarshalBinary()
if err != nil {
panic(err)
}
scalarFromBytes(ret, pBin)
return ret
}
func scalarFromBytes(ret kyber.Scalar, data []byte) kyber.Scalar {
h := blake2b.Sum256(data)
ret.SetBytes(h[:])
return ret
}
func commitToData(data []byte, suite *bn256.Suite) common.TCommitment {
if len(data) == 0 {
return nil
}
h := blake2b.Sum256(data)
ret := &terminalCommitment{Scalar: suite.G1().Scalar()}
ret.Scalar.SetBytes(h[:])
return ret
}