Merge pull request #9 from vacp2p/update-poseidon

refactor(poseidon): use correct constants in Poseidon
This commit is contained in:
G 2022-10-04 23:02:30 +02:00 committed by GitHub
commit b8d0a298c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 905 additions and 497 deletions

File diff suppressed because it is too large Load Diff

View File

@ -79,20 +79,20 @@ contract RLN {
require(receiver != address(0), "RLN, _withdraw: empty receiver address");
// derive public key
uint256 pubkey = hash([secret, 0]);
uint256 pubkey = hash(secret);
require(members[_pubkeyIndex] == pubkey, "RLN, _withdraw: not verified");
// delete member
members[_pubkeyIndex] = 0;
// refund deposit
(bool sent, _) = receiver.call{value: MEMBERSHIP_DEPOSIT}("");
(bool sent, bytes memory data) = receiver.call{value: MEMBERSHIP_DEPOSIT}("");
require(sent, "transfer failed");
emit MemberWithdrawn(pubkey, _pubkeyIndex);
}
function hash(uint256[2] memory input) internal view returns (uint256) {
function hash(uint256 input) internal view returns (uint256) {
return poseidonHasher.hash(input);
}
}

View File

@ -1,4 +1,4 @@
import { expect } from "chai";
import { assert } from "chai";
import { ethers } from "hardhat";
describe("Rln", function () {
@ -9,12 +9,41 @@ describe("Rln", function () {
await poseidonHasher.deployed();
console.log("PoseidonHasher deployed to:", poseidonHasher.address);
const Rln = await ethers.getContractFactory("RLN");
const rln = await Rln.deploy(1000000000000000,20,poseidonHasher.address);
const rln = await Rln.deploy(1000000000000000, 20, poseidonHasher.address);
await rln.deployed();
console.log("Rln deployed to:", rln.address);
const price = await rln.MEMBERSHIP_DEPOSIT();
// A valid pair of (id_secret, id_commitment) generated in rust
const id_secret = "0x2a09a9fd93c590c26b91effbb2499f07e8f7aa12e2b4940a3aed2411cb65e11c"
const id_commitment = "0x0c3ac305f6a4fe9bfeb3eba978bc876e2a99208b8b56c80160cfb54ba8f02368"
const res_register = await rln.register(id_commitment, {value: price});
const txRegisterReceipt = await res_register.wait();
const reg_pubkey = txRegisterReceipt.events[0].args.pubkey;
const reg_tree_index = txRegisterReceipt.events[0].args.index;
// We ensure the registered id_commitment is the one we passed
assert(reg_pubkey.toHexString() === id_commitment, "registered commitment doesn't match passed commitment");
// We withdraw our id_commitment
const receiver_address = "0x000000000000000000000000000000000000dead";
const res_withdraw = await rln.withdraw(id_secret, reg_tree_index, receiver_address);
const txWithdrawReceipt = await res_withdraw.wait();
const wit_pubkey = txWithdrawReceipt.events[0].args.pubkey;
const wit_tree_index = txWithdrawReceipt.events[0].args.index;
// We ensure the registered id_commitment is the one we passed and that the index is the same
assert(wit_pubkey.toHexString() === id_commitment, "withdraw commitment doesn't match registered commitmet");
assert(wit_tree_index.toHexString() === reg_tree_index.toHexString(), "withdraw index doesn't match registered index");
});
});
});

21
test/poseidon.ts Normal file
View File

@ -0,0 +1,21 @@
import { expect } from "chai";
import { ethers } from "hardhat";
describe("Rln", function () {
it("Deploying", async function () {
const PoseidonHasher = await ethers.getContractFactory("PoseidonHasher");
const poseidonHasher = await PoseidonHasher.deploy();
await poseidonHasher.deployed();
console.log("PoseidonHasher deployed to:", poseidonHasher.address);
// We test hashing for a random number
const hash = await poseidonHasher.hash("19014214495641488759237505126948346942972912379615652741039992445865937985820");
console.log("Hash:", hash);
//Expect 0x0c3ac305f6a4fe9bfeb3eba978bc876e2a99208b8b56c80160cfb54ba8f02368
});
});