Simple merkle rfc compatibility (#2713)

* Begin simple merkle compatibility PR

* Fix query_test

* Use trillian test vectors

* Change the split point per RFC 6962

* update spec

* refactor innerhash to match spec

* Update changelog

* Address @liamsi's comments

* Write the comment requested by @liamsi
This commit is contained in:
Dev Ojha
2019-01-13 17:02:38 -06:00
committed by Ethan Buchman
parent 1f68318875
commit ec53ce359b
15 changed files with 233 additions and 112 deletions

View File

@ -31,13 +31,14 @@ func (tx Tx) String() string {
// Txs is a slice of Tx.
type Txs []Tx
// Hash returns the simple Merkle root hash of the transactions.
// Hash returns the Merkle root hash of the transaction hashes.
// i.e. the leaves of the tree are the hashes of the txs.
func (txs Txs) Hash() []byte {
// These allocations will be removed once Txs is switched to [][]byte,
// ref #2603. This is because golang does not allow type casting slices without unsafe
txBzs := make([][]byte, len(txs))
for i := 0; i < len(txs); i++ {
txBzs[i] = txs[i]
txBzs[i] = txs[i].Hash()
}
return merkle.SimpleHashFromByteSlices(txBzs)
}
@ -69,7 +70,7 @@ func (txs Txs) Proof(i int) TxProof {
l := len(txs)
bzs := make([][]byte, l)
for i := 0; i < l; i++ {
bzs[i] = txs[i]
bzs[i] = txs[i].Hash()
}
root, proofs := merkle.SimpleProofsFromByteSlices(bzs)
@ -87,8 +88,8 @@ type TxProof struct {
Proof merkle.SimpleProof
}
// LeadHash returns the hash of the this proof refers to.
func (tp TxProof) LeafHash() []byte {
// Leaf returns the hash(tx), which is the leaf in the merkle tree which this proof refers to.
func (tp TxProof) Leaf() []byte {
return tp.Data.Hash()
}
@ -104,7 +105,7 @@ func (tp TxProof) Validate(dataHash []byte) error {
if tp.Proof.Total <= 0 {
return errors.New("Proof total must be positive")
}
valid := tp.Proof.Verify(tp.RootHash, tp.LeafHash())
valid := tp.Proof.Verify(tp.RootHash, tp.Leaf())
if valid != nil {
return errors.New("Proof is not internally consistent")
}