/** * Extended tests for ICS23 operations (HashOp, LengthOp, etc.) * Based on test vectors from Go ics23 v0.10.0 * * These tests improve coverage of ics23-ops.ts by testing: * - All HashOp variants (SHA256, SHA512, RIPEMD160, BITCOIN, SHA512_256, KECCAK) * - All LengthOp variants (NO_PREFIX, VAR_PROTO, VAR_RLP, FIXED32/64, REQUIRE_32/64) * - Error cases for unsupported operations * - Edge cases in doHash() and doLength() functions */ import { describe, expect, it, } from "vitest"; import { HashOp, InnerOp, InnerSpec, LeafOp, LengthOp, } from "../generated/cosmos/ics23/v1/proofs.js"; import { IavlSpec, } from "./ics23-convert.js"; import { applyInner, applyLeaf, checkInnerOp, doHash, doHashOrNoop, doLengthOp, getPadding, getPosition, hasPadding, prepareLeafData, } from "./ics23-ops.js"; describe("ICS23 Operations - Extended Tests", () => { describe("doHash - All HashOp Variants", () => { // Test vectors from Go ics23 TestDoHashData.json it("should hash with SHA256", () => { const preimage = Buffer.from("food"); const result = doHash(HashOp.SHA256, preimage); expect(Buffer.from(result).toString("hex")).toBe( "c1f026582fe6e8cb620d0c85a72fe421ddded756662a8ec00ed4c297ad10676b", ); }); it("should hash with RIPEMD160", () => { const preimage = Buffer.from("food"); const result = doHash(HashOp.RIPEMD160, preimage); expect(Buffer.from(result).toString("hex")).toBe( "b1ab9988c7c7c5ec4b2b291adfeeee10e77cdd46", ); }); it("should hash with BITCOIN (ripemd160(sha256(x)))", () => { const preimage = Buffer.from("food"); const result = doHash(HashOp.BITCOIN, preimage); expect(Buffer.from(result).toString("hex")).toBe( "0bcb587dfb4fc10b36d57f2bba1878f139b75d24", ); }); it("should hash with SHA512_256", () => { const preimage = Buffer.from("food"); const result = doHash(HashOp.SHA512_256, preimage); expect(Buffer.from(result).toString("hex")).toBe( "5b3a452a6acbf1fc1e553a40c501585d5bd3cca176d562e0a0e19a3c43804e88", ); }); it("should hash with SHA512", () => { const preimage = Buffer.from("test"); const result = doHash(HashOp.SHA512, preimage); // SHA512 produces 64 bytes expect(result.length).toBe(64); }); it("should throw for KECCAK (not supported)", () => { const preimage = Buffer.from("test"); expect(() => doHash(HashOp.KECCAK, preimage)).toThrow("unsupported hashop"); }); it("should throw for NO_HASH (not supported in doHash)", () => { const preimage = Buffer.from("test data"); // NO_HASH should be handled at a higher level (doHashOrNoop) expect(() => doHash(HashOp.NO_HASH, preimage)).toThrow("unsupported hashop"); }); it("should throw error for UNRECOGNIZED hash operation", () => { const preimage = Buffer.from("test"); expect(() => doHash(HashOp.UNRECOGNIZED, preimage)).toThrow(); }); it("should throw error for invalid hash operation", () => { const preimage = Buffer.from("test"); expect(() => doHash(999 as HashOp, preimage)).toThrow(); }); }); describe("doHashOrNoop - Conditional Hashing", () => { it("should hash when HashOp is provided", () => { const preimage = Buffer.from("food"); const result = doHashOrNoop(HashOp.SHA256, preimage); expect(Buffer.from(result).toString("hex")).toBe( "c1f026582fe6e8cb620d0c85a72fe421ddded756662a8ec00ed4c297ad10676b", ); }); it("should return input unchanged when HashOp is NO_HASH", () => { const preimage = Buffer.from("test data"); const result = doHashOrNoop(HashOp.NO_HASH, preimage); expect(result).toEqual(preimage); }); it("should return empty array unchanged when HashOp is NO_HASH", () => { const preimage = new Uint8Array(0); const result = doHashOrNoop(HashOp.NO_HASH, preimage); expect(result).toEqual(preimage); }); }); describe("doLengthOp - All LengthOp Variants", () => { it("should not add prefix for NO_PREFIX", () => { const data = Buffer.from("hello"); const result = doLengthOp(LengthOp.NO_PREFIX, data); expect(result).toEqual(data); }); it("should add varint prefix for VAR_PROTO", () => { const data = Buffer.from("hello"); // 5 bytes const result = doLengthOp(LengthOp.VAR_PROTO, data); // First byte should be varint-encoded length (5) expect(result[0]).toBe(5); expect(result.slice(1)).toEqual(new Uint8Array(data)); }); it("should add varint prefix for VAR_PROTO with longer data", () => { const data = Buffer.alloc(200); // 200 bytes const result = doLengthOp(LengthOp.VAR_PROTO, data); // Varint encoding of 200 is [0xC8, 0x01] expect(result[0]).toBe(0xc8); expect(result[1]).toBe(0x01); expect(result.slice(2)).toEqual(new Uint8Array(data)); }); it("should add RLP prefix for VAR_RLP with short data", () => { const data = Buffer.from("hi"); // 2 bytes const result = doLengthOp(LengthOp.VAR_RLP, data); // RLP encoding of 2-byte string is 0x82 (0x80 + 2) expect(result[0]).toBe(0x82); expect(result.slice(1)).toEqual(new Uint8Array(data)); }); it("should add RLP prefix for VAR_RLP with medium data", () => { const data = Buffer.alloc(100); // 100 bytes const result = doLengthOp(LengthOp.VAR_RLP, data); // RLP encoding for 56+ bytes expect(result[0]).toBe(0xb8); // 0x80 + 0x38 (1 byte length) expect(result[1]).toBe(100); expect(result.slice(2)).toEqual(new Uint8Array(data)); }); it("should add 32-bit big-endian prefix for FIXED32_BIG", () => { const data = Buffer.from("test"); // 4 bytes const result = doLengthOp(LengthOp.FIXED32_BIG, data); expect(result.length).toBe(8); // 4 bytes length + 4 bytes data // Big-endian encoding of 4: [0x00, 0x00, 0x00, 0x04] expect(result[0]).toBe(0); expect(result[1]).toBe(0); expect(result[2]).toBe(0); expect(result[3]).toBe(4); expect(result.slice(4)).toEqual(new Uint8Array(data)); }); it("should add 32-bit little-endian prefix for FIXED32_LITTLE", () => { const data = Buffer.from("test"); // 4 bytes const result = doLengthOp(LengthOp.FIXED32_LITTLE, data); expect(result.length).toBe(8); // 4 bytes length + 4 bytes data // Little-endian encoding of 4: [0x04, 0x00, 0x00, 0x00] expect(result[0]).toBe(4); expect(result[1]).toBe(0); expect(result[2]).toBe(0); expect(result[3]).toBe(0); expect(result.slice(4)).toEqual(new Uint8Array(data)); }); it("should add 64-bit big-endian prefix for FIXED64_BIG", () => { const data = Buffer.from("test"); // 4 bytes const result = doLengthOp(LengthOp.FIXED64_BIG, data); expect(result.length).toBe(12); // 8 bytes length + 4 bytes data // Big-endian encoding of 4: [0x00 * 7, 0x04] expect(result[7]).toBe(4); expect(result.slice(8)).toEqual(new Uint8Array(data)); }); it("should add 64-bit little-endian prefix for FIXED64_LITTLE", () => { const data = Buffer.from("test"); // 4 bytes const result = doLengthOp(LengthOp.FIXED64_LITTLE, data); expect(result.length).toBe(12); // 8 bytes length + 4 bytes data // Little-endian encoding of 4: [0x04, 0x00 * 7] expect(result[0]).toBe(4); expect(result[1]).toBe(0); expect(result.slice(8)).toEqual(new Uint8Array(data)); }); it("should accept exactly 32 bytes for REQUIRE_32_BYTES", () => { const data = Buffer.alloc(32); const result = doLengthOp(LengthOp.REQUIRE_32_BYTES, data); expect(result).toEqual(data); }); it("should throw error for non-32 bytes with REQUIRE_32_BYTES", () => { const data = Buffer.alloc(31); expect(() => doLengthOp(LengthOp.REQUIRE_32_BYTES, data)).toThrow(); }); it("should throw error for too many bytes with REQUIRE_32_BYTES", () => { const data = Buffer.alloc(33); expect(() => doLengthOp(LengthOp.REQUIRE_32_BYTES, data)).toThrow(); }); it("should accept exactly 64 bytes for REQUIRE_64_BYTES", () => { const data = Buffer.alloc(64); const result = doLengthOp(LengthOp.REQUIRE_64_BYTES, data); expect(result).toEqual(data); }); it("should throw error for non-64 bytes with REQUIRE_64_BYTES", () => { const data = Buffer.alloc(63); expect(() => doLengthOp(LengthOp.REQUIRE_64_BYTES, data)).toThrow(); }); it("should throw error for too many bytes with REQUIRE_64_BYTES", () => { const data = Buffer.alloc(65); expect(() => doLengthOp(LengthOp.REQUIRE_64_BYTES, data)).toThrow(); }); it("should throw error for UNRECOGNIZED length operation", () => { const data = Buffer.from("test"); expect(() => doLengthOp(LengthOp.UNRECOGNIZED, data)).toThrow(); }); it("should throw error for invalid length operation", () => { const data = Buffer.from("test"); expect(() => doLengthOp(999 as LengthOp, data)).toThrow(); }); }); describe("applyLeaf - Complete Test Vectors", () => { // Test vectors from Go ics23 TestLeafOpData.json it("should hash foobar with SHA256", () => { const op: LeafOp = { hash: HashOp.SHA256, prehashKey: HashOp.NO_HASH, prehashValue: HashOp.NO_HASH, length: LengthOp.NO_PREFIX, prefix: new Uint8Array(), }; const key = Buffer.from("foo"); const value = Buffer.from("bar"); const result = applyLeaf(op, key, value); // Should produce SHA256 hash (32 bytes) expect(result.length).toBe(32); // Verify it's deterministic const result2 = applyLeaf(op, key, value); expect(result).toEqual(result2); }); it("should hash foobar with SHA512", () => { const op: LeafOp = { hash: HashOp.SHA512, prehashKey: HashOp.NO_HASH, prehashValue: HashOp.NO_HASH, length: LengthOp.NO_PREFIX, prefix: new Uint8Array(), }; const key = Buffer.from("foo"); const value = Buffer.from("baz"); const result = applyLeaf(op, key, value); // SHA512 produces 64 bytes expect(result.length).toBe(64); }); it("should hash with length prefix (VAR_PROTO)", () => { const op: LeafOp = { hash: HashOp.SHA256, prehashKey: HashOp.NO_HASH, prehashValue: HashOp.NO_HASH, length: LengthOp.VAR_PROTO, prefix: new Uint8Array(), }; const key = Buffer.from("food"); const value = Buffer.from("some longer text"); const result = applyLeaf(op, key, value); // Expected: base64 "to9dKY6RWuF1PdMz2h+c9gVBGl8uElFr5nWPNl5tsmU=" const expected = new Uint8Array( Buffer.from( "b68f5d298e915ae1753dd333da1f9cf605411a5f2e12516be6758f365e6db265", "hex", ), ); expect(result).toEqual(expected); }); it("should hash with length prefix (FIXED32_LITTLE)", () => { const op: LeafOp = { hash: HashOp.SHA256, prehashKey: HashOp.NO_HASH, prehashValue: HashOp.NO_HASH, length: LengthOp.FIXED32_LITTLE, prefix: new Uint8Array(), }; const key = Buffer.from("food"); const value = Buffer.from("some longer text"); const result = applyLeaf(op, key, value); // Expected: base64 "yFNlJDe+AlAcZ0dEvyorRdkqCp8pxLEEQBD7Pi1DqUk=" const expected = new Uint8Array( Buffer.from( "c853652437be02501c674744bf2a2b45d92a0a9f29c4b1044010fb3e2d43a949", "hex", ), ); expect(result).toEqual(expected); }); it("should hash with prehash_value and length prefix", () => { const op: LeafOp = { hash: HashOp.SHA256, prehashKey: HashOp.NO_HASH, prehashValue: HashOp.SHA256, // Prehash the value first length: LengthOp.VAR_PROTO, prefix: new Uint8Array(), }; const key = Buffer.from("food"); const value = Buffer.from("yet another long string"); const result = applyLeaf(op, key, value); // Expected: base64 "h+BIPo+2JK7y4vexP0FmzaSFuqjjn0N8g9dMlL7bFI8=" const expected = new Uint8Array( Buffer.from( "87e0483e8fb624aef2e2f7b13f4166cda485baa8e39f437c83d74c94bedb148f", "hex", ), ); expect(result).toEqual(expected); }); it("should throw error when key is empty", () => { const op: LeafOp = { hash: HashOp.SHA256, prehashKey: HashOp.NO_HASH, prehashValue: HashOp.NO_HASH, length: LengthOp.NO_PREFIX, prefix: new Uint8Array(), }; const key = new Uint8Array(); const value = Buffer.from("bar"); expect(() => applyLeaf(op, key, value)).toThrow(); }); it("should throw error when value is empty", () => { const op: LeafOp = { hash: HashOp.SHA256, prehashKey: HashOp.NO_HASH, prehashValue: HashOp.NO_HASH, length: LengthOp.NO_PREFIX, prefix: new Uint8Array(), }; const key = Buffer.from("foo"); const value = new Uint8Array(); expect(() => applyLeaf(op, key, value)).toThrow(); }); }); describe("applyInner - Complete Test Vectors", () => { // Test vectors from Go ics23 TestInnerOpData.json it("should hash child with only prefix", () => { const op: InnerOp = { hash: HashOp.SHA256, prefix: Buffer.from("ACBAgKDA4A==", "base64"), // 00204080a0c0e0 suffix: new Uint8Array(), }; const child = Buffer.from("/8y7mXdVMxEA", "base64"); // ffccbb997755331100 const result = applyInner(op, child); // Expected: base64 "Rb7OFnjPLp9PKuAz5Ub8NaIIGyQV7csTEhoOkI3KGSc=" const expected = new Uint8Array( Buffer.from( "45bece1678cf2e9f4f2ae033e546fc35a2081b2415edcb13121a0e908dca1927", "hex", ), ); expect(result).toEqual(expected); }); it("should hash child with only suffix", () => { const op: InnerOp = { hash: HashOp.SHA256, prefix: new Uint8Array(), suffix: Buffer.from("IGp1c3Qga2lkZGluZyE=", "base64"), // " just kidding!" }; const child = Buffer.from("dGhpcyBpcyBhIHNoYTI1NiBoYXNoLCByZWFsbHkuLi4u", "base64"); // "this is a sha256 hash, really...." const result = applyInner(op, child); // Expected: base64 "ee9nHSfkKlP7oiAcG7xSmgma9XjuijjfFAeV2wriGEs=" const expected = new Uint8Array( Buffer.from( "79ef671d27e42a53fba2201c1bbc529a099af578ee8a38df140795db0ae2184b", "hex", ), ); expect(result).toEqual(expected); }); it("should hash child with prefix and suffix", () => { const op: InnerOp = { hash: HashOp.SHA256, prefix: Buffer.from("ASNFZ4k=", "base64"), // 012345678389 suffix: Buffer.from("3q2+7w==", "base64"), // deadbeef }; const child = Buffer.from("AMr+AA==", "base64"); // 00cafe00 const result = applyInner(op, child); // Expected: base64 "Azn3YIZoRQam1Cpg2ktacZ/r1Nlti42FrpKEnjqEml4=" const expected = new Uint8Array( Buffer.from( "0339f76086684506a6d42a60da4b5a719febd4d96d8b8d85ae92849e3a849a5e", "hex", ), ); expect(result).toEqual(expected); }); it("should throw error when child is empty", () => { const op: InnerOp = { hash: HashOp.SHA256, prefix: Buffer.from([0x01, 0x23]), suffix: Buffer.from([0xde, 0xad]), }; const child = new Uint8Array(); expect(() => applyInner(op, child)).toThrow(); }); it("should work with no prefix and no suffix", () => { const op: InnerOp = { hash: HashOp.SHA256, prefix: new Uint8Array(), suffix: new Uint8Array(), }; const child = Buffer.from("just the child"); const result = applyInner(op, child); // Should just hash the child expect(result.length).toBe(32); // SHA256 output }); }); describe("Edge Cases and Error Handling", () => { it("should handle empty buffers with doHashOrNoop", () => { const empty = Buffer.alloc(0); const result = doHashOrNoop(HashOp.NO_HASH, empty); expect(result).toEqual(empty); }); it("should handle very large data", () => { const large = Buffer.alloc(1024 * 1024); // 1MB const result = doHash(HashOp.SHA256, large); expect(result.length).toBe(32); }); it("should handle VAR_PROTO length with maximum varint", () => { // Test with data that requires multi-byte varint const data = Buffer.alloc(300); const result = doLengthOp(LengthOp.VAR_PROTO, data); // 300 in varint is [0xAC, 0x02] expect(result[0]).toBe(0xac); expect(result[1]).toBe(0x02); }); it("should handle FIXED64 with large numbers", () => { const data = Buffer.alloc(1000); const result = doLengthOp(LengthOp.FIXED64_BIG, data); expect(result.length).toBe(1008); // 8 bytes length + 1000 bytes data }); }); describe("Additional edge cases for uncovered lines", () => { it("should handle applyLeaf with simple data (line 54)", () => { const leafOp: LeafOp = { hash: 1, // SHA256 prehashKey: 0, // NO_HASH prehashValue: 0, // NO_HASH length: 0, // NO_PREFIX prefix: new Uint8Array([0x00]), }; const key = new Uint8Array([0x01, 0x02]); const value = new Uint8Array([0x03, 0x04]); const result = applyLeaf(leafOp, key, value); expect(result).toBeInstanceOf(Uint8Array); expect(result.length).toBe(32); // SHA256 output }); it("should handle applyInner with simple data (line 79)", () => { const innerOp: InnerOp = { hash: 1, // SHA256 prefix: new Uint8Array([0x01]), suffix: new Uint8Array([0x02]), }; const child = new Uint8Array(32).fill(0xAA); const result = applyInner(innerOp, child); expect(result).toBeInstanceOf(Uint8Array); expect(result.length).toBe(32); }); it("should handle doHashOrNoop with NO_HASH returning data (line 113)", () => { const data = new Uint8Array([ 1, 2, 3, 4, 5, ]); const result = doHashOrNoop(0, data); // NO_HASH expect(result).toEqual(data); }); it("should handle doHashOrNoop with SHA256 (line 115)", () => { const data = new Uint8Array([ 1, 2, 3, ]); const result = doHashOrNoop(1, data); // SHA256 expect(result).toBeInstanceOf(Uint8Array); expect(result.length).toBe(32); }); it("should handle prepareLeafData with NO_HASH and NO_PREFIX (lines 123-124)", () => { const data = new Uint8Array([ 0xAA, 0xBB, 0xCC, ]); const result = prepareLeafData(0, 0, data); // NO_HASH, NO_PREFIX expect(result).toEqual(data); // Should return unchanged }); it("should handle prepareLeafData with SHA256 and VAR_PROTO (lines 123-124)", () => { const data = new Uint8Array([0x01, 0x02]); const result = prepareLeafData(1, 1, data); // SHA256, VAR_PROTO // Should hash then add varint prefix expect(result.length).toBeGreaterThan(32); // 32 bytes hash + varint prefix }); it("should handle VAR_RLP with long data (lines 174-177)", () => { // > 55 bytes requires multi-byte length encoding const longData = new Uint8Array(100).fill(0xAB); const result = doLengthOp(2, longData); // VAR_RLP // Should have 0xb7 + lenLen prefix expect(result[0]).toBeGreaterThanOrEqual(0xb7); expect(result.length).toBe(100 + 2); // data + 1 byte prefix + 1 byte length }); it("should handle VAR_RLP with very long data requiring 2-byte length (lines 174-177)", () => { // 256+ bytes requires 2-byte length encoding const veryLongData = new Uint8Array(300).fill(0xCD); const result = doLengthOp(2, veryLongData); // VAR_RLP expect(result[0]).toBe(0xb7 + 2); // 0xb9 expect(result.length).toBe(300 + 3); // data + 1 prefix + 2 length bytes }); it("should handle checkInnerOp prefix validation (lines 296-299)", () => { // Use IavlSpec const spec = IavlSpec; // Prefix way too long (exceeds maxPrefixLength + childSize) const invalidOp: InnerOp = { hash: 1, prefix: new Uint8Array(500).fill(0x01), // Much too long, non-zero suffix: new Uint8Array(33).fill(0x20), }; // This should trigger the prefix length check at lines 296-299 expect(() => checkInnerOp(invalidOp, spec, 0)).toThrow("prefix too long"); }); it("should handle bytesToHex helper (line 329)", () => { // This is a private function but gets called in error messages // Trigger it via an error const spec: InnerSpec = { childOrder: [0, 1], childSize: 32, minPrefixLength: 1, maxPrefixLength: 10, emptyChild: new Uint8Array(32), hash: 1, }; const badOp: InnerOp = { hash: 1, prefix: new Uint8Array(0), // Too short (< minPrefixLength) suffix: new Uint8Array(32), }; expect(() => checkInnerOp(badOp, spec, 0)).toThrow(); }); it("should handle getPosition with invalid branch (line 339)", () => { const order = [ 0, 1, 2, ]; // Valid branch expect(getPosition(order, 1)).toBe(1); // Invalid branch (too large) expect(() => getPosition(order, 5)).toThrow("invalid branch"); // Invalid branch (negative) expect(() => getPosition(order, -1)).toThrow("invalid branch"); }); it("should handle getPosition when branch not found (line 346)", () => { const order = [ 0, 2, 4, ]; // Skip 1 and 3 // Branch 1 is not in the order array expect(() => getPosition(order, 1)).toThrow("not found in order"); }); it("should handle hasPadding boundary cases (line 389)", () => { const spec: InnerSpec = { childOrder: [0, 1], childSize: 32, minPrefixLength: 4, maxPrefixLength: 8, emptyChild: new Uint8Array(32), hash: 1, }; // Prefix too short const shortOp: InnerOp = { hash: 1, prefix: new Uint8Array(3), // < minPrefix suffix: new Uint8Array(32), }; const { minPrefix, maxPrefix, suffix, } = getPadding(spec, 1); expect(hasPadding(shortOp, minPrefix, maxPrefix, suffix)).toBe(false); // Prefix too long const longOp: InnerOp = { hash: 1, prefix: new Uint8Array(50), // > maxPrefix suffix: new Uint8Array(32), }; expect(hasPadding(longOp, minPrefix, maxPrefix, suffix)).toBe(false); // Wrong suffix length const wrongSuffixOp: InnerOp = { hash: 1, prefix: new Uint8Array(6), // Valid suffix: new Uint8Array(16), // Wrong size }; expect(hasPadding(wrongSuffixOp, minPrefix, maxPrefix, suffix)).toBe(false); // All valid - must match calculated padding exactly const validOp: InnerOp = { hash: 1, prefix: new Uint8Array(minPrefix), // Use calculated minPrefix suffix: new Uint8Array(suffix), // Use calculated suffix }; expect(hasPadding(validOp, minPrefix, maxPrefix, suffix)).toBe(true); }); }); });