Initial import of verae-tree-node from zapier monorepo

This commit is contained in:
George Lambert 2026-09-11 14:37:01 -04:00
commit 223be46a6f
6 changed files with 193 additions and 0 deletions

34
test/merkle.test.js Normal file
View file

@ -0,0 +1,34 @@
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import {
sha256Hex,
buildMerkle,
merkleProof,
verifyProof,
shardIndex,
} from '../src/merkle.js';
describe('merkle', () => {
it('one leaf is its own root', () => {
const leaf = sha256Hex('only');
const t = buildMerkle([leaf]);
assert.equal(t.root, leaf);
assert.equal(verifyProof(leaf, merkleProof(t.layers, 0), t.root), true);
});
it('three leaves: each proof verifies, unknown leaf does not', () => {
const leaves = ['a', 'b', 'c'].map((s) => sha256Hex(s));
const t = buildMerkle(leaves);
for (let i = 0; i < leaves.length; i += 1) {
const proof = merkleProof(t.layers, i);
assert.equal(verifyProof(leaves[i], proof, t.root), true);
}
assert.equal(verifyProof(sha256Hex('nope'), merkleProof(t.layers, 0), t.root), false);
});
it('shards stably into three nodes', () => {
const h = sha256Hex('x');
assert.equal(shardIndex(h, 3), shardIndex(h, 3));
assert.ok(shardIndex(h, 3) >= 0 && shardIndex(h, 3) < 3);
});
});