|
| 1 | +import { MapDB, bytesToHex } from '@ethereumjs/util' |
| 2 | +import { loadVerkleCrypto } from 'verkle-cryptography-wasm' |
| 3 | +import { assert, beforeAll, describe, it } from 'vitest' |
| 4 | + |
| 5 | +import { createVerkleTree } from '../src/constructors.js' |
| 6 | + |
| 7 | +describe('rust-verkle test vectors', () => { |
| 8 | + let verkleCrypto: Awaited<ReturnType<typeof loadVerkleCrypto>> |
| 9 | + beforeAll(async () => { |
| 10 | + verkleCrypto = await loadVerkleCrypto() |
| 11 | + }) |
| 12 | + it('should produce the correct commitment', async () => { |
| 13 | + // Test from python implementation |
| 14 | + //https://github.com/crate-crypto/verkle-trie-ref/blob/483f40c737f27bc8f059870f862cf6c244159cd4/verkle/verkle_test.py#L63 |
| 15 | + // It inserts a single value and then verifies that the hash of the root node matches (not the `trie.root` which is a serialized commitment and not the hash) |
| 16 | + const key = Uint8Array.from([ |
| 17 | + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, |
| 18 | + 27, 28, 29, 30, 31, 32, |
| 19 | + ]) |
| 20 | + const trie = await createVerkleTree({ verkleCrypto, db: new MapDB() }) |
| 21 | + await trie.put(key.slice(0, 31), [key[31]], [key]) |
| 22 | + |
| 23 | + const path = await trie.findPath(key.slice(0, 31)) |
| 24 | + |
| 25 | + assert.equal( |
| 26 | + bytesToHex(path.stack[0][0].hash()), |
| 27 | + '0x029b6c4c8af9001f0ac76472766c6579f41eec84a73898da06eb97ebdab80a09', |
| 28 | + ) |
| 29 | + assert.equal( |
| 30 | + bytesToHex(trie.root()), |
| 31 | + '0x6f5e7cfc3a158a64e5718b0d2f18f564171342380f5808f3d2a82f7e7f3c2778', |
| 32 | + ) |
| 33 | + }) |
| 34 | + it('should produce correct commitments after value updates', async () => { |
| 35 | + // Variant of previous test that puts 0s at a specific key and then updates that value |
| 36 | + // https://github.com/crate-crypto/verkle-trie-ref/blob/483f40c737f27bc8f059870f862cf6c244159cd4/verkle/verkle_test.py#L96 |
| 37 | + const key = Uint8Array.from([ |
| 38 | + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, |
| 39 | + 27, 28, 29, 30, 31, 32, |
| 40 | + ]) |
| 41 | + const stem = key.slice(0, 31) |
| 42 | + const trie = await createVerkleTree({ verkleCrypto, db: new MapDB() }) |
| 43 | + await trie.put(stem, [key[31]], [new Uint8Array(32)]) |
| 44 | + let path = await trie.findPath(stem) |
| 45 | + assert.equal( |
| 46 | + bytesToHex(path.stack[0][0].hash()), |
| 47 | + '0x77a0747bd526d9d9af60bd5665d24d6cb421f5c8e726b1de62f914f3ff9a361c', |
| 48 | + ) |
| 49 | + await trie.put(stem, [key[31]], [key]) |
| 50 | + path = await trie.findPath(key.slice(0, 31)) |
| 51 | + |
| 52 | + assert.equal( |
| 53 | + bytesToHex(path.stack[0][0].hash()), |
| 54 | + '0x029b6c4c8af9001f0ac76472766c6579f41eec84a73898da06eb97ebdab80a09', |
| 55 | + ) |
| 56 | + assert.equal( |
| 57 | + bytesToHex(trie.root()), |
| 58 | + '0x6f5e7cfc3a158a64e5718b0d2f18f564171342380f5808f3d2a82f7e7f3c2778', |
| 59 | + ) |
| 60 | + }) |
| 61 | +}) |
0 commit comments