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

Sample class group discriminant faster #677

Merged
merged 6 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fastcrypto-vdf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ experimental = []
[dev-dependencies]
hex = "0.4.3"
criterion = "0.5.1"
rand = "0.8.4"

[[bench]]
name = "vdf"
Expand Down
49 changes: 39 additions & 10 deletions fastcrypto-vdf/benches/vdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
extern crate criterion;

use criterion::measurement::Measurement;
use criterion::{BenchmarkGroup, Criterion};
use criterion::{BenchmarkGroup, BenchmarkId, Criterion};
use fastcrypto_vdf::class_group::{Discriminant, QuadraticForm};
use fastcrypto_vdf::vdf::wesolowski::ClassGroupVDF;
use fastcrypto_vdf::vdf::VDF;
use num_bigint::BigInt;
use num_traits::Num;
use rand::{thread_rng, RngCore};

struct VerificationInputs {
iterations: u64,
Expand Down Expand Up @@ -48,29 +49,57 @@ fn verify(c: &mut Criterion) {

let mut group = c.benchmark_group(format!("VDF verify ({})", dep));

// Note that the number of iterations are quite low, but this has very little influence on the benchmark results.

//1024 bits
verify_single(VerificationInputs {
iterations: 1000,
iterations: 4000000,
discriminant: "cd711f181153e08e08e5ba156db0c4e9469de76f2bd6b64f068f5007918727f5eaa5f6a0e090f82682a4ebf87befdea8f1253265d700ee3ca6b0fdb2677c633c7f37b62f0e0c13b402def0ba9abaf15e4c53bfb6bda0c7a0cad4439864af3eb9af6d6c4b10286eb8ff5e2de5b009196bc60c3000fde8d4b89b7674e61bc2d23f".to_string(),
result: "030039c78c39cff6c29052bfc1453616ec7a47251509b9dbc33d1036bebd4d12e6711a51deb327120310f96be04c90fd4c3b1dab9617c3133132b827abe7bb2348707da8164b964e1b95cd6a8eaf36ffb80bab1f750410e793daec8228b222bd00370100".to_string(),
proof: "000075d043db5f619f5cb4e8ef7729c7cac154434c33d6e52dd086b90a52c7b1231890eda9d1365100e88993e332f0a99bb7763f215de2fb6b632445beeeff22b657dc90d4e110ed03eac10ec445117d211208c79dd4933ba58b8e17b4c54ef1824c0100".to_string(),
result: "02007e64ab640cfd39daeaeab7400797917a2635fca5988ae1ba5e9a7a0b234faf361ae103d36d0838574524a5bc0a6b0bdd8f5e8c90774e92194df23fd5929b343bc7e47a1a07270949ae1b37505a63414aec987e06eabc6738d1ec02b32d6da3690100".to_string(),
proof: "0200c879069103e13f66c38ac8f34a8ec48ec7033f442128de49c7adf0732359e4da4682bff7ad6a2ca2767a39f9eaf4dec9c80fb950d2cb603458738b3d0e17855e6bf0586455e99e75fa23f0ae59e1a922c1d5b18b234428766028f5856b872f720100".to_string(),
}, &mut group);

// 2048 bits
verify_single(VerificationInputs {
iterations: 1000,
iterations: 4000000,
discriminant: "f6901cd003679e2f451cda55b032fb49222a9b595b9e5948b793d2d7338d4da01937c637739e7f980d481b742c0fdc5255847ccc848359db822ed6ca7f33bdd54a207e24679c9f1f7e64be59e1bed7afbaa999770743984ed997c2c8187b5a80a0df200c040ac152dd6bb3bfdf3a7f151f2ddbd9debf6c841cebdc9f450cb42f51529ba04e6bda874b43461ed104b39257559bed53200d093f8e6c48f2b1c91e15e37ce695924eafd78fa4ba11e519f9a885399264d1a885d353ce128f1e044ef2feda125167e38ad5db7931b752847388c900868bc6bff2d83f7a6e055c618d3abc0ae104520df25508f40323c35d2d992303e12f1ae7bc44ffd5861d9f768f".to_string(),
result: "02001222c470df6df6e1321aa1c28279d0c64663c7f066888ff6cd854dcd5deb71f63dfe0b867675180fada390e0d7b1ff735b55fea2b88123a32d1e1239126b275578ea26a4a89e5ef290e2b7b8d072ab819d5b9422770339dc87fd4dc4ebf6add3e391067a557be4be5436355ab11035609d5a3dc71e95cf2a0dcbb228b85d9750a1dc670ac51822d7eff49b5cacd4a8cc485e53bbf7e44f95e7fd5ec55fca44eb91c4831b1e839d8b4c8453dce8be69698bc5cb8fa45120d201057e4d72a6746b0100".to_string(),
proof: "03008b91b20ab570b701d394aa095d8c670d95a8a3b26af966e979a27acf417421360ea54014668a121139ab11fe92cc0a8d192a8a675f244f3016ed23a7a82d9dd70de089d5bcb5bb0c9535923b2656b19c8cf0cc6e0e4c800c44fc17e16a1b96572f6e0e0967709af259b854a51bec270e5cf73cc4efa93791ac6a84dc2ab77f02d0234ac60b2a04740644ac845204c67f9063ab139e9a0eb25c4417c892ca52299202d3854243d7eb58cc46a837745a1eb92699eb89138eec89467f7226380b040600".to_string(),
result: "0000d37421051f4f437a727a8d21825ef02a9c33744766947a59140d532756f231d42d8add13fe76e747b130a29becb75c3a3389ee1472325a479afb4275b7e9cf0cedc957e4409cfdea69e901fc8d810617381c0492de46e0387ee42eb3065468ceec55d17f072fa691341ff5b6d835abf35a47b90c127658c4bf4ec8ea6a4ae4177bd96aad7454c36e7bde4bb360a519c9d7b73ecd776d44d18d6441bc5fbe8724227c623477b5c307b89dcea707e1db547d4d0e8c7814e9f24ceedb55653585310100".to_string(),
proof: "02000607e9272f517e3d7aaa2d3f5bcb5925b9e9c46e432b6b292223df502cc4487b5841d9c4f3746adfd1f058482220d38ddf4c6daf30d9cc0cf0cebb36a5b1ef9189e4ed78b022fae17b9fc2e16c6d3450df52877f67b3c7c06db17eb1f1ecb8c78310aa622935ff76abfb3bdf153604359438c1364269a80dd434149b658a6b6629ec86ba04a339b07b4ab71c1f2417c64a2cfa49138a62d0c02e753c1060df68a3f9ed04fc357742b6f927f7550ba79e52c429031ba3f353b9dd5fef6147c4190100".to_string(),
}, &mut group);
}

fn sample_discriminant(c: &mut Criterion) {
#[cfg(not(feature = "gmp"))]
let dep = "num-bigint";

#[cfg(feature = "gmp")]
let dep = "gmp";

let bit_lengths = [128, 256, 512, 1024, 2048];

let mut seed = [0u8; 32];

let mut rng = thread_rng();

for bit_length in bit_lengths {
c.bench_with_input(
BenchmarkId::new(
format!("Sample class group discriminant ({})", dep),
bit_length,
),
&bit_length,
|b, n| {
b.iter(|| {
rng.try_fill_bytes(&mut seed).unwrap();
Discriminant::from_seed(&seed, *n).unwrap();
})
},
);
}
}

criterion_group! {
name = vdf_benchmarks;
config = Criterion::default().sample_size(100);
targets = verify,
targets = verify, sample_discriminant
}

criterion_main!(vdf_benchmarks);
16 changes: 13 additions & 3 deletions fastcrypto-vdf/src/vdf/wesolowski.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use fastcrypto::error::FastCryptoError::{InvalidInput, InvalidProof};
use fastcrypto::error::FastCryptoResult;
use fastcrypto::hash::HashFunction;
use fastcrypto::hash::Sha256;
use num_bigint::{BigInt, Sign};
use num_bigint::{BigInt, BigUint, Sign};
use num_integer::Integer;
use num_prime::nt_funcs::is_prime;
use std::cmp::min;
use std::marker::PhantomData;
use std::ops::Neg;
Expand Down Expand Up @@ -204,12 +203,23 @@ fn hash_prime(seed: &[u8], length: usize, bitmask: &[usize]) -> FastCryptoResult
// The implementations of the primality test used below might be slightly different from the
// one used by chiavdf, but since the risk of a false positive is very small (4^{-100}) this
// is not an issue.
if is_prime(&x.to_biguint().unwrap(), None).probably() {
if is_prime(&x.to_biguint().unwrap()) {
return Ok(x);
}
}
}

#[cfg(feature = "gmp")]
fn is_prime(x: &BigUint) -> bool {
let y = rug::Integer::from_digits(&x.to_bytes_be(), rug::integer::Order::Msf);
y.is_probably_prime(30) != rug::integer::IsPrime::No
}

#[cfg(not(feature = "gmp"))]
fn is_prime(x: &BigUint) -> bool {
num_prime::nt_funcs::is_prime(x, None).probably()
}

impl Discriminant {
/// Compute a valid discriminant (aka a negative prime equal to 3 mod 4) based on the given seed.
pub fn from_seed(seed: &[u8], length: usize) -> FastCryptoResult<Self> {
Expand Down
Loading