Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the default class group hash parameters #819

Merged
merged 15 commits into from
Sep 13, 2024
9 changes: 3 additions & 6 deletions fastcrypto-cli/src/vdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,8 @@ fn execute(cmd: Command) -> Result<String, Error> {
Command::Hash(arguments) => {
let input = hex::decode(arguments.message)
.map_err(|_| Error::new(ErrorKind::InvalidInput, "Invalid message."))?;
let output =
QuadraticForm::hash_to_group_with_default_parameters(&input, &DISCRIMINANT_3072)
.map_err(|_| {
Error::new(ErrorKind::InvalidInput, "The k parameter was too big")
})?;
let output = QuadraticForm::hash_to_group(&input, &DISCRIMINANT_3072)
.map_err(|_| Error::new(ErrorKind::InvalidInput, "The discriminant is invalid."))?;

let output_bytes = hex::encode(bcs::to_bytes(&output).unwrap());

Expand Down Expand Up @@ -266,7 +263,7 @@ mod tests {
message: seed.clone(),
}))
.unwrap();
let expected = "Output: bc010082ee2d187c87dd0253b19d73db019c2f11e29ee9a70ff48082860c05c17f8e786276e999605f086e3d58124f378e06ff904aa3f623f5fd1903d23b381a2b1abc23ea3004f84defb5d1c7c825134305373308a7c287110cce7ff96fed79b366d4272ef03226aecdce76b5f6f946bff527e56391a3f72682403f8f48a584ba6769a96ed0742d299060759f8ba227ff87011035dc097b54a76f4c7ed7d60a530f92500d7228b15ac3addceea196a341b4f06217d5881952025e650e81ea02247f8e37e7ddede21f1f8e34fc1d23aafcfddd9cae9c9fe068b069591abd638bf143b2e7ca646c298a69883dac05dec9b9c77d211f06dd0325b851a464b34d0a4084363c5b8c4fea79d38544f7d583d55931374c467239fd4ba7ba3a9d2ef0457d910c18e42575ff039da6814e585238647f910eb90bd78b750ee315ebbb67703168c9f25d3484191093d7e99ed160041d27188165eef767d6cfe735037a8d33c670087a0c4be4c404022525f2261ce411d4f2afa2c450891eed6ad1fe2ec72d361e3d73a63da32fc8e5f445fe1d559dcc41a13e87bba38827cc5da40ad097da50617b216c4ad7aa6c50a20b0b840273b671e2027f8a3acb3651d9fc8e45ede649d232df111bf9c49c68afc0e699ef359e86e8912d0902b6e908988f6994b51ae2f8ab1a26a0bf96ee722d05f7cddf61d25899645648de8821de536d6175d2d28729c6f56a99fbf383043bccc3183a45ea3b3b4d599443c5672d9a26e6e2d7475bb8b76bcc5d393bc3939904028b278613c65986c0529d0ed75d26567154d61029833cff4bda9e3649b2b7196e43b5182b2b2b27020cf99d90de2aa3f927ea03453352855508a39466098b211f486a5c83ce48005287a2d941c734d93abe091e08860f28a462ea061f060129da80d7998d7cf036be70248c5b96582de577929fae4e77e8ea61419dd6571894e892c57153e79fd80ab68bc1b248a15841e5f91943fc4b71d6e5f841ed6dffca3bef0dafa79211aa607f55d5fa5cd0225d9a9cb26d0438ebaa402019f84ac75aeecb40d2fc75fc9449f117f98c230ad561771a8c621a3bfc2116017549eccd9a52f51a6eb0992d57042f684e95f5a12481c570ff57167be444c7f0b71c2aba4178253f577b488d6007f1ea4d8c05b663c539c334a8fcbb91284881dda609ee18f8133c98dc815ce17154a5bbfb380250895455b6c6ce227de43e8fa0b593e39deb4d280e92579dc344972361372ee1cd1c40197bcbec2262d43331acab5ae8bb0a70b87e7d503c40867b43febefedf7a59dabab669c6e94fc321d2a67816e36b982b21a5914eb0f5e5b343f5d6e0c7db24476ca3a18ae3f4d5fff78d7fc917272db5f21db999faa6626243aeb3d9f2939c0ddc06c94aae74fa58c903fd56ff634ec1eaafb3a7965318946f45473fc3ddea8f9972ea73645bfe132569514e40ac9b9abddcd6fe5623573e3ab724eb2adab38d9064b259952ac83ee999ddea5edae12a0e4d08822c4c9e499f498ca66da90272bf6d3d41ad0d86";
let expected = "Output: 227afa0a257a3fa140fad0ba29ade524a7cc097c706a7c59c363fede402870cbf192ef33fc06eddf144b2023a50256b388f8fd5a6ef59dd7275e2f7ae1288bef605158fa97e2af2684604395314a5176a655a940bb3ac3de025d92c53e95f69368f53575775e965be811f89c08d0c9dc702daea57afb3d6e5e48b8255fc93901580f01837abf0a6e94e3a29d6a9bcca669c611d3f94256b72c7a67415391607d89e15a3f82880bae011276e96f097895311e45caaa81dc7b35e3fe97d83cd392e7b94e1ddfa0f684be3e9106e0bda15c0153d2f8a6ba1528b796ca6bd24a5164eed50e63f7cd5837ef7c9a6d52451d66fca2221f04efe44b446896843b1bbf4ebc322d80781259afcc1a5780eedd3cde2cd98e44d3bfadb19a0253b6f0e7f957469e41ce8bc48f9516412b61097c858cc9fac2489f5a6467e1516d8ab93cf7849c34428589110b9d1e3d4d4e114480fe14db77eb545cd13b3a4f6fe43dc9002ec52d1400fbdba8a34ffab8b5200a002f0b13691f635304a2d266bee80263dbe996e03fe8840902922d814d5535f97a2602c24a276b95e48dc802d9e09288f411360ae69357d0c0018474992daa45dc6cde5a2fe02ae9d2";
assert_eq!(expected, result);
}
}
38 changes: 18 additions & 20 deletions fastcrypto-vdf/benches/class_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ fn class_group_ops_single<M: Measurement>(
let discriminant =
Discriminant::try_from(BigInt::from_str_radix(discriminant_string, 10).unwrap()).unwrap();
let discriminant_size = discriminant.bits();
let x =
QuadraticForm::hash_to_group_with_default_parameters(&[0, 1, 2], &discriminant).unwrap();
let y =
QuadraticForm::hash_to_group_with_default_parameters(&[3, 4, 5], &discriminant).unwrap();
let x = QuadraticForm::hash_to_group(&[0, 1, 2], &discriminant).unwrap();
let y = QuadraticForm::hash_to_group(&[3, 4, 5], &discriminant).unwrap();
let z = y.clone();

group.bench_function(format!("Compose/{}", discriminant_size), move |b| {
Expand All @@ -42,27 +40,27 @@ fn class_group_ops(c: &mut Criterion) {
class_group_ops_single("-3956718340719431033560816005739172412770466038703883350122595604635576709778731043309649272634605425735063624961596232735523376358742586480084965522907889249504047757258883253188259997112353246294323895993621766064597867526555590598296171109353515435289599237581716817331650248439511065683812661746851260538223197043808247010651962962398117206670503394901226393544809521031397039151671384417895714291888633743136733122871000628264376743806741659217599861141099968091237561343232177937280916663464976799422526037223295835103220909629798144507776992742385829474209304917863091971426479552645067278361106093545925188309289850090156462919761399169738282697646499095260815062205117198915610910901974886408275636330747461116245431578813689565691787676837733109337359377237752443898143986060895378572144245069588193342265623936118710486379006798704589510096698445426370143184307451927004120706539519891935325715903611926844068327127", & mut group);
}

fn qf_from_seed_single<M: Measurement>(discriminant_string: &str, group: &mut BenchmarkGroup<M>) {
for k in [1, 2, 4, 8, 16, 32, 64] {
let discriminant =
Discriminant::try_from(BigInt::from_str_radix(discriminant_string, 10).unwrap())
.unwrap();
fn hash_to_class_group_single<M: Measurement>(
discriminant_string: &str,
group: &mut BenchmarkGroup<M>,
) {
let discriminant =
Discriminant::try_from(BigInt::from_str_radix(discriminant_string, 10).unwrap()).unwrap();
jonas-lj marked this conversation as resolved.
Show resolved Hide resolved
let bits = discriminant.bits();

let bits = discriminant.bits();
group.bench_function(format!("{} bits/{}", bits, k), move |b| {
let mut seed = [0u8; 32];
thread_rng().fill_bytes(&mut seed);
b.iter(|| QuadraticForm::hash_to_group(&seed, &discriminant, k))
});
}
group.bench_function(format!("{} bits, default", bits), move |b| {
let mut seed = [0u8; 32];
thread_rng().fill_bytes(&mut seed);
b.iter(|| QuadraticForm::hash_to_group(&seed, &discriminant))
});
}

fn qf_from_seed(c: &mut Criterion) {
let mut group: BenchmarkGroup<_> = c.benchmark_group("QF from seed".to_string());
qf_from_seed_single("-177936553897922261333164712410242884021141613334565149505848952826212491241771489747671651876796162246463307642229416178115038439759411468976306741727054586407871065937781422970533238726218439981299971510624199735799141406458705159532021234549127390010928558972193365915987838285442951665356778289757172235943", & mut group);
qf_from_seed_single("-23847247576959809407620518371643827142851600013982827283452201808476362679737007351478256611970011299818463606695939850757804842744423478520579143718664008954313076284985803848944695073539132381339505840123539160487844863469984182420796118328898791241477525662002452306976347345400556627774302685021609312012814515255952971689521318993322456321786703461532401532357072300154368747634872702010117806469012662322855405602958496469070700447595558569638530268626808054913729109827208897181454138769346082237168707092766224594001630621792974930286552997453103372073081585433844131063808863644588266240224171853046660887399", & mut group);
qf_from_seed_single("-197094279717529776652945533421408519016291293185778176422038767173246838389717778782272450609952179792102389097362657787152898007436991089430517979761145200893975140029279440383697629952398509684430189989830512427761221044255503309237697000446508821686655886069366603792908696660367648281136978401042076354619587515552611650395121072487799107192700364331538210709886133279169829259881605487142555274403314509719321602412760314496712012939372327177464352472192738122541539747842405435171078768578664156285412471750348778431888800482596404122201686947621151032470989798594881908508768154982514267787085456831726879055929531619461354230569362180363281846948763424056650300352728927552479847814231289623672826128091486169286759", & mut group);
qf_from_seed_single("-3956718340719431033560816005739172412770466038703883350122595604635576709778731043309649272634605425735063624961596232735523376358742586480084965522907889249504047757258883253188259997112353246294323895993621766064597867526555590598296171109353515435289599237581716817331650248439511065683812661746851260538223197043808247010651962962398117206670503394901226393544809521031397039151671384417895714291888633743136733122871000628264376743806741659217599861141099968091237561343232177937280916663464976799422526037223295835103220909629798144507776992742385829474209304917863091971426479552645067278361106093545925188309289850090156462919761399169738282697646499095260815062205117198915610910901974886408275636330747461116245431578813689565691787676837733109337359377237752443898143986060895378572144245069588193342265623936118710486379006798704589510096698445426370143184307451927004120706539519891935325715903611926844068327127", & mut group);
hash_to_class_group_single("-177936553897922261333164712410242884021141613334565149505848952826212491241771489747671651876796162246463307642229416178115038439759411468976306741727054586407871065937781422970533238726218439981299971510624199735799141406458705159532021234549127390010928558972193365915987838285442951665356778289757172235943", & mut group);
hash_to_class_group_single("-23847247576959809407620518371643827142851600013982827283452201808476362679737007351478256611970011299818463606695939850757804842744423478520579143718664008954313076284985803848944695073539132381339505840123539160487844863469984182420796118328898791241477525662002452306976347345400556627774302685021609312012814515255952971689521318993322456321786703461532401532357072300154368747634872702010117806469012662322855405602958496469070700447595558569638530268626808054913729109827208897181454138769346082237168707092766224594001630621792974930286552997453103372073081585433844131063808863644588266240224171853046660887399", & mut group);
hash_to_class_group_single("-197094279717529776652945533421408519016291293185778176422038767173246838389717778782272450609952179792102389097362657787152898007436991089430517979761145200893975140029279440383697629952398509684430189989830512427761221044255503309237697000446508821686655886069366603792908696660367648281136978401042076354619587515552611650395121072487799107192700364331538210709886133279169829259881605487142555274403314509719321602412760314496712012939372327177464352472192738122541539747842405435171078768578664156285412471750348778431888800482596404122201686947621151032470989798594881908508768154982514267787085456831726879055929531619461354230569362180363281846948763424056650300352728927552479847814231289623672826128091486169286759", & mut group);
hash_to_class_group_single("-3956718340719431033560816005739172412770466038703883350122595604635576709778731043309649272634605425735063624961596232735523376358742586480084965522907889249504047757258883253188259997112353246294323895993621766064597867526555590598296171109353515435289599237581716817331650248439511065683812661746851260538223197043808247010651962962398117206670503394901226393544809521031397039151671384417895714291888633743136733122871000628264376743806741659217599861141099968091237561343232177937280916663464976799422526037223295835103220909629798144507776992742385829474209304917863091971426479552645067278361106093545925188309289850090156462919761399169738282697646499095260815062205117198915610910901974886408275636330747461116245431578813689565691787676837733109337359377237752443898143986060895378572144245069588193342265623936118710486379006798704589510096698445426370143184307451927004120706539519891935325715903611926844068327127", & mut group);
}

criterion_group! {
Expand Down
2 changes: 1 addition & 1 deletion fastcrypto-vdf/src/class_group/discriminant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::str::FromStr;
pub struct Discriminant(#[serde(with = "crate::class_group::bigint_serde")] BigInt);

lazy_static! {
/// Fixed 3072 bit discriminant. Generated from the seed [1,2,3] using [Discriminant::from_seed].
/// Fixed 3072 bit discriminant. Generated from the seed `[1,2,3]` using [Discriminant::from_seed].
// TODO: Generate this using a seed that we provably cannot influence.
pub static ref DISCRIMINANT_3072: Discriminant = Discriminant(BigInt::from_str("-4080390101490206102067801750685552291425412528983716161454985565795560716833845004659207152503580931176637478422335625954692628868126419714053340412299850300602673802493259771830686596468801304317015718872352674945215883546019961626928140286675493693757393881479657605888983279619347902770789061953207866325747708864327315769009839190765716943013935708854055658243676903245686125751909996824976354309908771869043784640567352757672203749399825983258156684652782580603170228640173640869773628592618889352385821753919281706169861276929330689892675986265846043432389737049521845230769417696140636288030698887830215613149485135897148487896368642774768920061430225392365148291796645740474628778185683682893521776342856643134668770656709308404166182149870849376649591338267281149794078240401323227967073641261327798339424740171219484355109588337730742391198073121589465833677609362668436116144203312494461735357918360857667357985711").unwrap());
}
Expand Down
Loading
Loading