Skip to content

Commit

Permalink
feat: integrate LazyIMT with getting the merkle proof from the chain
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Apr 15, 2024
1 parent a647a84 commit 71f310a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 43 deletions.
44 changes: 22 additions & 22 deletions deployments/11155111/latest.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"prettier:write": "prettier --write **/*.{json,md,yml} --ignore-path=.prettierignore"
},
"dependencies": {
"@zk-kit/imt.sol": "2.0.0-beta",
"@zk-kit/imt.sol": "https://gitpkg.now.sh/privacy-scaling-explorations/zk-kit/packages/imt.sol?0699fd1e5ad3683ae0090e0626f75d7834145500",
"poseidon-solidity": "^0.0.5"
}
}
18 changes: 10 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion remappings.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
forge-std/=lib/forge-std/src/
@zk-kit/imt.sol/=node_modules/@zk-kit/imt.sol/
@zk-kit/imt.sol/=node_modules/@zk-kit/imt.sol/contracts
poseidon-solidity/=node_modules/poseidon-solidity/
12 changes: 6 additions & 6 deletions script/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ if [ "$contract_name" = "rln" ]; then
--chain $chain_name \
$poseidon_t3_name

# Get the BinaryIMT contract address from ./broadcast/Deploy.s.sol/$chain_id/run-latest.json
binary_imt_name=$(cat ./broadcast/Deploy.s.sol/$chain_id/run-latest.json | jq -r '.["transactions"][1]["contractName"]')
binary_imt_address=$(cat ./broadcast/Deploy.s.sol/$chain_id/run-latest.json | jq -r '.["transactions"][1]["contractAddress"]')
# Get the LazyIMT contract address from ./broadcast/Deploy.s.sol/$chain_id/run-latest.json
lazy_imt_name=$(cat ./broadcast/Deploy.s.sol/$chain_id/run-latest.json | jq -r '.["transactions"][1]["contractName"]')
lazy_imt_address=$(cat ./broadcast/Deploy.s.sol/$chain_id/run-latest.json | jq -r '.["transactions"][1]["contractAddress"]')

echo "Verifying $binary_imt_name library"
forge verify-contract $binary_imt_address \
echo "Verifying $lazy_imt_name library"
forge verify-contract $lazy_imt_address \
--libraries "poseidon-solidity/PoseidonT3.sol:$poseidon_t3_name:$poseidon_t3_address" \
--watch \
--chain $chain_name \
$binary_imt_name
$lazy_imt_name

# Get the Verifier contract address from ./broadcast/Deploy.s.sol/$chain_id/run-latest.json
verifier_name=$(cat ./broadcast/Deploy.s.sol/$chain_id/run-latest.json | jq -r '.["transactions"][2]["contractName"]')
Expand Down
14 changes: 9 additions & 5 deletions src/RlnBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pragma solidity ^0.8.19;

import { IVerifier } from "./IVerifier.sol";
import { BinaryIMT, BinaryIMTData } from "@zk-kit/imt.sol/BinaryIMT.sol";
import { LazyIMT, LazyIMTData } from "@zk-kit/imt.sol/LazyIMT.sol";

/// The tree is full
error FullTree();
Expand Down Expand Up @@ -84,7 +84,7 @@ abstract contract RlnBase {
uint32 public immutable deployedBlockNumber;

/// @notice the Incremental Merkle Tree
BinaryIMTData public imtData;
LazyIMTData public imtData;

/// Emitted when a new member is added to the set
/// @param idCommitment The idCommitment of the member
Expand All @@ -107,7 +107,7 @@ abstract contract RlnBase {
SET_SIZE = 1 << depth;
verifier = IVerifier(_verifier);
deployedBlockNumber = uint32(block.number);
BinaryIMT.initWithDefaultZeroes(imtData, 20);
LazyIMT.init(imtData, 20);
}

/// Allows a user to register as a member
Expand All @@ -130,7 +130,7 @@ abstract contract RlnBase {
members[idCommitment] = idCommitmentIndex;
indexToCommitment[idCommitmentIndex] = idCommitment;
memberExists[idCommitment] = true;
BinaryIMT.insert(imtData, idCommitment);
LazyIMT.insert(imtData, idCommitment);
stakedAmounts[idCommitment] = stake;

emit MemberRegistered(idCommitment, idCommitmentIndex);
Expand Down Expand Up @@ -237,7 +237,7 @@ abstract contract RlnBase {
}

function root() external view returns (uint256) {
return imtData.root;
return LazyIMT.root(imtData, 20);
}

function getCommitments(uint256 startIndex, uint256 endIndex) public view returns (uint256[] memory) {
Expand All @@ -250,4 +250,8 @@ abstract contract RlnBase {
}
return commitments;
}

function merkleProofElements(uint40 index) public view returns (uint256[] memory) {
return LazyIMT.merkleProofElements(imtData, index, 20);
}
}

0 comments on commit 71f310a

Please sign in to comment.