Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Bryce committed Sep 27, 2024
1 parent d75fd67 commit c91e8eb
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 26 deletions.
2 changes: 1 addition & 1 deletion massifs/peakstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func PeakStackMap(massifHeight uint8, mmrSize uint64) map[uint64]int {
// XXX:TODO there is likely a more efficient way to do this using
// PeaksBitmap or a variation of it, but this isn't a terribly hot path.
stackMap := map[uint64]int{}
iPeaks := mmr.Peaks(mmrSize)
iPeaks := mmr.PosPeaks(mmrSize)
for i, ip := range iPeaks {
if mmr.PosHeight(ip) < uint64(massifHeight-1) {
continue
Expand Down
4 changes: 2 additions & 2 deletions mmr/consistentroots.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var (
)

func ConsistentRoots(hasher hash.Hash, fromSize uint64, accumulatorfrom [][]byte, proofs [][][]byte) ([][]byte, error) {
frompeaks := Peaks(fromSize)
frompeaks := PosPeaks(fromSize)

if len(frompeaks) != len(proofs) {
return nil, ErrAccumulatorProofLen
Expand All @@ -21,7 +21,7 @@ func ConsistentRoots(hasher hash.Hash, fromSize uint64, accumulatorfrom [][]byte

for iacc := 0; iacc < len(accumulatorfrom); iacc++ {
// remembering that peaks are 1 based (for now)
root := IncludedRoot(hasher, frompeaks[iacc] - 1, accumulatorfrom[iacc], proofs[iacc])
root := IncludedRoot(hasher, frompeaks[iacc]-1, accumulatorfrom[iacc], proofs[iacc])
// The nature of MMR's is that many nodes are committed by the
// same accumulator peak, and that peak changes with
// low frequency.
Expand Down
6 changes: 3 additions & 3 deletions mmr/peaks.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// 1 3 6 10 13 18
// / \ / \ / \ / \ / \
// 0 1 2 4 5 8 9 11 12 16 17
func Peaks(mmrSize uint64) []uint64 {
func PosPeaks(mmrSize uint64) []uint64 {
if mmrSize == 0 {
return nil
}
Expand Down Expand Up @@ -60,7 +60,7 @@ func Peaks(mmrSize uint64) []uint64 {
func PeakHashes(store indexStoreGetter, mmrSize uint64) ([][]byte, error) {
// Note: we can implement this directly any time we want, but lets re-use the testing for Peaks
var path [][]byte
for _, pos := range Peaks(mmrSize) {
for _, pos := range PosPeaks(mmrSize) {
stored, err := store.Get(pos - 1)
if err != nil {
return nil, err
Expand Down Expand Up @@ -92,7 +92,7 @@ func PeakHashes(store indexStoreGetter, mmrSize uint64) ([][]byte, error) {
//
// Example:
//
// peaks = Peaks(18) = [14, 17]
// peaks = PosPeaks(18) = [14, 17]
// peakBits = LeafCount(18) = 101
// 1 = d = proof len for 6
// 2 = IndexHeight(6)
Expand Down
12 changes: 6 additions & 6 deletions mmr/peaks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestPeaks(t *testing.T) {
func TestPosPeaks(t *testing.T) {
type args struct {
mmrSize uint64
}
Expand All @@ -30,8 +30,8 @@ func TestPeaks(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Peaks(tt.args.mmrSize); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Peaks() = %v, want %v", got, tt.want)
if got := PosPeaks(tt.args.mmrSize); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PosPeaks() = %v, want %v", got, tt.want)
}
})
}
Expand Down Expand Up @@ -66,8 +66,8 @@ func TestPeaksKAT_MMR39(t *testing.T) {
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%d", tt.mmrSize), func(t *testing.T) {
if got := Peaks(tt.mmrSize); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Peaks() = %v, want %v", got, tt.want)
if got := PosPeaks(tt.mmrSize); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PosPeaks() = %v, want %v", got, tt.want)
}
})
}
Expand Down Expand Up @@ -236,7 +236,7 @@ func TestPeaks2(t *testing.T) {
t.Run(fmt.Sprintf("Peaks2(%d)", pos), func(t *testing.T) {
fmt.Printf("Peaks2(mmrSize: %d):", pos)
peaks := PeaksOld(pos)
peaks2 := Peaks(pos)
peaks2 := PosPeaks(pos)
assert.Equal(t, peaks, peaks2)
fmt.Printf(" %v", peaks)
fmt.Printf("\n")
Expand Down
4 changes: 2 additions & 2 deletions mmr/proofbagged.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// The root is defined as the 'bagging' of all peaks, starting with the highest.
// So its simply a call to BagPeaksRHS for _all_ peaks in the MMR of the provided size.
func GetRoot(mmrSize uint64, store indexStoreGetter, hasher hash.Hash) ([]byte, error) {
peaks := Peaks(mmrSize)
peaks := PosPeaks(mmrSize)
// The root is ALL the peaks. Note that bagging essentially accumulates them in a binary tree.
return BagPeaksRHS(store, hasher, 0, peaks)
}
Expand Down Expand Up @@ -66,7 +66,7 @@ func IndexProofBagged(mmrSize uint64, store indexStoreGetter, hasher hash.Hash,
return nil, err
}

peaks := Peaks(mmrSize)
peaks := PosPeaks(mmrSize)

if rightSibling, err = BagPeaksRHS(store, hasher, iLocalPeak+1, peaks); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion mmr/proofofconsistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func IndexConsistencyProof(
}

// Find the peaks corresponding to the previous mmr
peaksA := Peaks(mmrSizeA)
peaksA := PosPeaks(mmrSizeA)

// Now generate peak proofs against the new mmr size, using the peak indices
// as the input indices to prove
Expand Down
2 changes: 1 addition & 1 deletion mmr/proofofconsistency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func TestIndexConsistencyProofBagged(t *testing.T) {
return
}

iPeaks := Peaks(got.MMRSizeA)
iPeaks := PosPeaks(got.MMRSizeA)
peakHashesA, err := PeakBagRHS(store, hasher, 0, iPeaks)
if err != nil {
t.Errorf("PeakBagRHS: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion mmr/proofofconsistencybagged.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func IndexConsistencyProofBagged(
}

// Find the peaks corresponding to the previous mmr
peaksA := Peaks(mmrSizeA)
peaksA := PosPeaks(mmrSizeA)

// Now generate peak proofs against the new mmr size, using the peak indices
// as the input indices to prove
Expand Down
31 changes: 31 additions & 0 deletions mmr/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,40 @@ package mmr

import (
"bytes"
"errors"
"fmt"
"hash"
)

var (
ErrVerifyInclusionFailed = errors.New("verify inclusion failed")
)

func VerifyInclusion(
store indexStoreGetter, hasher hash.Hash, mmrSize uint64, leafHash []byte, iNode uint64, proof [][]byte,
) (bool, error) {

peaks, err := PeakHashes(store, mmrSize)
if err != nil {
return false, err
}

// Get the index of the peak commiting the proven element
ipeak := PeakIndex(LeafCount(mmrSize), len(proof))

if ipeak >= len(peaks) {
return false, fmt.Errorf(
"%w: accumulator index for proof out of range for the provided mmr size", ErrVerifyInclusionFailed)
}

root := IncludedRoot(hasher, iNode, leafHash, proof)
if !bytes.Equal(root, peaks[ipeak]) {
return false, fmt.Errorf(
"%w: proven root not present in the accumulator", ErrVerifyInclusionFailed)
}
return true, nil
}

// VerifyInclusionPath returns true if the leafHash combined with path, reproduces the provided root
//
// To facilitate the concatenated proof paths used for consistency proofs, it
Expand Down
2 changes: 1 addition & 1 deletion mmr/verifybagged.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func VerifyFirstInclusionPathBagged(
mmrSize uint64, hasher hash.Hash, leafHash []byte, iNode uint64, proof [][]byte, root []byte,
) (bool, int) {

peaks := Peaks(mmrSize)
peaks := PosPeaks(mmrSize)
peakMap := map[uint64]bool{}

// Deal with the degenerate case where iNode is a perfect peak. The proof will be nil.
Expand Down
2 changes: 1 addition & 1 deletion mmr/verifybagged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func TestVerify(t *testing.T) {
assert.Equal(t, tt.expectProofNodes.iLocalPeak, iLocalPeak, "local peak incorrect")
assert.Equal(t, getNodes(db, tt.expectProofNodes.local...), localPath)

peaks := Peaks(tt.args.mmrSize)
peaks := PosPeaks(tt.args.mmrSize)

peakBits := PeaksBitmap(tt.args.mmrSize)

Expand Down
4 changes: 2 additions & 2 deletions mmr/verifyconsistencybagged.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func VerifyConsistencyBagged(
// peak nodes must be at the same indices in mmr B for the update to be
// considered consistent. However, if mmr b has additional entries at all,
// some or all of those peaks from A will no longer be peaks in B.
peakPositions := Peaks(proof.MMRSizeA)
peakPositions := PosPeaks(proof.MMRSizeA)

var ok bool
iPeakHashA := 0
Expand Down Expand Up @@ -83,7 +83,7 @@ func CheckConsistencyBagged(
store indexStoreGetter, hasher hash.Hash,
cp ConsistencyProof, rootA []byte) (bool, []byte, error) {

iPeaks := Peaks(cp.MMRSizeA)
iPeaks := PosPeaks(cp.MMRSizeA)

// logger.Sugar.Infof(".... PeakBagRHS: %v", iPeaks)
peakHashesA, err := PeakBagRHS(store, hasher, 0, iPeaks)
Expand Down
10 changes: 5 additions & 5 deletions tests/massifs/localmassifreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestLocalMassifReaderGetVerifiedContext(t *testing.T) {
return nil, massifs.MMRState{}, err
}

peakIndices := mmr.Peaks(mmrSizeOld)
peakIndices := mmr.PosPeaks(mmrSizeOld)
// Remember, the peaks are *positions*
peaks, err := mmr.PeakHashes(mc, mmrSizeOld)
require.NoError(t, err)
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestLocalMassifReaderGetVerifiedContext(t *testing.T) {

// this time, tamper a peak after the seal, this simulates the
// case where the extension is inconsistent with the seal.
peaks := mmr.Peaks(mc.RangeCount())
peaks := mmr.PosPeaks(mc.RangeCount())

// Note: we take the *last* peak, because it corresponds to the
// most recent log entries. In this case we want the fresh
Expand Down Expand Up @@ -275,15 +275,15 @@ func TestLocalMassifReaderGetVerifiedContext(t *testing.T) {

mmrSizeOld := sizeBeforeLeaves(mc, 8)
require.GreaterOrEqual(t, mmrSizeOld, mc.Start.FirstIndex)
peaks := mmr.Peaks(mmrSizeOld)
peaks := mmr.PosPeaks(mmrSizeOld)
// remember, the peaks are *positions*
tamperNode(mc, peaks[len(peaks)-1]-1)

case tenantId3InconsistentLogUpdate:
// tamper *after* the seal
// this time, tamper a peak after the seal, this simulates the
// case where the extension is inconsistent with the seal.
peaks := mmr.Peaks(mc.RangeCount())
peaks := mmr.PosPeaks(mc.RangeCount())
// Remember, the peaks are *positions*
tamperNode(mc, peaks[len(peaks)-1]-1)

Expand All @@ -305,7 +305,7 @@ func TestLocalMassifReaderGetVerifiedContext(t *testing.T) {
require.NoError(t, err)
mmrSizeOld := sizeBeforeLeaves(mc, 8)
require.GreaterOrEqual(t, mmrSizeOld, mc.Start.FirstIndex)
peaks := mmr.Peaks(mmrSizeOld)
peaks := mmr.PosPeaks(mmrSizeOld)
// remember, the peaks are *positions*
tamperNode(mc, peaks[len(peaks)-1]-1)

Expand Down

0 comments on commit c91e8eb

Please sign in to comment.