Skip to content

Commit

Permalink
Optimze VDF (MystenLabs#706)
Browse files Browse the repository at this point in the history
* Update reduction

* mutable reduction

* Get rid of closure

* Update numbigint

* Use multi-scalar multiplication for VDF verification

* fmt

* clippy

* Clean up

* Clean up benchmark

* Test

* Optimise fs

* Update tests

* Remove unused function

* constant

* comment

* Clean up

* Trait bounds

* align names

* Review

* cleanup

* Refactor hashprime

* Refactor: get rid of scalar_size constant

* Remove complaint function

* Rename test

* fix bench
  • Loading branch information
jonas-lj authored Dec 11, 2023
1 parent 90ce053 commit 14d62bb
Show file tree
Hide file tree
Showing 21 changed files with 597 additions and 281 deletions.
29 changes: 15 additions & 14 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions fastcrypto-cli/src/vdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ mod tests {
iterations,
}))
.unwrap();
let expected = "Output: 010027d513249bf8d6ad8cc854052080111a420b2771fab2ac566e63cb6a389cfe42c7920b90871fd1ea0b85e80d157d48e6759546cdcfef4a25b3f013b982c2970dfaa8d67e5f87564a91698ffd1407c505372fc52b0313f444937991c63b6b00040401\nProof: 0000aadd0fceb7cab33ad9991aaddfb234473d2c4dc987225cba6f1c6a259e01e893fecede62b459db56474f840e0da0e4de3d0b2da709083620dccfed9451dc3c1b4f911167c85f887dacaa6cac52db94682f9ddc73c18613d4ecf6513580ec2f270302";
let expected = "Output: 010027d513249bf8d6ad8cc854052080111a420b2771fab2ac566e63cb6a389cfe42c7920b90871fd1ea0b85e80d157d48e6759546cdcfef4a25b3f013b982c2970dfaa8d67e5f87564a91698ffd1407c505372fc52b0313f444937991c63b6b00040401\nProof: 0200a79fea1d00b2d1bf7863098980146ad080d400141ff2333652cbcee96b524f273461f8e2e65d8b713663f7083954ef6246ea08d09e6909a047f34065bcfe1e2013c8e523a8a59a01fafa008c637240097d082486c8cc52803d5cad3d4e2aa9130402";
assert_eq!(expected, result);

