feat: integrate plugins from rln-interep-contract

This commit is contained in:
rymnc 2023-03-29 11:58:09 +05:30
parent e31da6ac40
commit 5d59fbfe41
No known key found for this signature in database
GPG Key ID: C740033EE3F41EBD
19 changed files with 12034 additions and 37954 deletions

52
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,52 @@
name: CI
on:
push:
branches: [main]
paths:
- '**.sol'
- 'scripts/**.ts'
- 'test/**.ts'
- 'hardhat.config.ts'
- 'package.json'
- '.github/workflows/ci.yml'
pull_request:
branches: [main]
paths:
- '**.sol'
- 'scripts/**.ts'
- 'test/**.ts'
- 'hardhat.config.ts'
- 'package.json'
- '.github/workflows/ci.yml'
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 16
cache: 'yarn'
- id: dependencies
run: yarn install
- id: lint
run: yarn lint
- id: test
run: yarn test
- id: coverage
run: yarn coverage
- id: upload-coverage
# run only in pull requests
if: github.event_name == 'pull_request'
uses: zgosalvez/github-actions-report-lcov@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
coverage-files: coverage/lcov.info
artifact-name: code-coverage-report

4
.husky/pre-commit Normal file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx lint-staged

4
.solcover.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
istanbulReporter: ["lcov"],
istanbulFolder: "coverage",
};

203
LICENSE-APACHE Normal file
View File

@ -0,0 +1,203 @@
Copyright (c) 2023 Vac Research
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

25
LICENSE-MIT Normal file
View File

@ -0,0 +1,25 @@
Copyright (c) 2023 Vac Research
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE O THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

View File

