diff --git a/.gitmodules b/.gitmodules index b7cb5924..e47105ca 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,7 +1,3 @@ -[submodule "rln/vendor/rln"] - path = rln/vendor/rln - ignore = dirty - url = https://github.com/Rate-Limiting-Nullifier/rln_circuits.git [submodule "semaphore/vendor/semaphore"] path = semaphore/vendor/semaphore ignore = dirty diff --git a/rln/README.md b/rln/README.md index 3aab6ec5..fe6ae2b2 100644 --- a/rln/README.md +++ b/rln/README.md @@ -56,7 +56,7 @@ However, if `N` is too big, this might require a bigger Powers of Tau ceremony t In such case we refer to the official [Circom documentation](https://docs.circom.io/getting-started/proving-circuits/#powers-of-tau) for instructions on how to run an appropriate Powers of Tau ceremony and Phase 2 in order to compile the desired circuit. -Currently, the `rln` module comes with three [pre-compiled](https://github.com/vacp2p/zerokit/tree/master/rln/resources) RLN circuits having Merkle tree of height `15`, `19` and `20`, respectively. +Currently, the `rln` module comes with 2 [pre-compiled](https://github.com/vacp2p/zerokit/tree/master/rln/resources) RLN circuits having Merkle tree of height `20` and `32`, respectively. ## Getting started diff --git a/rln/resources/tree_height_15/rln.wasm b/rln/resources/tree_height_15/rln.wasm deleted file mode 100644 index dbbfb652..00000000 Binary files a/rln/resources/tree_height_15/rln.wasm and /dev/null differ diff --git a/rln/resources/tree_height_15/rln_final.zkey b/rln/resources/tree_height_15/rln_final.zkey deleted file mode 100644 index 89a7c381..00000000 Binary files a/rln/resources/tree_height_15/rln_final.zkey and /dev/null differ diff --git a/rln/resources/tree_height_15/verification_key.json b/rln/resources/tree_height_15/verification_key.json deleted file mode 100644 index e4bb03c8..00000000 --- a/rln/resources/tree_height_15/verification_key.json +++ /dev/null @@ -1,119 +0,0 @@ -{ - "protocol": "groth16", - "curve": "bn128", - "nPublic": 6, - "vk_alpha_1": [ - "20124996762962216725442980738609010303800849578410091356605067053491763969391", - "9118593021526896828671519912099489027245924097793322973632351264852174143923", - "1" - ], - "vk_beta_2": [ - [ - "4693952934005375501364248788849686435240706020501681709396105298107971354382", - "14346958885444710485362620645446987998958218205939139994511461437152241966681" - ], - [ - "16851772916911573982706166384196538392731905827088356034885868448550849804972", - "823612331030938060799959717749043047845343400798220427319188951998582076532" - ], - [ - "1", - "0" - ] - ], - "vk_gamma_2": [ - [ - "10857046999023057135944570762232829481370756359578518086990519993285655852781", - "11559732032986387107991004021392285783925812861821192530917403151452391805634" - ], - [ - "8495653923123431417604973247489272438418190587263600148770280649306958101930", - "4082367875863433681332203403145435568316851327593401208105741076214120093531" - ], - [ - "1", - "0" - ] - ], - "vk_delta_2": [ - [ - "1361919643088555407518565462732544232965454074504004321739078395285189557133", - "20823246840633598579879223919854294301857184404415306521912631074982696570306" - ], - [ - "7088590198103342249937795923142619828109070290720888704402714617857746884833", - "8191367139632195506244169264298620546181137131063303219908889318280111188437" - ], - [ - "1", - "0" - ] - ], - "vk_alphabeta_12": [ - [ - [ - "12608968655665301215455851857466367636344427685631271961542642719683786103711", - "9849575605876329747382930567422916152871921500826003490242628251047652318086" - ], - [ - "6322029441245076030714726551623552073612922718416871603535535085523083939021", - "8700115492541474338049149013125102281865518624059015445617546140629435818912" - ], - [ - "10674973475340072635573101639867487770811074181475255667220644196793546640210", - "2926286967251299230490668407790788696102889214647256022788211245826267484824" - ] - ], - [ - [ - "9660441540778523475944706619139394922744328902833875392144658911530830074820", - "19548113127774514328631808547691096362144426239827206966690021428110281506546" - ], - [ - "1870837942477655969123169532603615788122896469891695773961478956740992497097", - "12536105729661705698805725105036536744930776470051238187456307227425796690780" - ], - [ - "21811903352654147452884857281720047789720483752548991551595462057142824037334", - "19021616763967199151052893283384285352200445499680068407023236283004353578353" - ] - ] - ], - "IC": [ - [ - "17643142412395322664866141827318671249236739056291610144830020671604112279111", - "13273439661778801509295280274403992505521239023074387826870538372514206268318", - "1" - ], - [ - "12325966053136615826793633393742326952102053533176311103856731330114882211366", - "6439956820140153832120005353467272867287237423425778281905068783317736451260", - "1" - ], - [ - "20405310272367450124741832665322768131899487413829191383721623069139009993137", - "21336772016824870564600007750206596010566056069977718959140462128560786193566", - "1" - ], - [ - "4007669092231576644992949839487535590075070172447826102934640178940614212519", - "7597503385395289202372182678960254605827199004598882158153019657732525465207", - "1" - ], - [ - "4545695279389338758267531646940033299700127241196839077811942492841603458462", - "6635771967009274882904456432128877995932122611166121203658485990305433499873", - "1" - ], - [ - "7876954805169515500747828488548350352651069599547377092970620945851311591012", - "7571431725691513008054581132582771105743462534789373657638701712901679323321", - "1" - ], - [ - "5563973122249220346301217166900152021860462617567141574881706390202619333219", - "5147729144109676590873823097632042430451708874867871369293332620382492068692", - "1" - ] - ] -} \ No newline at end of file diff --git a/rln/resources/tree_height_19/rln.wasm b/rln/resources/tree_height_19/rln.wasm deleted file mode 100644 index 0d3da9a7..00000000 Binary files a/rln/resources/tree_height_19/rln.wasm and /dev/null differ diff --git a/rln/resources/tree_height_19/rln_final.zkey b/rln/resources/tree_height_19/rln_final.zkey deleted file mode 100644 index de38bad1..00000000 Binary files a/rln/resources/tree_height_19/rln_final.zkey and /dev/null differ diff --git a/rln/resources/tree_height_19/verification_key.json b/rln/resources/tree_height_19/verification_key.json deleted file mode 100644 index 48455a56..00000000 --- a/rln/resources/tree_height_19/verification_key.json +++ /dev/null @@ -1,119 +0,0 @@ -{ - "protocol": "groth16", - "curve": "bn128", - "nPublic": 6, - "vk_alpha_1": [ - "20124996762962216725442980738609010303800849578410091356605067053491763969391", - "9118593021526896828671519912099489027245924097793322973632351264852174143923", - "1" - ], - "vk_beta_2": [ - [ - "4693952934005375501364248788849686435240706020501681709396105298107971354382", - "14346958885444710485362620645446987998958218205939139994511461437152241966681" - ], - [ - "16851772916911573982706166384196538392731905827088356034885868448550849804972", - "823612331030938060799959717749043047845343400798220427319188951998582076532" - ], - [ - "1", - "0" - ] - ], - "vk_gamma_2": [ - [ - "10857046999023057135944570762232829481370756359578518086990519993285655852781", - "11559732032986387107991004021392285783925812861821192530917403151452391805634" - ], - [ - "8495653923123431417604973247489272438418190587263600148770280649306958101930", - "4082367875863433681332203403145435568316851327593401208105741076214120093531" - ], - [ - "1", - "0" - ] - ], - "vk_delta_2": [ - [ - "16125279975606773676640811113051624654121459921695914044301154938920321009721", - "14844345250267029614093295465313288254479124604567709177260777529651293576873" - ], - [ - "20349277326920398483890518242229158117668855310237215044647746783223259766294", - "19338776107510040969200058390413661029003750817172740054990168933780935479540" - ], - [ - "1", - "0" - ] - ], - "vk_alphabeta_12": [ - [ - [ - "12608968655665301215455851857466367636344427685631271961542642719683786103711", - "9849575605876329747382930567422916152871921500826003490242628251047652318086" - ], - [ - "6322029441245076030714726551623552073612922718416871603535535085523083939021", - "8700115492541474338049149013125102281865518624059015445617546140629435818912" - ], - [ - "10674973475340072635573101639867487770811074181475255667220644196793546640210", - "2926286967251299230490668407790788696102889214647256022788211245826267484824" - ] - ], - [ - [ - "9660441540778523475944706619139394922744328902833875392144658911530830074820", - "19548113127774514328631808547691096362144426239827206966690021428110281506546" - ], - [ - "1870837942477655969123169532603615788122896469891695773961478956740992497097", - "12536105729661705698805725105036536744930776470051238187456307227425796690780" - ], - [ - "21811903352654147452884857281720047789720483752548991551595462057142824037334", - "19021616763967199151052893283384285352200445499680068407023236283004353578353" - ] - ] - ], - "IC": [ - [ - "5645604624116784480262312750033349186912223090668673154853165165224747369512", - "5656337658385597582701340925622307146226708710361427687425735166776477641124", - "1" - ], - [ - "8216930132302312821663833393171053651364962198587857550991047765311607638330", - "19934865864074163318938688021560358348660709566570123384268356491416384822148", - "1" - ], - [ - "11046959016591768534564223076484566731774575511709349452804727872479525392631", - "9401797690410912638766111919371607085248054251975419812613989999345815833269", - "1" - ], - [ - "13216594148914395028254776738842380005944817065680915990743659996725367876414", - "11541283802841111343960351782994043892623551381569479006737253908665900144087", - "1" - ], - [ - "6957074593219251760608960101283708711892008557897337713430173510328411964571", - "21673833055087220750009279957462375662312260098732685145862504142183400549467", - "1" - ], - [ - "20795071270535109448604057031148356571036039566776607847840379441839742201050", - "21654952744643117202636583766828639581880877547772465264383291983528268115687", - "1" - ], - [ - "19143058772755719660075704757531991493801758701561469885274062297246796623789", - "3996020163280925980543600106196205910576345230982361007978823537163123181007", - "1" - ] - ] -} \ No newline at end of file diff --git a/rln/resources/tree_height_20/rln.wasm b/rln/resources/tree_height_20/rln.wasm index 04aaeef7..0d190033 100644 Binary files a/rln/resources/tree_height_20/rln.wasm and b/rln/resources/tree_height_20/rln.wasm differ diff --git a/rln/resources/tree_height_20/rln_final.zkey b/rln/resources/tree_height_20/rln_final.zkey index c6cc7d49..6ac73984 100644 Binary files a/rln/resources/tree_height_20/rln_final.zkey and b/rln/resources/tree_height_20/rln_final.zkey differ diff --git a/rln/resources/tree_height_20/verification_key.json b/rln/resources/tree_height_20/verification_key.json index 5974ffb0..2b06f6ef 100644 --- a/rln/resources/tree_height_20/verification_key.json +++ b/rln/resources/tree_height_20/verification_key.json @@ -1,20 +1,20 @@ { "protocol": "groth16", "curve": "bn128", - "nPublic": 6, + "nPublic": 5, "vk_alpha_1": [ - "20124996762962216725442980738609010303800849578410091356605067053491763969391", - "9118593021526896828671519912099489027245924097793322973632351264852174143923", + "20491192805390485299153009773594534940189261866228447918068658471970481763042", + "9383485363053290200918347156157836566562967994039712273449902621266178545958", "1" ], "vk_beta_2": [ [ - "4693952934005375501364248788849686435240706020501681709396105298107971354382", - "14346958885444710485362620645446987998958218205939139994511461437152241966681" + "6375614351688725206403948262868962793625744043794305715222011528459656738731", + "4252822878758300859123897981450591353533073413197771768651442665752259397132" ], [ - "16851772916911573982706166384196538392731905827088356034885868448550849804972", - "823612331030938060799959717749043047845343400798220427319188951998582076532" + "10505242626370262277552901082094356697409835680220590971873171140371331206856", + "21847035105528745403288232691147584728191162732299865338377159692350059136679" ], [ "1", @@ -37,12 +37,12 @@ ], "vk_delta_2": [ [ - "8353516066399360694538747105302262515182301251524941126222712285088022964076", - "9329524012539638256356482961742014315122377605267454801030953882967973561832" + "19381091659114342708346442379630615922843118562844893610615650294875683946211", + "15552996376058085216886912770055643184213525326040483512352171392245952819143" ], [ - "16805391589556134376869247619848130874761233086443465978238468412168162326401", - "10111259694977636294287802909665108497237922060047080343914303287629927847739" + "12588624700930877548123639492983751769950518160033583190559237442988989476511", + "21566485901320212986683930067069402517509385157875328621749793094655522223502" ], [ "1", @@ -52,67 +52,62 @@ "vk_alphabeta_12": [ [ [ - "12608968655665301215455851857466367636344427685631271961542642719683786103711", - "9849575605876329747382930567422916152871921500826003490242628251047652318086" + "2029413683389138792403550203267699914886160938906632433982220835551125967885", + "21072700047562757817161031222997517981543347628379360635925549008442030252106" ], [ - "6322029441245076030714726551623552073612922718416871603535535085523083939021", - "8700115492541474338049149013125102281865518624059015445617546140629435818912" + "5940354580057074848093997050200682056184807770593307860589430076672439820312", + "12156638873931618554171829126792193045421052652279363021382169897324752428276" ], [ - "10674973475340072635573101639867487770811074181475255667220644196793546640210", - "2926286967251299230490668407790788696102889214647256022788211245826267484824" + "7898200236362823042373859371574133993780991612861777490112507062703164551277", + "7074218545237549455313236346927434013100842096812539264420499035217050630853" ] ], [ [ - "9660441540778523475944706619139394922744328902833875392144658911530830074820", - "19548113127774514328631808547691096362144426239827206966690021428110281506546" + "7077479683546002997211712695946002074877511277312570035766170199895071832130", + "10093483419865920389913245021038182291233451549023025229112148274109565435465" ], [ - "1870837942477655969123169532603615788122896469891695773961478956740992497097", - "12536105729661705698805725105036536744930776470051238187456307227425796690780" + "4595479056700221319381530156280926371456704509942304414423590385166031118820", + "19831328484489333784475432780421641293929726139240675179672856274388269393268" ], [ - "21811903352654147452884857281720047789720483752548991551595462057142824037334", - "19021616763967199151052893283384285352200445499680068407023236283004353578353" + "11934129596455521040620786944827826205713621633706285934057045369193958244500", + "8037395052364110730298837004334506829870972346962140206007064471173334027475" ] ] ], "IC": [ [ - "11992897507809711711025355300535923222599547639134311050809253678876341466909", - "17181525095924075896332561978747020491074338784673526378866503154966799128110", + "4920513730204767532050733107749276406754520419375654722016092399980613788208", + "10950491564509418434657706642388934308456795265036074733953533582377584967294", "1" ], [ - "17018665030246167677911144513385572506766200776123272044534328594850561667818", - "18601114175490465275436712413925513066546725461375425769709566180981674884464", + "6815064660695497986531118446154820702646540722664044216580897159556261271171", + "17838140936832571103329556013529166877877534025488014783346458943575275015438", "1" ], [ - "18799470100699658367834559797874857804183288553462108031963980039244731716542", - "13064227487174191981628537974951887429496059857753101852163607049188825592007", + "16364982450206976302246609763791333525052810246590359380676749324389440643932", + "17092624338100676284548565502349491320314889021833923882585524649862570629227", "1" ], [ - "17432501889058124609368103715904104425610382063762621017593209214189134571156", - "13406815149699834788256141097399354592751313348962590382887503595131085938635", + "3679639231485547795420532910726924727560917141402837495597760107842698404034", + "16213191511474848247596810551723578773353083440353745908057321946068926848382", "1" ], [ - "10320964835612716439094703312987075811498239445882526576970512041988148264481", - "9024164961646353611176283204118089412001502110138072989569118393359029324867", + "9215428431027260354679105025212521481930206886203677270216204485256690813172", + "934602510541226149881779979217731465262250233587980565969044391353665291792", "1" ], [ - "718355081067365548229685160476620267257521491773976402837645005858953849298", - "14635482993933988261008156660773180150752190597753512086153001683711587601974", - "1" - ], - [ - "11777720285956632126519898515392071627539405001940313098390150593689568177535", - "8483603647274280691250972408211651407952870456587066148445913156086740744515", + "8935861545794299876685457331391349387048184820319250771243971382360441890897", + "4993459033694759724715904486381952906869986989682015547152342336961693234616", "1" ] ] diff --git a/rln/resources/tree_height_32/rln.wasm b/rln/resources/tree_height_32/rln.wasm new file mode 100644 index 00000000..94901cb2 Binary files /dev/null and b/rln/resources/tree_height_32/rln.wasm differ diff --git a/rln/resources/tree_height_32/rln_final.zkey b/rln/resources/tree_height_32/rln_final.zkey new file mode 100644 index 00000000..44e08f92 Binary files /dev/null and b/rln/resources/tree_height_32/rln_final.zkey differ diff --git a/rln/resources/tree_height_32/verification_key.json b/rln/resources/tree_height_32/verification_key.json new file mode 100644 index 00000000..67877066 --- /dev/null +++ b/rln/resources/tree_height_32/verification_key.json @@ -0,0 +1,114 @@ +{ + "protocol": "groth16", + "curve": "bn128", + "nPublic": 5, + "vk_alpha_1": [ + "20491192805390485299153009773594534940189261866228447918068658471970481763042", + "9383485363053290200918347156157836566562967994039712273449902621266178545958", + "1" + ], + "vk_beta_2": [ + [ + "6375614351688725206403948262868962793625744043794305715222011528459656738731", + "4252822878758300859123897981450591353533073413197771768651442665752259397132" + ], + [ + "10505242626370262277552901082094356697409835680220590971873171140371331206856", + "21847035105528745403288232691147584728191162732299865338377159692350059136679" + ], + [ + "1", + "0" + ] + ], + "vk_gamma_2": [ + [ + "10857046999023057135944570762232829481370756359578518086990519993285655852781", + "11559732032986387107991004021392285783925812861821192530917403151452391805634" + ], + [ + "8495653923123431417604973247489272438418190587263600148770280649306958101930", + "4082367875863433681332203403145435568316851327593401208105741076214120093531" + ], + [ + "1", + "0" + ] + ], + "vk_delta_2": [ + [ + "3689226096868373144622340732612563195789744807442014147637039988348252818659", + "18947459102520510468597269280688700807407684209892273827108603062925288762423" + ], + [ + "5816405977664254142436796931495067997250259145480168934320978750042633353708", + "14555486789839131710516067578112557185806110684461247253491378577062852578892" + ], + [ + "1", + "0" + ] + ], + "vk_alphabeta_12": [ + [ + [ + "2029413683389138792403550203267699914886160938906632433982220835551125967885", + "21072700047562757817161031222997517981543347628379360635925549008442030252106" + ], + [ + "5940354580057074848093997050200682056184807770593307860589430076672439820312", + "12156638873931618554171829126792193045421052652279363021382169897324752428276" + ], + [ + "7898200236362823042373859371574133993780991612861777490112507062703164551277", + "7074218545237549455313236346927434013100842096812539264420499035217050630853" + ] + ], + [ + [ + "7077479683546002997211712695946002074877511277312570035766170199895071832130", + "10093483419865920389913245021038182291233451549023025229112148274109565435465" + ], + [ + "4595479056700221319381530156280926371456704509942304414423590385166031118820", + "19831328484489333784475432780421641293929726139240675179672856274388269393268" + ], + [ + "11934129596455521040620786944827826205713621633706285934057045369193958244500", + "8037395052364110730298837004334506829870972346962140206007064471173334027475" + ] + ] + ], + "IC": [ + [ + "5412646265162057015134786739992128493053406364679846617542694915593022919217", + "9665511386935901867415947590751330959748921059696950821222365265700369811120", + "1" + ], + [ + "4294362651275803035824711662252687124584574009834787359330648404293309808795", + "1861758671717754835450145961645465880215655915164196594175485865489885224285", + "1" + ], + [ + "1911114017568107170522785254288953144010421698038439931935418407428234018676", + "13761363892532562822351086117281964648116890138564516558345965908415019790129", + "1" + ], + [ + "16312980235585837964428386585067529342038135099260965575497230302984635878053", + "20286500347141875536561618770383759234192052027362539966911091298688849002783", + "1" + ], + [ + "21038649368092225315431823433752123495654049075935052064397443455654061176031", + "6976971039866104284556300526186000690370678593992968176463280189048347216392", + "1" + ], + [ + "971745799362951123575710699973701411260115357326598060711339429906895409324", + "12959821343398475313407440786226277845673045139874184400082186049649123071798", + "1" + ] + ] +} \ No newline at end of file diff --git a/rln/src/circuit.rs b/rln/src/circuit.rs index 22e75922..de7b29c0 100644 --- a/rln/src/circuit.rs +++ b/rln/src/circuit.rs @@ -30,12 +30,11 @@ const VK_FILENAME: &str = "verification_key.json"; const WASM_FILENAME: &str = "rln.wasm"; // These parameters are used for tests -// Note that the circuit and keys in TEST_RESOURCES_FOLDER are compiled for Merkle trees of height 15, 19 and 20 +// Note that the circuit and keys in TEST_RESOURCES_FOLDER are compiled for Merkle trees of height 20 & 32 // Changing these parameters to other values than these defaults will cause zkSNARK proof verification to fail -pub const TEST_PARAMETERS_INDEX: usize = 2; -pub const TEST_TREE_HEIGHT: usize = [15, 19, 20][TEST_PARAMETERS_INDEX]; -pub const TEST_RESOURCES_FOLDER: &str = - ["tree_height_15", "tree_height_19", "tree_height_20"][TEST_PARAMETERS_INDEX]; +pub const TEST_PARAMETERS_INDEX: usize = 0; +pub const TEST_TREE_HEIGHT: usize = [20, 32][TEST_PARAMETERS_INDEX]; +pub const TEST_RESOURCES_FOLDER: &str = ["tree_height_20", "tree_height_32"][TEST_PARAMETERS_INDEX]; #[cfg(not(target_arch = "wasm32"))] static RESOURCES_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/resources"); diff --git a/rln/src/protocol.rs b/rln/src/protocol.rs index 06596577..fd269557 100644 --- a/rln/src/protocol.rs +++ b/rln/src/protocol.rs @@ -37,8 +37,7 @@ pub struct RLNWitnessInput { path_elements: Vec, identity_path_index: Vec, x: Fr, - epoch: Fr, - rln_identifier: Fr, + external_nullifier: Fr, } #[derive(Debug, PartialEq)] @@ -49,8 +48,7 @@ pub struct RLNProofValues { pub root: Fr, // Public Inputs: pub x: Fr, - pub epoch: Fr, - pub rln_identifier: Fr, + pub external_nullifier: Fr, } pub fn serialize_field_element(element: Fr) -> Vec { @@ -106,8 +104,7 @@ pub fn serialize_witness(rln_witness: &RLNWitnessInput) -> Result> { serialized.append(&mut vec_fr_to_bytes_le(&rln_witness.path_elements)?); serialized.append(&mut vec_u8_to_bytes_le(&rln_witness.identity_path_index)?); serialized.append(&mut fr_to_bytes_le(&rln_witness.x)); - serialized.append(&mut fr_to_bytes_le(&rln_witness.epoch)); - serialized.append(&mut fr_to_bytes_le(&rln_witness.rln_identifier)); + serialized.append(&mut fr_to_bytes_le(&rln_witness.external_nullifier)); serialized.append(&mut fr_to_bytes_le(&rln_witness.user_message_limit)); serialized.append(&mut fr_to_bytes_le(&rln_witness.message_id)); @@ -134,10 +131,7 @@ pub fn deserialize_witness(serialized: &[u8]) -> Result<(RLNWitnessInput, usize) let (x, read) = bytes_le_to_fr(&serialized[all_read..]); all_read += read; - let (epoch, read) = bytes_le_to_fr(&serialized[all_read..]); - all_read += read; - - let (rln_identifier, read) = bytes_le_to_fr(&serialized[all_read..]); + let (external_nullifier, read) = bytes_le_to_fr(&serialized[all_read..]); all_read += read; let (user_message_limit, read) = bytes_le_to_fr(&serialized[all_read..]); @@ -158,8 +152,7 @@ pub fn deserialize_witness(serialized: &[u8]) -> Result<(RLNWitnessInput, usize) path_elements, identity_path_index, x, - epoch, - rln_identifier, + external_nullifier, user_message_limit, message_id, }, @@ -185,10 +178,7 @@ pub fn proof_inputs_to_rln_witness( ))?; all_read += 8; - let (epoch, read) = bytes_le_to_fr(&serialized[all_read..]); - all_read += read; - - let (rln_identifier, read) = bytes_le_to_fr(&serialized[all_read..]); + let (external_nullifier, read) = bytes_le_to_fr(&serialized[all_read..]); all_read += read; let (user_message_limit, read) = bytes_le_to_fr(&serialized[all_read..]); @@ -218,8 +208,7 @@ pub fn proof_inputs_to_rln_witness( user_message_limit, message_id, x, - epoch, - rln_identifier, + external_nullifier, }, all_read, )) @@ -261,17 +250,14 @@ pub fn rln_witness_from_json(input_json_str: &str) -> Result { let x = str_to_fr(&input_json["x"].to_string(), 10)?; - let epoch = str_to_fr(&input_json["epoch"].to_string(), 16)?; - - let rln_identifier = str_to_fr(&input_json["rln_identifier"].to_string(), 10)?; + let external_nullifier = str_to_fr(&input_json["external_nullifier"].to_string(), 16)?; Ok(RLNWitnessInput { identity_secret, path_elements, identity_path_index, x, - epoch, - rln_identifier, + external_nullifier, user_message_limit, message_id, }) @@ -286,8 +272,7 @@ pub fn rln_witness_from_values( identity_secret: Fr, merkle_proof: &MerkleProof, x: Fr, - epoch: Fr, - rln_identifier: Fr, + external_nullifier: Fr, user_message_limit: Fr, message_id: Fr, ) -> Result { @@ -301,8 +286,7 @@ pub fn rln_witness_from_values( path_elements, identity_path_index, x, - epoch, - rln_identifier, + external_nullifier, user_message_limit, message_id, }) @@ -332,8 +316,7 @@ pub fn random_rln_witness(tree_height: usize) -> RLNWitnessInput { path_elements, identity_path_index, x, - epoch, - rln_identifier, + external_nullifier: poseidon_hash(&[epoch, rln_identifier]), user_message_limit, message_id, } @@ -343,9 +326,8 @@ pub fn proof_values_from_witness(rln_witness: &RLNWitnessInput) -> Result Result Vec { let mut serialized: Vec = Vec::new(); serialized.append(&mut fr_to_bytes_le(&rln_proof_values.root)); - serialized.append(&mut fr_to_bytes_le(&rln_proof_values.epoch)); + serialized.append(&mut fr_to_bytes_le(&rln_proof_values.external_nullifier)); serialized.append(&mut fr_to_bytes_le(&rln_proof_values.x)); serialized.append(&mut fr_to_bytes_le(&rln_proof_values.y)); serialized.append(&mut fr_to_bytes_le(&rln_proof_values.nullifier)); - serialized.append(&mut fr_to_bytes_le(&rln_proof_values.rln_identifier)); serialized } @@ -390,7 +370,7 @@ pub fn deserialize_proof_values(serialized: &[u8]) -> (RLNProofValues, usize) { let (root, read) = bytes_le_to_fr(&serialized[all_read..]); all_read += read; - let (epoch, read) = bytes_le_to_fr(&serialized[all_read..]); + let (external_nullifier, read) = bytes_le_to_fr(&serialized[all_read..]); all_read += read; let (x, read) = bytes_le_to_fr(&serialized[all_read..]); @@ -402,17 +382,13 @@ pub fn deserialize_proof_values(serialized: &[u8]) -> (RLNProofValues, usize) { let (nullifier, read) = bytes_le_to_fr(&serialized[all_read..]); all_read += read; - let (rln_identifier, read) = bytes_le_to_fr(&serialized[all_read..]); - all_read += read; - ( RLNProofValues { y, nullifier, root, x, - epoch, - rln_identifier, + external_nullifier, }, all_read, ) @@ -421,14 +397,14 @@ pub fn deserialize_proof_values(serialized: &[u8]) -> (RLNProofValues, usize) { pub fn prepare_prove_input( identity_secret: Fr, id_index: usize, - epoch: Fr, + external_nullifier: Fr, signal: &[u8], ) -> Vec { let mut serialized: Vec = Vec::new(); serialized.append(&mut fr_to_bytes_le(&identity_secret)); serialized.append(&mut normalize_usize(id_index)); - serialized.append(&mut fr_to_bytes_le(&epoch)); + serialized.append(&mut fr_to_bytes_le(&external_nullifier)); serialized.append(&mut normalize_usize(signal.len())); serialized.append(&mut signal.to_vec()); @@ -663,7 +639,7 @@ pub fn generate_proof_with_witness( /// Returns an error if `rln_witness.message_id` is not within `rln_witness.user_message_limit`. pub fn inputs_for_witness_calculation( rln_witness: &RLNWitnessInput, -) -> Result<[(&str, Vec); 8]> { +) -> Result<[(&str, Vec); 7]> { message_id_range_check(&rln_witness.message_id, &rln_witness.user_message_limit)?; // We confert the path indexes to field elements @@ -688,10 +664,9 @@ pub fn inputs_for_witness_calculation( ("path_elements", path_elements), ("identity_path_index", identity_path_index), ("x", vec![to_bigint(&rln_witness.x)?]), - ("epoch", vec![to_bigint(&rln_witness.epoch)?]), ( - "rln_identifier", - vec![to_bigint(&rln_witness.rln_identifier)?], + "external_nullifier", + vec![to_bigint(&rln_witness.external_nullifier)?], ), ( "user_message_limit", @@ -779,8 +754,7 @@ pub fn verify_proof( proof_values.root, proof_values.nullifier, proof_values.x, - proof_values.epoch, - proof_values.rln_identifier, + proof_values.external_nullifier, ]; // Check that the proof is valid @@ -827,8 +801,7 @@ pub fn get_json_inputs(rln_witness: &RLNWitnessInput) -> Result { pub fn verify(&self, mut input_data: R) -> Result { // Input data is serialized for Curve as: // serialized_proof (compressed, 4*32 bytes) || serialized_proof_values (6*32 bytes), i.e. - // [ proof<128> | root<32> | epoch<32> | share_x<32> | share_y<32> | nullifier<32> | rln_identifier<32> ] + // [ proof<128> | root<32> | external_nullifier<32> | share_x<32> | share_y<32> | nullifier<32> ] let mut input_byte: Vec = Vec::new(); input_data.read_to_end(&mut input_byte)?; let proof = ArkProof::deserialize_compressed(&mut Cursor::new(&input_byte[..128]))?; @@ -729,6 +729,8 @@ impl RLN<'_> { let (rln_witness, _) = proof_inputs_to_rln_witness(&mut self.tree, &witness_byte)?; let proof_values = proof_values_from_witness(&rln_witness)?; + dbg!(&proof_values.root); + let proof = generate_proof(self.witness_calculator, &self.proving_key, &rln_witness)?; // Note: we export a serialization of ark-groth16::Proof not semaphore::Proof @@ -808,13 +810,18 @@ impl RLN<'_> { let signal: Vec = serialized[all_read..all_read + signal_len].to_vec(); let verified = verify_proof(&self.verification_key, &proof, &proof_values)?; + let x = hash_to_field(&signal); + + dbg!( + verified, + self.tree.root() == proof_values.root, + self.tree.root(), + proof_values.root, + x == proof_values.x + ); // Consistency checks to counter proof tampering - let x = hash_to_field(&signal); - Ok(verified - && (self.tree.root() == proof_values.root) - && (x == proof_values.x) - && (proof_values.rln_identifier == hash_to_field(RLN_IDENTIFIER))) + Ok(verified && (self.tree.root() == proof_values.root) && (x == proof_values.x)) } /// Verifies a zkSNARK RLN proof against the provided proof values and a set of allowed Merkle tree roots. @@ -889,9 +896,7 @@ impl RLN<'_> { // First consistency checks to counter proof tampering let x = hash_to_field(&signal); - let partial_result = verified - && (x == proof_values.x) - && (proof_values.rln_identifier == hash_to_field(RLN_IDENTIFIER)); + let partial_result = verified && (x == proof_values.x); // We skip root validation if proof is already invalid if !partial_result { @@ -1120,15 +1125,13 @@ impl RLN<'_> { input_proof_data_1.read_to_end(&mut serialized)?; // We skip deserialization of the zk-proof at the beginning let (proof_values_1, _) = deserialize_proof_values(&serialized[128..]); - let external_nullifier_1 = - utils_poseidon_hash(&[proof_values_1.epoch, proof_values_1.rln_identifier]); + let external_nullifier_1 = proof_values_1.external_nullifier; let mut serialized: Vec = Vec::new(); input_proof_data_2.read_to_end(&mut serialized)?; // We skip deserialization of the zk-proof at the beginning let (proof_values_2, _) = deserialize_proof_values(&serialized[128..]); - let external_nullifier_2 = - utils_poseidon_hash(&[proof_values_2.epoch, proof_values_2.rln_identifier]); + let external_nullifier_2 = proof_values_2.external_nullifier; // We continue only if the proof values are for the same epoch // The idea is that proof values that go as input to this function are verified first (with zk-proof verify), hence ensuring validity of epoch and other fields. @@ -1651,6 +1654,7 @@ mod test { // Before checking public verify API, we check that the (deserialized) proof generated by prove is actually valid let proof = ArkProof::deserialize_compressed(&mut Cursor::new(&serialized_proof)).unwrap(); let verified = verify_proof(&rln.verification_key, &proof, &proof_values); + // dbg!(verified.unwrap()); assert!(verified.unwrap()); // We prepare the input to prove API, consisting of serialized_proof (compressed, 4*32 bytes) || serialized_proof_values (6*32 bytes) @@ -1675,7 +1679,9 @@ mod test { let mut leaves: Vec = Vec::new(); let mut rng = thread_rng(); for _ in 0..no_of_leaves { - leaves.push(Fr::rand(&mut rng)); + let id_commitment = Fr::rand(&mut rng); + let rate_commitment = utils_poseidon_hash(&[id_commitment, Fr::from(100)]); + leaves.push(rate_commitment); } // We create a new RLN instance @@ -1692,7 +1698,7 @@ mod test { // We set as leaf rate_commitment after storing its index let identity_index = rln.tree.leaves_set(); - let user_message_limit = Fr::from(100); + let user_message_limit = Fr::from(65535); let rate_commitment = utils_poseidon_hash(&[id_commitment, user_message_limit]); let mut buffer = Cursor::new(fr_to_bytes_le(&rate_commitment)); rln.set_next_leaf(&mut buffer).unwrap(); @@ -1710,8 +1716,10 @@ mod test { let mut serialized: Vec = Vec::new(); serialized.append(&mut fr_to_bytes_le(&identity_secret_hash)); serialized.append(&mut normalize_usize(identity_index)); - serialized.append(&mut fr_to_bytes_le(&epoch)); - serialized.append(&mut fr_to_bytes_le(&rln_identifier)); + serialized.append(&mut fr_to_bytes_le(&utils_poseidon_hash(&[ + epoch, + rln_identifier, + ]))); serialized.append(&mut fr_to_bytes_le(&user_message_limit)); serialized.append(&mut fr_to_bytes_le(&Fr::from(1))); serialized.append(&mut normalize_usize(signal.len())); @@ -1873,21 +1881,21 @@ mod test { rln.set_next_leaf(&mut buffer).unwrap(); // We generate a random signal - let mut rng = rand::thread_rng(); + let mut rng = thread_rng(); let signal: [u8; 32] = rng.gen(); // We generate a random epoch let epoch = hash_to_field(b"test-epoch"); // We generate a random rln_identifier let rln_identifier = hash_to_field(b"test-rln-identifier"); + let external_nullifier = utils_poseidon_hash(&[epoch, rln_identifier]); // We prepare input for generate_rln_proof API // input_data is [ identity_secret<32> | id_index<8> | epoch<32> | rln_identifier<32> | user_message_limit<32> | message_id<32> | signal_len<8> | signal ] let mut serialized: Vec = Vec::new(); serialized.append(&mut fr_to_bytes_le(&identity_secret_hash)); serialized.append(&mut normalize_usize(identity_index)); - serialized.append(&mut fr_to_bytes_le(&epoch)); - serialized.append(&mut fr_to_bytes_le(&rln_identifier)); + serialized.append(&mut fr_to_bytes_le(&external_nullifier)); serialized.append(&mut fr_to_bytes_le(&user_message_limit)); serialized.append(&mut fr_to_bytes_le(&Fr::from(1))); serialized.append(&mut normalize_usize(signal.len())); @@ -1926,7 +1934,7 @@ mod test { .verify_with_roots(&mut input_buffer.clone(), &mut roots_buffer) .unwrap(); - assert!(verified == false); + assert_eq!(verified, false); // We get the root of the tree obtained adding one leaf per time let mut buffer = Cursor::new(Vec::::new()); diff --git a/rln/tests/protocol.rs b/rln/tests/protocol.rs index 0e08d7e6..177dbe21 100644 --- a/rln/tests/protocol.rs +++ b/rln/tests/protocol.rs @@ -414,13 +414,13 @@ mod test { // We set the remaining values to random ones let epoch = hash_to_field(b"test-epoch"); let rln_identifier = hash_to_field(b"test-rln-identifier"); + let external_nullifier = poseidon_hash(&[epoch, rln_identifier]); let rln_witness: RLNWitnessInput = rln_witness_from_values( identity_secret_hash, &merkle_proof, x, - epoch, - rln_identifier, + external_nullifier, user_message_limit, Fr::from(1), ) diff --git a/rln/tests/public.rs b/rln/tests/public.rs index d80b36e3..6ef1c526 100644 --- a/rln/tests/public.rs +++ b/rln/tests/public.rs @@ -1,5 +1,6 @@ #[cfg(test)] mod test { + use ark_ff::BigInt; use ark_std::{rand::thread_rng, UniformRand}; use rand::Rng; use rln::circuit::{Fr, TEST_RESOURCES_FOLDER, TEST_TREE_HEIGHT}; @@ -24,7 +25,7 @@ mod test { // generate identity let identity_secret_hash = hash_to_field(b"test-merkle-proof"); let id_commitment = utils_poseidon_hash(&vec![identity_secret_hash]); - let rate_commitment = utils_poseidon_hash(&[id_commitment, 1.into()]); + let rate_commitment = utils_poseidon_hash(&[id_commitment, user_message_limit.into()]); // We pass rate_commitment as Read buffer to RLN's set_leaf let mut buffer = Cursor::new(fr_to_bytes_le(&rate_commitment)); @@ -35,25 +36,17 @@ mod test { rln.get_root(&mut buffer).unwrap(); let (root, _) = bytes_le_to_fr(&buffer.into_inner()); - if TEST_TREE_HEIGHT == 15 { - assert_eq!( - root, - str_to_fr( - "0x1984f2e01184aef5cb974640898a5f5c25556554e2b06d99d4841badb8b198cd", - 16 - ) - .unwrap() - ); - } else if TEST_TREE_HEIGHT == 19 { + if TEST_TREE_HEIGHT == 20 { assert_eq!( root, - str_to_fr( - "0x219ceb53f2b1b7a6cf74e80d50d44d68ecb4a53c6cc65b25593c8d56343fb1fe", - 16 - ) - .unwrap() + Fr::from(BigInt([ + 17110646155607829651, + 5040045984242729823, + 6965416728592533086, + 2328960363755461975 + ])) ); - } else if TEST_TREE_HEIGHT == 20 { + } else if TEST_TREE_HEIGHT == 32 { assert_eq!( root, str_to_fr( @@ -155,7 +148,7 @@ mod test { vec![1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // We add the remaining elements for the case TEST_TREE_HEIGHT = 20 - if TEST_TREE_HEIGHT == 19 || TEST_TREE_HEIGHT == 20 { + if TEST_TREE_HEIGHT == 20 || TEST_TREE_HEIGHT == 32 { expected_path_elements.append(&mut vec![ str_to_fr( "0x22f98aa9ce704152ac17354914ad73ed1167ae6596af510aa5b3649325e06c92", diff --git a/rln/vendor/rln b/rln/vendor/rln deleted file mode 160000 index fc86ad15..00000000 --- a/rln/vendor/rln +++ /dev/null @@ -1 +0,0 @@ -Subproject commit fc86ad156ac55b7f805b82ff98501e4eb567bcef