Skip to content

Commit cb579cc

Browse files
committed
fix description, add comments
1 parent 96de546 commit cb579cc

9 files changed

Lines changed: 94 additions & 116 deletions

File tree

cmd/proofgen/main.go

Lines changed: 86 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,13 @@ func subtreeInclusionProbes(rootDir string) error {
387387

388388
func corruptedSubtreeInclusionProbes(dir string, leafIdx, treeSize uint64, proof [][]byte, root, leafHash []byte) error {
389389
happyPath := inclusionProbe{leafIdx, treeSize, root, leafHash, proof, "happy path", false}
390-
if err := writeSubtreeInclusionProbePair(dir, happyPath); err != nil {
390+
if err := convertToSubtreeInclusionProbesAndWrite(dir, happyPath); err != nil {
391391
return err
392392
}
393393

394394
probes := invalidInclusionProof(leafIdx, treeSize, proof, root, leafHash)
395395
for _, p := range probes {
396-
if err := writeSubtreeInclusionProbePair(dir, p); err != nil {
396+
if err := convertToSubtreeInclusionProbesAndWrite(dir, p); err != nil {
397397
return err
398398
}
399399
}
@@ -419,7 +419,7 @@ func singleEntrySubtreeInclusionProbes(dir string) error {
419419
{emptyHash, emptyHash, "empty root and leaf", true},
420420
} {
421421
probe := inclusionProbe{0, 1, p.root, p.leaf, proof, p.desc, p.wantErr}
422-
if err := writeSubtreeInclusionProbePair(dir, probe); err != nil {
422+
if err := convertToSubtreeInclusionProbesAndWrite(dir, probe); err != nil {
423423
return err
424424
}
425425
}
@@ -441,17 +441,17 @@ func staticSubtreeInclusionProbes(rootDir string) error {
441441
}
442442

443443
randomLeaf := inclusionProbe{p.index, p.size, []byte{}, sha256SomeHash, proof, "random leaf", true}
444-
if err := writeSubtreeInclusionProbePair(dir, randomLeaf); err != nil {
444+
if err := convertToSubtreeInclusionProbesAndWrite(dir, randomLeaf); err != nil {
445445
return err
446446
}
447447

448448
emptyRoot := inclusionProbe{p.index, p.size, sha256EmptyTreeHash, []byte{}, proof, "empty root", true}
449-
if err := writeSubtreeInclusionProbePair(dir, emptyRoot); err != nil {
449+
if err := convertToSubtreeInclusionProbesAndWrite(dir, emptyRoot); err != nil {
450450
return err
451451
}
452452

453453
emptyRootRandomLeaf := inclusionProbe{p.index, p.size, sha256EmptyTreeHash, sha256SomeHash, proof, "empty root and random leaf", true}
454-
if err := writeSubtreeInclusionProbePair(dir, emptyRootRandomLeaf); err != nil {
454+
if err := convertToSubtreeInclusionProbesAndWrite(dir, emptyRootRandomLeaf); err != nil {
455455
return err
456456
}
457457
}
@@ -467,145 +467,123 @@ func errorSubtreeInclusionProbes(rootDir string) error {
467467

468468
leafHash := rfc6962.DefaultHasher.HashLeaf(leaves[0])
469469

470-
tests := []struct {
471-
filename string
472-
probe subtreeInclusionProbe
473-
}{
470+
tests := []subtreeInclusionProbe{
474471
{
475-
filename: "everything-zero.json",
476-
probe: subtreeInclusionProbe{
477-
LeafIdx: 0,
478-
Start: 0,
479-
End: 0,
480-
Root: sha256EmptyTreeHash,
481-
LeafHash: leafHash,
482-
Proof: nil,
483-
Desc: "empty root",
484-
WantError: true,
485-
},
472+
LeafIdx: 0,
473+
Start: 0,
474+
End: 0,
475+
Root: sha256EmptyTreeHash,
476+
LeafHash: leafHash,
477+
Proof: nil,
478+
Desc: "everything zero",
479+
WantError: true,
486480
},
487481
{
488-
filename: "start-equals-end.json",
489-
probe: subtreeInclusionProbe{
490-
LeafIdx: 0,
491-
Start: 1,
492-
End: 1,
493-
Root: sha256EmptyTreeHash,
494-
LeafHash: leafHash,
495-
Proof: nil,
496-
Desc: "empty root",
497-
WantError: true,
498-
},
482+
LeafIdx: 0,
483+
Start: 1,
484+
End: 1,
485+
Root: sha256EmptyTreeHash,
486+
LeafHash: leafHash,
487+
Proof: nil,
488+
Desc: "start equals end",
489+
WantError: true,
499490
},
500491
{
501-
filename: "invalid-subtree.json",
502-
probe: subtreeInclusionProbe{
503-
LeafIdx: 3,
504-
Start: 3,
505-
End: 5,
506-
Root: sha256EmptyTreeHash,
507-
LeafHash: leafHash,
508-
Proof: nil,
509-
Desc: "empty root",
510-
WantError: true,
511-
},
492+
LeafIdx: 3,
493+
Start: 3,
494+
End: 5,
495+
Root: sha256EmptyTreeHash,
496+
LeafHash: leafHash,
497+
Proof: nil,
498+
Desc: "invalid subtree",
499+
WantError: true,
512500
},
513501
{
514-
filename: "invalid-large-subtree.json",
515-
probe: subtreeInclusionProbe{
516-
LeafIdx: 1,
517-
Start: 1,
518-
End: 9223372036854775810,
519-
Root: sha256EmptyTreeHash,
520-
LeafHash: leafHash,
521-
Proof: nil,
522-
Desc: "empty root",
523-
WantError: true,
524-
},
502+
LeafIdx: 1,
503+
Start: 1,
504+
End: 9223372036854775810,
505+
Root: sha256EmptyTreeHash,
506+
LeafHash: leafHash,
507+
Proof: nil,
508+
Desc: "invalid large subtree",
509+
WantError: true,
525510
},
526511
{
527-
filename: "oob-left.json",
528-
probe: subtreeInclusionProbe{
529-
LeafIdx: 0,
530-
Start: 1,
531-
End: 2,
532-
Root: sha256EmptyTreeHash,
533-
LeafHash: leafHash,
534-
Proof: nil,
535-
Desc: "empty root",
536-
WantError: true,
537-
},
512+
LeafIdx: 0,
513+
Start: 1,
514+
End: 2,
515+
Root: sha256EmptyTreeHash,
516+
LeafHash: leafHash,
517+
Proof: nil,
518+
Desc: "oob left",
519+
WantError: true,
538520
},
539521
{
540-
filename: "oob-right.json",
541-
probe: subtreeInclusionProbe{
542-
LeafIdx: 3,
543-
Start: 0,
544-
End: 2,
545-
Root: sha256EmptyTreeHash,
546-
LeafHash: leafHash,
547-
Proof: nil,
548-
Desc: "empty root",
549-
WantError: true,
550-
},
522+
LeafIdx: 3,
523+
Start: 0,
524+
End: 2,
525+
Root: sha256EmptyTreeHash,
526+
LeafHash: leafHash,
527+
Proof: nil,
528+
Desc: "oob right",
529+
WantError: true,
551530
},
552531
{
553-
filename: "oob-right-2.json",
554-
probe: subtreeInclusionProbe{
555-
LeafIdx: 3,
556-
Start: 0,
557-
End: 3,
558-
Root: sha256EmptyTreeHash,
559-
LeafHash: leafHash,
560-
Proof: nil,
561-
Desc: "empty root",
562-
WantError: true,
563-
},
532+
LeafIdx: 3,
533+
Start: 0,
534+
End: 3,
535+
Root: sha256EmptyTreeHash,
536+
LeafHash: leafHash,
537+
Proof: nil,
538+
Desc: "oob right 2",
539+
WantError: true,
564540
},
565541
{
566-
filename: "start-larger-than-end.json",
567-
probe: subtreeInclusionProbe{
568-
LeafIdx: 0,
569-
Start: 2,
570-
End: 1,
571-
Root: sha256EmptyTreeHash,
572-
LeafHash: leafHash,
573-
Proof: nil,
574-
Desc: "empty root",
575-
WantError: true,
576-
},
542+
LeafIdx: 0,
543+
Start: 2,
544+
End: 1,
545+
Root: sha256EmptyTreeHash,
546+
LeafHash: leafHash,
547+
Proof: nil,
548+
Desc: "start larger than end",
549+
WantError: true,
577550
},
578551
}
579552

580553
for _, tc := range tests {
581-
if err := writeSubtreeInclusionProbeWithFilename(dir, tc.filename, tc.probe); err != nil {
554+
if err := writeSubtreeInclusionProbe(dir, tc); err != nil {
582555
return err
583556
}
584557
}
585558

586559
return nil
587560
}
588561

589-
func writeSubtreeInclusionProbeWithFilename(dir, filename string, probe subtreeInclusionProbe) error {
562+
func writeSubtreeInclusionProbe(dir string, probe subtreeInclusionProbe) error {
563+
fn := fileName(probe.Desc)
564+
590565
probeJson, err := json.MarshalIndent(probe, "", " ")
591566
if err != nil {
592567
return fmt.Errorf("marshaling probe: %s", err)
593568
}
594569

595-
fileLocation := filepath.Join(dir, filename)
570+
fileLocation := filepath.Join(dir, fn)
596571
if err := os.WriteFile(fileLocation, probeJson, 0644); err != nil {
597-
return fmt.Errorf("writing probe: %s: %s", filename, err)
572+
return fmt.Errorf("writing probe: %s: %s", fn, err)
598573
}
599574

600575
return nil
601576
}
602577

603-
func writeSubtreeInclusionProbe(dir string, probe subtreeInclusionProbe) error {
604-
fn := fileName(probe.Desc)
605-
return writeSubtreeInclusionProbeWithFilename(dir, fn, probe)
606-
}
607-
608-
func writeSubtreeInclusionProbePair(dir string, p inclusionProbe) error {
578+
// convertToSubtreeInclusionProbesAndWrite generates subtree inclusion proofs
579+
// from inclusion proofs and writes them.
580+
//
581+
// An inclusion proof for an entry at index in a tree of a given size leads to
582+
// two subtree inclusion proofs:
583+
// - one for for an entry at index in a subtree of the same given size.
584+
// - a second one for an entry shifted by bitCeil(size) for the subtree
585+
// [bitCeil(size), size+bitCeil(size)).
586+
func convertToSubtreeInclusionProbesAndWrite(dir string, p inclusionProbe) error {
609587
sp1 := toSubtreeInclusionProbe(p)
610588
if err := writeSubtreeInclusionProbe(dir, sp1); err != nil {
611589
return err

testdata/subtreeinclusion/errors/everything-zero.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"root": "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
66
"leafHash": "bjQLnP+zepicpUTmu3gKLHiQHT+zNzh2hRGjBhevoB0=",
77
"proof": null,
8-
"desc": "empty root",
8+
"desc": "everything zero",
99
"wantErr": true
1010
}

testdata/subtreeinclusion/errors/invalid-large-subtree.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"root": "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
66
"leafHash": "bjQLnP+zepicpUTmu3gKLHiQHT+zNzh2hRGjBhevoB0=",
77
"proof": null,
8-
"desc": "empty root",
8+
"desc": "invalid large subtree",
99
"wantErr": true
1010
}

testdata/subtreeinclusion/errors/invalid-subtree.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"root": "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
66
"leafHash": "bjQLnP+zepicpUTmu3gKLHiQHT+zNzh2hRGjBhevoB0=",
77
"proof": null,
8-
"desc": "empty root",
8+
"desc": "invalid subtree",
99
"wantErr": true
1010
}

testdata/subtreeinclusion/errors/oob-left.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"root": "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
66
"leafHash": "bjQLnP+zepicpUTmu3gKLHiQHT+zNzh2hRGjBhevoB0=",
77
"proof": null,
8-
"desc": "empty root",
8+
"desc": "oob left",
99
"wantErr": true
1010
}

testdata/subtreeinclusion/errors/oob-right-2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"root": "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
66
"leafHash": "bjQLnP+zepicpUTmu3gKLHiQHT+zNzh2hRGjBhevoB0=",
77
"proof": null,
8-
"desc": "empty root",
8+
"desc": "oob right 2",
99
"wantErr": true
1010
}

testdata/subtreeinclusion/errors/oob-right.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"root": "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
66
"leafHash": "bjQLnP+zepicpUTmu3gKLHiQHT+zNzh2hRGjBhevoB0=",
77
"proof": null,
8-
"desc": "empty root",
8+
"desc": "oob right",
99
"wantErr": true
1010
}

testdata/subtreeinclusion/errors/start-equals-end.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"root": "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
66
"leafHash": "bjQLnP+zepicpUTmu3gKLHiQHT+zNzh2hRGjBhevoB0=",
77
"proof": null,
8-
"desc": "empty root",
8+
"desc": "start equals end",
99
"wantErr": true
1010
}

testdata/subtreeinclusion/errors/start-larger-than-end.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"root": "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
66
"leafHash": "bjQLnP+zepicpUTmu3gKLHiQHT+zNzh2hRGjBhevoB0=",
77
"proof": null,
8-
"desc": "empty root",
8+
"desc": "start larger than end",
99
"wantErr": true
1010
}

0 commit comments

Comments
 (0)