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

PoC: onchain rln tree + root #31

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
877 changes: 7 additions & 870 deletions contracts/PoseidonHasher.sol

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contracts/Rln.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity 0.8.15;

import "./RlnBase.sol";

contract Rln is RlnBase {
contract RLN is RlnBase {
constructor(uint256 membershipDeposit, uint256 depth, address _poseidonHasher, address _verifier)
RlnBase(membershipDeposit, depth, _poseidonHasher, _verifier)
{}
Expand Down
20 changes: 16 additions & 4 deletions contracts/RlnBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity 0.8.15;

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

/// The tree is full
error FullTree();
Expand Down Expand Up @@ -61,6 +62,7 @@ abstract contract RlnBase {
/// maps from idCommitment to their index in the set
mapping(uint256 => uint256) public members;

/// @notice The membership status of each member
mapping(uint256 => bool) public memberExists;

/// @notice The balance of each user that can be withdrawn
Expand All @@ -75,6 +77,9 @@ abstract contract RlnBase {
/// @notice the deployed block number
uint32 public immutable deployedBlockNumber;

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

/// Emitted when a new member is added to the set
/// @param idCommitment The idCommitment of the member
/// @param index The index of the member in the set
Expand All @@ -97,6 +102,7 @@ abstract contract RlnBase {
poseidonHasher = PoseidonHasher(_poseidonHasher);
verifier = IVerifier(_verifier);
deployedBlockNumber = uint32(block.number);
BinaryIMT.initWithDefaultZeroes(imtData, 20);
}

/// Allows a user to register as a member
Expand All @@ -118,6 +124,7 @@ abstract contract RlnBase {

members[idCommitment] = idCommitmentIndex;
rymnc marked this conversation as resolved.
Show resolved Hide resolved
memberExists[idCommitment] = true;
BinaryIMT.insert(imtData, idCommitment);
stakedAmounts[idCommitment] = stake;

emit MemberRegistered(idCommitment, idCommitmentIndex);
Expand Down Expand Up @@ -164,6 +171,7 @@ abstract contract RlnBase {
members[idCommitment] = 0;
memberExists[idCommitment] = false;
stakedAmounts[idCommitment] = 0;
// TODO: remove from IMT

// refund deposit
withdrawalBalance[receiver] += amountToTransfer;
Expand Down Expand Up @@ -191,10 +199,10 @@ abstract contract RlnBase {
}

/// Hashes a value using the Poseidon hasher
/// NOTE: The variant of Poseidon we use accepts only 1 input, assume n=2, and the second input is 0
/// @param input The value to hash
function hash(uint256 input) internal view returns (uint256) {
return poseidonHasher.hash(input);
/// NOTE: The variant of Poseidon we use accepts only 1 input, assume n=2
/// @param inputs The values to hash
function hash(uint256[2] memory inputs) internal view returns (uint256) {
rymnc marked this conversation as resolved.
Show resolved Hide resolved
return poseidonHasher.hash(inputs);
}

function isValidCommitment(uint256 idCommitment) public view returns (bool) {
Expand All @@ -215,4 +223,8 @@ abstract contract RlnBase {
[idCommitment, uint256(uint160(receiver))]
);
}

function computeRoot() external view returns (uint256) {
rymnc marked this conversation as resolved.
Show resolved Hide resolved
return imtData.root;
}
}
8 changes: 8 additions & 0 deletions deploy/001_deploy_poseidon_hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {

const [deployer] = await getUnnamedAccounts();

const deployRes = await deploy("PoseidonT3", {
from: deployer,
log: true,
});

await deploy("PoseidonHasher", {
from: deployer,
log: true,
libraries: {
PoseidonT3: deployRes.address,
},
});
};
export default func;
Expand Down
16 changes: 14 additions & 2 deletions deploy/003_deploy_rln.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
.address;
const rlnVerifierAddress = (await deployments.get("Verifier")).address;

await deploy("Rln", {
const deployRes = await deploy("BinaryIMT", {
from: deployer,
log: true,
libraries: {
PoseidonT3: (await deployments.get("PoseidonT3")).address,
},
});

await deploy("RLN", {
from: deployer,
log: true,
args: [1000000000000000, 20, poseidonHasherAddress, rlnVerifierAddress],
libraries: {
BinaryIMT: deployRes.address,
},
});
};

export default func;
func.tags = ["Rln"];
func.dependencies = ["PoseidonHasher", "RlnVerifier"];
func.dependencies = ["PoseidonHasher", "RlnVerifier", "BinaryIMT"];
Loading
Loading