From 732566c1ae53747678801be2bcec678b7152b79a Mon Sep 17 00:00:00 2001 From: Mara Mihali Date: Fri, 15 Jul 2022 08:27:30 +0100 Subject: [PATCH] turn shake into poseidon --- Cargo.toml | 3 + src/commitments.rs | 54 ++++++++--- src/group.rs | 5 +- src/lib.rs | 5 + src/parameters.rs | 228 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 278 insertions(+), 17 deletions(-) create mode 100644 src/parameters.rs diff --git a/Cargo.toml b/Cargo.toml index 5cb1159..1aa150f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,11 +26,14 @@ itertools = "0.10.0" colored = "2.0.0" flate2 = "1.0.14" thiserror = "1.0" +json = "0.12.4" ark-ff = { version = "^0.3.0", default-features = false } ark-ec = { version = "^0.3.0", default-features = false } ark-std = { version = "^0.3.0"} ark-bls12-377 = { version = "^0.3.0", features = ["r1cs","curve"] } ark-serialize = { version = "^0.3.0", features = ["derive"] } +ark-sponge = { version = "^0.3.0" , features = ["r1cs"] } + lazy_static = "1.4.0" rand = { version = "0.8", features = [ "std", "std_rng" ] } num-bigint = { version = "0.4" } diff --git a/src/commitments.rs b/src/commitments.rs index e28f124..35888c4 100644 --- a/src/commitments.rs +++ b/src/commitments.rs @@ -1,13 +1,17 @@ use crate::group::{CompressGroupElement, DecompressGroupElement}; - -use super::group::{GroupElement, VartimeMultiscalarMul, GROUP_BASEPOINT, GroupElementAffine}; +use crate::parameters::*; +use super::group::{GroupElement, VartimeMultiscalarMul, GROUP_BASEPOINT, GroupElementAffine, CurveField}; use super::scalar::Scalar; +use ark_bls12_377::Fq; use ark_ff::PrimeField; +use ark_sponge::CryptographicSponge; use digest::{ExtendableOutput, Input}; use sha3::Shake256; use std::io::Read; +use std::str::FromStr; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; use ark_ec::{ProjectiveCurve, AffineCurve}; +use ark_sponge::poseidon::{PoseidonParameters, PoseidonSponge}; #[derive(Debug)] pub struct MultiCommitGens { @@ -17,25 +21,47 @@ pub struct MultiCommitGens { } impl MultiCommitGens { - pub fn new(n: usize, label: &[u8]) -> Self { - let mut shake = Shake256::default(); - shake.input(label); - let mut generator_encoded = Vec::new(); - GROUP_BASEPOINT.serialize(&mut generator_encoded).unwrap(); - shake.input(generator_encoded); - - let mut reader = shake.xof_result(); + pub fn poseidon_params() -> PoseidonParameters { + let arks = P1["ark"] + .members() + .map(|ark| { + ark.members() + .map(|v| Fq::from_str(v.as_str().unwrap()).unwrap()) + .collect::>() + }) + .collect::>(); + let mds = P1["mds"] + .members() + .map(|m| { + m.members() + .map(|v| Fq::from_str(v.as_str().unwrap()).unwrap()) + .collect::>() + }) + .collect::>(); + PoseidonParameters::new( + P1["full_rounds"].as_u32().unwrap(), + P1["partial_rounds"].as_u32().unwrap(), + P1["alpha"].as_u64().unwrap(), + mds, + arks, + ) + } + pub fn new(n: usize, label: &[u8]) -> Self { + let params = MultiCommitGens::poseidon_params(); + let mut sponge = PoseidonSponge::new(¶ms); + sponge.absorb(&label); + sponge.absorb(&GROUP_BASEPOINT.into_affine()); + let mut gens: Vec = Vec::new(); - let mut uniform_bytes = [0u8; 64]; for _ in 0..n + 1 { let mut el_aff: Option = None; while el_aff.is_some() != true { - reader.read_exact(&mut uniform_bytes).unwrap(); + let uniform_bytes = sponge.squeeze_bytes(64); el_aff = GroupElementAffine::from_random_bytes(&uniform_bytes); } let el = el_aff.unwrap().mul_by_cofactor_to_projective(); - gens.push(el); - } + gens.push(el); + } MultiCommitGens { n, diff --git a/src/group.rs b/src/group.rs index 9bd44f9..f2cd96b 100644 --- a/src/group.rs +++ b/src/group.rs @@ -1,9 +1,7 @@ -use ark_bls12_377::FrParameters; -use ark_ec::group::Group; use ark_ec::{ msm::VariableBaseMSM, }; -use ark_ff::{PrimeField, Fp256, Zero}; +use ark_ff::{PrimeField}; use digest::DynDigest; use lazy_static::lazy_static; use num_bigint::BigInt; @@ -17,6 +15,7 @@ use ark_serialize::*; pub type GroupElement = ark_bls12_377::G1Projective; pub type GroupElementAffine = ark_bls12_377::G1Affine; +pub type CurveField = ark_bls12_377::Fq; #[derive(Clone, Eq, PartialEq, Hash, Debug, CanonicalSerialize, CanonicalDeserialize)] pub struct CompressedGroup(pub Vec); diff --git a/src/lib.rs b/src/lib.rs index 77fcba7..2cad5f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,9 @@ extern crate rand; extern crate lazy_static; extern crate ark_std; +#[macro_use] +extern crate json; + #[cfg(feature = "multicore")] extern crate rayon; @@ -23,6 +26,7 @@ mod errors; mod group; mod math; mod nizk; +mod parameters; mod product_tree; mod r1csinstance; mod r1csproof; @@ -34,6 +38,7 @@ mod timer; mod transcript; mod unipoly; + use core::{cmp::max}; use std::borrow::Borrow; use errors::{ProofVerifyError, R1CSError}; diff --git a/src/parameters.rs b/src/parameters.rs new file mode 100644 index 0000000..480d5be --- /dev/null +++ b/src/parameters.rs @@ -0,0 +1,228 @@ +// Copyright: https://github.com/nikkolasg/ark-dkg/blob/main/src/parameters.rs +use json::JsonValue; +use lazy_static::lazy_static; + + +lazy_static! { + // bls12377_rate2_constraints: + pub static ref P1: JsonValue = object! { + "ark" => array![ + array![ + "123249878756453098914639601843199176451997132612914162343590671120179979107846114348064675842753496966502226470504", + "53905766173893895260794869709237214385817399454448711667759505042599362214601718682151848385057179500606557721647", + "69894258097921391480299485244196654800484322255007476002974737215216019155108287854575586445309048623300976500186" + ], + array![ + "199269668121875174262570566455689951380204776091573924798659006690560053061129973379838694860592058095048653996886", + "238380155638054426865611280966399840311283670977656700124343990049337832223435242290330416091629395326468367200694", + "212599814638151740594239938840408336056840064513659388805072396583467200575230295920880684207605497942975271963482" + ], + array![ + "168914555474650585865140636066457509664822869961119817085015902784107763611497575013588473080216753361935154707010", + "53776337623194839368137436133474167179306472987260969806083684345990583528478024243778418311781192352786333037262", + "248867522100291115924418017563087071912585010573958563496624003376931076896846052799391847772671448846373554213551" + ], + array![ + "35631741058397496924366231156673935881565943972602937094522045989256363839293709188784238224940964750407897277330", + "7156811532468409927576845751990203959972794842929038664826572233020786824205198784067484739611297952558975673525", + "15979461281492123433122857594463244790261784547146673175073000444677214597242748768087325039007316516299176001509" + ], + array![ + "49754305211992756860291736450940496115500536038609822532547985566439150683219315252172063528174877535028115611426", + "216949553183571701463265648286619401188451882876550757881148401346730830975776784112086074385527611896268776861443", + "154083689848809196835533626226861291475925228965341568449375421928198779718328545609801450631059855774468437183675" + ], + array![ + "29502137502944860067241987089780210878930586459241857665873534227953181087265906053919742367508518196418106799806", + "132373035808136518827992049261301947450498154936614023679388954300081661784851944028690271115929087672833323628947", + "215747065685210104280208334912564361804699328020235674942496660758226155688200145092731052953352829033676863042630" + ], + array![ + "199648585544625597282043439398719700409246757664428471828724582419530290323495031580337339234017647369916547108958", + "249575928844995465269738608819476286372884074177639142297081916221358214871660642843838074316560663218386973740173", + "74982114655706235696493453220768307411520767156884132118410225505977592728838652389837915751053304413004683265639" + ], + array![ + "106210893563839260576304917669354671677368166928359922623554581531406088660838991706361575276657684361659801532597", + "11585440423875492387746565618452234080951922019833673083821688269701182965167436520603220148800340540649190539129", + "37259364694251003983990539546703073907090415386678577600390274977885009271501265285951467194762590248232970812844" + ], + array![ + "55837576930986823158863800928077105077853280536700135646766922885911998320579725325719074294029609849816879406734", + "116196118812458208678900768001429737210506949071720002979523997962887466062064707950742955679705357069634209515723", + "24815444638034932833671809997597970940772642987124330190627003560135207315166813788012165972582101193880572012425" + ], + array![ + "8273799170260651595038492091530332589844019793817674372861920239816475852471908767091347071442643736888815451573", + "136990111822759715389631392741048451444971543778803264358207793191138912342988121207664006283186301023235486962908", + "18927153358572748727167231887593945930709178220781358813059367890606662567925981344966823750216495960065937779382" + ], + array![ + "14853717798346258618706074618305350695598054492875071420312670809589654546598863746625188519698040835608660556159", + "176244718044988586163620753193829773891006448729185890339575543133809251309372861124810944047181141986328457412271", + "110233743777966819273995158642051347290508079434162581354613179685804039325709118867348142870653771761630005888307" + ], + array![ + "161450408187838611032909671423510614052593225149162808041124828019352169325631782682210492475825053268732766729188", + "98500573657597535150392453836987141880178711694344573271124963035313026654066107879785978599420939724454330812177", + "215876031358183401857867635719035351422270130594078940310356834104879903855422762837568172975859284057413791888463" + ], + array![ + "204131296462104965227188513952192358580161695271034405933269755582850293970852406144296664401269366372941792250467", + "249055944105228847655227995674839790690527612872758434023675475202902983562708467495202781909125241976893640769485", + "229583286868130259500413761228235662329364304128164289006746728927752301094007770574061957905615623121952293733410" + ], + array![ + "97517137752483519086795583001379387731583152856232248443468839338330057977841917349007821334306740790291136905974", + "123488479251161582154755930609622851433258511862463208593787895860046694339616550157942520077460765622263030118175", + "71432639825611523000280189495110508914555485498103026713477936527348359478511563831157563324853527351478004088468" + ], + array![ + "91036072174315573792700064386146501824720160045153964050728880763049550271037560479809028105202996773568857740730", + "22543564450401763754262340909190687557385187274502421381039682479049063587284520644182139148382788770792136350730", + "142332951471076179551307567596387601171650552060403080229506160329597397458669457278907083453911143048367692807957" + ], + array![ + "132220734042377172239294549962805515211262743615319266088172915692615455860531484953442975677793502323549653807013", + "93545141080589996877640088231346264823743396787843686206971590288437291906435217842171096954488932034021955982341", + "240853888813002049402641151657197764532471620278969626757294146309548064471722973918761650243980940919903584631021" + ], + array![ + "213503951761453329038225269663723790274543267128942326856880800168236861547603473591480303861374397603917184363409", + "89903237953544441905563167047407202265037317870234905464628470820413104873403912116742106741939288646681955585592", + "227121824801807544842683518849178395477499272684097761652696447845872786929195257751449337349649535876783186356932" + ], + array![ + "146971666607035715052553690155718843961663952406456998981945817009558492075030732771578449344145496025583596767529", + "134089029253068479750825302615074040106242441439845487647903191411265000857473209669062720892950980761449114307448", + "240876825504060088346683291079269022914405381209699533928214418428379986520457497863030431018122239809907227823545" + ], + array![ + "198679995161578152944752940670334322637799809857648522826858388680172266023884005933440419287476164086770000386213", + "80453254513068178946616210391952329341738228131537630777936072121633132376974015675425930731821852982135052772824", + "51768068183070369841309308465744117994964313769378589398952388439182600629247824076033474616974680361718264496789" + ], + array![ + "243786304512064454489442716415645262128218178912097043568997297339729319251009514316482045843945939785873311024862", + "132173037488875105639933852791191619959134471035456041997878656537714362390384670197604289467581846432000497395848", + "138604002173172705882182745730007697550901886293221788738303534900559003963900219115006541529324886578352274293799" + ], + array![ + "81783919742603431816536303551235523053319325628680028340677898253811841771498386894771134375242031554657528159968", + "89996400559826291686063370272745776928773053585174906250124744120004601102635457051808673956966087024872962073778", + "12344123991576028812375373502965766640863831483294590816896451707123374600150201588149068234468387476695336142872" + ], + array![ + "126658015711577921340966771989453650175842088716604137911295183366663485409400992312567694787591845600554914654115", + "164573749458837881364642242529191795392373682411872943164652677729048094673511958737424619008331062199862267652935", + "143664707544522749631081019060087611028964440272897357239195964754781588855456478370128855886667526444876450715220" + ], + array![ + "190063502426458192727293662114673159337018305482738016082798402909947909388433256561924969169284825978832455579368", + "200570271046622734241692574928890759512247601848653772722076665026354776331148830989844078413438205377226077381532", + "138002415082423685424410551811447526297743243297262932785520614237184932570821640271043572260989269814779470761461" + ], + array![ + "16788676705864143878780230479765282864054741033672656690224477402805235181341884274547412331727211099012342081859", + "204290600886783875333612666138119904239583082229871768433568000092203989815186589303588884701205693229512519768754", + "87038987841167673770859932175226012933997089943393502222169060963262863845214906568997443646438042896398425595517" + ], + array![ + "36339730403510893807656584803012696279422432037251730189319369181711761371163710757212065138707754369092877655154", + "23719136079159372599286451744989936807954969964666516807332295420486880070514166596679589399139358707568583760908", + "56393335057571631799160728164218189604902690263179612889078150181027528679320914138536210530501845237163318197428" + ], + array![ + "205825956035491267343111682188790766922328411605275469211275484195313659964988531094479492782154028581379936224444", + "14251323509232608512846002255486393977548730149242264667463070512925839406395836441387775340864744223546556498715", + "78428895560820169309169428677090706087502853851935641954584167534512067284012881590143110425966068532035695668777" + ], + array![ + "75494383501361595510879099604200999089073272552094921752996800680267084650818676639185519371499429119407927521694", + "71654751419236499966546173490894599834311797714598165686807217633186393301928260640596079166780877531085221325785", + "200578082042519003217027186194032673613554519507662494009516442239977006673663941756393116663841297396793491871200" + ], + array![ + "120280384146306862951854508424447098979618461682025441151850969362942271625861150381428890843919546149633622105768", + "227475425496153223669855864055613669014065977392917058770175352117179491094064142348157299350182313499504389083442", + "251127263423734302912203519333198755054413799582445749881827904612771493287021107263113755730642765378206506332728" + ], + array![ + "83702595636895308967051271584382753663326775825724154674461807131791275318302215831042606082449545102374950849149", + "72457985217378059985209058682320070298806205003882947360107581077425648268857982638575115120572096951096305132848", + "12116600973201943572988978934130839409963908949941838392365368398743958008280031214900074753572240221871297157796" + ], + array![ + "240872572144156225455781664515486127362275317595363215915293841253269790726868349873274949641462036923410553640448", + "145005621445512968320023394688234446061157047306027479183225589915851108312974841851900985683181027983777819469749", + "223934906758737028193582875327881601162900418521869327818828928797111524239009182764598636421899745113893918838102" + ], + array![ + "16041135858962966773434394701665023495889307936385789447051685789622713476233465453520183391926457145978975456780", + "100995326650741809373350376300291093265611246694300366918949313510272548230989953212376186670081618363334860819266", + "198113061836041953087296741499457296947901762958345262407373960882722071735229745555760175641534017765920249851403" + ], + array![ + "160310964282803191210156178260469498817686363777861893444880694842806130876775742977058740003184226096711472502332", + "188713129639597187156378905515616933770775761286091242780337441914651066308540192205692023798450603034519453279164", + "144177371846162732968346932904974285173557315948314203099016729242538001323624139665700501564547696462348047085475" + ], + array![ + "79270873425284875854185620699109770597921524227459035513636651263949603822776268395349011178814936290604749327216", + "66634508562919326060253106867724866375414704994924403132729353386392729560099833340809999504328104294822126690206", + "153929451747036516277146884279088023240503545576502622475104547924498837499332163003522743849174380874173903478589" + ], + array![ + "65951591639970943843478787167093376292485300299245482252716091066831460583153445126961516774641242644059740963631", + "218283324593072992330537678366612521138133713936527225314279366375484764183384762101590493464257294993736058798003", + "255801326343293104028075157882719596846119525365262151647658801094843254475907908556215545683201236013153654096091" + ], + array![ + "226255389453600272835601278226928175590352392261397636954040403683064727971365284972741836048745971086673805312770", + "30094566584570359029617856208266980210102789615056943080637739339632299082666382408767896640283618386400863011377", + "171014403954507192635907791911496156579477488568451453501143540559952206171633891640382019016227963532953321760176" + ], + array![ + "166057204219683871752892448206953243424627338207417177280506199576386068200127812837156087933305873775343563022702", + "189980739384556361714711372786771245267076300911771323385655044819119270337048535106665515768517077503660696853087", + "160509966668023670725615598656132311085788181242287915812481624013950278259314541983309947248633680202474798784113" + ], + array![ + "121604680206118278311858973633579806987780447456690173958929756615242378735587345162043644789250322132552405934838", + "162490787868836358365957714904092588505217178719637049967797863955517541278871433068812149053958672871873339777657", + "186725839885149672835245872626306502017366920295670132626156737796246154714707858273955752031344539280320214023217" + ], + array![ + "1595442381035683601009655514607864917155264882908420917897267779293136954609652688808389170558528873507396022657", + "136133658372771228168254201060050291177683595113705517331628662542619211285959494716428905546778127973286832435248", + "235707281471584662954139438770000959801075760015072690205031932435280838811659817426504701946918628382850116491607" + ], + array![ + "218394064516331833020386245120198448098388776182164066507039096886784654454748249393443008924076322437744672962940", + "171630003249069743969583651512237853143542592922081517495872510118379411011409238640358871094120884164999614012", + "106352495811714591674517100311841383873861724084673517408579093193910563925812357978278276551276192431523493134802" + ] + ], + "mds" => array![ + array![ + "35463799792750972803746014831251318629433070651916394903137949221437209577677273605833717469941575569104741526451", + "18525374364661750307440824350340771293424609245159218207409253749617918442029080961367157063966182839254983576724", + "96313611821735511449591580163083975587347120205529218061849469348716252837177987500111192232021055962542059542412" + ], + array![ + "184610826894298373826952030256215485452556494530798726246415694794196222735666067140505346074672032818873376193660", + "169170114062164939552104715979827042386033829996509029655899361104098442853225147615546393356393444238242438049980", + "24177241132903335121524689415818818107920151023402250200813429563196326173884815770339346817801446861279643703952" + ], + array![ + "17228430949886884828033371768349883299641066192821547195081333400086665473981454169936377873256566147576607049992", + "35113533023170247280272066588387614578863541036869539331927201531038853371598133096624809442419922813566246641442", + "225762263795139846379155325981635321549752796953252150370574780810431415761301654496442331322761087421338650655933" + ] + ], + "rate" => 2, + "alpha"=> 17, + "full_rounds" => 8, + "partial_rounds" => 31 + }; +}