Skip to content

Commit

Permalink
do not override
Browse files Browse the repository at this point in the history
  • Loading branch information
rauljordan committed Sep 18, 2024
1 parent 242b86c commit 5915a85
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions state-commitments/optimized/history_commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,38 @@ type Commitment struct {
LastLeaf common.Hash
}

// NewCommitment produces a history commitment from a list of leaves that are virtually padded using
// the last leaf in the list to some virtual length, without making those extra allocations needed to do so.
// Virtual must be >= len(leaves).
func NewCommitment(leaves []common.Hash, virtual uint64) (*Commitment, error) {
if len(leaves) == 0 {
return nil, errors.New("must commit to at least one leaf")
}
if virtual == 0 {
return nil, errors.New("virtual size cannot be zero")
}
if virtual < uint64(len(leaves)) {
return nil, errors.New("virtual size must be less than or equal to the number of leaves")
}
comm := NewCommitter()
leavesForProofs := make([]common.Hash, len(leaves))
leavesForCommit := make([]common.Hash, len(leaves))
copy(leavesForProofs, leaves)
firstLeaf := leavesForProofs[0]
lastLeaf := leavesForProofs[len(leavesForProofs)-1]
for i := 0; i < len(leavesForProofs); i++ {
result, err := comm.hash(leavesForProofs[i][:])
firstLeaf := leaves[0]
lastLeaf := leaves[len(leaves)-1]
for i, leaf := range leaves {
result, err := comm.hash(leaf[:])
if err != nil {
return nil, err
}
leavesForCommit[i] = result
leavesForProofs[i] = result
}
var firstLeafProof, lastLeafProof []common.Hash
var err error
if isPowTwo(virtual) {
ok, err := isPowTwo(virtual)
if err != nil {
return nil, err
}
if !ok {
firstLeafProof, err = comm.computeMerkleProof(0, leavesForProofs, virtual)
if err != nil {
return nil, err
Expand Down Expand Up @@ -529,9 +537,9 @@ func trimZeroes(hashes []common.Hash) []common.Hash {
return newHashes
}

func isPowTwo(n uint64) bool {
func isPowTwo(n uint64) (bool, error) {
if n == 0 {
return false
return false, errors.New("n must be non-zero")
}
return (n & (n - 1)) == 0
return (n & (n - 1)) == 0, nil
}

0 comments on commit 5915a85

Please sign in to comment.