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 12 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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ETHERSCAN_API_KEY=<YOUR_ETHERSCAN_KEY>
SEPOLIA_URL=https://eth-sepolia.alchemyapi.io/v2/<YOUR ALCHEMY KEY>
POLYGON_ZKEVM_TESTNET_URL=https://rpc.public.zkevm-test.net
PRIVATE_KEY=<YOUR_PRIVATE_KEY>
882 changes: 0 additions & 882 deletions contracts/PoseidonHasher.sol

This file was deleted.

6 changes: 3 additions & 3 deletions contracts/Rln.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pragma solidity 0.8.15;

import "./RlnBase.sol";

contract Rln is RlnBase {
constructor(uint256 membershipDeposit, uint256 depth, address _poseidonHasher, address _verifier)
RlnBase(membershipDeposit, depth, _poseidonHasher, _verifier)
contract RLN is RlnBase {
constructor(uint256 membershipDeposit, uint256 depth, address _verifier)
RlnBase(membershipDeposit, depth, _verifier)
{}

function _validateRegistration(uint256 idCommitment) internal pure override {}
Expand Down
52 changes: 37 additions & 15 deletions contracts/RlnBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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 @@ -40,7 +40,13 @@ error InsufficientContractBalance();
/// Invalid proof
error InvalidProof();

/// Invalid pagination query
error InvalidPaginationQuery(uint256 startIndex, uint256 endIndex);

abstract contract RlnBase {
/// @notice The Field
uint256 public constant Q = 21888242871839275222246405745257275088548364400416034343698204186575808495617;

/// @notice The deposit amount required to register as a member
uint256 public immutable MEMBERSHIP_DEPOSIT;

Expand All @@ -61,20 +67,24 @@ abstract contract RlnBase {
/// maps from idCommitment to their index in the set
mapping(uint256 => uint256) public members;

/// @notice the index to commitment mapping
mapping(uint256 => uint256) public indexToCommitment;

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

/// @notice The balance of each user that can be withdrawn
mapping(address => uint256) public withdrawalBalance;

/// @notice The Poseidon hasher contract
PoseidonHasher public immutable poseidonHasher;

/// @notice The groth16 verifier contract
IVerifier public immutable verifier;

/// @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 @@ -90,13 +100,13 @@ abstract contract RlnBase {
_;
}

constructor(uint256 membershipDeposit, uint256 depth, address _poseidonHasher, address _verifier) {
constructor(uint256 membershipDeposit, uint256 depth, address _verifier) {
MEMBERSHIP_DEPOSIT = membershipDeposit;
DEPTH = depth;
SET_SIZE = 1 << depth;
poseidonHasher = PoseidonHasher(_poseidonHasher);
verifier = IVerifier(_verifier);
deployedBlockNumber = uint32(block.number);
BinaryIMT.initWithDefaultZeroes(imtData, 20);
}

/// Allows a user to register as a member
Expand All @@ -117,7 +127,9 @@ abstract contract RlnBase {
if (idCommitmentIndex >= SET_SIZE) revert FullTree();

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

emit MemberRegistered(idCommitment, idCommitmentIndex);
Expand Down Expand Up @@ -162,8 +174,10 @@ abstract contract RlnBase {
// delete member
uint256 index = members[idCommitment];
members[idCommitment] = 0;
indexToCommitment[index] = 0;
memberExists[idCommitment] = false;
stakedAmounts[idCommitment] = 0;
// TODO: remove from IMT

// refund deposit
withdrawalBalance[receiver] += amountToTransfer;
Expand All @@ -190,15 +204,8 @@ abstract contract RlnBase {
payable(msg.sender).transfer(amount);
}

/// 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);
}

function isValidCommitment(uint256 idCommitment) public view returns (bool) {
return idCommitment != 0 && idCommitment < poseidonHasher.Q();
function isValidCommitment(uint256 idCommitment) public pure returns (bool) {
return idCommitment != 0 && idCommitment < Q;
}

/// @dev Groth16 proof verification
Expand All @@ -215,4 +222,19 @@ abstract contract RlnBase {
[idCommitment, uint256(uint160(receiver))]
);
}

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

function getCommitments(uint256 startIndex, uint256 endIndex) public view returns (uint256[] memory) {
if (startIndex >= endIndex) revert InvalidPaginationQuery(startIndex, endIndex);
if (endIndex > idCommitmentIndex) revert InvalidPaginationQuery(startIndex, endIndex);

uint256[] memory commitments = new uint256[](endIndex - startIndex);
for (uint256 i = startIndex; i < endIndex; i++) {
commitments[i - startIndex] = indexToCommitment[i];
}
return commitments;
}
}
4 changes: 2 additions & 2 deletions deploy/001_deploy_poseidon_hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {

const [deployer] = await getUnnamedAccounts();

await deploy("PoseidonHasher", {
await deploy("PoseidonT3", {
from: deployer,
log: true,
});
};
export default func;
func.tags = ["PoseidonHasher"];
func.tags = ["PoseidonT3"];
21 changes: 21 additions & 0 deletions deploy/003_deploy_binary_imt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployments, getUnnamedAccounts } = hre;
const { deploy } = deployments;

const [deployer] = await getUnnamedAccounts();

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

export default func;
func.tags = ["BinaryIMT"];
func.dependencies = ["PoseidonT3"];
16 changes: 10 additions & 6 deletions deploy/003_deploy_rln.ts → deploy/004_deploy_rln.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {

const [deployer] = await getUnnamedAccounts();

const poseidonHasherAddress = (await deployments.get("PoseidonHasher"))
.address;
const rlnVerifierAddress = (await deployments.get("Verifier")).address;

await deploy("Rln", {
const binaryIMTAddress = (await deployments.get("BinaryIMT")).address;

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

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