Skip to content

Commit 66be553

Browse files
committed
utils: export proofPositions
proofpositions is needed for the new block download protocol that requires the client to compute for which proof positions it needs. Exporting gives utreexod the ability to call it.
1 parent c3014ac commit 66be553

7 files changed

+32
-31
lines changed

mappollard.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ func (m *MapPollard) undoDeletion(proof Proof, hashes []Hash) error {
760760
// Calculate the positions of the proofs and translate them if needed and
761761
// then place in the proof hashes into the calculated positions.
762762
sortedTargets := copySortedFunc(proof.Targets, uint64Less)
763-
proofPos, _ := proofPositions(sortedTargets, m.NumLeaves, treeRows(m.NumLeaves))
763+
proofPos, _ := ProofPositions(sortedTargets, m.NumLeaves, treeRows(m.NumLeaves))
764764
if treeRows(m.NumLeaves) != m.TotalRows {
765765
proofPos = m.trimProofPos(proofPos, m.NumLeaves)
766766
proofPos = translatePositions(proofPos, treeRows(m.NumLeaves), m.TotalRows)
@@ -952,7 +952,7 @@ func (m *MapPollard) Prove(proveHashes []Hash) (Proof, error) {
952952
targets := copySortedFunc(origTargets, uint64Less)
953953

954954
// The positions of the hashes we need to prove the passed in targets.
955-
proofPos, _ := proofPositions(targets, m.NumLeaves, m.TotalRows)
955+
proofPos, _ := ProofPositions(targets, m.NumLeaves, m.TotalRows)
956956

957957
// Go through all the needed positions and grab the hashes for them.
958958
// If the node doesn't exist, check that it's calculateable. If it is,
@@ -997,7 +997,7 @@ func (m *MapPollard) VerifyPartialProof(origTargets []uint64, delHashes, proofHa
997997
targets := copySortedFunc(origTargets, uint64Less)
998998

999999
// Figure out what hashes at which positions are needed.
1000-
proofPositions, _ := proofPositions(targets, m.NumLeaves, treeRows(m.NumLeaves))
1000+
proofPositions, _ := ProofPositions(targets, m.NumLeaves, treeRows(m.NumLeaves))
10011001

10021002
// Translate the proof positions if needed.
10031003
if treeRows(m.NumLeaves) != m.TotalRows {
@@ -1043,7 +1043,7 @@ func (m *MapPollard) GetMissingPositions(origTargets []uint64) []uint64 {
10431043
targets := copySortedFunc(origTargets, uint64Less)
10441044

10451045
// Generate the positions needed to prove this.
1046-
proofPos, _ := proofPositions(targets, m.NumLeaves, treeRows(m.NumLeaves))
1046+
proofPos, _ := ProofPositions(targets, m.NumLeaves, treeRows(m.NumLeaves))
10471047
if treeRows(m.NumLeaves) != m.TotalRows {
10481048
proofPos = translatePositions(proofPos, treeRows(m.NumLeaves), m.TotalRows)
10491049
}
@@ -1139,7 +1139,7 @@ func (m *MapPollard) ingest(delHashes []Hash, proof Proof) error {
11391139
}
11401140

11411141
// Calculate and ingest the proof.
1142-
proofPos, _ := proofPositions(hnp.positions, m.NumLeaves, m.TotalRows)
1142+
proofPos, _ := ProofPositions(hnp.positions, m.NumLeaves, m.TotalRows)
11431143
if treeRows(m.NumLeaves) != m.TotalRows && len(proofPos) != len(proof.Proof) {
11441144
proofPos = m.trimProofPos(proofPos, m.NumLeaves)
11451145
}

mappollard_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (m *MapPollard) checkPruned() error {
7878
m.CachedLeaves.ForEach(func(_ Hash, v uint64) error {
7979
neededPos[v] = struct{}{}
8080

81-
needs, computables := proofPositions([]uint64{v}, m.NumLeaves, m.TotalRows)
81+
needs, computables := ProofPositions([]uint64{v}, m.NumLeaves, m.TotalRows)
8282
for _, need := range needs {
8383
neededPos[need] = struct{}{}
8484
}
@@ -503,8 +503,8 @@ func FuzzMapPollardPrune(f *testing.F) {
503503
slices.Sort(prunedPositions)
504504

505505
// Calculate the nodes that should not exist after the prune.
506-
shouldNotExist, _ := proofPositions(prunedPositions, acc.NumLeaves, acc.TotalRows)
507-
exist, _ := proofPositions(targets, acc.NumLeaves, acc.TotalRows)
506+
shouldNotExist, _ := ProofPositions(prunedPositions, acc.NumLeaves, acc.TotalRows)
507+
exist, _ := ProofPositions(targets, acc.NumLeaves, acc.TotalRows)
508508
shouldNotExist = subtractSortedSlice(shouldNotExist, exist, uint64Cmp)
509509

510510
// Prune the randomly chosen hashes from the accumulator.
@@ -674,7 +674,7 @@ func TestGetMissingPositions(t *testing.T) {
674674
}
675675

676676
// Calculate the positions actually needed.
677-
needs, _ := proofPositions(proves, p.NumLeaves, treeRows(p.NumLeaves))
677+
needs, _ := ProofPositions(proves, p.NumLeaves, treeRows(p.NumLeaves))
678678
if treeRows(p.NumLeaves) != p.TotalRows {
679679
needs = translatePositions(needs, treeRows(p.NumLeaves), p.TotalRows)
680680
}

prove.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (p *Pollard) Prove(hashes []Hash) (Proof, error) {
8080
sort.Slice(sortedTargets, func(a, b int) bool { return sortedTargets[a] < sortedTargets[b] })
8181

8282
// Get the positions of all the hashes that are needed to prove the targets
83-
proofPositions, _ := proofPositions(sortedTargets, p.NumLeaves, treeRows(p.NumLeaves))
83+
proofPositions, _ := ProofPositions(sortedTargets, p.NumLeaves, treeRows(p.NumLeaves))
8484

8585
// Fetch all the proofs from the accumulator.
8686
proof.Proof = make([]Hash, len(proofPositions))
@@ -760,12 +760,12 @@ func GetMissingPositions(numLeaves uint64, proofTargets, desiredTargets []uint64
760760
}
761761

762762
// desiredPositions are all the positions that are needed to proof the desiredTargets.
763-
desiredPositions, _ := proofPositions(desiredTargets, numLeaves, forestRows)
763+
desiredPositions, _ := ProofPositions(desiredTargets, numLeaves, forestRows)
764764

765765
// havePositions represent all the positions in the tree we already have access to.
766766
// Since targets and computablePositions are something we already have, append
767767
// those to the havePositions.
768-
havePositions, computablePos := proofPositions(targets, numLeaves, forestRows)
768+
havePositions, computablePos := ProofPositions(targets, numLeaves, forestRows)
769769
havePositions = append(havePositions, targets...)
770770
havePositions = append(havePositions, computablePos...)
771771
sort.Slice(havePositions, func(a, b int) bool { return havePositions[a] < havePositions[b] })
@@ -783,12 +783,12 @@ func AddProof(proofA, proofB Proof, targetHashesA, targetHashesB []Hash, numLeav
783783

784784
// Calculate proof hashes for proof A and add positions to the proof hashes.
785785
targetsA := copySortedFunc(proofA.Targets, uint64Less)
786-
proofPosA, calculateableA := proofPositions(targetsA, numLeaves, totalRows)
786+
proofPosA, calculateableA := ProofPositions(targetsA, numLeaves, totalRows)
787787
proofAndPosA := hashAndPos{proofPosA, proofA.Proof}
788788

789789
// Calculate proof hashes for proof B and add positions to the proof hashes.
790790
targetsB := copySortedFunc(proofB.Targets, uint64Less)
791-
proofPosB, calculateableB := proofPositions(targetsB, numLeaves, totalRows)
791+
proofPosB, calculateableB := ProofPositions(targetsB, numLeaves, totalRows)
792792
proofAndPosB := hashAndPos{proofPosB, proofB.Proof}
793793

794794
// Add the proof hashes of proofA and proofB.
@@ -896,12 +896,12 @@ func (p *Proof) updateProofRemove(blockTargets []uint64, cachedHashes []Hash, up
896896

897897
// Attach positions to the proofs.
898898
sortedCachedTargets := copySortedFunc(p.Targets, uint64Less)
899-
proofPos, _ := proofPositions(sortedCachedTargets, numLeaves, totalRows)
899+
proofPos, _ := ProofPositions(sortedCachedTargets, numLeaves, totalRows)
900900
oldProofs := toHashAndPos(proofPos, p.Proof)
901901
newProofs := hashAndPos{make([]uint64, 0, len(p.Proof)), make([]Hash, 0, len(p.Proof))}
902902

903903
// Grab all the positions of the needed proof hashes.
904-
neededPos, _ := proofPositions(targetsWithHash.positions, numLeaves, totalRows)
904+
neededPos, _ := ProofPositions(targetsWithHash.positions, numLeaves, totalRows)
905905

906906
// Grab the un-needed positions. These are un-needed as they were proofs
907907
// for the now deleted targets.
@@ -1026,7 +1026,7 @@ func pruneEdges(hnp hashAndPos, numAdds, numLeaves uint64, forestRows, prevFores
10261026
// proof hashes that could not have existed before the add.
10271027
func (p *Proof) undoAdd(numAdds, numLeaves uint64, cachedHashes []Hash, toDestroy []uint64) ([]Hash, error) {
10281028
targetsWithHash := toHashAndPos(p.Targets, cachedHashes)
1029-
proofPos, _ := proofPositions(targetsWithHash.positions, numLeaves, treeRows(numLeaves))
1029+
proofPos, _ := ProofPositions(targetsWithHash.positions, numLeaves, treeRows(numLeaves))
10301030
proofWithPos := toHashAndPos(proofPos, p.Proof)
10311031

10321032
forestRows := treeRows(numLeaves)
@@ -1129,7 +1129,7 @@ func (p *Proof) undoAdd(numAdds, numLeaves uint64, cachedHashes []Hash, toDestro
11291129

11301130
// There may be extra proof hashes that we don't need anymore. Calculate the
11311131
// needed positions and remove the rest.
1132-
neededProofPos, _ := proofPositions(targetsWithHash.positions, numLeaves-numAdds, prevForestRows)
1132+
neededProofPos, _ := ProofPositions(targetsWithHash.positions, numLeaves-numAdds, prevForestRows)
11331133
proofWithPos = getHashAndPosSubset(proofWithPos, neededProofPos)
11341134

11351135
// Set the proof.
@@ -1150,7 +1150,7 @@ func (p *Proof) undoDel(blockTargets []uint64, blockHashes, cachedHashes []Hash,
11501150
}
11511151

11521152
targetsWithHashes := toHashAndPos(p.Targets, cachedHashes)
1153-
proofPos, _ := proofPositions(targetsWithHashes.positions, numLeaves, totalRows)
1153+
proofPos, _ := ProofPositions(targetsWithHashes.positions, numLeaves, totalRows)
11541154
proofWithPos := toHashAndPos(proofPos, p.Proof)
11551155

11561156
// Detwin the block targets.
@@ -1247,7 +1247,7 @@ func (p *Proof) undoDel(blockTargets []uint64, blockHashes, cachedHashes []Hash,
12471247

12481248
// Only extract the proof hashes that are needed for the targets after
12491249
// the remap.
1250-
neededProofPos, _ := proofPositions(targetsWithHashes.positions, numLeaves, totalRows)
1250+
neededProofPos, _ := ProofPositions(targetsWithHashes.positions, numLeaves, totalRows)
12511251
proofWithPos = getHashAndPosSubset(proofWithPos, neededProofPos)
12521252

12531253
p.Proof = proofWithPos.hashes
@@ -1286,7 +1286,7 @@ func GetProofSubset(proof Proof, hashes []Hash, wants []uint64, numLeaves uint64
12861286
sort.Sort(posAndHashes)
12871287

12881288
// Put positions onto the proof hashes.
1289-
positions, _ := proofPositions(proofTargetsCopy, numLeaves, treeRows(numLeaves))
1289+
positions, _ := ProofPositions(proofTargetsCopy, numLeaves, treeRows(numLeaves))
12901290
proofPos := toHashAndPos(positions, proof.Proof)
12911291

12921292
// Merge the proof positions and its hashes along with the calculated intermediate nodes
@@ -1298,7 +1298,7 @@ func GetProofSubset(proof Proof, hashes []Hash, wants []uint64, numLeaves uint64
12981298
targetHashesWithPos = getHashAndPosSubset(targetHashesWithPos, sortedWants)
12991299

13001300
// Grab the positions that we need to prove the wants.
1301-
wantProofPos, _ := proofPositions(targetHashesWithPos.positions, numLeaves, treeRows(numLeaves))
1301+
wantProofPos, _ := ProofPositions(targetHashesWithPos.positions, numLeaves, treeRows(numLeaves))
13021302

13031303
// Extract the proof positions we want and then sanity check to see that we have everything.
13041304
posAndHashes = getHashAndPosSubset(posAndHashes, wantProofPos)
@@ -1350,7 +1350,7 @@ func (p *Proof) updateProofAdd(adds, cachedDelHashes []Hash, remembers []uint32,
13501350
origTargetsWithHash := toHashAndPos(p.Targets, cachedDelHashes)
13511351

13521352
// Attach positions to the proof.
1353-
proofPos, _ := proofPositions(origTargetsWithHash.positions, beforeNumLeaves, treeRows(beforeNumLeaves))
1353+
proofPos, _ := ProofPositions(origTargetsWithHash.positions, beforeNumLeaves, treeRows(beforeNumLeaves))
13541354
proofWithPos := toHashAndPos(proofPos, p.Proof)
13551355

13561356
// Remap the positions if we moved up a after the addition row.
@@ -1391,7 +1391,7 @@ func (p *Proof) updateProofAdd(adds, cachedDelHashes []Hash, remembers []uint32,
13911391
origTargetsWithHash = mergeSortedHashAndPos(remembersWithHash, origTargetsWithHash)
13921392

13931393
// Grab all the new nodes after this add.
1394-
neededProofPositions, _ := proofPositions(origTargetsWithHash.positions, beforeNumLeaves+uint64(len(adds)), treeRows(beforeNumLeaves+uint64(len(adds))))
1394+
neededProofPositions, _ := ProofPositions(origTargetsWithHash.positions, beforeNumLeaves+uint64(len(adds)), treeRows(beforeNumLeaves+uint64(len(adds))))
13951395

13961396
// Add all the new proof hashes to the proof.
13971397
newProofWithPos := hashAndPos{}

prove_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ func calcDelHashAndProof(p *Pollard, proof Proof, missingPositions, desiredPosit
6868
}
6969

7070
// Attach positions to the proof hashes.
71-
proofPos, _ := proofPositions(proof.Targets, p.NumLeaves, treeRows(p.NumLeaves))
71+
proofPos, _ := ProofPositions(proof.Targets, p.NumLeaves, treeRows(p.NumLeaves))
7272
currentHashes := toHashAndPos(proofPos, proof.Proof)
7373
// Append the needed hashes to the proof.
7474
currentHashes.AppendMany(neededHashes.positions, neededHashes.hashes)
7575

7676
// As new targets are added, we're able to calulate positions that we couldn't before. These positions
7777
// may already exist as proofs. Remove these as duplicates are not expected during proof verification.
78-
_, calculateables := proofPositions(desiredPositions, p.NumLeaves, treeRows(p.NumLeaves))
78+
_, calculateables := ProofPositions(desiredPositions, p.NumLeaves, treeRows(p.NumLeaves))
7979
for _, cal := range calculateables {
8080
idx := slices.IndexFunc(currentHashes.positions, func(elem uint64) bool { return elem == cal })
8181
if idx != -1 {
@@ -450,7 +450,7 @@ func FuzzUpdateProofRemove(f *testing.F) {
450450
pollardBeforeStr, p.String())
451451
}
452452

453-
cachedProofPos, _ := proofPositions(cachedProof.Targets, p.NumLeaves, treeRows(p.NumLeaves))
453+
cachedProofPos, _ := ProofPositions(cachedProof.Targets, p.NumLeaves, treeRows(p.NumLeaves))
454454
if len(cachedProofPos) != len(cachedProof.Proof) {
455455
t.Fatalf("FuzzUpdateProofRemove Fail. CachedProof has these hashes:\n%v\n"+
456456
"for these targets:\n%v\n"+

stump_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func checkUpdateData(updateData UpdateData, adds, delHashes, prevRoots []Hash, p
234234
*/
235235
// Attach the hashes to their positions.
236236
targetsWithHash := toHashAndPos(proof.Targets, delHashes)
237-
proofPos, _ := proofPositions(targetsWithHash.positions, updateData.PrevNumLeaves, treeRows(updateData.PrevNumLeaves))
237+
proofPos, _ := ProofPositions(targetsWithHash.positions, updateData.PrevNumLeaves, treeRows(updateData.PrevNumLeaves))
238238
proofWithPositions := toHashAndPos(proofPos, proof.Proof)
239239

240240
// Update accordingly.

utils.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,10 @@ func proofPosition(target uint64, numLeaves uint64, totalRows uint8) []uint64 {
553553
return proofs
554554
}
555555

556-
// proofPositions returns all the positions that are needed to prove targets passed in.
556+
// ProofPositions returns all the positions that are needed to prove targets passed in along with
557+
// all the positions that are able to be computed.
557558
// NOTE: the passed in targets MUST be sorted.
558-
func proofPositions(origTargets []uint64, numLeaves uint64, totalRows uint8) ([]uint64, []uint64) {
559+
func ProofPositions(origTargets []uint64, numLeaves uint64, totalRows uint8) ([]uint64, []uint64) {
559560
targets := make([]uint64, len(origTargets))
560561
copy(targets, origTargets)
561562

utils_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestProofPosition(t *testing.T) {
2727

2828
for _, test := range tests {
2929
got := proofPosition(test.position, test.numLeaves, test.totalRows)
30-
expect, _ := proofPositions([]uint64{test.position}, test.numLeaves, test.totalRows)
30+
expect, _ := ProofPositions([]uint64{test.position}, test.numLeaves, test.totalRows)
3131

3232
if !reflect.DeepEqual(got, expect) {
3333
t.Fatalf("expected %v, got %v for numleaves %d, totalrows %d",

0 commit comments

Comments
 (0)