@ -1,17 +1,32 @@
The [RLN](contracts/Rln.sol) and [PoseidonHasher](contracts/PoseidonHasher.sol) smart contracts are initially borrowed from the following repository https://github.com/kilic/rlnapp and some modifications are made on top of them. # Hardhat Project for rln-contract
They may undertake further changes in the future as needed.
## Requirements
The following will need to be installed in order to use this repo. Please follow the links and instructions.
- [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
- You'll know you've done it right if you can run `git --version`
- [Foundry / Foundryup](https://github.com/gakonst/foundry)
- This will install `forge`, `cast`, and `anvil`
- You can test you've installed them right by running `forge --version` and get an output like: `forge 0.2.0 (92f8951 2022-08-06T00:09:32.96582Z)`
- To get the latest of each, just run `foundryup`
- [Yarn](https://classic.yarnpkg.com/lang/en/docs/install)
# Hardhat Project for Rln-membership-contract
## Compilation ## Compilation
```shell ```shell
npx hardhat compile yarn compile
``` ```
## Testing ## Testing with Hardhat
```shell ```shell
npx hardhat test yarn test:hardhat
```
## Testing with Foundry
```shell
yarn test:foundry
``` ```
## Deploying ## Deploying
@ -19,15 +34,19 @@ npx hardhat test
- To deploy on local node, first start the local node and then run the deploy script - To deploy on local node, first start the local node and then run the deploy script
```shell ```shell
npx hardhat node yarn node
npx hardhat run --network localhost scripts/deploy.ts npx deploy:localhost
``` ```
- To deploy to an target network (like Goerli), use the name as mentioned in the Hardhat config file. - To deploy to an target network (like Sepolia), use the name as mentioned in the Hardhat config file.
```shell ```shell
npx hardhat run --network <your-network> scripts/deploy.js yarn deploy:sepolia
``` ```
## References ## References
For more information, see https://hardhat.org/hardhat-runner/docs/guides/project-setup For more information, see https://hardhat.org/hardhat-runner/docs/guides/project-setup
## License
Dual-licensed under MIT or Apache 2.0, refer to [LICENSE-MIT](LICENSE-MIT) or [LICENSE-APACHE](LICENSE-APACHE) for more information.

View File

@ -1,3 +1,7 @@
// SPDX-License-Identifier: (MIT OR Apache-2.0)
// Forked from https://github.com/kilic/rlnapp/
pragma solidity 0.8.15; pragma solidity 0.8.15;
interface IPoseidonHasher { interface IPoseidonHasher {
@ -7,147 +11,281 @@ interface IPoseidonHasher {
} }
contract PoseidonHasher is IPoseidonHasher { contract PoseidonHasher is IPoseidonHasher {
uint256 constant Q = 21888242871839275222246405745257275088548364400416034343698204186575808495617; uint256 constant Q =
uint256 constant C0 = 4417881134626180770308697923359573201005643519861877412381846989312604493735; 21888242871839275222246405745257275088548364400416034343698204186575808495617;
uint256 constant C1 = 5433650512959517612316327474713065966758808864213826738576266661723522780033; uint256 constant C0 =
uint256 constant C2 = 13641176377184356099764086973022553863760045607496549923679278773208775739952; 4417881134626180770308697923359573201005643519861877412381846989312604493735;
uint256 constant C3 = 17949713444224994136330421782109149544629237834775211751417461773584374506783; uint256 constant C1 =
uint256 constant C4 = 13765628375339178273710281891027109699578766420463125835325926111705201856003; 5433650512959517612316327474713065966758808864213826738576266661723522780033;
uint256 constant C5 = 19179513468172002314585757290678967643352171735526887944518845346318719730387; uint256 constant C2 =
uint256 constant C6 = 5157412437176756884543472904098424903141745259452875378101256928559722612176; 13641176377184356099764086973022553863760045607496549923679278773208775739952;
uint256 constant C7 = 535160875740282236955320458485730000677124519901643397458212725410971557409; uint256 constant C3 =
uint256 constant C8 = 1050793453380762984940163090920066886770841063557081906093018330633089036729; 17949713444224994136330421782109149544629237834775211751417461773584374506783;
uint256 constant C9 = 10665495010329663932664894101216428400933984666065399374198502106997623173873; uint256 constant C4 =
uint256 constant C10 = 19965634623406616956648724894636666805991993496469370618546874926025059150737; 13765628375339178273710281891027109699578766420463125835325926111705201856003;
uint256 constant C11 = 13007250030070838431593222885902415182312449212965120303174723305710127422213; uint256 constant C5 =
uint256 constant C12 = 16877538715074991604507979123743768693428157847423939051086744213162455276374; 19179513468172002314585757290678967643352171735526887944518845346318719730387;
uint256 constant C13 = 18211747749504876135588847560312685184956239426147543810126553367063157141465; uint256 constant C6 =
uint256 constant C14 = 18151553319826126919739798892854572062191241985315767086020821632812331245635; 5157412437176756884543472904098424903141745259452875378101256928559722612176;
uint256 constant C15 = 19957033149976712666746140949846950406660099037474791840946955175819555930825; uint256 constant C7 =
uint256 constant C16 = 3469514863538261843186854830917934449567467100548474599735384052339577040841; 535160875740282236955320458485730000677124519901643397458212725410971557409;
uint256 constant C17 = 989698510043911779243192466312362856042600749099921773896924315611668507708; uint256 constant C8 =
uint256 constant C18 = 12568377015646290945235387813564567111330046038050864455358059568128000172201; 1050793453380762984940163090920066886770841063557081906093018330633089036729;
uint256 constant C19 = 20856104135605479600325529349246932565148587186338606236677138505306779314172; uint256 constant C9 =
uint256 constant C20 = 8206918720503535523121349917159924938835810381723474192155637697065780938424; 10665495010329663932664894101216428400933984666065399374198502106997623173873;
uint256 constant C21 = 1309058477013932989380617265069188723120054926187607548493110334522527703566; uint256 constant C10 =
uint256 constant C22 = 14076116939332667074621703729512195584105250395163383769419390236426287710606; 19965634623406616956648724894636666805991993496469370618546874926025059150737;
uint256 constant C23 = 10153498892749751942204288991871286290442690932856658983589258153608012428674; uint256 constant C11 =
uint256 constant C24 = 18202499207234128286137597834010475797175973146805180988367589376893530181575; 13007250030070838431593222885902415182312449212965120303174723305710127422213;
uint256 constant C25 = 12739388830157083522877690211447248168864006284243907142044329113461613743052; uint256 constant C12 =
uint256 constant C26 = 15123358710467780770838026754240340042441262572309759635224051333176022613949; 16877538715074991604507979123743768693428157847423939051086744213162455276374;
uint256 constant C27 = 19925004701844594370904593774447343836015483888496504201331110250494635362184; uint256 constant C13 =
uint256 constant C28 = 10352416606816998476681131583320899030072315953910679608943150613208329645891; 18211747749504876135588847560312685184956239426147543810126553367063157141465;
uint256 constant C29 = 10567371822366244361703342347428230537114808440249611395507235283708966113221; uint256 constant C14 =
uint256 constant C30 = 5635498582763880627392290206431559361272660937399944184533035305989295959602; 18151553319826126919739798892854572062191241985315767086020821632812331245635;
uint256 constant C31 = 11866432933224219174041051738704352719163271639958083608224676028593315904909; uint256 constant C15 =
uint256 constant C32 = 5795020705294401441272215064554385591292330721703923167136157291459784140431; 19957033149976712666746140949846950406660099037474791840946955175819555930825;
uint256 constant C33 = 9482202378699252817564375087302794636287866584767523335624368774856230692758; uint256 constant C16 =
uint256 constant C34 = 4245237636894546151746468406560945873445548423466753843402086544922216329298; 3469514863538261843186854830917934449567467100548474599735384052339577040841;
uint256 constant C35 = 12000500941313982757584712677991730019124834399479314697467598397927435905133; uint256 constant C17 =
uint256 constant C36 = 7596790274058425558167520209857956363736666939016807569082239187494363541787; 989698510043911779243192466312362856042600749099921773896924315611668507708;
uint256 constant C37 = 2484867918246116343205467273440098378820186751202461278013576281097918148877; uint256 constant C18 =
uint256 constant C38 = 18312645949449997391810445935615409295369169383463185688973803378104013950190; 12568377015646290945235387813564567111330046038050864455358059568128000172201;
uint256 constant C39 = 15320686572748723004980855263301182130424010735782762814513954166519592552733; uint256 constant C19 =
uint256 constant C40 = 12618438900597948888520621062416758747872180395546164387827245287017031303859; 20856104135605479600325529349246932565148587186338606236677138505306779314172;
uint256 constant C41 = 17438141672027706116733201008397064011774368832458707512367404736905021019585; uint256 constant C20 =
uint256 constant C42 = 6374197807230665998865688675365359100400438034755781666913068586172586548950; 8206918720503535523121349917159924938835810381723474192155637697065780938424;
uint256 constant C43 = 2189398913433273865510950346186699930188746169476472274335177556702504595264; uint256 constant C21 =
uint256 constant C44 = 6268495580028970231803791523870131137294646402347399003576649137450213034606; 1309058477013932989380617265069188723120054926187607548493110334522527703566;
uint256 constant C45 = 17896250365994900261202920044129628104272791547990619503076839618914047059275; uint256 constant C22 =
uint256 constant C46 = 13692156312448722528008862371944543449350293305158722920787736248435893008873; 14076116939332667074621703729512195584105250395163383769419390236426287710606;
uint256 constant C47 = 15234446864368744483209945022439268713300180233589581910497691316744177619376; uint256 constant C23 =
uint256 constant C48 = 1572426502623310766593681563281600503979671244997798691029595521622402217227; 10153498892749751942204288991871286290442690932856658983589258153608012428674;
uint256 constant C49 = 80103447810215150918585162168214870083573048458555897999822831203653996617; uint256 constant C24 =
uint256 constant C50 = 8228820324013669567851850635126713973797711779951230446503353812192849106342; 18202499207234128286137597834010475797175973146805180988367589376893530181575;
uint256 constant C51 = 5375851433746509614045812476958526065449377558695752132494533666370449415873; uint256 constant C25 =
uint256 constant C52 = 12115998939203497346386774317892338270561208357481805380546938146796257365018; 12739388830157083522877690211447248168864006284243907142044329113461613743052;
uint256 constant C53 = 9764067909645821279940531410531154041386008396840887338272986634350423466622; uint256 constant C26 =
uint256 constant C54 = 8538708244538850542384936174629541085495830544298260335345008245230827876882; 15123358710467780770838026754240340042441262572309759635224051333176022613949;
uint256 constant C55 = 7140127896620013355910287215441004676619168261422440177712039790284719613114; uint256 constant C27 =
uint256 constant C56 = 14297402962228458726038826185823085337698917275385741292940049024977027409762; 19925004701844594370904593774447343836015483888496504201331110250494635362184;
uint256 constant C57 = 6667115556431351074165934212337261254608231545257434281887966406956835140819; uint256 constant C28 =
uint256 constant C58 = 20226761165244293291042617464655196752671169026542832236139342122602741090001; 10352416606816998476681131583320899030072315953910679608943150613208329645891;
uint256 constant C59 = 12038289506489256655759141386763477208196694421666339040483042079632134429119; uint256 constant C29 =
uint256 constant C60 = 19027757334170818571203982241812412991528769934917288000224335655934473717551; 10567371822366244361703342347428230537114808440249611395507235283708966113221;
uint256 constant C61 = 16272152964456553579565580463468069884359929612321610357528838696790370074720; uint256 constant C30 =
uint256 constant C62 = 2500392889689246014710135696485946334448570271481948765283016105301740284071; 5635498582763880627392290206431559361272660937399944184533035305989295959602;
uint256 constant C63 = 8595254970528530312401637448610398388203855633951264114100575485022581946023; uint256 constant C31 =
uint256 constant C64 = 11635945688914011450976408058407206367914559009113158286982919675551688078198; 11866432933224219174041051738704352719163271639958083608224676028593315904909;
uint256 constant C65 = 614739068603482619581328040478536306925147663946742687395148680260956671871; uint256 constant C32 =
uint256 constant C66 = 18692271780377861570175282183255720350972693125537599213951106550953176268753; 5795020705294401441272215064554385591292330721703923167136157291459784140431;
uint256 constant C67 = 4987059230784976306647166378298632695585915319042844495357753339378260807164; uint256 constant C33 =
uint256 constant C68 = 21851403978498723616722415377430107676258664746210815234490134600998983955497; 9482202378699252817564375087302794636287866584767523335624368774856230692758;
uint256 constant C69 = 9830635451186415300891533983087800047564037813328875992115573428596207326204; uint256 constant C34 =
uint256 constant C70 = 4842706106434537116860242620706030229206345167233200482994958847436425185478; 4245237636894546151746468406560945873445548423466753843402086544922216329298;
uint256 constant C71 = 6422235064906823218421386871122109085799298052314922856340127798647926126490; uint256 constant C35 =
uint256 constant C72 = 4564364104986856861943331689105797031330091877115997069096365671501473357846; 12000500941313982757584712677991730019124834399479314697467598397927435905133;
uint256 constant C73 = 1944043894089780613038197112872830569538541856657037469098448708685350671343; uint256 constant C36 =
uint256 constant C74 = 21179865974855950600518216085229498748425990426231530451599322283119880194955; 7596790274058425558167520209857956363736666939016807569082239187494363541787;
uint256 constant C75 = 14296697761894107574369608843560006996183955751502547883167824879840894933162; uint256 constant C37 =
uint256 constant C76 = 12274619649702218570450581712439138337725246879938860735460378251639845671898; 2484867918246116343205467273440098378820186751202461278013576281097918148877;
uint256 constant C77 = 16371396450276899401411886674029075408418848209575273031725505038938314070356; uint256 constant C38 =
uint256 constant C78 = 3702561221750983937578095019779188631407216522704543451228773892695044653565; 18312645949449997391810445935615409295369169383463185688973803378104013950190;
uint256 constant C79 = 19721616877735564664624984774636557499099875603996426215495516594530838681980; uint256 constant C39 =
uint256 constant C80 = 6383350109027696789969911008057747025018308755462287526819231672217685282429; 15320686572748723004980855263301182130424010735782762814513954166519592552733;
uint256 constant C81 = 20860583956177367265984596617324237471765572961978977333122281041544719622905; uint256 constant C40 =
uint256 constant C82 = 5766390934595026947545001478457407504285452477687752470140790011329357286275; 12618438900597948888520621062416758747872180395546164387827245287017031303859;
uint256 constant C83 = 4043175758319898049344746138515323336207420888499903387536875603879441092484; uint256 constant C41 =
uint256 constant C84 = 15579382179133608217098622223834161692266188678101563820988612253342538956534; 17438141672027706116733201008397064011774368832458707512367404736905021019585;
uint256 constant C85 = 1864640783252634743892105383926602930909039567065240010338908865509831749824; uint256 constant C42 =
uint256 constant C86 = 15943719865023133586707144161652035291705809358178262514871056013754142625673; 6374197807230665998865688675365359100400438034755781666913068586172586548950;
uint256 constant C87 = 2326415993032390211558498780803238091925402878871059708106213703504162832999; uint256 constant C43 =
uint256 constant C88 = 19995326402773833553207196590622808505547443523750970375738981396588337910289; 2189398913433273865510950346186699930188746169476472274335177556702504595264;
uint256 constant C89 = 5143583711361588952673350526320181330406047695593201009385718506918735286622; uint256 constant C44 =
uint256 constant C90 = 15436006486881920976813738625999473183944244531070780793506388892313517319583; 6268495580028970231803791523870131137294646402347399003576649137450213034606;
uint256 constant C91 = 16660446760173633166698660166238066533278664023818938868110282615200613695857; uint256 constant C45 =
uint256 constant C92 = 4966065365695755376133119391352131079892396024584848298231004326013366253934; 17896250365994900261202920044129628104272791547990619503076839618914047059275;
uint256 constant C93 = 20683781957411705574951987677641476019618457561419278856689645563561076926702; uint256 constant C46 =
uint256 constant C94 = 17280836839165902792086432296371645107551519324565649849400948918605456875699; 13692156312448722528008862371944543449350293305158722920787736248435893008873;
uint256 constant C95 = 17045635513701208892073056357048619435743564064921155892004135325530808465371; uint256 constant C47 =
uint256 constant C96 = 17055032967194400710390142791334572297458033582458169295920670679093585707295; 15234446864368744483209945022439268713300180233589581910497691316744177619376;
uint256 constant C97 = 15727174639569115300068198908071514334002742825679221638729902577962862163505; uint256 constant C48 =
uint256 constant C98 = 1001755657610446661315902885492677747789366510875120894840818704741370398633; 1572426502623310766593681563281600503979671244997798691029595521622402217227;
uint256 constant C99 = 18638547332826171619311285502376343504539399518545103511265465604926625041234; uint256 constant C49 =
uint256 constant C100 = 6751954224763196429755298529194402870632445298969935050224267844020826420799; 80103447810215150918585162168214870083573048458555897999822831203653996617;
uint256 constant C101 = 3526747115904224771452549517614107688674036840088422555827581348280834879405; uint256 constant C50 =
uint256 constant C102 = 15705897908180497062880001271426561999724005008972544196300715293701537574122; 8228820324013669567851850635126713973797711779951230446503353812192849106342;
uint256 constant C103 = 574386695213920937259007343820417029802510752426579750428758189312416867750; uint256 constant C51 =
uint256 constant C104 = 15973040855000600860816974646787367136127946402908768408978806375685439868553; 5375851433746509614045812476958526065449377558695752132494533666370449415873;
uint256 constant C105 = 20934130413948796333037139460875996342810005558806621330680156931816867321122; uint256 constant C52 =
uint256 constant C106 = 6918585327145564636398173845411579411526758237572034236476079610890705810764; 12115998939203497346386774317892338270561208357481805380546938146796257365018;
uint256 constant C107 = 14158163500813182062258176233162498241310167509137716527054939926126453647182; uint256 constant C53 =
uint256 constant C108 = 4164602626597695668474100217150111342272610479949122406544277384862187287433; 9764067909645821279940531410531154041386008396840887338272986634350423466622;
uint256 constant C109 = 12146526846507496913615390662823936206892812880963914267275606265272996025304; uint256 constant C54 =
uint256 constant C110 = 10153527926900017763244212043512822363696541810586522108597162891799345289938; 8538708244538850542384936174629541085495830544298260335345008245230827876882;
uint256 constant C111 = 13564663485965299104296214940873270349072051793008946663855767889066202733588; uint256 constant C55 =
uint256 constant C112 = 5612449256997576125867742696783020582952387615430650198777254717398552960096; 7140127896620013355910287215441004676619168261422440177712039790284719613114;
uint256 constant C113 = 12151885480032032868507892738683067544172874895736290365318623681886999930120; uint256 constant C56 =
uint256 constant C114 = 380452237704664384810613424095477896605414037288009963200982915188629772177; 14297402962228458726038826185823085337698917275385741292940049024977027409762;
uint256 constant C115 = 9067557551252570188533509616805287919563636482030947363841198066124642069518; uint256 constant C57 =
uint256 constant C116 = 21280306817619711661335268484199763923870315733198162896599997188206277056900; 6667115556431351074165934212337261254608231545257434281887966406956835140819;
uint256 constant C117 = 5567165819557297006750252582140767993422097822227408837378089569369734876257; uint256 constant C58 =
uint256 constant C118 = 10411936321072105429908396649383171465939606386380071222095155850987201580137; 20226761165244293291042617464655196752671169026542832236139342122602741090001;
uint256 constant C119 = 21338390051413922944780864872652000187403217966653363270851298678606449622266; uint256 constant C59 =
uint256 constant C120 = 12156296560457833712186127325312904760045212412680904475497938949653569234473; 12038289506489256655759141386763477208196694421666339040483042079632134429119;
uint256 constant C121 = 4271647814574748734312113971565139132510281260328947438246615707172526380757; uint256 constant C60 =
uint256 constant C122 = 9061738206062369647211128232833114177054715885442782773131292534862178874950; 19027757334170818571203982241812412991528769934917288000224335655934473717551;
uint256 constant C123 = 10134551893627587797380445583959894183158393780166496661696555422178052339133; uint256 constant C61 =
uint256 constant C124 = 8932270237664043612366044102088319242789325050842783721780970129656616386103; 16272152964456553579565580463468069884359929612321610357528838696790370074720;
uint256 constant C125 = 3339412934966886386194449782756711637636784424032779155216609410591712750636; uint256 constant C62 =
uint256 constant C126 = 9704903972004596791086522314847373103670545861209569267884026709445485704400; 2500392889689246014710135696485946334448570271481948765283016105301740284071;
uint256 constant C127 = 17467570179597572575614276429760169990940929887711661192333523245667228809456; uint256 constant C63 =
uint256 constant M00 = 2910766817845651019878574839501801340070030115151021261302834310722729507541; 8595254970528530312401637448610398388203855633951264114100575485022581946023;
uint256 constant M01 = 19727366863391167538122140361473584127147630672623100827934084310230022599144; uint256 constant C64 =
uint256 constant M10 = 5776684794125549462448597414050232243778680302179439492664047328281728356345; 11635945688914011450976408058407206367914559009113158286982919675551688078198;
uint256 constant M11 = 8348174920934122550483593999453880006756108121341067172388445916328941978568; uint256 constant C65 =
614739068603482619581328040478536306925147663946742687395148680260956671871;
uint256 constant C66 =
18692271780377861570175282183255720350972693125537599213951106550953176268753;
uint256 constant C67 =
4987059230784976306647166378298632695585915319042844495357753339378260807164;
uint256 constant C68 =
21851403978498723616722415377430107676258664746210815234490134600998983955497;
uint256 constant C69 =
9830635451186415300891533983087800047564037813328875992115573428596207326204;
uint256 constant C70 =
4842706106434537116860242620706030229206345167233200482994958847436425185478;
uint256 constant C71 =
6422235064906823218421386871122109085799298052314922856340127798647926126490;
uint256 constant C72 =
4564364104986856861943331689105797031330091877115997069096365671501473357846;
uint256 constant C73 =
1944043894089780613038197112872830569538541856657037469098448708685350671343;
uint256 constant C74 =
21179865974855950600518216085229498748425990426231530451599322283119880194955;
uint256 constant C75 =
14296697761894107574369608843560006996183955751502547883167824879840894933162;
uint256 constant C76 =
12274619649702218570450581712439138337725246879938860735460378251639845671898;
uint256 constant C77 =
16371396450276899401411886674029075408418848209575273031725505038938314070356;
uint256 constant C78 =
3702561221750983937578095019779188631407216522704543451228773892695044653565;
uint256 constant C79 =
19721616877735564664624984774636557499099875603996426215495516594530838681980;
uint256 constant C80 =
6383350109027696789969911008057747025018308755462287526819231672217685282429;
uint256 constant C81 =
20860583956177367265984596617324237471765572961978977333122281041544719622905;
uint256 constant C82 =
5766390934595026947545001478457407504285452477687752470140790011329357286275;
uint256 constant C83 =
4043175758319898049344746138515323336207420888499903387536875603879441092484;
uint256 constant C84 =
15579382179133608217098622223834161692266188678101563820988612253342538956534;
uint256 constant C85 =
1864640783252634743892105383926602930909039567065240010338908865509831749824;
uint256 constant C86 =
15943719865023133586707144161652035291705809358178262514871056013754142625673;
uint256 constant C87 =
2326415993032390211558498780803238091925402878871059708106213703504162832999;
uint256 constant C88 =
19995326402773833553207196590622808505547443523750970375738981396588337910289;
uint256 constant C89 =
5143583711361588952673350526320181330406047695593201009385718506918735286622;
uint256 constant C90 =
15436006486881920976813738625999473183944244531070780793506388892313517319583;
uint256 constant C91 =
16660446760173633166698660166238066533278664023818938868110282615200613695857;
uint256 constant C92 =
4966065365695755376133119391352131079892396024584848298231004326013366253934;
uint256 constant C93 =
20683781957411705574951987677641476019618457561419278856689645563561076926702;
uint256 constant C94 =
17280836839165902792086432296371645107551519324565649849400948918605456875699;
uint256 constant C95 =
17045635513701208892073056357048619435743564064921155892004135325530808465371;
uint256 constant C96 =
17055032967194400710390142791334572297458033582458169295920670679093585707295;
uint256 constant C97 =
15727174639569115300068198908071514334002742825679221638729902577962862163505;
uint256 constant C98 =
1001755657610446661315902885492677747789366510875120894840818704741370398633;
uint256 constant C99 =
18638547332826171619311285502376343504539399518545103511265465604926625041234;
uint256 constant C100 =
6751954224763196429755298529194402870632445298969935050224267844020826420799;
uint256 constant C101 =
3526747115904224771452549517614107688674036840088422555827581348280834879405;
uint256 constant C102 =
15705897908180497062880001271426561999724005008972544196300715293701537574122;
uint256 constant C103 =
574386695213920937259007343820417029802510752426579750428758189312416867750;
uint256 constant C104 =
15973040855000600860816974646787367136127946402908768408978806375685439868553;
uint256 constant C105 =
20934130413948796333037139460875996342810005558806621330680156931816867321122;
uint256 constant C106 =
6918585327145564636398173845411579411526758237572034236476079610890705810764;
uint256 constant C107 =
14158163500813182062258176233162498241310167509137716527054939926126453647182;
uint256 constant C108 =
4164602626597695668474100217150111342272610479949122406544277384862187287433;
uint256 constant C109 =
12146526846507496913615390662823936206892812880963914267275606265272996025304;
uint256 constant C110 =
10153527926900017763244212043512822363696541810586522108597162891799345289938;
uint256 constant C111 =
13564663485965299104296214940873270349072051793008946663855767889066202733588;
uint256 constant C112 =
5612449256997576125867742696783020582952387615430650198777254717398552960096;
uint256 constant C113 =
12151885480032032868507892738683067544172874895736290365318623681886999930120;
uint256 constant C114 =
380452237704664384810613424095477896605414037288009963200982915188629772177;
uint256 constant C115 =
9067557551252570188533509616805287919563636482030947363841198066124642069518;
uint256 constant C116 =
21280306817619711661335268484199763923870315733198162896599997188206277056900;
uint256 constant C117 =
5567165819557297006750252582140767993422097822227408837378089569369734876257;
uint256 constant C118 =
10411936321072105429908396649383171465939606386380071222095155850987201580137;
uint256 constant C119 =
21338390051413922944780864872652000187403217966653363270851298678606449622266;
uint256 constant C120 =
12156296560457833712186127325312904760045212412680904475497938949653569234473;
uint256 constant C121 =
4271647814574748734312113971565139132510281260328947438246615707172526380757;
uint256 constant C122 =
9061738206062369647211128232833114177054715885442782773131292534862178874950;
uint256 constant C123 =
10134551893627587797380445583959894183158393780166496661696555422178052339133;
uint256 constant C124 =
8932270237664043612366044102088319242789325050842783721780970129656616386103;
uint256 constant C125 =
3339412934966886386194449782756711637636784424032779155216609410591712750636;
uint256 constant C126 =
9704903972004596791086522314847373103670545861209569267884026709445485704400;
uint256 constant C127 =
17467570179597572575614276429760169990940929887711661192333523245667228809456;
uint256 constant M00 =
2910766817845651019878574839501801340070030115151021261302834310722729507541;
uint256 constant M01 =
19727366863391167538122140361473584127147630672623100827934084310230022599144;
uint256 constant M10 =
5776684794125549462448597414050232243778680302179439492664047328281728356345;
uint256 constant M11 =
8348174920934122550483593999453880006756108121341067172388445916328941978568;
function hash(uint256 input) external pure override returns (uint256 result) { function hash(
uint256 input
) external pure override returns (uint256 result) {
return _hash(input); return _hash(input);
} }
function _hash(uint256 input) internal pure returns (uint256 result) { function _hash(uint256 input) internal pure returns (uint256 result) {
assembly { assembly {
// Poseidon parameters should be t = 2, RF = 8, RP = 56 // Poseidon parameters should be t = 2, RF = 8, RP = 56
// We load the characteristic // We load the characteristic
@ -862,7 +1000,6 @@ contract PoseidonHasher is IPoseidonHasher {
s1 := add(mulmod(s0, M10, q), mulmod(s1, M11, q)) s1 := add(mulmod(s0, M10, q), mulmod(s1, M11, q))
s0 := t s0 := t
//ROUND 63 - FULL //ROUND 63 - FULL
s0 := add(s0, C126) s0 := add(s0, C126)
s1 := add(s1, C127) s1 := add(s1, C127)
@ -874,9 +1011,7 @@ contract PoseidonHasher is IPoseidonHasher {
// MIX // MIX
s0 := mod(add(mulmod(s0, M00, q), mulmod(s1, M01, q)), q) s0 := mod(add(mulmod(s0, M00, q), mulmod(s1, M01, q)), q)
result := s0 result := s0
} }
} }
@ -886,6 +1021,7 @@ contract PoseidonHasher is IPoseidonHasher {
// The hash of 0 // The hash of 0
function _identity() internal pure returns (uint256) { function _identity() internal pure returns (uint256) {
return 0x2a09a9fd93c590c26b91effbb2499f07e8f7aa12e2b4940a3aed2411cb65e11c; return
0x2a09a9fd93c590c26b91effbb2499f07e8f7aa12e2b4940a3aed2411cb65e11c;
} }
} }

View File

@ -1,3 +1,5 @@
// SPDX-License-Identifier: (MIT OR Apache-2.0)
pragma solidity 0.8.15; pragma solidity 0.8.15;
import {IPoseidonHasher} from "./PoseidonHasher.sol"; import {IPoseidonHasher} from "./PoseidonHasher.sol";
@ -7,15 +9,14 @@ contract RLN {
uint256 public immutable DEPTH; uint256 public immutable DEPTH;
uint256 public immutable SET_SIZE; uint256 public immutable SET_SIZE;
uint256 public pubkeyIndex = 0; uint256 public idCommitmentIndex;
// This mapping is used to keep track of the public keys that have been registered mapping(uint256 => uint256) public stakedAmounts;
// with the stake mapping(uint256 => bool) public members;
mapping(uint256 => uint256) public members;
IPoseidonHasher public poseidonHasher; IPoseidonHasher public poseidonHasher;
event MemberRegistered(uint256 pubkey, uint256 index); event MemberRegistered(uint256 idCommitment, uint256 index);
event MemberWithdrawn(uint256 pubkey); event MemberWithdrawn(uint256 idCommitment);
constructor( constructor(
uint256 membershipDeposit, uint256 membershipDeposit,
@ -28,27 +29,44 @@ contract RLN {
poseidonHasher = IPoseidonHasher(_poseidonHasher); poseidonHasher = IPoseidonHasher(_poseidonHasher);
} }
function register(uint256 pubkey) external payable { function register(uint256 idCommitment) external payable {
require(members[pubkey] == 0, "RLN, register: pubkey already registered"); require(
require(pubkeyIndex < SET_SIZE, "RLN, register: set is full"); msg.value == MEMBERSHIP_DEPOSIT,
require(msg.value == MEMBERSHIP_DEPOSIT, "RLN, register: membership deposit is not satisfied"); "RLN, register: membership deposit is not satisfied"
_register(pubkey); );
_register(idCommitment, msg.value);
} }
function registerBatch(uint256[] calldata pubkeys) external payable { function registerBatch(uint256[] calldata idCommitments) external payable {
uint256 pubkeylen = pubkeys.length; uint256 idCommitmentlen = idCommitments.length;
require(pubkeyIndex + pubkeylen <= SET_SIZE, "RLN, registerBatch: set is full"); require(
require(msg.value == MEMBERSHIP_DEPOSIT * pubkeylen, "RLN, registerBatch: membership deposit is not satisfied"); idCommitmentIndex + idCommitmentlen <= SET_SIZE,
for (uint256 i = 0; i < pubkeylen; i++) { "RLN, registerBatch: set is full"
_register(pubkeys[i]); );
require(
msg.value == MEMBERSHIP_DEPOSIT * idCommitmentlen,
"RLN, registerBatch: membership deposit is not satisfied"
);
for (uint256 i = 0; i < idCommitmentlen; i++) {
_register(idCommitments[i], msg.value / idCommitmentlen);
} }
} }
function _register(uint256 pubkey) internal { function _register(uint256 idCommitment, uint256 stake) internal {
// Set the pubkey to the value of the tx require(
members[pubkey] = msg.value; !members[idCommitment],
emit MemberRegistered(pubkey, pubkeyIndex); "RLN, _register: member already registered"
pubkeyIndex += 1; );
require(idCommitmentIndex < SET_SIZE, "RLN, register: set is full");
if (stake != 0) {
members[idCommitment] = true;
stakedAmounts[idCommitment] = stake;
} else {
members[idCommitment] = true;
stakedAmounts[idCommitment] = 0;
}
emit MemberRegistered(idCommitment, idCommitmentIndex);
idCommitmentIndex += 1;
} }
function withdrawBatch( function withdrawBatch(
@ -57,36 +75,69 @@ contract RLN {
) external { ) external {
uint256 batchSize = secrets.length; uint256 batchSize = secrets.length;
require(batchSize != 0, "RLN, withdrawBatch: batch size zero"); require(batchSize != 0, "RLN, withdrawBatch: batch size zero");
require(batchSize == receivers.length, "RLN, withdrawBatch: batch size mismatch receivers"); require(
batchSize == secrets.length,
"RLN, withdrawBatch: batch size mismatch secrets"
);
require(
batchSize == receivers.length,
"RLN, withdrawBatch: batch size mismatch receivers"
);
for (uint256 i = 0; i < batchSize; i++) { for (uint256 i = 0; i < batchSize; i++) {
_withdraw(secrets[i], receivers[i]); _withdraw(secrets[i], receivers[i]);
} }
} }
function withdraw( function withdraw(uint256 secret, address payable receiver) external {
uint256 secret,
address payable receiver
) external {
_withdraw(secret, receiver); _withdraw(secret, receiver);
} }
function _withdraw( function withdraw(uint256 secret) external {
uint256 secret, _withdraw(secret);
address payable receiver }
) internal {
// derive public key function _withdraw(uint256 secret, address payable receiver) internal {
uint256 pubkey = hash(secret); // derive idCommitment
require(members[pubkey] != 0, "RLN, _withdraw: member doesn't exist"); uint256 idCommitment = hash(secret);
require(receiver != address(0), "RLN, _withdraw: empty receiver address");
// check if member is registered
require(members[idCommitment], "RLN, _withdraw: member not registered");
// check if member has stake
require(
stakedAmounts[idCommitment] != 0,
"RLN, _withdraw: member has no stake"
);
require(
receiver != address(0),
"RLN, _withdraw: empty receiver address"
);
// delete member
members[idCommitment] = false;
stakedAmounts[idCommitment] = 0;
// refund deposit // refund deposit
(bool sent, bytes memory data) = receiver.call{value: members[pubkey]}(""); (bool sent, ) = receiver.call{value: stakedAmounts[idCommitment]}("");
require(sent, "transfer failed"); require(sent, "transfer failed");
// delete member only if refund is successful emit MemberWithdrawn(idCommitment);
members[pubkey] = 0; }
emit MemberWithdrawn(pubkey); function _withdraw(uint256 secret) internal {
// derive idCommitment
uint256 idCommitment = hash(secret);
// check if member is registered
require(members[idCommitment], "RLN, _withdraw: member not registered");
require(stakedAmounts[idCommitment] == 0, "RLN, _withdraw: staked");
// delete member
members[idCommitment] = false;
emit MemberWithdrawn(idCommitment);
} }
function hash(uint256 input) internal view returns (uint256) { function hash(uint256 input) internal view returns (uint256) {

View File

@ -0,0 +1,16 @@
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("PoseidonHasher", {
from: deployer,
log: true,
});
};
export default func;
func.tags = ["PoseidonHasher"];

21
deploy/002_deploy_rln.ts Normal file
View File

@ -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();
const poseidonHasherAddress = (await deployments.get("PoseidonHasher"))
.address;
await deploy("RLN", {
from: deployer,
log: true,
args: [1000000000000000, 20, poseidonHasherAddress],
});
};
export default func;
func.tags = ["RLN"];
func.dependencies = ["PoseidonHasher"];

View File

@ -1,35 +1,47 @@
import * as dotenv from "dotenv"; import * as dotenv from "dotenv";
import { HardhatUserConfig, task } from "hardhat/config"; import { HardhatUserConfig } from "hardhat/config";
import { NetworksUserConfig } from "hardhat/types";
import "hardhat-deploy";
import "@nomiclabs/hardhat-ethers";
import "@nomiclabs/hardhat-waffle"; import "@nomiclabs/hardhat-waffle";
import "hardhat-gas-reporter"; import "hardhat-gas-reporter";
import "solidity-coverage"; import "solidity-coverage";
dotenv.config(); dotenv.config();
const {GOERLI_URL,PRIVATE_KEY} = process.env; const { SEPOLIA_URL, PRIVATE_KEY } = process.env;
const getNetworkConfig = (): NetworksUserConfig | undefined => {
// This is a sample Hardhat task. To learn how to create your own go to if (SEPOLIA_URL && PRIVATE_KEY) {
// https://hardhat.org/guides/create-task.html return {
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => { goerli: {
const accounts = await hre.ethers.getSigners(); url: SEPOLIA_URL,
accounts: [PRIVATE_KEY],
for (const account of accounts) { forking: {
console.log(account.address); url: SEPOLIA_URL,
},
},
localhost_integration: {
url: "http://localhost:8545",
},
};
} }
}); return undefined;
};
// You need to export an object to set up your config // You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more // Go to https://hardhat.org/config/ to learn more
const config: HardhatUserConfig = { const config: HardhatUserConfig = {
solidity: "0.8.15", solidity: {
networks: { compilers: [
goerli: { {
url: GOERLI_URL, version: "0.8.15",
accounts: [`${PRIVATE_KEY}`] },
} ],
} },
networks: getNetworkConfig(),
}; };
export default config; export default config;

36830
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,20 +1,44 @@
{ {
"name": "hardhat-project", "name": "rln-contract",
"license": "(MIT OR Apache-2.0)",
"scripts": {
"start": "hardhat node",
"compile": "hardhat compile",
"test:hardhat": "hardhat test",
"test:hardhat:localhost": "yarn test:hardhat --network localhost",
"test:hardhat:sepolia": "yarn test:hardhat --network sepolia",
"test:foundry": "forge test",
"deploy": "hardhat run scripts/deploy.ts --network",
"deploy:sepolia": "yarn deploy sepolia",
"deploy:localhost": "yarn deploy localhost",
"coverage": "hardhat coverage",
"fmt": "prettier --write \"**/*.{js,ts}\"",
"lint": "prettier --check \"**/*.{js,ts}\"",
"prepare": "husky install"
},
"devDependencies": { "devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.6", "@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers",
"@nomiclabs/hardhat-etherscan": "^3.1.0", "@nomiclabs/hardhat-etherscan": "^3.1.0",
"@nomiclabs/hardhat-waffle": "^2.0.3", "@nomiclabs/hardhat-waffle": "^2.0.3",
"@types/mocha": "^9.1.1", "@types/mocha": "^9.1.1",
"chai": "^4.3.6", "chai": "^4.3.6",
"ethereum-waffle": "^3.4.4", "ethereum-waffle": "^3.4.4",
"ethers": "^5.6.9", "ethers": "^5.7.2",
"hardhat": "^2.9.9", "hardhat": "^2.9.9",
"hardhat-deploy": "0.11.20",
"hardhat-gas-reporter": "^1.0.8", "hardhat-gas-reporter": "^1.0.8",
"husky": "^8.0.2",
"lint-staged": "^13.0.3",
"solidity-coverage": "^0.7.21", "solidity-coverage": "^0.7.21",
"ts-node": "^10.8.1", "ts-node": "^10.8.1",
"typescript": "^4.7.4" "typescript": "^4.7.4"
}, },
"dependencies": { "dependencies": {
"dotenv": "^16.0.1" "dotenv": "^16.0.1"
},
"lint-staged": {
"**/*": [
"prettier --write --ignore-unknown"
]
} }
} }

View File

@ -1,37 +0,0 @@
// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// When running the script with `npx hardhat run <script>` you'll find the Hardhat
// Runtime Environment's members available in the global scope.
import { ethers } from "hardhat";
async function main() {
// Hardhat always runs the compile task when running scripts with its command
// line interface.
//
// If this script is run directly using `node` you may want to call compile
// manually to make sure everything is compiled
// await hre.run('compile');
// We get the contract to deploy
const PoseidonHasher = await ethers.getContractFactory("PoseidonHasher");
const poseidonHasher = await PoseidonHasher.deploy();
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);
await rln.deployed();
console.log("Rln deployed to:", rln.address);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});

View File

@ -1,74 +0,0 @@
import { assert, 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);
const Rln = await ethers.getContractFactory("RLN");
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, receiver_address);
const txWithdrawReceipt = await res_withdraw.wait();
const wit_pubkey = txWithdrawReceipt.events[0].args.pubkey;
// 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 commitment");
const pubkeyIndex = (await rln.pubkeyIndex()).toNumber();
assert(pubkeyIndex === 1, "pubkeyIndex should be 1");
});
it("should not allow dupe registrations", async () => {
const PoseidonHasher = await ethers.getContractFactory("PoseidonHasher");
const poseidonHasher = await PoseidonHasher.deploy();
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);
await rln.deployed();
console.log("Rln deployed to:", rln.address);
const price = await rln.MEMBERSHIP_DEPOSIT();
// A valid id_commitment generated in rust
const id_commitment = "0x0c3ac305f6a4fe9bfeb3eba978bc876e2a99208b8b56c80160cfb54ba8f02368"
await rln.register(id_commitment, {value: price});
expect(rln.register(id_commitment, {value: price})).to.be.revertedWith("RLN, register: pubkey already registered");
});
});