let invalid_discriminant = "abcx".to_string();
Expand All @@ -190,7 +190,7 @@ mod tests {
let discriminant = "ff6cb04c161319209d438b6f016a9c3703b69fef3bb701550eb556a7b2dfec8676677282f2dd06c5688c51439c59e5e1f9efe8305df1957d6b7bf3433493668680e8b8bb05262cbdf4d020dafa8d5a3433199b8b53f6d487b3f37a4ab59493f050d1e2b535b7e9be19c0201055c0d7a07db3aaa67fe0eed63b63d86558668a27".to_string();
let iterations = 1000u64;
let output = "010027d513249bf8d6ad8cc854052080111a420b2771fab2ac566e63cb6a389cfe42c7920b90871fd1ea0b85e80d157d48e6759546cdcfef4a25b3f013b982c2970dfaa8d67e5f87564a91698ffd1407c505372fc52b0313f444937991c63b6b00040401".to_string();
let proof = "0000aadd0fceb7cab33ad9991aaddfb234473d2c4dc987225cba6f1c6a259e01e893fecede62b459db56474f840e0da0e4de3d0b2da709083620dccfed9451dc3c1b4f911167c85f887dacaa6cac52db94682f9ddc73c18613d4ecf6513580ec2f270302".to_string();
let proof = "0200a79fea1d00b2d1bf7863098980146ad080d400141ff2333652cbcee96b524f273461f8e2e65d8b713663f7083954ef6246ea08d09e6909a047f34065bcfe1e2013c8e523a8a59a01fafa008c637240097d082486c8cc52803d5cad3d4e2aa9130402".to_string();
let result = execute(Command::Verify(VerifyArguments {
discriminant,
iterations,
Expand Down
4 changes: 2 additions & 2 deletions fastcrypto-vdf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ repository = "https://github.com/MystenLabs/fastcrypto"

[dependencies]
fastcrypto = { path = "../fastcrypto" }
num-bigint = "0.4.3"
num-traits = "0.2.16"
num-bigint = "0.4.4"
num-traits = "0.2.17"
num-integer = "0.1.45"
num-modular = "0.6.1"
num-prime = { version = "0.4.3", features = ["big-int"] }
Expand Down
1 change: 1 addition & 0 deletions fastcrypto-vdf/benches/class_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use criterion::measurement::Measurement;
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkGroup, Criterion};
use fastcrypto::groups::Doubling;
use fastcrypto_vdf::class_group::{Discriminant, QuadraticForm};
use fastcrypto_vdf::ParameterizedGroupElement;
use num_bigint::BigInt;
Expand Down
59 changes: 52 additions & 7 deletions fastcrypto-vdf/benches/vdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ extern crate criterion;

use criterion::measurement::Measurement;
use criterion::{BenchmarkGroup, BenchmarkId, Criterion};
use fastcrypto::groups::multiplier::windowed::WindowedScalarMultiplier;
use fastcrypto_vdf::class_group::{Discriminant, QuadraticForm};
use fastcrypto_vdf::vdf::wesolowski::StrongVDF;
use fastcrypto_vdf::hash_prime::{hash_prime_with_index, verify_prime, DefaultPrimalityCheck};
use fastcrypto_vdf::vdf::wesolowski::CHALLENGE_SIZE;
use fastcrypto_vdf::vdf::wesolowski::{FastVerifier, StrongFiatShamir, StrongVDF};
use fastcrypto_vdf::vdf::VDF;
use fastcrypto_vdf::Parameter;
use num_bigint::BigInt;
Expand All @@ -29,16 +32,29 @@ fn verify_single<M: Measurement>(parameters: VerificationInputs, c: &mut Benchma

let result_bytes = hex::decode(parameters.result).unwrap();
let result = QuadraticForm::from_bytes(&result_bytes, &discriminant).unwrap();
let result_copy = result.clone();

let proof_bytes = hex::decode(parameters.proof).unwrap();
let proof = QuadraticForm::from_bytes(&proof_bytes, &discriminant).unwrap();
let proof_copy = proof.clone();

let input = QuadraticForm::generator(&discriminant);
let input_copy = input.clone();

let vdf = StrongVDF::new(discriminant, parameters.iterations);
let vdf = StrongVDF::new(discriminant.clone(), parameters.iterations);
c.bench_function(discriminant_size.to_string(), move |b| {
b.iter(|| vdf.verify(&input, &result, &proof))
});

let vdf = StrongVDF::new(discriminant.clone(), parameters.iterations);
let fast_verify: FastVerifier<
QuadraticForm,
StrongFiatShamir<QuadraticForm, CHALLENGE_SIZE, DefaultPrimalityCheck>,
WindowedScalarMultiplier<QuadraticForm, BigInt, 256, 5>,
> = FastVerifier::new(vdf, input_copy);
c.bench_function(format!("{} fast", discriminant_size), move |b| {
b.iter(|| fast_verify.verify(&result_copy, &proof_copy))
});
}

fn verify(c: &mut Criterion) {
Expand All @@ -62,16 +78,16 @@ fn verify(c: &mut Criterion) {
}

fn sample_discriminant(c: &mut Criterion) {
let bit_lengths = [128, 256, 512, 1024, 2048];
let byte_lengths = [16, 32, 64, 128, 256];

let mut seed = [0u8; 32];

let mut rng = thread_rng();

for bit_length in bit_lengths {
for byte_length in byte_lengths {
c.bench_with_input(
BenchmarkId::new("Sample class group discriminant".to_string(), bit_length),
&bit_length,
BenchmarkId::new("Sample class group discriminant".to_string(), byte_length),
&byte_length,
|b, n| {
b.iter(|| {
rng.try_fill_bytes(&mut seed).unwrap();
Expand All @@ -82,10 +98,39 @@ fn sample_discriminant(c: &mut Criterion) {
}
}

fn verify_discriminant(c: &mut Criterion) {
let byte_lengths = [16, 32, 64, 128, 256];
let seed = [0u8; 32];

for byte_length in byte_lengths {
let (i, _) = hash_prime_with_index::<DefaultPrimalityCheck>(
&seed,
byte_length,
&[0, 1, 8 * byte_length - 1],
);

c.bench_with_input(
BenchmarkId::new("Verify discriminant".to_string(), byte_length),
&byte_length,
|b, n| {
b.iter(|| {
verify_prime::<DefaultPrimalityCheck>(
&seed,
*n,
&[0, 1, 8 * byte_length - 1],
i,
)
.unwrap()
})
},
);
}
}

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

criterion_main!(vdf_benchmarks);
Loading

0 comments on commit 14d62bb

Please sign in to comment.