From 766366c6125764c8944645c1ed307d649eeffaf3 Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Wed, 26 Jul 2023 16:12:37 +0530 Subject: [PATCH 1/3] chore: convert rln to rlnbase, and inherit into rln --- contracts/PoseidonHasher.sol | 405 ++++++++++++----------------------- contracts/Rln.sol | 182 +--------------- contracts/RlnBase.sol | 193 +++++++++++++++++ contracts/RlnVerifier.sol | 2 +- deploy/003_deploy_rln.ts | 4 +- docs/index.md | 48 ++++- test/PoseidonHasher.t.sol | 4 +- test/RLN.t.sol | 69 ++---- test/RLNApp.t.sol | 71 ++++++ test/Verifier.sol | 24 +-- test/rln.test.ts | 10 +- 11 files changed, 492 insertions(+), 520 deletions(-) create mode 100644 contracts/RlnBase.sol create mode 100644 test/RLNApp.t.sol diff --git a/contracts/PoseidonHasher.sol b/contracts/PoseidonHasher.sol index 50348c1..9b95466 100644 --- a/contracts/PoseidonHasher.sol +++ b/contracts/PoseidonHasher.sol @@ -11,276 +11,141 @@ interface IPoseidonHasher { } contract PoseidonHasher is IPoseidonHasher { - uint256 constant Q = - 21888242871839275222246405745257275088548364400416034343698204186575808495617; - uint256 constant C0 = - 4417881134626180770308697923359573201005643519861877412381846989312604493735; - uint256 constant C1 = - 5433650512959517612316327474713065966758808864213826738576266661723522780033; - uint256 constant C2 = - 13641176377184356099764086973022553863760045607496549923679278773208775739952; - uint256 constant C3 = - 17949713444224994136330421782109149544629237834775211751417461773584374506783; - uint256 constant C4 = - 13765628375339178273710281891027109699578766420463125835325926111705201856003; - uint256 constant C5 = - 19179513468172002314585757290678967643352171735526887944518845346318719730387; - uint256 constant C6 = - 5157412437176756884543472904098424903141745259452875378101256928559722612176; - uint256 constant C7 = - 535160875740282236955320458485730000677124519901643397458212725410971557409; - uint256 constant C8 = - 1050793453380762984940163090920066886770841063557081906093018330633089036729; - uint256 constant C9 = - 10665495010329663932664894101216428400933984666065399374198502106997623173873; - uint256 constant C10 = - 19965634623406616956648724894636666805991993496469370618546874926025059150737; - uint256 constant C11 = - 13007250030070838431593222885902415182312449212965120303174723305710127422213; - uint256 constant C12 = - 16877538715074991604507979123743768693428157847423939051086744213162455276374; - uint256 constant C13 = - 18211747749504876135588847560312685184956239426147543810126553367063157141465; - uint256 constant C14 = - 18151553319826126919739798892854572062191241985315767086020821632812331245635; - uint256 constant C15 = - 19957033149976712666746140949846950406660099037474791840946955175819555930825; - uint256 constant C16 = - 3469514863538261843186854830917934449567467100548474599735384052339577040841; - uint256 constant C17 = - 989698510043911779243192466312362856042600749099921773896924315611668507708; - uint256 constant C18 = - 12568377015646290945235387813564567111330046038050864455358059568128000172201; - uint256 constant C19 = - 20856104135605479600325529349246932565148587186338606236677138505306779314172; - uint256 constant C20 = - 8206918720503535523121349917159924938835810381723474192155637697065780938424; - uint256 constant C21 = - 1309058477013932989380617265069188723120054926187607548493110334522527703566; - uint256 constant C22 = - 14076116939332667074621703729512195584105250395163383769419390236426287710606; - uint256 constant C23 = - 10153498892749751942204288991871286290442690932856658983589258153608012428674; - uint256 constant C24 = - 18202499207234128286137597834010475797175973146805180988367589376893530181575; - uint256 constant C25 = - 12739388830157083522877690211447248168864006284243907142044329113461613743052; - uint256 constant C26 = - 15123358710467780770838026754240340042441262572309759635224051333176022613949; - uint256 constant C27 = - 19925004701844594370904593774447343836015483888496504201331110250494635362184; - uint256 constant C28 = - 10352416606816998476681131583320899030072315953910679608943150613208329645891; - uint256 constant C29 = - 10567371822366244361703342347428230537114808440249611395507235283708966113221; - uint256 constant C30 = - 5635498582763880627392290206431559361272660937399944184533035305989295959602; - uint256 constant C31 = - 11866432933224219174041051738704352719163271639958083608224676028593315904909; - uint256 constant C32 = - 5795020705294401441272215064554385591292330721703923167136157291459784140431; - uint256 constant C33 = - 9482202378699252817564375087302794636287866584767523335624368774856230692758; - uint256 constant C34 = - 4245237636894546151746468406560945873445548423466753843402086544922216329298; - uint256 constant C35 = - 12000500941313982757584712677991730019124834399479314697467598397927435905133; - uint256 constant C36 = - 7596790274058425558167520209857956363736666939016807569082239187494363541787; - uint256 constant C37 = - 2484867918246116343205467273440098378820186751202461278013576281097918148877; - uint256 constant C38 = - 18312645949449997391810445935615409295369169383463185688973803378104013950190; - uint256 constant C39 = - 15320686572748723004980855263301182130424010735782762814513954166519592552733; - uint256 constant C40 = - 12618438900597948888520621062416758747872180395546164387827245287017031303859; - uint256 constant C41 = - 17438141672027706116733201008397064011774368832458707512367404736905021019585; - uint256 constant C42 = - 6374197807230665998865688675365359100400438034755781666913068586172586548950; - uint256 constant C43 = - 2189398913433273865510950346186699930188746169476472274335177556702504595264; - uint256 constant C44 = - 6268495580028970231803791523870131137294646402347399003576649137450213034606; - uint256 constant C45 = - 17896250365994900261202920044129628104272791547990619503076839618914047059275; - uint256 constant C46 = - 13692156312448722528008862371944543449350293305158722920787736248435893008873; - uint256 constant C47 = - 15234446864368744483209945022439268713300180233589581910497691316744177619376; - uint256 constant C48 = - 1572426502623310766593681563281600503979671244997798691029595521622402217227; - uint256 constant C49 = - 80103447810215150918585162168214870083573048458555897999822831203653996617; - uint256 constant C50 = - 8228820324013669567851850635126713973797711779951230446503353812192849106342; - uint256 constant C51 = - 5375851433746509614045812476958526065449377558695752132494533666370449415873; - uint256 constant C52 = - 12115998939203497346386774317892338270561208357481805380546938146796257365018; - uint256 constant C53 = - 9764067909645821279940531410531154041386008396840887338272986634350423466622; - uint256 constant C54 = - 8538708244538850542384936174629541085495830544298260335345008245230827876882; - uint256 constant C55 = - 7140127896620013355910287215441004676619168261422440177712039790284719613114; - uint256 constant C56 = - 14297402962228458726038826185823085337698917275385741292940049024977027409762; - uint256 constant C57 = - 6667115556431351074165934212337261254608231545257434281887966406956835140819; - uint256 constant C58 = - 20226761165244293291042617464655196752671169026542832236139342122602741090001; - uint256 constant C59 = - 12038289506489256655759141386763477208196694421666339040483042079632134429119; - uint256 constant C60 = - 19027757334170818571203982241812412991528769934917288000224335655934473717551; - uint256 constant C61 = - 16272152964456553579565580463468069884359929612321610357528838696790370074720; - uint256 constant C62 = - 2500392889689246014710135696485946334448570271481948765283016105301740284071; - uint256 constant C63 = - 8595254970528530312401637448610398388203855633951264114100575485022581946023; - uint256 constant C64 = - 11635945688914011450976408058407206367914559009113158286982919675551688078198; - 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) { + uint256 constant Q = 21888242871839275222246405745257275088548364400416034343698204186575808495617; + uint256 constant C0 = 4417881134626180770308697923359573201005643519861877412381846989312604493735; + uint256 constant C1 = 5433650512959517612316327474713065966758808864213826738576266661723522780033; + uint256 constant C2 = 13641176377184356099764086973022553863760045607496549923679278773208775739952; + uint256 constant C3 = 17949713444224994136330421782109149544629237834775211751417461773584374506783; + uint256 constant C4 = 13765628375339178273710281891027109699578766420463125835325926111705201856003; + uint256 constant C5 = 19179513468172002314585757290678967643352171735526887944518845346318719730387; + uint256 constant C6 = 5157412437176756884543472904098424903141745259452875378101256928559722612176; + uint256 constant C7 = 535160875740282236955320458485730000677124519901643397458212725410971557409; + uint256 constant C8 = 1050793453380762984940163090920066886770841063557081906093018330633089036729; + uint256 constant C9 = 10665495010329663932664894101216428400933984666065399374198502106997623173873; + uint256 constant C10 = 19965634623406616956648724894636666805991993496469370618546874926025059150737; + uint256 constant C11 = 13007250030070838431593222885902415182312449212965120303174723305710127422213; + uint256 constant C12 = 16877538715074991604507979123743768693428157847423939051086744213162455276374; + uint256 constant C13 = 18211747749504876135588847560312685184956239426147543810126553367063157141465; + uint256 constant C14 = 18151553319826126919739798892854572062191241985315767086020821632812331245635; + uint256 constant C15 = 19957033149976712666746140949846950406660099037474791840946955175819555930825; + uint256 constant C16 = 3469514863538261843186854830917934449567467100548474599735384052339577040841; + uint256 constant C17 = 989698510043911779243192466312362856042600749099921773896924315611668507708; + uint256 constant C18 = 12568377015646290945235387813564567111330046038050864455358059568128000172201; + uint256 constant C19 = 20856104135605479600325529349246932565148587186338606236677138505306779314172; + uint256 constant C20 = 8206918720503535523121349917159924938835810381723474192155637697065780938424; + uint256 constant C21 = 1309058477013932989380617265069188723120054926187607548493110334522527703566; + uint256 constant C22 = 14076116939332667074621703729512195584105250395163383769419390236426287710606; + uint256 constant C23 = 10153498892749751942204288991871286290442690932856658983589258153608012428674; + uint256 constant C24 = 18202499207234128286137597834010475797175973146805180988367589376893530181575; + uint256 constant C25 = 12739388830157083522877690211447248168864006284243907142044329113461613743052; + uint256 constant C26 = 15123358710467780770838026754240340042441262572309759635224051333176022613949; + uint256 constant C27 = 19925004701844594370904593774447343836015483888496504201331110250494635362184; + uint256 constant C28 = 10352416606816998476681131583320899030072315953910679608943150613208329645891; + uint256 constant C29 = 10567371822366244361703342347428230537114808440249611395507235283708966113221; + uint256 constant C30 = 5635498582763880627392290206431559361272660937399944184533035305989295959602; + uint256 constant C31 = 11866432933224219174041051738704352719163271639958083608224676028593315904909; + uint256 constant C32 = 5795020705294401441272215064554385591292330721703923167136157291459784140431; + uint256 constant C33 = 9482202378699252817564375087302794636287866584767523335624368774856230692758; + uint256 constant C34 = 4245237636894546151746468406560945873445548423466753843402086544922216329298; + uint256 constant C35 = 12000500941313982757584712677991730019124834399479314697467598397927435905133; + uint256 constant C36 = 7596790274058425558167520209857956363736666939016807569082239187494363541787; + uint256 constant C37 = 2484867918246116343205467273440098378820186751202461278013576281097918148877; + uint256 constant C38 = 18312645949449997391810445935615409295369169383463185688973803378104013950190; + uint256 constant C39 = 15320686572748723004980855263301182130424010735782762814513954166519592552733; + uint256 constant C40 = 12618438900597948888520621062416758747872180395546164387827245287017031303859; + uint256 constant C41 = 17438141672027706116733201008397064011774368832458707512367404736905021019585; + uint256 constant C42 = 6374197807230665998865688675365359100400438034755781666913068586172586548950; + uint256 constant C43 = 2189398913433273865510950346186699930188746169476472274335177556702504595264; + uint256 constant C44 = 6268495580028970231803791523870131137294646402347399003576649137450213034606; + uint256 constant C45 = 17896250365994900261202920044129628104272791547990619503076839618914047059275; + uint256 constant C46 = 13692156312448722528008862371944543449350293305158722920787736248435893008873; + uint256 constant C47 = 15234446864368744483209945022439268713300180233589581910497691316744177619376; + uint256 constant C48 = 1572426502623310766593681563281600503979671244997798691029595521622402217227; + uint256 constant C49 = 80103447810215150918585162168214870083573048458555897999822831203653996617; + uint256 constant C50 = 8228820324013669567851850635126713973797711779951230446503353812192849106342; + uint256 constant C51 = 5375851433746509614045812476958526065449377558695752132494533666370449415873; + uint256 constant C52 = 12115998939203497346386774317892338270561208357481805380546938146796257365018; + uint256 constant C53 = 9764067909645821279940531410531154041386008396840887338272986634350423466622; + uint256 constant C54 = 8538708244538850542384936174629541085495830544298260335345008245230827876882; + uint256 constant C55 = 7140127896620013355910287215441004676619168261422440177712039790284719613114; + uint256 constant C56 = 14297402962228458726038826185823085337698917275385741292940049024977027409762; + uint256 constant C57 = 6667115556431351074165934212337261254608231545257434281887966406956835140819; + uint256 constant C58 = 20226761165244293291042617464655196752671169026542832236139342122602741090001; + uint256 constant C59 = 12038289506489256655759141386763477208196694421666339040483042079632134429119; + uint256 constant C60 = 19027757334170818571203982241812412991528769934917288000224335655934473717551; + uint256 constant C61 = 16272152964456553579565580463468069884359929612321610357528838696790370074720; + uint256 constant C62 = 2500392889689246014710135696485946334448570271481948765283016105301740284071; + uint256 constant C63 = 8595254970528530312401637448610398388203855633951264114100575485022581946023; + uint256 constant C64 = 11635945688914011450976408058407206367914559009113158286982919675551688078198; + 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) { return _hash(input); } diff --git a/contracts/Rln.sol b/contracts/Rln.sol index 7683b93..1ad5053 100644 --- a/contracts/Rln.sol +++ b/contracts/Rln.sol @@ -1,180 +1,18 @@ // SPDX-License-Identifier: MIT - pragma solidity 0.8.15; -import {IPoseidonHasher} from "./PoseidonHasher.sol"; -import {IVerifier} from "./IVerifier.sol"; - -/// The tree is full -error FullTree(); - -/// Invalid deposit amount -/// @param required The required deposit amount -/// @param provided The provided deposit amount -error InsufficientDeposit(uint256 required, uint256 provided); - -/// Member is already registered -error DuplicateIdCommitment(); - -/// Invalid receiver address, when the receiver is the contract itself or 0x0 -error InvalidReceiverAddress(address to); - -/// Member is not registered -error MemberNotRegistered(uint256 idCommitment); - -/// Member has no stake -error MemberHasNoStake(uint256 idCommitment); - -/// User has insufficient balance to withdraw -error InsufficientWithdrawalBalance(); - -/// Contract has insufficient balance to return -error InsufficientContractBalance(); - -/// Invalid proof -error InvalidProof(); - -contract RLN { - /// @notice The deposit amount required to register as a member - uint256 public immutable MEMBERSHIP_DEPOSIT; - - /// @notice The depth of the merkle tree - uint256 public immutable DEPTH; - - /// @notice The size of the merkle tree, i.e 2^depth - uint256 public immutable SET_SIZE; - - /// @notice The index of the next member to be registered - uint256 public idCommitmentIndex = 1; - - /// @notice The amount of eth staked by each member - /// maps from idCommitment to the amount staked - mapping(uint256 => uint256) public stakedAmounts; - - /// @notice The membership status of each member - /// maps from idCommitment to their index in the set - mapping(uint256 => uint256) public members; - - /// @notice The balance of each user that can be withdrawn - mapping(address => uint256) public withdrawalBalance; - - /// @notice The Poseidon hasher contract - IPoseidonHasher public immutable poseidonHasher; - - /// @notice The groth16 verifier contract - IVerifier public immutable verifier; - - /// 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 - event MemberRegistered(uint256 idCommitment, uint256 index); - - /// Emitted when a member is removed from the set - /// @param idCommitment The idCommitment of the member - /// @param index The index of the member in the set - event MemberWithdrawn(uint256 idCommitment, uint256 index); - - constructor(uint256 membershipDeposit, uint256 depth, address _poseidonHasher, address _verifier) { - MEMBERSHIP_DEPOSIT = membershipDeposit; - DEPTH = depth; - SET_SIZE = 1 << depth; - poseidonHasher = IPoseidonHasher(_poseidonHasher); - verifier = IVerifier(_verifier); - } - - /// Allows a user to register as a member - /// @param idCommitment The idCommitment of the member - function register(uint256 idCommitment) external payable { - if (msg.value != MEMBERSHIP_DEPOSIT) { - revert InsufficientDeposit(MEMBERSHIP_DEPOSIT, msg.value); - } - _register(idCommitment, msg.value); - } - - /// Registers a member - /// @param idCommitment The idCommitment of the member - /// @param stake The amount of eth staked by the member - function _register(uint256 idCommitment, uint256 stake) internal { - if (members[idCommitment] != 0) revert DuplicateIdCommitment(); - if (idCommitmentIndex >= SET_SIZE) revert FullTree(); - - members[idCommitment] = idCommitmentIndex; - stakedAmounts[idCommitment] = stake; - - emit MemberRegistered(idCommitment, idCommitmentIndex); - idCommitmentIndex += 1; - } - - /// @dev Allows a user to slash a member - /// @param idCommitment The idCommitment of the member - function slash(uint256 idCommitment, address payable receiver, uint256[8] calldata proof) external { - _slash(idCommitment, receiver, proof); - } - - /// @dev Slashes a member by removing them from the set, and adding their - /// stake to the receiver's available withdrawal balance - /// @param idCommitment The idCommitment of the member - /// @param receiver The address to receive the funds - function _slash(uint256 idCommitment, address payable receiver, uint256[8] calldata proof) internal { - if (receiver == address(this) || receiver == address(0)) { - revert InvalidReceiverAddress(receiver); - } - - if (members[idCommitment] == 0) revert MemberNotRegistered(idCommitment); - // check if member is registered - if (stakedAmounts[idCommitment] == 0) { - revert MemberHasNoStake(idCommitment); - } - - if (!_verifyProof(idCommitment, receiver, proof)) { - revert InvalidProof(); - } - - uint256 amountToTransfer = stakedAmounts[idCommitment]; - - // delete member - uint256 index = members[idCommitment]; - members[idCommitment] = 0; - stakedAmounts[idCommitment] = 0; - - // refund deposit - withdrawalBalance[receiver] += amountToTransfer; - - emit MemberWithdrawn(idCommitment, index); - } - - /// Allows a user to withdraw funds allocated to them upon slashing a member - function withdraw() external { - uint256 amount = withdrawalBalance[msg.sender]; - - if (amount == 0) revert InsufficientWithdrawalBalance(); - if (amount > address(this).balance) { - revert InsufficientContractBalance(); - } - - withdrawalBalance[msg.sender] = 0; +import "./RlnBase.sol"; - payable(msg.sender).transfer(amount); - } +contract Rln is RlnBase { + constructor(uint256 membershipDeposit, uint256 depth, address _poseidonHasher, address _verifier) + RlnBase(membershipDeposit, depth, _poseidonHasher, _verifier) + {} - /// 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 _validateRegistration(uint256 idCommitment) internal pure override {} - /// @dev Groth16 proof verification - function _verifyProof(uint256 idCommitment, address receiver, uint256[8] calldata proof) + function _validateSlash(uint256 idCommitment, address payable receiver, uint256[8] calldata proof) internal - view - returns (bool) - { - return verifier.verifyProof( - [proof[0], proof[1]], - [[proof[2], proof[3]], [proof[4], proof[5]]], - [proof[6], proof[7]], - [idCommitment, uint256(uint160(receiver))] - ); - } + pure + override + {} } diff --git a/contracts/RlnBase.sol b/contracts/RlnBase.sol new file mode 100644 index 0000000..bf1db5a --- /dev/null +++ b/contracts/RlnBase.sol @@ -0,0 +1,193 @@ +// SPDX-License-Identifier: MIT + +pragma solidity 0.8.15; + +import {IPoseidonHasher} from "./PoseidonHasher.sol"; +import {IVerifier} from "./IVerifier.sol"; + +/// The tree is full +error FullTree(); + +/// Invalid deposit amount +/// @param required The required deposit amount +/// @param provided The provided deposit amount +error InsufficientDeposit(uint256 required, uint256 provided); + +/// Member is already registered +error DuplicateIdCommitment(); + +/// Failed validation on registration/slashing +error FailedValidation(); + +/// Invalid receiver address, when the receiver is the contract itself or 0x0 +error InvalidReceiverAddress(address to); + +/// Member is not registered +error MemberNotRegistered(uint256 idCommitment); + +/// Member has no stake +error MemberHasNoStake(uint256 idCommitment); + +/// User has insufficient balance to withdraw +error InsufficientWithdrawalBalance(); + +/// Contract has insufficient balance to return +error InsufficientContractBalance(); + +/// Invalid proof +error InvalidProof(); + +abstract contract RlnBase { + /// @notice The deposit amount required to register as a member + uint256 public immutable MEMBERSHIP_DEPOSIT; + + /// @notice The depth of the merkle tree + uint256 public immutable DEPTH; + + /// @notice The size of the merkle tree, i.e 2^depth + uint256 public immutable SET_SIZE; + + /// @notice The index of the next member to be registered + uint256 public idCommitmentIndex = 1; + + /// @notice The amount of eth staked by each member + /// maps from idCommitment to the amount staked + mapping(uint256 => uint256) public stakedAmounts; + + /// @notice The membership status of each member + /// maps from idCommitment to their index in the set + mapping(uint256 => uint256) public members; + + /// @notice The balance of each user that can be withdrawn + mapping(address => uint256) public withdrawalBalance; + + /// @notice The Poseidon hasher contract + IPoseidonHasher public immutable poseidonHasher; + + /// @notice The groth16 verifier contract + IVerifier public immutable verifier; + + /// 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 + event MemberRegistered(uint256 idCommitment, uint256 index); + + /// Emitted when a member is removed from the set + /// @param idCommitment The idCommitment of the member + /// @param index The index of the member in the set + event MemberWithdrawn(uint256 idCommitment, uint256 index); + + constructor(uint256 membershipDeposit, uint256 depth, address _poseidonHasher, address _verifier) { + MEMBERSHIP_DEPOSIT = membershipDeposit; + DEPTH = depth; + SET_SIZE = 1 << depth; + poseidonHasher = IPoseidonHasher(_poseidonHasher); + verifier = IVerifier(_verifier); + } + + /// Allows a user to register as a member + /// @param idCommitment The idCommitment of the member + function register(uint256 idCommitment) external payable { + if (msg.value != MEMBERSHIP_DEPOSIT) { + revert InsufficientDeposit(MEMBERSHIP_DEPOSIT, msg.value); + } + _validateRegistration(idCommitment); + _register(idCommitment, msg.value); + } + + /// Registers a member + /// @param idCommitment The idCommitment of the member + /// @param stake The amount of eth staked by the member + function _register(uint256 idCommitment, uint256 stake) internal { + if (members[idCommitment] != 0) revert DuplicateIdCommitment(); + if (idCommitmentIndex >= SET_SIZE) revert FullTree(); + + members[idCommitment] = idCommitmentIndex; + stakedAmounts[idCommitment] = stake; + + emit MemberRegistered(idCommitment, idCommitmentIndex); + idCommitmentIndex += 1; + } + + /// @dev Inheriting contracts MUST override this function + function _validateRegistration(uint256 idCommitment) internal view virtual; + + /// @dev Allows a user to slash a member + /// @param idCommitment The idCommitment of the member + function slash(uint256 idCommitment, address payable receiver, uint256[8] calldata proof) external { + _validateSlash(idCommitment, receiver, proof); + _slash(idCommitment, receiver, proof); + } + + /// @dev Slashes a member by removing them from the set, and adding their + /// stake to the receiver's available withdrawal balance + /// @param idCommitment The idCommitment of the member + /// @param receiver The address to receive the funds + function _slash(uint256 idCommitment, address payable receiver, uint256[8] calldata proof) internal { + if (receiver == address(this) || receiver == address(0)) { + revert InvalidReceiverAddress(receiver); + } + + if (members[idCommitment] == 0) revert MemberNotRegistered(idCommitment); + // check if member is registered + if (stakedAmounts[idCommitment] == 0) { + revert MemberHasNoStake(idCommitment); + } + + if (!_verifyProof(idCommitment, receiver, proof)) { + revert InvalidProof(); + } + + uint256 amountToTransfer = stakedAmounts[idCommitment]; + + // delete member + uint256 index = members[idCommitment]; + members[idCommitment] = 0; + stakedAmounts[idCommitment] = 0; + + // refund deposit + withdrawalBalance[receiver] += amountToTransfer; + + emit MemberWithdrawn(idCommitment, index); + } + + function _validateSlash(uint256 idCommitment, address payable receiver, uint256[8] calldata proof) + internal + view + virtual; + + /// Allows a user to withdraw funds allocated to them upon slashing a member + function withdraw() external { + uint256 amount = withdrawalBalance[msg.sender]; + + if (amount == 0) revert InsufficientWithdrawalBalance(); + if (amount > address(this).balance) { + revert InsufficientContractBalance(); + } + + withdrawalBalance[msg.sender] = 0; + + 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); + } + + /// @dev Groth16 proof verification + function _verifyProof(uint256 idCommitment, address receiver, uint256[8] calldata proof) + internal + view + returns (bool) + { + return verifier.verifyProof( + [proof[0], proof[1]], + [[proof[2], proof[3]], [proof[4], proof[5]]], + [proof[6], proof[7]], + [idCommitment, uint256(uint160(receiver))] + ); + } +} diff --git a/contracts/RlnVerifier.sol b/contracts/RlnVerifier.sol index 22d7d0f..bbd0494 100644 --- a/contracts/RlnVerifier.sol +++ b/contracts/RlnVerifier.sol @@ -300,4 +300,4 @@ contract Verifier { return false; } } -} \ No newline at end of file +} diff --git a/deploy/003_deploy_rln.ts b/deploy/003_deploy_rln.ts index 3a800c4..cd6ad04 100644 --- a/deploy/003_deploy_rln.ts +++ b/deploy/003_deploy_rln.ts @@ -11,12 +11,12 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { .address; const rlnVerifierAddress = (await deployments.get("Verifier")).address; - await deploy("RLN", { + await deploy("Rln", { from: deployer, log: true, args: [1000000000000000, 20, poseidonHasherAddress, rlnVerifierAddress], }); }; export default func; -func.tags = ["RLN"]; +func.tags = ["Rln"]; func.dependencies = ["PoseidonHasher", "RlnVerifier"]; diff --git a/docs/index.md b/docs/index.md index 6767c96..26b9c26 100644 --- a/docs/index.md +++ b/docs/index.md @@ -844,6 +844,28 @@ Hashes the input using the Poseidon hash function, n = 2, second input is the co function _hash(uint256 input) internal pure returns (uint256 result) ``` +## Rln + +### constructor + +```solidity +constructor(uint256 membershipDeposit, uint256 depth, address _poseidonHasher, address _verifier) public +``` + +### \_validateRegistration + +```solidity +function _validateRegistration(uint256 idCommitment) internal pure +``` + +_Inheriting contracts MUST override this function_ + +### \_validateSlash + +```solidity +function _validateSlash(uint256 idCommitment, address payable receiver, uint256[8] proof) internal pure +``` + ## FullTree ```solidity @@ -875,6 +897,14 @@ error DuplicateIdCommitment() Member is already registered +## FailedValidation + +```solidity +error FailedValidation() +``` + +Failed validation on registration/slashing + ## InvalidReceiverAddress ```solidity @@ -923,7 +953,7 @@ error InvalidProof() Invalid proof -## RLN +## RlnBase ### MEMBERSHIP_DEPOSIT @@ -1032,7 +1062,7 @@ Emitted when a member is removed from the set ### constructor ```solidity -constructor(uint256 membershipDeposit, uint256 depth, address _poseidonHasher, address _verifier) public +constructor(uint256 membershipDeposit, uint256 depth, address _poseidonHasher, address _verifier) internal ``` ### register @@ -1064,6 +1094,14 @@ Registers a member | idCommitment | uint256 | The idCommitment of the member | | stake | uint256 | The amount of eth staked by the member | +### \_validateRegistration + +```solidity +function _validateRegistration(uint256 idCommitment) internal view virtual +``` + +_Inheriting contracts MUST override this function_ + ### slash ```solidity @@ -1097,6 +1135,12 @@ stake to the receiver's available withdrawal balance_ | receiver | address payable | The address to receive the funds | | proof | uint256[8] | | +### \_validateSlash + +```solidity +function _validateSlash(uint256 idCommitment, address payable receiver, uint256[8] proof) internal view virtual +``` + ### withdraw ```solidity diff --git a/test/PoseidonHasher.t.sol b/test/PoseidonHasher.t.sol index 7a24fc4..a30e422 100644 --- a/test/PoseidonHasher.t.sol +++ b/test/PoseidonHasher.t.sol @@ -19,9 +19,7 @@ contract PoseidonHasherTest is Test { function testHasher() public { assertEq( - poseidon.hash( - 19014214495641488759237505126948346942972912379615652741039992445865937985820 - ), + poseidon.hash(19014214495641488759237505126948346942972912379615652741039992445865937985820), 0x0c3ac305f6a4fe9bfeb3eba978bc876e2a99208b8b56c80160cfb54ba8f02368 ); } diff --git a/test/RLN.t.sol b/test/RLN.t.sol index 5f6e90f..b76078f 100644 --- a/test/RLN.t.sol +++ b/test/RLN.t.sol @@ -8,11 +8,10 @@ import "forge-std/Test.sol"; import "forge-std/StdCheats.sol"; import "forge-std/console.sol"; - -contract RLNTest is Test { +contract RlnTest is Test { using stdStorage for StdStorage; - RLN public rln; + Rln public rln; PoseidonHasher public poseidon; TrueVerifier public trueVerifier; FalseVerifier public falseVerifier; @@ -22,13 +21,12 @@ contract RLNTest is Test { uint256 public constant SET_SIZE = 1048576; uint256[8] public zeroedProof = [0, 0, 0, 0, 0, 0, 0, 0]; - /// @dev Setup the testing environment. function setUp() public { poseidon = new PoseidonHasher(); trueVerifier = new TrueVerifier(); falseVerifier = new FalseVerifier(); - rln = new RLN(MEMBERSHIP_DEPOSIT, DEPTH, address(poseidon), address(trueVerifier)); + rln = new Rln(MEMBERSHIP_DEPOSIT, DEPTH, address(poseidon), address(trueVerifier)); } /// @dev Ensure that you can hash a value. @@ -44,9 +42,7 @@ contract RLNTest is Test { assertEq(rln.members(idCommitment), 1); } - function test__InvalidRegistration__DuplicateCommitment( - uint256 idCommitment - ) public { + function test__InvalidRegistration__DuplicateCommitment(uint256 idCommitment) public { rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment); assertEq(rln.stakedAmounts(idCommitment), MEMBERSHIP_DEPOSIT); assertEq(rln.members(idCommitment), 1); @@ -54,25 +50,15 @@ contract RLNTest is Test { rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment); } - function test__InvalidRegistration__InsufficientDeposit( - uint256 idCommitment - ) public { + function test__InvalidRegistration__InsufficientDeposit(uint256 idCommitment) public { uint256 badDepositAmount = MEMBERSHIP_DEPOSIT - 1; - vm.expectRevert( - abi.encodeWithSelector( - InsufficientDeposit.selector, - MEMBERSHIP_DEPOSIT, - badDepositAmount - ) - ); + vm.expectRevert(abi.encodeWithSelector(InsufficientDeposit.selector, MEMBERSHIP_DEPOSIT, badDepositAmount)); rln.register{value: badDepositAmount}(idCommitment); } - function test__InvalidRegistration__FullSet( - uint256 idCommitmentSeed - ) public { + function test__InvalidRegistration__FullSet(uint256 idCommitmentSeed) public { vm.assume(idCommitmentSeed < 2 ** 255 - SET_SIZE); - RLN tempRln = new RLN( + Rln tempRln = new Rln( MEMBERSHIP_DEPOSIT, 2, address(rln.poseidonHasher()), @@ -111,9 +97,7 @@ contract RLNTest is Test { rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment); assertEq(rln.stakedAmounts(idCommitment), MEMBERSHIP_DEPOSIT); - vm.expectRevert( - abi.encodeWithSelector(InvalidReceiverAddress.selector, address(0)) - ); + vm.expectRevert(abi.encodeWithSelector(InvalidReceiverAddress.selector, address(0))); rln.slash(idCommitment, payable(address(0)), zeroedProof); } @@ -121,29 +105,17 @@ contract RLNTest is Test { uint256 idCommitment = 19014214495641488759237505126948346942972912379615652741039992445865937985820; rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment); assertEq(rln.stakedAmounts(idCommitment), MEMBERSHIP_DEPOSIT); - vm.expectRevert( - abi.encodeWithSelector( - InvalidReceiverAddress.selector, - address(rln) - ) - ); + vm.expectRevert(abi.encodeWithSelector(InvalidReceiverAddress.selector, address(rln))); rln.slash(idCommitment, payable(address(rln)), zeroedProof); } - function test__InvalidSlash__InvalidIdCommitment( - uint256 idCommitment - ) public { - vm.expectRevert( - abi.encodeWithSelector(MemberNotRegistered.selector, idCommitment) - ); + function test__InvalidSlash__InvalidIdCommitment(uint256 idCommitment) public { + vm.expectRevert(abi.encodeWithSelector(MemberNotRegistered.selector, idCommitment)); rln.slash(idCommitment, payable(address(this)), zeroedProof); } // this shouldn't be possible, but just in case - function test__InvalidSlash__NoStake( - uint256 idCommitment, - address payable to - ) public { + function test__InvalidSlash__NoStake(uint256 idCommitment, address payable to) public { // avoid precompiles, etc assumePayable(to); assumeNoPrecompiles(to); @@ -157,23 +129,16 @@ contract RLNTest is Test { assertEq(rln.members(idCommitment), 0); // manually set members[idCommitment] to true using vm - stdstore - .target(address(rln)) - .sig("members(uint256)") - .with_key(idCommitment) - .depth(0) - .checked_write(true); - - vm.expectRevert( - abi.encodeWithSelector(MemberHasNoStake.selector, idCommitment) - ); + stdstore.target(address(rln)).sig("members(uint256)").with_key(idCommitment).depth(0).checked_write(true); + + vm.expectRevert(abi.encodeWithSelector(MemberHasNoStake.selector, idCommitment)); rln.slash(idCommitment, to, zeroedProof); } function test__InvalidSlash__InvalidProof() public { uint256 idCommitment = 19014214495641488759237505126948346942972912379615652741039992445865937985820; - RLN tempRln = new RLN( + Rln tempRln = new Rln( MEMBERSHIP_DEPOSIT, 2, address(rln.poseidonHasher()), diff --git a/test/RLNApp.t.sol b/test/RLNApp.t.sol new file mode 100644 index 0000000..2e145d2 --- /dev/null +++ b/test/RLNApp.t.sol @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: Unlicense +pragma solidity ^0.8.15; + +import "../contracts/PoseidonHasher.sol"; +import "../contracts/RlnBase.sol"; +import "./Verifier.sol"; +import "forge-std/Test.sol"; +import "forge-std/StdCheats.sol"; +import "forge-std/console.sol"; + +contract RlnApp is RlnBase { + uint256 public constant allowedIdCommitment = + 21888242871839275222246405745257275088548364400416034343698204186575808495617; + + constructor(uint256 membershipDeposit, uint256 depth, address _poseidonHasher, address _verifier) + RlnBase(membershipDeposit, depth, _poseidonHasher, _verifier) + {} + + function _validateRegistration(uint256 idCommitment) internal pure override { + if (idCommitment != allowedIdCommitment) revert FailedValidation(); + } + + function _validateSlash(uint256 idCommitment, address payable receiver, uint256[8] calldata proof) + internal + pure + override + { + if (idCommitment == allowedIdCommitment) revert FailedValidation(); + } +} + +contract RLNAppTest is Test { + RlnApp public rlnApp; + PoseidonHasher public poseidon; + TrueVerifier public trueVerifier; + + uint256 public constant MEMBERSHIP_DEPOSIT = 1000000000000000; + uint256 public constant DEPTH = 20; + uint256 public constant SET_SIZE = 1048576; + uint256[8] public zeroedProof = [0, 0, 0, 0, 0, 0, 0, 0]; + + function setUp() public { + poseidon = new PoseidonHasher(); + trueVerifier = new TrueVerifier(); + rlnApp = new RlnApp(MEMBERSHIP_DEPOSIT, DEPTH, address(poseidon), address(trueVerifier)); + } + + function test__Constants() public { + // sanity checking + assertEq(rlnApp.MEMBERSHIP_DEPOSIT(), MEMBERSHIP_DEPOSIT); + assertEq(rlnApp.DEPTH(), DEPTH); + assertEq(rlnApp.SET_SIZE(), SET_SIZE); + } + + function test__InvalidRegistration(uint256 idCommitment) public { + vm.assume(idCommitment != rlnApp.allowedIdCommitment()); + vm.expectRevert(FailedValidation.selector); + rlnApp.register{value: MEMBERSHIP_DEPOSIT}(idCommitment); + } + + function test__ValidRegistration() public { + rlnApp.register{value: MEMBERSHIP_DEPOSIT}(rlnApp.allowedIdCommitment()); + } + + function test__InvalidSlash() public { + uint256 allowedIdCommitment = rlnApp.allowedIdCommitment(); + rlnApp.register{value: MEMBERSHIP_DEPOSIT}(allowedIdCommitment); + vm.expectRevert(FailedValidation.selector); + rlnApp.slash(allowedIdCommitment, payable(address(this)), zeroedProof); + } +} diff --git a/test/Verifier.sol b/test/Verifier.sol index 049993f..d122489 100644 --- a/test/Verifier.sol +++ b/test/Verifier.sol @@ -4,23 +4,21 @@ pragma solidity ^0.8.15; import {IVerifier} from "../contracts/IVerifier.sol"; contract TrueVerifier is IVerifier { - function verifyProof( - uint256[2] memory a, - uint256[2][2] memory b, - uint256[2] memory c, - uint256[2] memory input - ) external view returns (bool) { + function verifyProof(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[2] memory input) + external + pure + returns (bool) + { return true; } } contract FalseVerifier is IVerifier { - function verifyProof( - uint256[2] memory a, - uint256[2][2] memory b, - uint256[2] memory c, - uint256[2] memory input - ) external view returns (bool) { + function verifyProof(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[2] memory input) + external + pure + returns (bool) + { return false; } -} \ No newline at end of file +} diff --git a/test/rln.test.ts b/test/rln.test.ts index 19ea3a2..73f3498 100644 --- a/test/rln.test.ts +++ b/test/rln.test.ts @@ -1,13 +1,13 @@ import { expect } from "chai"; import { ethers, deployments } from "hardhat"; -describe("RLN", () => { +describe("Rln", () => { beforeEach(async () => { - await deployments.fixture(["RLN"]); + await deployments.fixture(["Rln"]); }); it("should register new memberships", async () => { - const rln = await ethers.getContract("RLN", ethers.provider.getSigner(0)); + const rln = await ethers.getContract("Rln", ethers.provider.getSigner(0)); const price = await rln.MEMBERSHIP_DEPOSIT(); @@ -30,7 +30,7 @@ describe("RLN", () => { }); it("should slash membership", async () => { - const rln = await ethers.getContract("RLN", ethers.provider.getSigner(0)); + const rln = await ethers.getContract("Rln", ethers.provider.getSigner(0)); const price = await rln.MEMBERSHIP_DEPOSIT(); @@ -55,7 +55,7 @@ describe("RLN", () => { }); it("should not allow multiple registrations with same pubkey", async () => { - const rln = await ethers.getContract("RLN", ethers.provider.getSigner(0)); + const rln = await ethers.getContract("Rln", ethers.provider.getSigner(0)); const price = await rln.MEMBERSHIP_DEPOSIT(); From 5d3b5dddcc23d64d4de3ca85ac77837c6ea6a684 Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Thu, 27 Jul 2023 09:50:59 +0530 Subject: [PATCH 2/3] fix: rename file --- test/{RLN.t.sol => Rln.sol} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/{RLN.t.sol => Rln.sol} (100%) diff --git a/test/RLN.t.sol b/test/Rln.sol similarity index 100% rename from test/RLN.t.sol rename to test/Rln.sol From 5cbeb45a8a9f33cc3d7c9de5dfe76ad9e02b6079 Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Thu, 27 Jul 2023 10:38:21 +0530 Subject: [PATCH 3/3] fix: test file name --- slither.config.json | 3 +++ test/{Rln.sol => Rln.t.sol} | 0 2 files changed, 3 insertions(+) create mode 100644 slither.config.json rename test/{Rln.sol => Rln.t.sol} (100%) diff --git a/slither.config.json b/slither.config.json new file mode 100644 index 0000000..0ba8e16 --- /dev/null +++ b/slither.config.json @@ -0,0 +1,3 @@ +{ + "filter_paths": "contracts" +} diff --git a/test/Rln.sol b/test/Rln.t.sol similarity index 100% rename from test/Rln.sol rename to test/Rln.t.sol