View File

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

111
test/rln.ts Normal file
View File

@ -0,0 +1,111 @@
import { expect } from "chai";
import { ethers, deployments } from "hardhat";
describe("RLN", () => {
beforeEach(async () => {
await deployments.fixture(["RLN"]);
});
it("should register new memberships", async () => {
const rln = await ethers.getContract("RLN", ethers.provider.getSigner(0));
const price = await rln.MEMBERSHIP_DEPOSIT();
// A valid pair of (id_secret, id_commitment) generated in rust
const idCommitment =
"0x0c3ac305f6a4fe9bfeb3eba978bc876e2a99208b8b56c80160cfb54ba8f02368";
const registerTx = await rln["register(uint256)"](idCommitment, {
value: price,
});
const txRegisterReceipt = await registerTx.wait();
const pubkey = txRegisterReceipt.events[0].args.idCommitment;
// We ensure the registered id_commitment is the one we passed
expect(
pubkey.toHexString() === idCommitment,
"registered commitment doesn't match passed commitment"
);
});
it("should withdraw membership", async () => {
const rln = await ethers.getContract("RLN", ethers.provider.getSigner(0));
const price = await rln.MEMBERSHIP_DEPOSIT();
// A valid pair of (id_secret, id_commitment) generated in rust
const idSecret =
"0x2a09a9fd93c590c26b91effbb2499f07e8f7aa12e2b4940a3aed2411cb65e11c";
const idCommitment =
"0x0c3ac305f6a4fe9bfeb3eba978bc876e2a99208b8b56c80160cfb54ba8f02368";
const registerTx = await rln["register(uint256)"](idCommitment, {
value: price,
});
await registerTx.wait();
// We withdraw our id_commitment
const receiverAddress = "0x000000000000000000000000000000000000dead";
const withdrawTx = await rln["withdraw(uint256,address)"](
idSecret,
receiverAddress
);
const txWithdrawReceipt = await withdrawTx.wait();
const withdrawalPk = txWithdrawReceipt.events[0].args.idCommitment;
// We ensure the registered id_commitment is the one we passed and that the index is the same
expect(
withdrawalPk.toHexString() === idCommitment,
"withdraw commitment doesn't match registered commitment"
);
});
it("should not withdraw stake without address", async () => {
const rln = await ethers.getContract("RLN", ethers.provider.getSigner(0));
const price = await rln.MEMBERSHIP_DEPOSIT();
// A valid pair of (id_secret, id_commitment) generated in rust
const idSecret =
"0x2a09a9fd93c590c26b91effbb2499f07e8f7aa12e2b4940a3aed2411cb65e11c";
const idCommitment =
"0x0c3ac305f6a4fe9bfeb3eba978bc876e2a99208b8b56c80160cfb54ba8f02368";
const registerTx = await rln["register(uint256)"](idCommitment, {
value: price,
});
await registerTx.wait();
// We withdraw our id_commitment
const withdrawTx = rln["withdraw(uint256)"](idSecret);
await expect(withdrawTx).to.be.revertedWith("RLN, _withdraw: staked");
});
it("should not allow multiple registrations with same pubkey", async () => {
const rln = await ethers.getContract("RLN", ethers.provider.getSigner(0));
const price = await rln.MEMBERSHIP_DEPOSIT();
// A valid pair of (id_secret, id_commitment) generated in rust
const idCommitment =
"0x0c3ac305f6a4fe9bfeb3eba978bc876e2a99208b8b56c80160cfb54ba8f02368";
const registerTx = await rln["register(uint256)"](idCommitment, {
value: price,
});
await registerTx.wait();
// Send the same tx again
const registerTx2 = rln["register(uint256)"](idCommitment, {
value: price,
});
await expect(registerTx2).to.be.revertedWith(
"RLN, _register: member already registered"
);
});
});

View File

@ -7,6 +7,13 @@
"outDir": "dist", "outDir": "dist",
"declaration": true "declaration": true
}, },
"include": ["./scripts", "./test", "./typechain"], "include": [
"files": ["./hardhat.config.ts"] "./scripts",
"./test",
"./typechain",
"deploy"
],
"files": [
"./hardhat.config.ts"
]
} }

10336
yarn.lock Normal file

File diff suppressed because it is too large Load Diff