/** * Tests for proof data structures (ProofInnerNode, ProofLeafNode, PathToLeaf) */ import { sha256, } from "@noble/hashes/sha2.js"; import { describe, expect, it, } from "vitest"; import { pathToLeafIndex, ProofInnerNode, ProofLeafNode, } from "./types.js"; /** * Helper to convert string to Uint8Array */ function str(s: string): Uint8Array { return new TextEncoder().encode(s); } /** * Helper to create a fake hash from a string (for testing) */ function fakeHash(s: string): Uint8Array { return sha256(str(s)); } describe("ProofLeafNode", () => { it("should compute hash correctly", () => { const key = str("mykey"); const value = str("myvalue"); const valueHash = sha256(value); const version = 5n; const leaf = new ProofLeafNode(key, valueHash, version); const hash = leaf.hash(); // Hash should be deterministic expect(hash).toBeInstanceOf(Uint8Array); expect(hash.length).toBe(32); // SHA256 output // Computing again should give same result const hash2 = leaf.hash(); expect(hash).toEqual(hash2); }); it("should produce different hashes for different keys", () => { const value = str("value"); const valueHash = sha256(value); const version = 1n; const leaf1 = new ProofLeafNode(str("key1"), valueHash, version); const leaf2 = new ProofLeafNode(str("key2"), valueHash, version); expect(leaf1.hash()).not.toEqual(leaf2.hash()); }); it("should produce different hashes for different values", () => { const key = str("key"); const version = 1n; const leaf1 = new ProofLeafNode(key, sha256(str("value1")), version); const leaf2 = new ProofLeafNode(key, sha256(str("value2")), version); expect(leaf1.hash()).not.toEqual(leaf2.hash()); }); it("should produce different hashes for different versions", () => { const key = str("key"); const valueHash = sha256(str("value")); const leaf1 = new ProofLeafNode(key, valueHash, 1n); const leaf2 = new ProofLeafNode(key, valueHash, 2n); expect(leaf1.hash()).not.toEqual(leaf2.hash()); }); it("should have readable toString", () => { const leaf = new ProofLeafNode(str("key"), str("hash"), 5n); const s = leaf.toString(); expect(s).toContain("ProofLeafNode"); expect(s).toContain("Key:"); expect(s).toContain("ValueHash:"); expect(s).toContain("Version:"); expect(s).toContain("5"); }); }); describe("ProofInnerNode", () => { it("should compute hash with left traversal", () => { const childHash = fakeHash("child"); const rightHash = fakeHash("right"); const inner = new ProofInnerNode( 3, // height 10n, // size 5n, // version null, // left (we traversed left) rightHash, // right sibling ); const hash = inner.hash(childHash); expect(hash).toBeInstanceOf(Uint8Array); expect(hash.length).toBe(32); }); it("should compute hash with right traversal", () => { const childHash = fakeHash("child"); const leftHash = fakeHash("left"); const inner = new ProofInnerNode( 3, // height 10n, // size 5n, // version leftHash, // left sibling null, // right (we traversed right) ); const hash = inner.hash(childHash); expect(hash).toBeInstanceOf(Uint8Array); expect(hash.length).toBe(32); }); it("should throw error if both left and right are set", () => { const childHash = fakeHash("child"); const leftHash = fakeHash("left"); const rightHash = fakeHash("right"); const inner = new ProofInnerNode(3, 10n, 5n, leftHash, rightHash); expect(() => inner.hash(childHash)).toThrow("both left and right child hashes are set"); }); it("should handle null sibling (both null siblings)", () => { const childHash = fakeHash("child"); const inner = new ProofInnerNode(1, 1n, 1n, null, null); // Should not throw const hash = inner.hash(childHash); expect(hash).toBeInstanceOf(Uint8Array); expect(hash.length).toBe(32); }); it("should produce different hashes for left vs right traversal", () => { const childHash = fakeHash("child"); const siblingHash = fakeHash("sibling"); const leftTraversal = new ProofInnerNode(3, 10n, 5n, null, siblingHash); const rightTraversal = new ProofInnerNode(3, 10n, 5n, siblingHash, null); const hash1 = leftTraversal.hash(childHash); const hash2 = rightTraversal.hash(childHash); expect(hash1).not.toEqual(hash2); }); it("should produce different hashes for different heights", () => { const childHash = fakeHash("child"); const inner1 = new ProofInnerNode(1, 10n, 5n, null, fakeHash("sibling")); const inner2 = new ProofInnerNode(2, 10n, 5n, null, fakeHash("sibling")); expect(inner1.hash(childHash)).not.toEqual(inner2.hash(childHash)); }); it("should produce different hashes for different sizes", () => { const childHash = fakeHash("child"); const inner1 = new ProofInnerNode(3, 10n, 5n, null, fakeHash("sibling")); const inner2 = new ProofInnerNode(3, 20n, 5n, null, fakeHash("sibling")); expect(inner1.hash(childHash)).not.toEqual(inner2.hash(childHash)); }); it("should produce different hashes for different versions", () => { const childHash = fakeHash("child"); const inner1 = new ProofInnerNode(3, 10n, 5n, null, fakeHash("sibling")); const inner2 = new ProofInnerNode(3, 10n, 6n, null, fakeHash("sibling")); expect(inner1.hash(childHash)).not.toEqual(inner2.hash(childHash)); }); it("should have readable toString", () => { const inner = new ProofInnerNode(3, 10n, 5n, fakeHash("left"), null); const s = inner.toString(); expect(s).toContain("ProofInnerNode"); expect(s).toContain("Height:"); expect(s).toContain("Size:"); expect(s).toContain("Version:"); expect(s).toContain("Left:"); expect(s).toContain("Right:"); }); }); describe("PathToLeaf", () => { it("should calculate index for left-only path", () => { // Path where we always went left (index should be 0) // Path is ROOT to LEAF: [root(size 4), leaf_parent(size 2)] const path = [new ProofInnerNode(2, 4n, 1n, null, fakeHash("r2")), new ProofInnerNode(1, 2n, 1n, null, fakeHash("r1"))]; const idx = pathToLeafIndex(path); expect(idx).toBe(0n); }); it("should calculate index for right-only path at depth 1", () => { // Simple tree with 2 leaves, we took right path // Parent has size 2, leaf has implied size 1 // went right, left subtree has size 1 const path = [new ProofInnerNode(1, 2n, 1n, fakeHash("left"), null)]; const idx = pathToLeafIndex(path); expect(idx).toBe(1n); // 2 - 1 = 1 }); it("should calculate index for mixed path", () => { // Tree with 4 leaves // Path is ROOT to LEAF: [root(size 4), leaf_parent(size 2)] // root: left=null, right set → went LEFT // leaf_parent: left set, right=null → went RIGHT // Index calculation: 0 (went left at root) + (2-1) (went right at parent) = 1 const path = [new ProofInnerNode(2, 4n, 1n, null, fakeHash("right")), new ProofInnerNode(1, 2n, 1n, fakeHash("left"), null)]; const idx = pathToLeafIndex(path); expect(idx).toBe(1n); }); it("should calculate index for right path in larger tree", () => { // Tree with 8 leaves, rightmost leaf (index 7) // Path is ROOT to LEAF: [root(size 8), grandparent(size 4), leaf_parent(size 2)] // All have left sibling, right=null (went right at each level) const path = [ new ProofInnerNode(3, 8n, 1n, fakeHash("left"), null), // root new ProofInnerNode(2, 4n, 1n, fakeHash("left"), null), // grandparent new ProofInnerNode(1, 2n, 1n, fakeHash("left"), null), // leaf's parent ]; const idx = pathToLeafIndex(path); // i=0 (root): went right, add (8 - 4) = 4 // i=1 (grandparent): went right, add (4 - 2) = 2 // i=2 (leaf_parent): went right, i==len-1, add (2 - 1) = 1 // Total: 4 + 2 + 1 = 7 ✓ expect(idx).toBe(7n); }); it("should return -1 for invalid path (both sides set)", () => { // invalid - both sides set! const path = [new ProofInnerNode(1, 2n, 1n, fakeHash("left"), fakeHash("right"))]; const idx = pathToLeafIndex(path); expect(idx).toBe(-1n); }); it("should handle empty path", () => { const idx = pathToLeafIndex([]); expect(idx).toBe(0n); }); it("should calculate correct indices for a 4-leaf tree", () => { // Tree structure: // root(4) // / \ // left(2) right(2) // / \ / \ // 0 1 2 3 // Leaf 0: left at root, left at parent - Path is ROOT to LEAF const path0 = [new ProofInnerNode(2, 4n, 1n, null, fakeHash("r2")), new ProofInnerNode(1, 2n, 1n, null, fakeHash("r1"))]; expect(pathToLeafIndex(path0)).toBe(0n); // Leaf 1: left at root, right at parent - Path is ROOT to LEAF const path1 = [new ProofInnerNode(2, 4n, 1n, null, fakeHash("r2")), new ProofInnerNode(1, 2n, 1n, fakeHash("l1"), null)]; expect(pathToLeafIndex(path1)).toBe(1n); // Leaf 2: right at root, left at parent - Path is ROOT to LEAF const path2 = [new ProofInnerNode(2, 4n, 1n, fakeHash("l2"), null), new ProofInnerNode(1, 2n, 1n, null, fakeHash("r1"))]; expect(pathToLeafIndex(path2)).toBe(2n); // Leaf 3: right at root, right at parent - Path is ROOT to LEAF const path3 = [new ProofInnerNode(2, 4n, 1n, fakeHash("l2"), null), new ProofInnerNode(1, 2n, 1n, fakeHash("l1"), null)]; expect(pathToLeafIndex(path3)).toBe(3n); }); });