diff --git a/Cargo.toml b/Cargo.toml index 4ae104f..9fb3f1d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ license = "MIT OR Apache-2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +once_cell = "1.8" ark-ff = "0.3" -ark-sponge = { git = "https://github.com/arkworks-rs/sponge", rev = "51d6fc9aac1fa69f44a04839202b5de828584ed8" } +ark-sponge = { git = "https://github.com/penumbra-zone/sponge", branch = "split-sponge" } ark-ed-on-bls12-377 = "0.3" diff --git a/src/hash.rs b/src/hash.rs new file mode 100644 index 0000000..1585cb2 --- /dev/null +++ b/src/hash.rs @@ -0,0 +1,57 @@ +use crate::{Fq, State}; + +/// Hash a single [`Fq`] element with the provided `domain_separator`. +pub fn hash_1(domain_separator: &Fq, value: Fq) -> Fq { + let mut state = State::from(crate::RATE_1_PARAMS.clone()); + + // Use the domain separator as the sponge's capacity element + state[0] = domain_separator.clone(); + state[1] = value; + + state.permute(); + state[1] +} + +/// Hash two [`Fq`] elements with the provided `domain_separator`. +pub fn hash_2(domain_separator: &Fq, value: (Fq, Fq)) -> Fq { + let mut state = State::from(crate::RATE_2_PARAMS.clone()); + + // Use the domain separator as the sponge's capacity element + state[0] = domain_separator.clone(); + state[1] = value.0; + state[2] = value.1; + + state.permute(); + state[1] +} + +/// Hash four [`Fq`] elements with the provided `domain_separator`. +pub fn hash_4(domain_separator: &Fq, value: (Fq, Fq, Fq, Fq)) -> Fq { + let mut state = State::from(crate::RATE_4_PARAMS.clone()); + + // Use the domain separator as the sponge's capacity element + state[0] = domain_separator.clone(); + state[1] = value.0; + state[2] = value.1; + state[3] = value.2; + state[4] = value.3; + + state.permute(); + state[1] +} + +/// Hash five [`Fq`] elements with the provided `domain_separator`. +pub fn hash_5(domain_separator: &Fq, value: (Fq, Fq, Fq, Fq, Fq)) -> Fq { + let mut state = State::from(crate::RATE_5_PARAMS.clone()); + + // Use the domain separator as the sponge's capacity element + state[0] = domain_separator.clone(); + state[1] = value.0; + state[2] = value.1; + state[3] = value.2; + state[4] = value.3; + state[5] = value.4; + + state.permute(); + state[1] +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index def84ee..c1610d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,45 +1,20 @@ -mod sponge; +//! An instantiation of Poseidon for the BLS12-377 scalar field. -pub mod params; +use once_cell::sync::Lazy; -// Since we depend on a git version of ark-sponge, re-exporting it here means -// our deps can access it without having to keep git revisions in sync. -// -// Going forward, this re-export should be removed and the functionality our -// deps need from direct use of ark-sponge should be folded into this crate. -// However, it's faster to iterate on required functionality without imposing -// hard compartmentalization boundaries from the start. -pub use ark_sponge; +mod hash; +mod params; -#[cfg(test)] -mod tests { - use super::*; +pub use hash::{hash_1, hash_2, hash_4, hash_5}; - #[test] - fn it_works() { - use ark_ed_on_bls12_377::Fq; // lazy import, fix - use ark_ff::{One, Zero}; - use ark_sponge::{ - poseidon::PoseidonSponge, CryptographicSponge, DuplexSpongeMode, - FieldBasedCryptographicSponge, - }; +/// Parameters for the rate-1 instance of Poseidon. +pub const RATE_1_PARAMS: Lazy> = Lazy::new(params::rate_1); +/// Parameters for the rate-2 instance of Poseidon. +pub const RATE_2_PARAMS: Lazy> = Lazy::new(params::rate_2); +/// Parameters for the rate-4 instance of Poseidon. +pub const RATE_4_PARAMS: Lazy> = Lazy::new(params::rate_4); +/// Parameters for the rate-5 instance of Poseidon. +pub const RATE_5_PARAMS: Lazy> = Lazy::new(params::rate_5); - // Current API has a `new()` method as part of the `CryptographicSponge` - // trait, but this method doesn't allow setting the initial state - // manually. Instead, the fields can be set manually. - // Slightly inconvenient that we have to initialize the mode. - let mut sponge = PoseidonSponge { - parameters: params::rate_2(), - state: vec![Fq::zero(); 3], - mode: DuplexSpongeMode::Absorbing { - next_absorb_index: 0, - }, - }; - - sponge.absorb(&Fq::one()); - sponge.absorb(&Fq::one()); - - let output = sponge.squeeze_native_field_elements(1); - dbg!(output); - } -} +pub use ark_ed_on_bls12_377::Fq; +pub use ark_sponge::poseidon::{Parameters, State}; \ No newline at end of file diff --git a/src/params.rs b/src/params.rs index d338826..0a9e60c 100644 --- a/src/params.rs +++ b/src/params.rs @@ -6,13 +6,13 @@ // Generated with `generate_mds.sage`. Do not edit manually. // Regenerate with: `sage vendor/generate_mds.sage > src/params.rs` +use crate::Parameters; use ark_ff::PrimeField; -use ark_sponge::poseidon::PoseidonParameters; /// Parameters for the rate-1 instance of Poseidon. /// /// Note: `F` must be the BLS12-377 scalar field. -pub fn rate_1() -> PoseidonParameters { +pub fn rate_1() -> Parameters { let mds = vec![ vec![ F::from_str( @@ -498,7 +498,7 @@ pub fn rate_1() -> PoseidonParameters { ], ]; - PoseidonParameters { + Parameters { full_rounds: 8, partial_rounds: 30, alpha: 17, @@ -512,7 +512,7 @@ pub fn rate_1() -> PoseidonParameters { /// Parameters for the rate-2 instance of Poseidon. /// /// Note: `F` must be the BLS12-377 scalar field. -pub fn rate_2() -> PoseidonParameters { +pub fn rate_2() -> Parameters { let mds = vec![ vec![ F::from_str( @@ -1232,7 +1232,7 @@ pub fn rate_2() -> PoseidonParameters { ], ]; - PoseidonParameters { + Parameters { full_rounds: 8, partial_rounds: 31, alpha: 17, @@ -1246,7 +1246,7 @@ pub fn rate_2() -> PoseidonParameters { /// Parameters for the rate-4 instance of Poseidon. /// /// Note: `F` must be the BLS12-377 scalar field. -pub fn rate_4() -> PoseidonParameters { +pub fn rate_4() -> Parameters { let mds = vec![ vec![ F::from_str( @@ -2305,7 +2305,7 @@ pub fn rate_4() -> PoseidonParameters { ], ]; - PoseidonParameters { + Parameters { full_rounds: 8, partial_rounds: 26, alpha: 17, @@ -2315,3 +2315,1207 @@ pub fn rate_4() -> PoseidonParameters { capacity: 1, } } + +/// Parameters for the rate-5 instance of Poseidon. +/// +/// Note: `F` must be the BLS12-377 scalar field. +pub fn rate_5() -> Parameters { + let mds = vec![ + vec![ + F::from_str( + "672066471166840632588411510740703526007097043043288776924746811512664490646", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5385927620493705582085278823585867802806998920576396762281958961421319508007", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3166870516933037094709526418876921585128925228864722197703602614347982369201", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5563007254381249144874671496644828623298286219868933533865833143447058544536", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "8250276181781810512497448772499921769001117628760242968677747310415353890064", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4219947387239501847810648140563749594702436340637056068687968666582677185547", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "8308373316915523582209156283088641331778038662853007003254477239916183657361", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "6626681293579679428240785279553788672028348140048656961122361358271476200667", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7542719281183663764513416673919703523522641297579307958935831505232827825555", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "6327791412065253665628625392513309828158657655024031941152528116821268257979", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7769033441512198663746127677920084472643822443411247802125712874964558071629", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "566406639784992242780825313033391068948411899063685777194120784399236200066", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "55794491081052635055923792458143065142699433121911054081592816776269649982", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2025067788465478321435999159099776332761612529526264343030986416200908291564", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3112925236435683242371071258551810715663224704190033055759399561178309878622", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3826712585725033190374017522401217919165733232883853962064117539811460916395", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1174745283730072398173866916818211802957638420795115460173119914193566370641", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7368247161873436113340833597655688624107266779212405285056128277321569066780", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "7825671885206988988950540198300969521586370431289052737602839201624872123928", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2176544332342101821613238372838361814461606188184989528586185293982482025036", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4086587823080459442283808702007339143811405138713778195661968711853549697517", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5631266975502578439352943226310703149319605598890740834809496317979572792100", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5148902127700344455598821624283173461297454145395320877600333748473975928681", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5947190074850597705038319360845895773165916837886370540852130971120981609956", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "4816000238296028977422513168756361121003963950491819109613043223654986070024", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3647416183539377793714397132278752185600890904852662101721411992583446793599", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1096848266792923595807440603677577032897070976109009018113358654973242762895", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3551372186382566745742719714779130380743921638774821506588373387735477913121", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5088193278699768009554220523523116435455373304834032974271154172035100570771", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "8029456723907600753959820034824168033135665691918170617841090524548989665774", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "7981003083847745147558531053749359948483548649454311924591718098323338753666", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "6695375767862876896778091479001979783361164964663735238614002375394326430496", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "8046189751420715145690577052064250944358242635883241130544725394304165446252", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4911753874901799379068183746240894415192736492544975579342251664984005607507", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5984133932876276273633457488809635194916116919604220176186827708493410230038", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4434219415228609726228295043856158344164464387641197015858693192417292612863", + ) + .map_err(|_| ()) + .unwrap(), + ], + ]; + let ark = vec![ + vec![ + F::from_str( + "4142789024066682058007045609495225080310439232397020447581643283020402654846", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "6409539797364261710552581321458029610407131037792921818391550494400966887624", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "434644021451189914185018744948313484129805290895594447443052237281550828302", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3978232313779409461010821071459237774651528475886330761645280386215988622552", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7027675418691353855077049716619550622043312043660992344940177187528247727783", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7189549166770373741329731605871268725917424160918539735183953407609782310671", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "7392912630620987537830295605300793622705671124264041073963605781672270141915", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5949536007459518301090366366909069410803066766901970408406849212090540394636", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7535044167388550589656345641705141193619697763422583102570700142917926460746", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1895787744275032126969510915721097499434363776951852141553831406798712945249", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4780490314493880536687998802667427160888142414946926741509958608649897801961", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "8372126916059111954256092325074913739667089775869134773945147503316973649611", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "6014855836494873623351975581130370543367648859926578635364092627872598058412", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3345904483218983873193806399382582517770045178367157384303058269833083252754", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "902087393777457366603907370284575597015462970269503048717648569756113584039", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7190292550318276214398313552808394584790419061886470473988181973281343780757", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2610765984066308393328114391595595723956099462161539001836037600787237342384", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5917746701105510628296061540376727068393075936267928775806949883353156098050", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "1544120197157255725054202969493775698265372148512362695186457816885198764098", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3154601528146586191529805548895490825439257316824950761721374935254450017105", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5632040662872487587088101375050520442085231006715959318713341318343750456309", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7305041445486140093866580689821947718515178869228669546867030124027259187237", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3985620461395645128883896772044359019642804503332145037015836308089888590232", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5039838231944825776917897441608048242186213927155500306890152810848341974812", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "3601569678245788527706695813801670388961127510634220552844343654739492644695", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "205234288395557346749214023471536298057388508892446606063903159248460092066", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "149101987103211771991327927827692640556911620408176100290586418839323044234", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3435511669910392247548655522227388629020495227124395897496115919202201958423", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3413671667902446892286179771879134444344429300201592934083792160748318246236", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7610952409059668622590203223446160708217928201909920657169231419353643539645", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "3200434446862292889208950343401937241201965981880575096216040177190605558173", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "8165607313238750369025907849048519202312642949338135644119921631537866844417", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5432282207954394876378030337261908971213608112113615258244119439227843131247", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1344007070255979392542466853284928583582044452937702005039847193909501407981", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2354142521332721574625205886950325892800455590935478848218273452814275325225", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1016937616853600282020208165804160497011769118191177437220523539941902393489", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "2002759693851535517568611947407459006773820845919424764626172216377005090547", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2861947517263347664338592979483725573667842634322673654672933613792804505644", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4622082908476410083286670201138165773322781640914243047922441301693321472984", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7848349054307977947753352030830141331999981351248897148565673797917746251243", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2635090520059500019661864086615522409798872905401305311748231832709078452746", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2181843080725597472744243109052445257669852758810629851303719579249798759504", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "4177958784543146626512235378267822677604732785747417608457601968708255499485", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4058848833340764581565262962494083335664915551719584286129679497808366647759", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3520810438340381254404690207705223621082909021213561676514038963438057352982", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3706747844008816288380470801735965548950933295920494172932050062353453026013", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "8139380413376315759255955331115332089257664354349116941280856523514815105641", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1816333954221186176328804836839709738076547257404691805463013441329538632558", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "7340485916200743279276570085958556798507770452421357119145466906520506506342", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2382452598490509367839105573671209204167961481055304382686838286886266207227", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5056115266426728450178391882608195595876171048681268304355720900505776906957", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7221669722700687417346373353960536661883467014204005276831020252277657076044", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "6368161538087610648488044291292734422987845922385448383069800148268651909616", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3936277978660994578484624612338287242284761330659507303379888042887209868219", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "7553885614632219548127688026174585776320152166623257619763178041781456016062", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7008931647336836592130220075320442774797563550276468470666422499764123409185", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5478929644476681096437469958231489102974161353940993351588559414552523375472", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1528166342661706125388227390767317047300390619784032346502269073828784859200", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5243241660201444889739219719496555878502439466360999397640446334244703048852", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "918092522455288617209745099309732321643550165636028720886999342662589364746", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "3018896071704599494587678562316946681958395317088821601441890425694787547773", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "21760554527324687002357019243073620051486602946257736612673464286111304851", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "6617299322445514002670256753901658353257661046330144343159313270124840300407", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "513174846394589492892516892292249974753441896224730309466074742328622991941", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4679524488075358285671313014655771919728938318939962505756741845833733230593", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1367639112737052497986932653133836953962145380255827533091418163093538406970", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "8387168497757638217267952532627697100644685452179458107510209976445609266655", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3392875491762833488653610406905134081957052665937031177845718708958657914804", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7560689444971407628258476313377878697697205734433871706857763279333824762855", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "723689333992989230073849589324114648273889652888418467697254672016492890004", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5891485173249339523379223288448657883700478294815122265518985886600289939700", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2146188678724470135846881017323261926153556509470150842613127280385315423430", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "4874880355887353001232208315814285285840262033661322395129791541388175651234", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "6168350640451697768990993303883633387387006187609416505118064684947204397304", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "6975793621673382674555171252774924917745108889944673072124982713806561725482", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4063907182954603193076687040792392678583619477871363692587216417365070850941", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5788331877803867705100368061803153698509698310757412464009789447320349584469", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7427520624591421856939809538841651140914692972227395081107421685563036894763", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "992168360099222192341033444104480564024800663224835956490318239422508684750", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2224670401791218384750413695729679910214908458419012223524658105202521196140", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5109134780145832004525124871602216050357012608141737081857351468019559651873", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2032937344817689055000131682336003884359337332986030232471997710678756074664", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3313742863447803033098180403532372330859846221889394191617673726237563262269", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "760215077312669098137486704025669959594496562254871635766954058635319433487", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "6935839211798937659784055008131602708847374430164859822530563797964932598700", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1673563329605058724105965600771468505923683434977712314158372952755791665101", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7471469164647220837870506637102701363780473070549326874338918292291624353642", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2851596451267806441339266146934956348004863149718852770512501852518014805930", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3513923924281796709536138766376440741552465863472420701988483747756664163934", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "97920448917513431649024347496907132618492784195433600281157216153760812696", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "1299003891247893574588735065292522607510317390448781519272360611621850856828", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7608831801232295722756959133217262701806829845646062666056135627717658081438", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2796870585100457312812302054449119313570064083051318031219169615811504845127", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4751559102844428649397704183527777787297076012438120696820074559245649646314", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1219439673853113792340300173186247996249367102884530407862469123523013083971", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3157103610619236629936530069231576407066158884828387332210368822678725218535", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "2140214921828858149915908823389079352725991448724491879340045030907325055762", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2606721986957561981325245591819707106541029602271159421934957021343675975784", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2587684196724330274503517333583933341997924958480303083909431044880120606449", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1183153629980760549326804634130338079002602968026018500950671963640303476825", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7411068016485402682322825352282479261119183821409417074641042202652468811638", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1616465736651597758295963585608811656583823854813412382746863998755440991074", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "833806457669532625158446158460326755462748095512482794504262389143621175351", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4074071340035297571271762272204219396807759100006901378273830785064551485962", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1119979170199325804158233209924637442523082412743565514921850168114631753211", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4325186103785259518614024482753721287729374183895900754002106893726233708024", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1129825494990051748021418145704292652613627009469735504942877650143407249589", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7135275277770998377034556802290955576448219083620429238895247915159267211419", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "4770160707215481084533958640215881447408046119948677124503030837587355426221", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7895336590269193028217052369908415014762407680575360282935984241072684273688", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2785695613365936757716092540000556580294975911863773551392460680485060968372", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "6359074816024367983802845581430765434510246278753636966915237021231717896923", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4134751064587747838388129451335439045214823578259625190652109110537710180551", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "960580735377423695872994397616108016237844609777393601920435423302156645662", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "5178886821817300062676266174379357047892714493964310974731352913024065889396", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1371117241418934191509604604522211201045829002846073882061151911980190634267", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3593291146070810978435992295622792674102387712222300421717906998730519807699", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "6103091238062925845254194934709448440624881445150405764974192295112041436759", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "471075258382275780006876644347677189368262277304360385258587080552046377966", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1558415498960552213241704009433360128041672577274390114589014204605400783336", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "2304602155590039329723010942243686591026632300487638755245462215905515855847", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1020241759742620332050884048937759456830262733630148120959672270666331600600", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4893936655321238299429294258147945105077968950607725420680276312435191525339", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "8364853003761848639504863779252861446739205641112231090942371643060883826773", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "8049796986420742846018275751452376434862812053513146546935736431621359919410", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1293239921425673430660897025143433077974838969258268884994339615096356996604", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "3854755057793743998382749043935645076321648392889271506938052408708736403452", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "522585692778965463655432524929951623311685861280538237726404406894423254440", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3908909915571308548230072593865212891852044556503574783379946457719549389081", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4218651554990944198508550088816975893804668931427333018436383659746424321512", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "24238527176102718445169759633308911501405988421827153471612681726930876273", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "6937931128257901400506140502408169950744837013312497307839762317721725352661", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "3837565647574361068761459036361038720977149057378981325662868867989693614839", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4890035469576006472419522095975513744184731204364769848826314423182331574716", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7647786745635294468560355266022761733592192845316853904619298703578397437462", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5410684378713263186548337983252960352059120561867029137014627845977008289113", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2777727321935341047315972812648831787320033689386937014682353054667182019162", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "900794005793984564574791698845516549743155020834465276189453385815858213276", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "3305660327823291872440763548526568332191574834454643066664159188858897943255", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1762188042455633427137702520675816545396284185254002959309669405982213803405", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7008455487053186778788477899337091678606379262916001975397093644885729545324", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1011594370964995437969393361589675964718976689802268764355961751166790125736", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5879739957830112537391830893420993498158638484391807168836352821737900685361", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2691375741527051215818052690680714232537453409195012342668272182132945173837", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "4964559559721773956068130998577625980867667783296469404261818464863202602492", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2534975621437408396429565112315510657143997150868376308990206846277323561715", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "6816277202532813055124543552393083627419502097277435780237889877981862738002", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2711803185465345924244546314360879525791216240668680511351812366362365642369", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1030670738037126147139767714062966187370714922588901320106500075447028338604", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5094446710376072746120344659212330145906896090626968415692729895089049720923", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "7744531341783360393048311812123268955901051222746159386902329507161704096287", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "6561921342145049868115652536972771656181259582433700836599103088462507597141", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5055628736201700741077688901942479532594627301853691447549342289253332532980", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5666661645980438021548358829681591445008304696560402406860771161435068059227", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "140324872005130005455001146200087933938074474697154885911563790851316615376", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "6121425267850669248741608368576428690583874769801761955054289005192519297116", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "1690637367123815043978558852575676249432632791359552345381452831660229747316", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5915379299366067288834890302866466919534786509941648906106763924537591500387", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3861703866861919732679892089942700952407907871642119468871179238045455189374", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "8075656504088434870830816762346555091572347266121881512716480410310948296260", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2881977446236343913167480363329960938771777346999462862415189676375231861966", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "612156207443156255870989677083594147429610697293345241352470306066505067658", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "5635207884973085203038888124358704199467661368991398427140976201056936645884", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "511624658545827424388447411541286616646703289631444526569369312079585974747", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7190205978910642226075842726904637825300912100540909439469845040037934961065", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4014815975714278713114902049870237659687905286057854480182340295176876512471", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2280493057501359625388563568612696012341520519038871745368974238506956668976", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2772020505142629559210278821025578368139699523867366470687228274561253029569", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "1399272768011567095953525354767497338810674418680846972655614876461867129182", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5103426021706267414010407858676780115822097977924243751941274662690191816295", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "2129841222516832513371144132963310454623844719065104993274380460463234054011", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1563470091917367144208878113180627490261613819652134000397838697611977910973", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "336207491463048992764846510718262363118515127283981000704219618435487685769", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3505966629005047690516420445991010772044667323794999555424230634395217556484", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "411529621724849932999694270803131456243889635467661223241617477462914950626", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "7560447707631862801413945329837191463252762217813356130303982541936919956820", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "4252495097502485986848322503353795765461160535327033462243677686082324365685", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5370236148445101660180667913317556730696050924652792191122031829883597185932", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1084753515387229436836047228417674152320581134492329201705551042643308536703", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1406917358861497070325676688263436875322676563706941260751263081895016933284", + ) + .map_err(|_| ()) + .unwrap(), + ], + vec![ + F::from_str( + "4769436455628711697829224789650248958123396442214964807929097390625873206998", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "1480929216941551565860589848980631491889116427970855668295759535298968878592", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5445133692749384174643712144099622844121919739628634159917740404441661433093", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5291189087491746553666609936754918439842213323507158842708286912986734525860", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "3970773309686131086713175478761589916654448597338797936080363853445349972981", + ) + .map_err(|_| ()) + .unwrap(), + F::from_str( + "5834293033190273456649844072666571376020329540007246603506170413887942823556", + ) + .map_err(|_| ()) + .unwrap(), + ], + ]; + + Parameters { + full_rounds: 8, + partial_rounds: 23, + alpha: 17, + ark, + mds, + rate: 5, + capacity: 1, + } +} diff --git a/src/sponge.rs b/src/sponge.rs deleted file mode 100644 index ab7223b..0000000 --- a/src/sponge.rs +++ /dev/null @@ -1,59 +0,0 @@ -use ark_ed_on_bls12_377::Fq; -use ark_ff::One; -use ark_sponge::{poseidon::PoseidonSponge, CryptographicSponge, FieldBasedCryptographicSponge}; - -use crate::params; - -// This struct will let us replace the implementation of the inner -// PoseidonSponge if we later choose to do so. -struct Sponge { - inner: PoseidonSponge, - // TODOs: Domain separation, mode variable, padding fcn, cache? -} - -impl Sponge { - fn new() -> Self { - let mut sponge = PoseidonSponge::::new(¶ms::rate_2()); - Sponge { inner: sponge } - } - - /// Take a single field element into the sponge. - fn absorb(&mut self, element: Fq) { - self.inner.absorb(&element) - } - - /// Produce a single field element. - fn squeeze(&mut self) -> Fq { - self.inner.squeeze_native_field_elements(1)[0] - } - - /// Hash variable-length input into a hash. - pub fn hash(&mut self, message: Vec, out_len: usize) -> Vec { - for i in 0..message.len() { - self.absorb(message[i]); - } - - // Domain separation - self.absorb(Fq::one()); - - let mut output = Vec::::new(); - for _i in 0..out_len { - output.push(self.squeeze()); - } - output - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_variable_len_hash() { - let out_len = 1; - let mut sponge = Sponge::new(); - let message = vec![Fq::one(), Fq::one()]; - let result = sponge.hash(message, out_len); - assert_eq!(result.len(), out_len); - } -} diff --git a/vendor/generate_mds.sage b/vendor/generate_mds.sage index 08489eb..eb452e3 100644 --- a/vendor/generate_mds.sage +++ b/vendor/generate_mds.sage @@ -330,11 +330,11 @@ def generate_poseidon_param_code( /// Parameters for the rate-{rate} instance of Poseidon. /// /// Note: `F` must be the BLS12-377 scalar field. -pub fn rate_{rate}() -> PoseidonParameters {{ +pub fn rate_{rate}() -> Parameters {{ """ closing = f""" - PoseidonParameters {{ + Parameters {{ full_rounds: {num_rounds - R_p}, partial_rounds: {R_p}, alpha: {alpha}, @@ -396,7 +396,7 @@ print("""//! Parameters for various Poseidon instances over the BLS12-377 scalar // Regenerate with: `sage vendor/generate_mds.sage > src/params.rs` use ark_ff::PrimeField; -use ark_sponge::poseidon::PoseidonParameters; +use crate::Parameters; """) @@ -417,6 +417,13 @@ generate_poseidon_param_code( arity = 4 state_size = arity + capacity_size num_rounds = calculate_num_poseidon_rounds(bls377, sec, alpha, capacity_size, state_size, p) +generate_poseidon_param_code( + poseidon_hash_name, bls377, alpha, state_size, num_rounds, optimize_mds=False, +) + +arity = 5 +state_size = arity + capacity_size +num_rounds = calculate_num_poseidon_rounds(bls377, sec, alpha, capacity_size, state_size, p) generate_poseidon_param_code( poseidon_hash_name, bls377, alpha, state_size, num_rounds, optimize_mds=False, ) \ No newline at end of file