Skip to content

Commit 03b647a

Browse files
authored
GetProofItems now get the proof values from the tree (#361)
A remnant of the time that the pre-state values were taken from the witness, the data to be passed to the proof serialization code was taken from it. But since the witness does not currently hold the initial values, and since the data is taken from the tree, this commit changes the interface for simplification. This is enshrining a slower way to do things, but given the complexity of the previous version, it's wiser to keep things simple at first, and worry about optimization later.
1 parent 18bdd06 commit 03b647a

File tree

4 files changed

+45
-39
lines changed

4 files changed

+45
-39
lines changed

proof_ipa.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func GetCommitmentsForMultiproof(root VerkleNode, keys [][]byte) (*ProofElements
7979
return root.GetProofItems(keylist(keys))
8080
}
8181

82-
func MakeVerkleMultiProof(root VerkleNode, keys [][]byte, keyvals map[string][]byte) (*Proof, []*Point, []byte, []*Fr, error) {
82+
func MakeVerkleMultiProof(root VerkleNode, keys [][]byte) (*Proof, []*Point, []byte, []*Fr, error) {
8383
// go-ipa won't accept no key as an input, catch this corner case
8484
// and return an empty result.
8585
if len(keys) == 0 {
@@ -94,13 +94,18 @@ func MakeVerkleMultiProof(root VerkleNode, keys [][]byte, keyvals map[string][]b
9494
return nil, nil, nil, nil, err
9595
}
9696

97-
var vals [][]byte
98-
for _, k := range keys {
99-
// TODO at the moment, do not include the post-data
100-
// val, _ := root.Get(k, nil)
101-
// vals = append(vals, val)
102-
vals = append(vals, keyvals[string(k)])
103-
}
97+
// NOTE this is leftover code from the time the proof was
98+
// made against the POST state. Since proofs are expected
99+
// to prove PRE and POST state in the future, I'm leaving
100+
// this for reference - eventhough it's unlikely that the
101+
// final version will look like this, but you never know.
102+
// var vals [][]byte
103+
// for _, k := range keys {
104+
// // TODO at the moment, do not include the post-data
105+
// // val, _ := root.Get(k, nil)
106+
// // vals = append(vals, val)
107+
// vals = append(vals, keyvals[string(k)])
108+
// }
104109

105110
cfg := GetConfig()
106111
mpArg := ipa.CreateMultiProof(tr, cfg.conf, pe.Cis, pe.Fis, pe.Zis)
@@ -127,7 +132,7 @@ func MakeVerkleMultiProof(root VerkleNode, keys [][]byte, keyvals map[string][]b
127132
ExtStatus: es,
128133
PoaStems: poas,
129134
Keys: keys,
130-
Values: vals,
135+
Values: pe.Vals,
131136
}
132137
return proof, pe.Cis, pe.Zis, pe.Yis, nil
133138
}
@@ -353,7 +358,7 @@ func TreeFromProof(proof *Proof, rootC *Point) (VerkleNode, error) {
353358
continue
354359
}
355360

356-
if bytes.Equal(k, info[string(p)].stem) {
361+
if bytes.Equal(k[:31], info[string(p)].stem) {
357362
values[k[31]] = proof.Values[i]
358363
}
359364
}

proof_test.go

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestProofVerifyTwoLeaves(t *testing.T) {
4141
root.Insert(ffx32KeyTest, zeroKeyTest, nil)
4242
root.Commit()
4343

44-
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{ffx32KeyTest}, map[string][]byte{string(ffx32KeyTest): zeroKeyTest})
44+
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{ffx32KeyTest})
4545

4646
cfg := GetConfig()
4747
if !VerifyVerkleProof(proof, cis, zis, yis, cfg) {
@@ -61,7 +61,7 @@ func TestProofVerifyMultipleLeaves(t *testing.T) {
6161
root.Insert(key, fourtyKeyTest, nil)
6262
}
6363

64-
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{keys[0]}, map[string][]byte{string(keys[0]): fourtyKeyTest})
64+
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{keys[0]})
6565

6666
cfg := GetConfig()
6767
if !VerifyVerkleProof(proof, cis, zis, yis, cfg) {
@@ -73,17 +73,15 @@ func TestMultiProofVerifyMultipleLeaves(t *testing.T) {
7373
const leafCount = 1000
7474

7575
keys := make([][]byte, leafCount)
76-
kv := make(map[string][]byte)
7776
root := New()
7877
for i := 0; i < leafCount; i++ {
7978
key := make([]byte, 32)
8079
rand.Read(key)
8180
keys[i] = key
8281
root.Insert(key, fourtyKeyTest, nil)
83-
kv[string(key)] = fourtyKeyTest
8482
}
8583

86-
proof, _, _, _, _ := MakeVerkleMultiProof(root, keys[0:2], kv)
84+
proof, _, _, _, _ := MakeVerkleMultiProof(root, keys[0:2])
8785

8886
pe, _, _, err := GetCommitmentsForMultiproof(root, keys[0:2])
8987
if err != nil {
@@ -100,12 +98,10 @@ func TestMultiProofVerifyMultipleLeavesWithAbsentStem(t *testing.T) {
10098

10199
var keys [][]byte
102100
var absentstem [31]byte
103-
kv := make(map[string][]byte)
104101
root := New()
105102
for i := 0; i < leafCount; i++ {
106103
key := make([]byte, 32)
107104
key[2] = byte(i)
108-
kv[string(key)] = fourtyKeyTest
109105
root.Insert(key, fourtyKeyTest, nil)
110106
if i%2 == 0 {
111107
keys = append(keys, key)
@@ -119,7 +115,7 @@ func TestMultiProofVerifyMultipleLeavesWithAbsentStem(t *testing.T) {
119115
absent[3] = 1 // and the stem differs
120116
keys = append(keys, absent)
121117

122-
proof, _, _, _, _ := MakeVerkleMultiProof(root, keys, kv)
118+
proof, _, _, _, _ := MakeVerkleMultiProof(root, keys)
123119

124120
pe, _, isabsent, err := GetCommitmentsForMultiproof(root, keys)
125121
if err != nil {
@@ -139,17 +135,14 @@ func TestMultiProofVerifyMultipleLeavesWithAbsentStem(t *testing.T) {
139135
}
140136

141137
func TestMultiProofVerifyMultipleLeavesCommitmentRedundancy(t *testing.T) {
142-
kv := make(map[string][]byte)
143138
keys := make([][]byte, 2)
144139
root := New()
145140
keys[0] = zeroKeyTest
146-
kv[string(zeroKeyTest)] = fourtyKeyTest
147141
root.Insert(keys[0], fourtyKeyTest, nil)
148142
keys[1] = oneKeyTest
149-
kv[string(oneKeyTest)] = fourtyKeyTest
150143
root.Insert(keys[1], fourtyKeyTest, nil)
151144

152-
proof, _, _, _, _ := MakeVerkleMultiProof(root, keys, kv)
145+
proof, _, _, _, _ := MakeVerkleMultiProof(root, keys)
153146

154147
pe, _, _, err := GetCommitmentsForMultiproof(root, keys)
155148
if err != nil {
@@ -166,7 +159,7 @@ func TestProofOfAbsenceInternalVerify(t *testing.T) {
166159
root.Insert(zeroKeyTest, zeroKeyTest, nil)
167160
root.Insert(oneKeyTest, zeroKeyTest, nil)
168161

169-
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{ffx32KeyTest}, map[string][]byte{})
162+
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{ffx32KeyTest})
170163

171164
cfg := GetConfig()
172165
if !VerifyVerkleProof(proof, cis, zis, yis, cfg) {
@@ -179,7 +172,7 @@ func TestProofOfAbsenceLeafVerify(t *testing.T) {
179172
root.Insert(zeroKeyTest, zeroKeyTest, nil)
180173
root.Insert(ffx32KeyTest, zeroKeyTest, nil)
181174

182-
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{oneKeyTest}, map[string][]byte{})
175+
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{oneKeyTest})
183176

184177
cfg := GetConfig()
185178
if !VerifyVerkleProof(proof, cis, zis, yis, cfg) {
@@ -196,7 +189,7 @@ func TestProofOfAbsenceLeafVerifyOtherSuffix(t *testing.T) {
196189
return ret
197190
}()
198191

199-
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{key}, map[string][]byte{})
192+
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{key})
200193

201194
cfg := GetConfig()
202195
if !VerifyVerkleProof(proof, cis, zis, yis, cfg) {
@@ -213,7 +206,7 @@ func TestProofOfAbsenceStemVerify(t *testing.T) {
213206
return ret
214207
}()
215208

216-
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{key}, map[string][]byte{})
209+
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{key})
217210

218211
cfg := GetConfig()
219212
if !VerifyVerkleProof(proof, cis, zis, yis, cfg) {
@@ -235,7 +228,7 @@ func BenchmarkProofCalculation(b *testing.B) {
235228
b.ReportAllocs()
236229

237230
for i := 0; i < b.N; i++ {
238-
MakeVerkleMultiProof(root, [][]byte{keys[len(keys)/2]}, map[string][]byte{})
231+
MakeVerkleMultiProof(root, [][]byte{keys[len(keys)/2]})
239232
}
240233
}
241234

@@ -250,7 +243,7 @@ func BenchmarkProofVerification(b *testing.B) {
250243
}
251244

252245
root.Commit()
253-
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{keys[len(keys)/2]}, map[string][]byte{})
246+
proof, cis, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{keys[len(keys)/2]})
254247

255248
b.ResetTimer()
256249
b.ReportAllocs()
@@ -273,7 +266,7 @@ func TestProofSerializationNoAbsentStem(t *testing.T) {
273266
root.Insert(key, fourtyKeyTest, nil)
274267
}
275268

276-
proof, _, _, _, _ := MakeVerkleMultiProof(root, [][]byte{keys[0]}, map[string][]byte{})
269+
proof, _, _, _, _ := MakeVerkleMultiProof(root, [][]byte{keys[0]})
277270

278271
vp, statediff, err := SerializeProof(proof)
279272
if err != nil {
@@ -307,7 +300,7 @@ func TestProofSerializationWithAbsentStem(t *testing.T) {
307300
absentkey[2] = 2
308301
absentkey[3] = 1
309302

310-
proof, _, _, _, _ := MakeVerkleMultiProof(root, [][]byte{absentkey[:]}, map[string][]byte{})
303+
proof, _, _, _, _ := MakeVerkleMultiProof(root, [][]byte{absentkey[:]})
311304

312305
vp, statediff, err := SerializeProof(proof)
313306
if err != nil {
@@ -343,7 +336,7 @@ func TestProofDeserialize(t *testing.T) {
343336
absentkey[2] = 2
344337
absentkey[3] = 1
345338

346-
proof, _, _, _, _ := MakeVerkleMultiProof(root, [][]byte{absentkey[:]}, map[string][]byte{})
339+
proof, _, _, _, _ := MakeVerkleMultiProof(root, [][]byte{absentkey[:]})
347340

348341
vp, statediff, err := SerializeProof(proof)
349342
if err != nil {
@@ -371,7 +364,7 @@ func TestProofOfAbsenceEdgeCase(t *testing.T) {
371364
root.Commit()
372365

373366
ret, _ := hex.DecodeString("0303030303030303030303030303030303030303030303030303030303030303")
374-
proof, cs, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{ret}, map[string][]byte{string(ret): nil})
367+
proof, cs, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{ret})
375368
cfg := GetConfig()
376369
if !VerifyVerkleProof(proof, cs, zis, yis, cfg) {
377370
t.Fatal("could not verify proof")
@@ -388,7 +381,7 @@ func TestProofOfAbsenceOtherMultipleLeaves(t *testing.T) {
388381

389382
ret1, _ := hex.DecodeString("0303030303030303030303030303030303030303030303030303030303030300")
390383
ret2, _ := hex.DecodeString("0303030303030303030303030303030303030303030303030303030303030301")
391-
proof, cs, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{ret1, ret2}, map[string][]byte{string(ret1): nil, string(ret2): nil})
384+
proof, cs, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{ret1, ret2})
392385
cfg := GetConfig()
393386
if !VerifyVerkleProof(proof, cs, zis, yis, cfg) {
394387
t.Fatal("could not verify proof")
@@ -407,7 +400,7 @@ func TestProofOfAbsenceNoneMultipleStems(t *testing.T) {
407400

408401
ret1, _ := hex.DecodeString("0303030303030303030303030303030303030303030303030303030303030300")
409402
ret2, _ := hex.DecodeString("0303030303030303030303030303030303030303030303030303030303030200")
410-
proof, cs, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{ret1, ret2}, map[string][]byte{string(ret1): nil, string(ret2): nil})
403+
proof, cs, zis, yis, _ := MakeVerkleMultiProof(root, [][]byte{ret1, ret2})
411404
cfg := GetConfig()
412405
if !VerifyVerkleProof(proof, cs, zis, yis, cfg) {
413406
t.Fatal("could not verify proof")
@@ -576,7 +569,7 @@ func TestStatelessDeserialize(t *testing.T) {
576569
root.Insert(k, fourtyKeyTest, nil)
577570
}
578571

579-
proof, _, _, _, _ := MakeVerkleMultiProof(root, keylist{zeroKeyTest, fourtyKeyTest}, map[string][]byte{string(zeroKeyTest): fourtyKeyTest, string(fourtyKeyTest): fourtyKeyTest})
572+
proof, _, _, _, _ := MakeVerkleMultiProof(root, keylist{zeroKeyTest, fourtyKeyTest})
580573

581574
serialized, statediff, err := SerializeProof(proof)
582575
if err != nil {
@@ -607,13 +600,13 @@ func TestStatelessDeserialize(t *testing.T) {
607600
}
608601
}
609602

610-
func TestStatelessDeserializeMissginChildNode(t *testing.T) {
603+
func TestStatelessDeserializeMissingChildNode(t *testing.T) {
611604
root := New()
612605
for _, k := range [][]byte{zeroKeyTest, oneKeyTest, ffx32KeyTest} {
613606
root.Insert(k, fourtyKeyTest, nil)
614607
}
615608

616-
proof, _, _, _, _ := MakeVerkleMultiProof(root, keylist{zeroKeyTest, fourtyKeyTest}, map[string][]byte{string(zeroKeyTest): fourtyKeyTest, string(fourtyKeyTest): nil})
609+
proof, _, _, _, _ := MakeVerkleMultiProof(root, keylist{zeroKeyTest, fourtyKeyTest})
617610

618611
serialized, statediff, err := SerializeProof(proof)
619612
if err != nil {
@@ -649,7 +642,7 @@ func TestStatelessDeserializeDepth2(t *testing.T) {
649642
root.Insert(k, fourtyKeyTest, nil)
650643
}
651644

652-
proof, _, _, _, _ := MakeVerkleMultiProof(root, keylist{zeroKeyTest, key1}, map[string][]byte{string(zeroKeyTest): fourtyKeyTest, string(key1): nil})
645+
proof, _, _, _, _ := MakeVerkleMultiProof(root, keylist{zeroKeyTest, key1})
653646

654647
serialized, statediff, err := SerializeProof(proof)
655648
if err != nil {

tree.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ type ProofElements struct {
9999
Yis []*Fr
100100
Fis [][]Fr
101101
ByPath map[string]*Point // Gather commitments by path
102+
Vals [][]byte // list of values read from the tree
102103

103104
// dedups flags the presence of each (Ci,zi) tuple
104105
dedups map[*Point]map[byte]struct{}
@@ -146,6 +147,8 @@ func (pe *ProofElements) Merge(other *ProofElements) {
146147
pe.ByPath[path] = C
147148
}
148149
}
150+
151+
pe.Vals = append(pe.Vals, other.Vals...)
149152
}
150153

151154
const (
@@ -810,6 +813,7 @@ func (n *InternalNode) GetProofItems(keys keylist) (*ProofElements, []byte, [][]
810813
// repeated as many times? It would be wasteful, so the
811814
// decoding code has to be aware of this corner case.
812815
esses = append(esses, extStatusAbsentEmpty|((n.depth+1)<<3))
816+
pe.Vals = append(pe.Vals, nil)
813817
continue
814818
}
815819

@@ -1271,6 +1275,7 @@ func (n *LeafNode) GetProofItems(keys keylist) (*ProofElements, []byte, [][]byte
12711275
Zis: []byte{0, 1},
12721276
Yis: []*Fr{&poly[0], &poly[1]}, // Should be 0
12731277
Fis: [][]Fr{poly[:], poly[:]},
1278+
Vals: make([][]byte, 0, len(keys)),
12741279
ByPath: map[string]*Point{},
12751280
}
12761281

@@ -1321,6 +1326,7 @@ func (n *LeafNode) GetProofItems(keys keylist) (*ProofElements, []byte, [][]byte
13211326
if len(esses) == 0 {
13221327
esses = append(esses, extStatusAbsentOther|(n.depth<<3))
13231328
poass = append(poass, n.stem)
1329+
pe.Vals = append(pe.Vals, nil)
13241330
}
13251331
continue
13261332
}
@@ -1357,6 +1363,7 @@ func (n *LeafNode) GetProofItems(keys keylist) (*ProofElements, []byte, [][]byte
13571363
// as all the information is available before fillSuffixTreePoly
13581364
// has to be called, save the count.
13591365
esses = append(esses, extStatusAbsentEmpty|(n.depth<<3))
1366+
pe.Vals = append(pe.Vals, nil)
13601367
continue
13611368
}
13621369

@@ -1384,6 +1391,7 @@ func (n *LeafNode) GetProofItems(keys keylist) (*ProofElements, []byte, [][]byte
13841391
pe.Zis = append(pe.Zis, 2*suffix, 2*suffix+1)
13851392
pe.Yis = append(pe.Yis, &leaves[0], &leaves[1])
13861393
pe.Fis = append(pe.Fis, suffPoly[:], suffPoly[:])
1394+
pe.Vals = append(pe.Vals, n.values[key[31]])
13871395
if len(esses) == 0 || esses[len(esses)-1] != extStatusPresent|(n.depth<<3) {
13881396
esses = append(esses, extStatusPresent|(n.depth<<3))
13891397
}

tree_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ func TestRustBanderwagonBlock48(t *testing.T) {
11221122

11231123
r := tree.Commit()
11241124

1125-
proof, cis, zis, yis, _ := MakeVerkleMultiProof(tree, keys, initialVals)
1125+
proof, cis, zis, yis, _ := MakeVerkleMultiProof(tree, keys)
11261126
vp, statediff, err := SerializeProof(proof)
11271127
if err != nil {
11281128
t.Fatal(err)

0 commit comments

Comments
 (0)