Skip to content

Commit

Permalink
test(smt): update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Nov 27, 2023
1 parent 1cd012f commit 1fe9004
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
44 changes: 22 additions & 22 deletions packages/smt/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { poseidon, smt } from "circomlibjs"
import sha256 from "crypto-js/sha256"
import { ChildNodes, SparseMerkleTree } from "../src"
import { ChildNodes, SMT } from "../src"

describe("Sparse Merkle tree", () => {
describe("SMT", () => {
const hash = (childNodes: ChildNodes) => sha256(childNodes.join("")).toString()
const testKeys = ["a", "3", "2b", "20", "9", "17"]

describe("Create hexadecimal trees", () => {
it("Should create an empty sparse Merkle tree", () => {
const tree = new SparseMerkleTree(hash)
const tree = new SMT(hash)

expect(tree.root).toBe("0")
})

it("Should not create a hexadecimal tree if the hash function does not return a hexadecimal", () => {
const hash = (childNodes: ChildNodes) => poseidon(childNodes)

const fun = () => new SparseMerkleTree(hash)
const fun = () => new SMT(hash)

expect(fun).toThrow()
})
})

describe("Add new entries (key/value) in the tree", () => {
it("Should add a new entry", () => {
const tree = new SparseMerkleTree(hash)
const tree = new SMT(hash)
const oldRoot = tree.root

tree.add("2", "a")
Expand All @@ -33,15 +33,15 @@ describe("Sparse Merkle tree", () => {
})

it("Should not add a new non-hexadecimal entry", () => {
const tree = new SparseMerkleTree(hash)
const tree = new SMT(hash)

const fun = () => tree.add(BigInt(2), BigInt(4))

expect(fun).toThrow()
})

it("Should not add a new entry with an existing key", () => {
const tree = new SparseMerkleTree(hash)
const tree = new SMT(hash)

tree.add("2", "a")
const fun = () => tree.add("2", "a")
Expand All @@ -50,7 +50,7 @@ describe("Sparse Merkle tree", () => {
})

it("Should add 6 new entries and create the correct root hash", () => {
const tree = new SparseMerkleTree(hash)
const tree = new SMT(hash)

for (const key of testKeys) {
tree.add(key, key)
Expand All @@ -62,7 +62,7 @@ describe("Sparse Merkle tree", () => {

describe("Get values from the tree", () => {
it("Should get a value from the tree using an existing key", () => {
const tree = new SparseMerkleTree(hash)
const tree = new SMT(hash)

tree.add("2", "a")
const value = tree.get("2")
Expand All @@ -71,7 +71,7 @@ describe("Sparse Merkle tree", () => {
})

it("Should not get a value from the tree using a non-existing key", () => {
const tree = new SparseMerkleTree(hash)
const tree = new SMT(hash)

tree.add("2", "a")
const value = tree.get("1")
Expand All @@ -82,7 +82,7 @@ describe("Sparse Merkle tree", () => {

describe("Update values in the tree", () => {
it("Should update a value of an existing key", () => {
const tree = new SparseMerkleTree(hash)
const tree = new SMT(hash)

tree.add("2", "a")
tree.update("2", "5")
Expand All @@ -91,7 +91,7 @@ describe("Sparse Merkle tree", () => {
})

it("Should not update a value with a non-existing key", () => {
const tree = new SparseMerkleTree(hash)
const tree = new SMT(hash)

const fun = () => tree.update("1", "5")

Expand All @@ -101,7 +101,7 @@ describe("Sparse Merkle tree", () => {

describe("Delete entries from the tree", () => {
it("Should delete an entry with an existing key", () => {
const tree = new SparseMerkleTree(hash)
const tree = new SMT(hash)

tree.add("2", "a")
tree.delete("2")
Expand All @@ -110,7 +110,7 @@ describe("Sparse Merkle tree", () => {
})

it("Should delete 3 entries and create the correct root hash", () => {
const tree = new SparseMerkleTree(hash)
const tree = new SMT(hash)

for (const key of testKeys) {
tree.add(key, key)
Expand All @@ -124,7 +124,7 @@ describe("Sparse Merkle tree", () => {
})

it("Should not delete an entry with a non-existing key", () => {
const tree = new SparseMerkleTree(hash)
const tree = new SMT(hash)

const fun = () => tree.delete("1")

Expand All @@ -134,7 +134,7 @@ describe("Sparse Merkle tree", () => {

describe("Create Merkle proofs and verify them", () => {
it("Should create some Merkle proofs and verify them", () => {
const tree = new SparseMerkleTree(hash)
const tree = new SMT(hash)

for (const key of testKeys) {
tree.add(key, key)
Expand All @@ -154,7 +154,7 @@ describe("Sparse Merkle tree", () => {
})

it("Should not verify a wrong Merkle proof", () => {
const tree = new SparseMerkleTree(hash)
const tree = new SMT(hash)

for (const key of testKeys) {
tree.add(key, key)
Expand All @@ -171,21 +171,21 @@ describe("Sparse Merkle tree", () => {
const hash = (childNodes: ChildNodes) => poseidon(childNodes)

it("Should create a big number tree", () => {
const tree = new SparseMerkleTree(hash, true)
const tree = new SMT(hash, true)

expect(tree.root).toEqual(BigInt(0))
})

it("Should not create a big number tree if the hash function does not return a big number", () => {
const hash = (childNodes: ChildNodes) => sha256(childNodes.join("")).toString()

const fun = () => new SparseMerkleTree(hash, true)
const fun = () => new SMT(hash, true)

expect(fun).toThrow()
})

it("Should add a big number new entry", () => {
const tree = new SparseMerkleTree(hash, true)
const tree = new SMT(hash, true)
const oldRoot = tree.root

tree.add(BigInt(2), BigInt(4))
Expand All @@ -194,7 +194,7 @@ describe("Sparse Merkle tree", () => {
})

it("Should not add a new non-big number entry", () => {
const tree = new SparseMerkleTree(hash, true)
const tree = new SMT(hash, true)

const fun = () => tree.add("2", "a")

Expand All @@ -205,7 +205,7 @@ describe("Sparse Merkle tree", () => {
describe("Matching with Circomlib smt implementation", () => {
it("Should create two trees with different implementations and match their root nodes", async () => {
const hash = (childNodes: ChildNodes) => poseidon(childNodes)
const tree = new SparseMerkleTree(hash, true)
const tree = new SMT(hash, true)
const tree2 = await smt.newMemEmptyTrie()
const entries: any = [
[
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4385,9 +4385,9 @@ __metadata:
languageName: unknown
linkType: soft

"@zk-kit/sparse-merkle-tree@workspace:packages/sparse-merkle-tree":
"@zk-kit/smt@workspace:packages/smt":
version: 0.0.0-use.local
resolution: "@zk-kit/sparse-merkle-tree@workspace:packages/sparse-merkle-tree"
resolution: "@zk-kit/smt@workspace:packages/smt"
dependencies:
"@types/crypto-js": ^4.1.1
circomlibjs: ^0.0.8
Expand Down

0 comments on commit 1fe9004

Please sign in to comment.