Skip to content

Commit

Permalink
Refactor VDF: Move Rug/GMP dependent part to other repo (#690)
Browse files Browse the repository at this point in the history
* Get rid of rug depenedency

* Clean up

* Refactor discriminant from seed

* Refactor

* Refactor and some docs

* Use ToBytes trait

* Update cli

* Update tests
  • Loading branch information
jonas-lj authored Nov 13, 2023
1 parent 7fdaa34 commit 3dee479
Show file tree
Hide file tree
Showing 16 changed files with 713 additions and 1,742 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ jobs:
run: cargo build --benches --features experimental,copy_key,unsecure_schemes
- name: cargo test
run: cargo nextest run --features experimental,copy_key,unsecure_schemes
- name: Test VDF with GMP
if: matrix.os == 'ubuntu-ghcloud'
run: cd fastcrypto-vdf && cargo nextest run --features gmp && cd ..
- name: Doctests
run: |
cargo test --doc --features experimental,copy_key,unsecure_schemes
Expand Down
28 changes: 0 additions & 28 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions fastcrypto-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ publish = ["crates-io"]
description = "Command line tools for cryptographic functions"
repository = "https://github.com/MystenLabs/fastcrypto"

[features]
gmp = ["fastcrypto-vdf/gmp"]

[dependencies]
clap = { version = "4.1.8", features = ["derive"] }
fastcrypto = { path = "../fastcrypto", features = ["copy_key"] }
Expand Down
17 changes: 9 additions & 8 deletions fastcrypto-cli/src/vdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

use clap::Parser;
use fastcrypto_vdf::class_group::{Discriminant, QuadraticForm};
use fastcrypto_vdf::vdf::wesolowski::ClassGroupVDF;
use fastcrypto_vdf::vdf::wesolowski::StrongVDF;
use fastcrypto_vdf::vdf::VDF;
use fastcrypto_vdf::ParameterizedGroupElement;
use fastcrypto_vdf::Parameter;
use fastcrypto_vdf::ToBytes;
use std::io::{Error, ErrorKind};

const DEFAULT_DISCRIMINANT_BIT_LENGTH: u64 = 1024;
Expand Down Expand Up @@ -99,13 +100,13 @@ fn execute(cmd: Command) -> Result<String, Error> {

let g = QuadraticForm::generator(&discriminant);

let vdf = ClassGroupVDF::new(discriminant, arguments.iterations);
let vdf = StrongVDF::new(discriminant, arguments.iterations);
let (output, proof) = vdf
.evaluate(&g)
.map_err(|_| Error::new(ErrorKind::Other, "VDF evaluation failed"))?;

let output_string = hex::encode(output.as_bytes());
let proof_string = hex::encode(proof.as_bytes());
let output_string = hex::encode(output.to_bytes());
let proof_string = hex::encode(proof.to_bytes());

let mut result = "Output: ".to_string();
result.push_str(&output_string);
Expand Down Expand Up @@ -137,7 +138,7 @@ fn execute(cmd: Command) -> Result<String, Error> {

let g = QuadraticForm::generator(&discriminant);

let vdf = ClassGroupVDF::new(discriminant, arguments.iterations);
let vdf = StrongVDF::new(discriminant, arguments.iterations);
let verifies = vdf.verify(&g, &output, &proof).is_ok();

let mut result = "Verified: ".to_string();
Expand Down Expand Up @@ -173,7 +174,7 @@ mod tests {
iterations,
}))
.unwrap();
let expected = "Output: 010027d513249bf8d6ad8cc854052080111a420b2771fab2ac566e63cb6a389cfe42c7920b90871fd1ea0b85e80d157d48e6759546cdcfef4a25b3f013b982c2970dfaa8d67e5f87564a91698ffd1407c505372fc52b0313f444937991c63b6b00040401\nProof: 0300999cca180ec6e2e51b5fb42b9d9b95e9c8b3407ee08f181d8a2699513d4d5d543c9918df4f7e9e9c476191e85a2a7bfdb5b7706c2866daafd9194c741c3f345aa9ab9731fca61eb863401a76966e9deecf5c79112351e99d27cfcdd108a41d1a0100";
let expected = "Output: 010027d513249bf8d6ad8cc854052080111a420b2771fab2ac566e63cb6a389cfe42c7920b90871fd1ea0b85e80d157d48e6759546cdcfef4a25b3f013b982c2970dfaa8d67e5f87564a91698ffd1407c505372fc52b0313f444937991c63b6b00040401\nProof: 0000aadd0fceb7cab33ad9991aaddfb234473d2c4dc987225cba6f1c6a259e01e893fecede62b459db56474f840e0da0e4de3d0b2da709083620dccfed9451dc3c1b4f911167c85f887dacaa6cac52db94682f9ddc73c18613d4ecf6513580ec2f270302";
assert_eq!(expected, result);

let invalid_discriminant = "abcx".to_string();
Expand All @@ -189,7 +190,7 @@ mod tests {
let discriminant = "ff6cb04c161319209d438b6f016a9c3703b69fef3bb701550eb556a7b2dfec8676677282f2dd06c5688c51439c59e5e1f9efe8305df1957d6b7bf3433493668680e8b8bb05262cbdf4d020dafa8d5a3433199b8b53f6d487b3f37a4ab59493f050d1e2b535b7e9be19c0201055c0d7a07db3aaa67fe0eed63b63d86558668a27".to_string();
let iterations = 1000u64;
let output = "010027d513249bf8d6ad8cc854052080111a420b2771fab2ac566e63cb6a389cfe42c7920b90871fd1ea0b85e80d157d48e6759546cdcfef4a25b3f013b982c2970dfaa8d67e5f87564a91698ffd1407c505372fc52b0313f444937991c63b6b00040401".to_string();
let proof = "0300999cca180ec6e2e51b5fb42b9d9b95e9c8b3407ee08f181d8a2699513d4d5d543c9918df4f7e9e9c476191e85a2a7bfdb5b7706c2866daafd9194c741c3f345aa9ab9731fca61eb863401a76966e9deecf5c79112351e99d27cfcdd108a41d1a0100".to_string();
let proof = "0000aadd0fceb7cab33ad9991aaddfb234473d2c4dc987225cba6f1c6a259e01e893fecede62b459db56474f840e0da0e4de3d0b2da709083620dccfed9451dc3c1b4f911167c85f887dacaa6cac52db94682f9ddc73c18613d4ecf6513580ec2f270302".to_string();
let result = execute(Command::Verify(VerifyArguments {
discriminant,
iterations,
Expand Down
2 changes: 0 additions & 2 deletions fastcrypto-vdf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ num-traits = "0.2.16"
num-integer = "0.1.45"
num-modular = "0.6.1"
num-prime = { version = "0.4.3", features = ["big-int"] }
rug = {version = "1.21.0", optional = true}

[features]
gmp = ["dep:rug"]
experimental = []

[dev-dependencies]
Expand Down
8 changes: 1 addition & 7 deletions fastcrypto-vdf/benches/class_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ fn class_group_ops_single<M: Measurement>(
}

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

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

let mut group: BenchmarkGroup<_> = c.benchmark_group(format!("Class Group ({})", dep));
let mut group: BenchmarkGroup<_> = c.benchmark_group("Class Group".to_string());

class_group_ops_single("-9458193260787340859710210783898414376413627187338129653105774703043377776905956484932486183722303201135571583745806165441941755833466966188398807387661571", & mut group);
class_group_ops_single("-173197108158285529655099692042166386683260486655764503111574151459397279244340625070436917386670107433539464870917173822190635872887684166173874718269704667936351650895772937202272326332043347073303124000059154982400685660701006453457007094026343973435157790533480400962985543272080923974737725172126369794019", &mut group);
Expand Down
24 changes: 5 additions & 19 deletions fastcrypto-vdf/benches/vdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ extern crate criterion;
use criterion::measurement::Measurement;
use criterion::{BenchmarkGroup, BenchmarkId, Criterion};
use fastcrypto_vdf::class_group::{Discriminant, QuadraticForm};
use fastcrypto_vdf::vdf::wesolowski::ClassGroupVDF;
use fastcrypto_vdf::vdf::wesolowski::StrongVDF;
use fastcrypto_vdf::vdf::VDF;
use fastcrypto_vdf::Parameter;
use num_bigint::BigInt;
use num_traits::Num;
use rand::{thread_rng, RngCore};
Expand All @@ -34,20 +35,14 @@ fn verify_single<M: Measurement>(parameters: VerificationInputs, c: &mut Benchma

let input = QuadraticForm::generator(&discriminant);

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

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

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

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

//1024 bits
verify_single(VerificationInputs {
Expand All @@ -67,12 +62,6 @@ fn verify(c: &mut Criterion) {
}

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];
Expand All @@ -81,10 +70,7 @@ fn sample_discriminant(c: &mut Criterion) {

for bit_length in bit_lengths {
c.bench_with_input(
BenchmarkId::new(
format!("Sample class group discriminant ({})", dep),
bit_length,
),
BenchmarkId::new("Sample class group discriminant".to_string(), bit_length),
&bit_length,
|b, n| {
b.iter(|| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

//! Functionality to compress/decompress and serialize/deserialize quadratic forms.
use crate::class_group::num_bigint::compressed::CompressedQuadraticForm::{
Generator, Nontrivial, Zero,
};
use crate::class_group::num_bigint::{Discriminant, QuadraticForm};
use crate::class_group::compressed::CompressedQuadraticForm::{Generator, Nontrivial, Zero};
use crate::class_group::{Discriminant, QuadraticForm};
use crate::ParameterizedGroupElement;
use fastcrypto::error::{FastCryptoError, FastCryptoResult};
use num_bigint::{BigInt, Sign};
Expand Down Expand Up @@ -392,10 +390,10 @@ fn partial_xgcd(a: &BigInt, b: &BigInt) -> FastCryptoResult<(BigInt, BigInt)> {

#[cfg(test)]
mod tests {
use crate::class_group::num_bigint::compressed::{
use crate::class_group::compressed::{
bigint_from_bytes, bigint_to_bytes, CompressedQuadraticForm,
};
use crate::class_group::num_bigint::{Discriminant, QuadraticForm};
use crate::class_group::{Discriminant, QuadraticForm};
use crate::ParameterizedGroupElement;
use num_bigint::BigInt;
use num_traits::Num;
Expand Down
Loading

0 comments on commit 3dee479

Please sign in to comment.