Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed incorrect test SMT elements generation #8

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 18 additions & 27 deletions test/data-structures/SparseMerkleTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ describe("SparseMerkleTree", () => {
let smtMock: SparseMerkleTreeMock;
let smtVerifier: SparseMerkleTreeMockVerifier;

const leaves: string[] = [];
let nonExistentLeaf: string;

before("setup", async () => {
const poseidonFacade = await deployPoseidonFacade();

Expand All @@ -31,20 +34,24 @@ describe("SparseMerkleTree", () => {
smtVerifier = await SparseMerkleTreeMockVerifier.deploy();
smtMock = await SparseMerkleTreeMock.deploy();

for (let i = 0; i < 10; i++) {
let rand: string;
do {
rand = ethers.hexlify(ethers.randomBytes(1));
} while (leaves.includes(rand));
leaves.push(rand);
}

nonExistentLeaf = leaves[9];

await reverter.snapshot();
});

afterEach(reverter.revert);

it("should prove the tree inclusion", async () => {
let leaves: string[] = [];

for (let i = 0; i < 9; i++) {
const rand = ethers.hexlify(ethers.randomBytes(30));

await smtMock.addElement(rand, rand);

leaves.push(rand);
await smtMock.addElement(leaves[i], leaves[i]);
}

const merkleProof = await smtMock.getProof(leaves[5]);
Expand Down Expand Up @@ -114,13 +121,9 @@ describe("SparseMerkleTree", () => {

it("should prove the tree exclusion", async () => {
for (let i = 0; i < 9; i++) {
const rand = ethers.hexlify(ethers.randomBytes(30));

await smtMock.addElement(rand, rand);
await smtMock.addElement(leaves[i], leaves[i]);
}

const nonExistentLeaf = ethers.hexlify(ethers.randomBytes(30));

const merkleProof = await smtMock.getProof(nonExistentLeaf);

const auxIsEmpty = BigInt(merkleProof.auxKey) == 0n ? 1 : 0;
Expand All @@ -147,8 +150,6 @@ describe("SparseMerkleTree", () => {

await smtMock.addElement(rand, rand);

const nonExistentLeaf = ethers.hexlify(ethers.randomBytes(30));

const merkleProof = await smtMock.getProof(nonExistentLeaf);

const auxIsEmpty = BigInt(merkleProof.auxKey) == 0n ? 1 : 0;
Expand All @@ -171,7 +172,7 @@ describe("SparseMerkleTree", () => {
});

it("should prove the tree exclusion for empty tree", async () => {
const nonExistentLeaf = ethers.hexlify(ethers.randomBytes(30));
const nonExistentLeaf = ethers.hexlify(ethers.randomBytes(1));

const merkleProof = await smtMock.getProof(nonExistentLeaf);

Expand Down Expand Up @@ -203,14 +204,8 @@ describe("SparseMerkleTree", () => {
});

it("should revert an incorrect tree inclusion", async () => {
let leaves: string[] = [];

for (let i = 0; i < 9; i++) {
const rand = ethers.hexlify(ethers.randomBytes(30));

await smtMock.addElement(rand, rand);

leaves.push(rand);
await smtMock.addElement(leaves[i], leaves[i]);
}

const merkleProof = await smtMock.getProof(leaves[5]);
Expand All @@ -233,13 +228,9 @@ describe("SparseMerkleTree", () => {

it("should revert an incorrect tree exclusion", async () => {
for (let i = 0; i < 9; i++) {
const rand = ethers.hexlify(ethers.randomBytes(30));

await smtMock.addElement(rand, rand);
await smtMock.addElement(leaves[i], leaves[i]);
}

const nonExistentLeaf = ethers.hexlify(ethers.randomBytes(30));

const merkleProof = await smtMock.getProof(nonExistentLeaf);

let auxIsEmpty = BigInt(merkleProof.auxKey) == 0n ? 1 : 0;
Expand Down
Loading