Add constructor functions for MerkleTreeBuilder
This commit is contained in:
parent
39737f33ec
commit
3673518b30
19
auditing.go
19
auditing.go
|
@ -12,6 +12,7 @@ package certspotter
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"errors"
|
||||||
"software.sslmate.com/src/certspotter/ct"
|
"software.sslmate.com/src/certspotter/ct"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -141,6 +142,24 @@ type MerkleTreeBuilder struct {
|
||||||
numLeaves uint64 // number of hashes added so far
|
numLeaves uint64 // number of hashes added so far
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func calculateStackSize (numLeaves uint64) int {
|
||||||
|
stackSize := 0
|
||||||
|
for numLeaves > 0 {
|
||||||
|
stackSize += int(numLeaves & 1)
|
||||||
|
numLeaves >>= 1
|
||||||
|
}
|
||||||
|
return stackSize
|
||||||
|
}
|
||||||
|
func EmptyMerkleTreeBuilder () *MerkleTreeBuilder {
|
||||||
|
return &MerkleTreeBuilder{}
|
||||||
|
}
|
||||||
|
func NewMerkleTreeBuilder (stack []ct.MerkleTreeNode, numLeaves uint64) (*MerkleTreeBuilder, error) {
|
||||||
|
if len(stack) != calculateStackSize(numLeaves) {
|
||||||
|
return nil, errors.New("NewMerkleTreeBuilder: incorrect stack size")
|
||||||
|
}
|
||||||
|
return &MerkleTreeBuilder{stack: stack, numLeaves: numLeaves}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (builder *MerkleTreeBuilder) Add(hash ct.MerkleTreeNode) {
|
func (builder *MerkleTreeBuilder) Add(hash ct.MerkleTreeNode) {
|
||||||
builder.stack = append(builder.stack, hash)
|
builder.stack = append(builder.stack, hash)
|
||||||
builder.numLeaves++
|
builder.numLeaves++
|
||||||
|
|
|
@ -254,9 +254,12 @@ func (s *Scanner) MakeMerkleTreeBuilder(sth *ct.SignedTreeHead) (*MerkleTreeBuil
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
reverseHashes(auditPath)
|
reverseHashes(auditPath)
|
||||||
builder = &MerkleTreeBuilder{numLeaves: sth.TreeSize - 1, stack: auditPath}
|
builder, err = NewMerkleTreeBuilder(auditPath, sth.TreeSize - 1)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Error returned bad audit proof for %x to %d", leafHash, sth.TreeSize)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
builder = &MerkleTreeBuilder{numLeaves: 0}
|
builder = EmptyMerkleTreeBuilder()
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.Add(leafHash)
|
builder.Add(leafHash)
|
||||||
|
|
Loading…
Reference in New Issue