Merge branch 'friedger-feat/test'

This commit is contained in:
Miguel Mota 2021-07-25 23:53:17 -07:00
commit 9351d56554
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9
2 changed files with 23 additions and 40 deletions

View File

@ -396,46 +396,28 @@ export class MerkleTree extends Base {
return [] return []
} }
if (this.isBitcoinTree && index === (this.leaves.length - 1)) { for (let i = 0; i < this.layers.length; i++) {
// Proof Generation for Bitcoin Trees const layer = this.layers[i]
const isRightNode = index % 2
const pairIndex = (isRightNode ? index - 1
: this.isBitcoinTree && index === layer.length - 1 && i < this.layers.length - 1
// Proof Generation for Bitcoin Trees
? index
// Proof Generation for Non-Bitcoin Trees
: index + 1)
for (let i = 0; i < this.layers.length - 1; i++) { if (pairIndex < layer.length) {
const layer = this.layers[i] proof.push({
const isRightNode = index % 2 position: isRightNode ? 'left' : 'right',
const pairIndex = (isRightNode ? index - 1 : index) data: layer[pairIndex]
})
if (pairIndex < layer.length) {
proof.push({
data: layer[pairIndex]
})
}
// set index to parent index
index = (index / 2) | 0
} }
return proof // set index to parent index
} else { index = (index / 2) | 0
// Proof Generation for Non-Bitcoin Trees
for (let i = 0; i < this.layers.length; i++) {
const layer = this.layers[i]
const isRightNode = index % 2
const pairIndex = (isRightNode ? index - 1 : index + 1)
if (pairIndex < layer.length) {
proof.push({
position: isRightNode ? 'left' : 'right',
data: layer[pairIndex]
})
}
// set index to parent index
index = (index / 2) | 0
}
return proof
} }
return proof
} }
/** /**

View File

@ -351,7 +351,7 @@ test('solidity keccak256 with duplicate leaves', t => {
}) })
test('sha-256 with option.isBitcoinTree', t => { test('sha-256 with option.isBitcoinTree', t => {
t.plan(2) t.plan(100)
/* Derived from: /* Derived from:
* http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html * http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html
@ -464,9 +464,10 @@ test('sha-256 with option.isBitcoinTree', t => {
const root = Buffer.from('871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a', 'hex') const root = Buffer.from('871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a', 'hex')
t.equal(tree.getRoot().toString('hex'), root.toString('hex')) t.equal(tree.getRoot().toString('hex'), root.toString('hex'))
const proof_0 = tree.getProof(leaves[0]) for (let i = 0; i < leaves.length; i++) {
const proof_0 = tree.getProof(leaves[i])
t.true(tree.verify(proof_0, leaves[0], root)) t.true(tree.verify(proof_0, leaves[i], root), 'proof verification for ' + i)
}
}) })
test('keccak256 - hex strings', t => { test('keccak256 - hex strings', t => {