From 8d1da81e91094d430c7420841472b1fba2cc85e9 Mon Sep 17 00:00:00 2001 From: MRain Date: Fri, 13 Sep 2024 13:00:15 -0400 Subject: [PATCH 1/4] deprecate minroot & add hashchain --- vdf/Cargo.toml | 19 ++++++--- vdf/benches/hashchain.rs | 34 ++++++++++++++++ vdf/src/hashchain.rs | 88 ++++++++++++++++++++++++++++++++++++++++ vdf/src/lib.rs | 2 + vdf/src/minroot.rs | 2 + 5 files changed, 139 insertions(+), 6 deletions(-) create mode 100644 vdf/benches/hashchain.rs create mode 100644 vdf/src/hashchain.rs diff --git a/vdf/Cargo.toml b/vdf/Cargo.toml index e25840a01..4f3077838 100644 --- a/vdf/Cargo.toml +++ b/vdf/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jf-vdf" -version = "0.1.0" +version = "0.1.1" description = "Verifiable delay function." authors = { workspace = true } edition = { workspace = true } @@ -11,14 +11,15 @@ documentation = { workspace = true } repository = { workspace = true } [dependencies] -ark-bls12-381 = { workspace = true } -ark-bn254 = { workspace = true } -ark-ec = { workspace = true } -ark-ff = { workspace = true } -ark-pallas = "0.4.0" +ark-bls12-381 = { workspace = true, optional = true } +ark-bn254 = { workspace = true, optional = true } +ark-ec = { workspace = true, optional = true } +ark-ff = { workspace = true, optional = true } +ark-pallas = { version = "0.4.0", optional = true } ark-serialize = { workspace = true } ark-std = { workspace = true } displaydoc = { workspace = true } +sha3 = { workspace = true } [dev-dependencies] criterion = "0.5.1" @@ -26,9 +27,15 @@ criterion = "0.5.1" [[bench]] name = "minroot" harness = false +required-features = ["minroot"] + +[[bench]] +name = "hashchain" +harness = false [features] default = [] +minroot = ["ark-bls12-381", "ark-bn254", "ark-ec", "ark-ff", "ark-pallas"] std = [ "ark-pallas/std", "ark-bls12-381/std", "ark-bn254/std", "ark-std/std", "ark-ff/std", "ark-ec/std", diff --git a/vdf/benches/hashchain.rs b/vdf/benches/hashchain.rs new file mode 100644 index 000000000..2386c961f --- /dev/null +++ b/vdf/benches/hashchain.rs @@ -0,0 +1,34 @@ +// Copyright (c) 2022 Espresso Systems (espressosys.com) +// This file is part of the Jellyfish library. + +// You should have received a copy of the MIT License +// along with the Jellyfish library. If not, see . + +#[macro_use] +extern crate criterion; +use ark_std::rand::rngs::StdRng; +use criterion::{Criterion, Throughput}; +use jf_vdf::{hashchain::HashChain, VDF}; + +fn minroot_bench(c: &mut Criterion) { + let mut benchmark_group = c.benchmark_group("HashChain"); + benchmark_group.sample_size(10); + let iterations = 1u64 << 22; + + benchmark_group.throughput(Throughput::Elements(iterations)); + let pp = HashChain::setup::(iterations, None).unwrap(); + let input = [0u8; 32]; + benchmark_group.bench_function("HashChain_sha3_keccak", |b| { + b.iter(|| HashChain::eval(&pp, &input).unwrap()) + }); + + benchmark_group.finish(); +} + +fn bench(c: &mut Criterion) { + minroot_bench(c); +} + +criterion_group!(benches, bench); + +criterion_main!(benches); diff --git a/vdf/src/hashchain.rs b/vdf/src/hashchain.rs new file mode 100644 index 000000000..9f6ac410a --- /dev/null +++ b/vdf/src/hashchain.rs @@ -0,0 +1,88 @@ +// Copyright (c) 2022 Espresso Systems (espressosys.com) +// This file is part of the Jellyfish library. + +// You should have received a copy of the MIT License +// along with the Jellyfish library. If not, see . +//! Instantiation of the hash chain delay function. + +use crate::{VDFError, VDF}; +use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; +use ark_std::vec::Vec; +use core::marker::PhantomData; +use sha3::Digest; + +/// Glorified bool type +type VerificationResult = Result<(), ()>; + +#[derive( + Copy, + Clone, + Debug, + Default, + Eq, + PartialEq, + Ord, + PartialOrd, + CanonicalSerialize, + CanonicalDeserialize, +)] +/// Public parameter for MinRoot DF, +pub struct HashChainParam { + /// Indicates the number of iterations + pub difficulty: u64, +} + +#[derive(Copy, Debug, Clone)] +/// Dummy struct for MinRoot delay function. +pub struct HashChain; + +impl VDF for HashChain { + type PublicParameter = HashChainParam; + type Proof = [u8; 32]; + type Input = [u8; 32]; + type Output = [u8; 32]; + + fn setup( + difficulty: u64, + prng: Option<&mut R>, + ) -> Result { + Ok(HashChainParam { difficulty }) + } + + fn eval( + pp: &Self::PublicParameter, + input: &Self::Input, + ) -> Result<(Self::Output, Self::Proof), VDFError> { + let mut output = *input; + for _ in 0..pp.difficulty { + output = sha3::Keccak256::digest(&input).into(); + } + Ok((output, output)) + } + + fn verify( + _pp: &Self::PublicParameter, + _input: &Self::Input, + output: &Self::Output, + proof: &Self::Proof, + ) -> Result { + Ok(if output == proof { Ok(()) } else { Err(()) }) + } +} + +#[cfg(test)] +mod test { + use crate::{hashchain::HashChain, VDF}; + use ark_std::rand::rngs::StdRng; + + #[test] + fn test_hashchain() { + let start = [0u8; 32]; + let pp = HashChain::setup::(100, None).unwrap(); + let (output, proof) = HashChain::eval(&pp, &start).unwrap(); + assert_eq!(output, proof); + assert!(HashChain::verify(&pp, &start, &output, &proof) + .unwrap() + .is_ok()); + } +} diff --git a/vdf/src/lib.rs b/vdf/src/lib.rs index d7f409b57..a48261197 100644 --- a/vdf/src/lib.rs +++ b/vdf/src/lib.rs @@ -25,6 +25,8 @@ use ark_std::{ }; use displaydoc::Display; +pub mod hashchain; +#[cfg(feature = "minroot")] pub mod minroot; /// VDF error type diff --git a/vdf/src/minroot.rs b/vdf/src/minroot.rs index 68aeb8d10..3d0254326 100644 --- a/vdf/src/minroot.rs +++ b/vdf/src/minroot.rs @@ -3,6 +3,7 @@ // You should have received a copy of the MIT License // along with the Jellyfish library. If not, see . +//! DEPRECATED! DO NOT USE UNLESS YOU CLEARLY UNDERSTAND THIS . //! Instantiation of the MinRoot Delay function . use crate::{VDFError, VDF}; @@ -68,6 +69,7 @@ where } } +#[derive(Copy, Clone, Debug)] /// Dummy struct for MinRoot delay function. pub struct MinRoot { _phantom: PhantomData, From de12a3272fe416a655c2b339538c5f43aaf314b3 Mon Sep 17 00:00:00 2001 From: MRain Date: Fri, 13 Sep 2024 13:07:18 -0400 Subject: [PATCH 2/4] changelog --- vdf/CHAGELOG.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/vdf/CHAGELOG.md b/vdf/CHAGELOG.md index bbee589dc..89f2d9640 100644 --- a/vdf/CHAGELOG.md +++ b/vdf/CHAGELOG.md @@ -3,6 +3,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## 0.1.0 +# 0.1.1 + +## Added + +- [#683](https://github.com/EspressoSystems/jellyfish/pull/683): add a simple hash chain delay function using Keccak. + +## Deprecated + +- [#683](https://github.com/EspressoSystems/jellyfish/pull/683): MinRoot delay function is now hidden under a feature flag. Add documentation about its security issue. + +# 0.1.0 - Initial release. VDF trait definition and (non-verifiable) MinRoot delay function implementation. From ffc603be8be262428dee883c35b5ac8520f9b619 Mon Sep 17 00:00:00 2001 From: MRain Date: Fri, 13 Sep 2024 13:45:48 -0400 Subject: [PATCH 3/4] fix stupid bug & ark-serialize derive --- Cargo.toml | 2 +- vdf/Cargo.toml | 1 + vdf/src/hashchain.rs | 5 ++++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0d8a0e24b..18d6062e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ ark-bw6-761 = { version = "0.4.0", default-features = false } ark-ec = { version = "0.4.0", default-features = false } ark-ff = { version = "0.4.0", default-features = false, features = [ "asm" ] } ark-poly = { version = "0.4.0", default-features = false } -ark-serialize = { version = "0.4.0", default-features = false } +ark-serialize = { version = "0.4.0", default-features = false, features = [ "derive" ] } ark-std = { version = "0.4.0", default-features = false } derivative = { version = "2", features = ["use_core"] } digest = { version = "0.10.1", default-features = false, features = [ "alloc" ] } diff --git a/vdf/Cargo.toml b/vdf/Cargo.toml index 4f3077838..2e8ae4674 100644 --- a/vdf/Cargo.toml +++ b/vdf/Cargo.toml @@ -19,6 +19,7 @@ ark-pallas = { version = "0.4.0", optional = true } ark-serialize = { workspace = true } ark-std = { workspace = true } displaydoc = { workspace = true } +serde = { workspace = true } sha3 = { workspace = true } [dev-dependencies] diff --git a/vdf/src/hashchain.rs b/vdf/src/hashchain.rs index 9f6ac410a..443d7fb06 100644 --- a/vdf/src/hashchain.rs +++ b/vdf/src/hashchain.rs @@ -9,6 +9,7 @@ use crate::{VDFError, VDF}; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; use ark_std::vec::Vec; use core::marker::PhantomData; +use serde::{Deserialize, Serialize}; use sha3::Digest; /// Glorified bool type @@ -25,6 +26,8 @@ type VerificationResult = Result<(), ()>; PartialOrd, CanonicalSerialize, CanonicalDeserialize, + Serialize, + Deserialize, )] /// Public parameter for MinRoot DF, pub struct HashChainParam { @@ -55,7 +58,7 @@ impl VDF for HashChain { ) -> Result<(Self::Output, Self::Proof), VDFError> { let mut output = *input; for _ in 0..pp.difficulty { - output = sha3::Keccak256::digest(&input).into(); + output = sha3::Keccak256::digest(&output).into(); } Ok((output, output)) } From 47d95e758296cc1582d55d74203358a85fc1b0ff Mon Sep 17 00:00:00 2001 From: MRain Date: Wed, 18 Sep 2024 09:11:15 -0400 Subject: [PATCH 4/4] deprecate VDF crate --- CODEOWNERS | 3 - Cargo.toml | 2 +- README.md | 1 - scripts/check_no_std.sh | 1 - scripts/run_tests.sh | 1 - vdf/CHAGELOG.md | 18 ---- vdf/Cargo.toml | 43 --------- vdf/benches/hashchain.rs | 34 ------- vdf/benches/minroot.rs | 50 ----------- vdf/src/hashchain.rs | 91 ------------------- vdf/src/lib.rs | 98 -------------------- vdf/src/minroot.rs | 188 --------------------------------------- 12 files changed, 1 insertion(+), 529 deletions(-) delete mode 100644 vdf/CHAGELOG.md delete mode 100644 vdf/Cargo.toml delete mode 100644 vdf/benches/hashchain.rs delete mode 100644 vdf/benches/minroot.rs delete mode 100644 vdf/src/hashchain.rs delete mode 100644 vdf/src/lib.rs delete mode 100644 vdf/src/minroot.rs diff --git a/CODEOWNERS b/CODEOWNERS index ad52a5e42..f558202f5 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -25,9 +25,6 @@ # Owner of `jf-rescue` crate /rescue/ @philippecamacho -# Owner of `jf-vdf` crate -/vdf/ @mrain - # Owner of `jf-vid` crate /vid/ @ggutoski @akonring diff --git a/Cargo.toml b/Cargo.toml index 18d6062e7..06d1c8d11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["aead", "commitment", "crhf", "elgamal", "merkle_tree", "pcs", "plonk", "prf", "relation", "rescue", "signature", "utilities", "vdf", "vid", "vrf"] +members = ["aead", "commitment", "crhf", "elgamal", "merkle_tree", "pcs", "plonk", "prf", "relation", "rescue", "signature", "utilities", "vid", "vrf"] resolver = "2" [workspace.package] diff --git a/README.md b/README.md index 397f29e68..7852dbe55 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,6 @@ For general discussions on Jellyfish PLONK, please join our [Discord channel](ht - ['jf-aead'](aead): authenticated encryption with associated data (AEAD) implementation. - ['jf-merkle-tree'](merkle_tree): various (vanilla, sparse, namespaced) Merkle tree trait definitions and implementations. - ['jf-pcs'](pcs): polynomial commitment scheme (PCS) trait definitions and univariate/multilinear KZG-PCS implementations. -- ['jf-vdf'](vdf): verifiable delay function (VDF) trait definitions and (non-verifiable) MinRoot implementation. - ['jf-vid'](vid): verifiable information dispersal (VID) trait definition and implementation. ### Plonk diff --git a/scripts/check_no_std.sh b/scripts/check_no_std.sh index 28929f375..7f5e31f83 100755 --- a/scripts/check_no_std.sh +++ b/scripts/check_no_std.sh @@ -9,7 +9,6 @@ cargo-nono check --no-default-features --package jf-merkle-tree --features "gadg cargo-nono check --no-default-features --package jf-pcs --features "test-srs" cargo-nono check --no-default-features --package jf-rescue --features "gadgets" cargo-nono check --no-default-features --package jf-signature --features "bls, schnorr, gadgets" -cargo-nono check --no-default-features --package jf-vdf cargo-nono check --no-default-features --package jf-vid --features "test-srs" cargo-nono check --no-default-features --package jf-aead cargo-nono check --no-default-features --package jf-elgamal --features "gadgets" diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh index fe3bf8d9e..ba237f301 100755 --- a/scripts/run_tests.sh +++ b/scripts/run_tests.sh @@ -10,7 +10,6 @@ cargo +nightly test --release -p jf-merkle-tree --features gadgets -- -Zunstable cargo +nightly test --release -p jf-pcs --features test-srs -- -Zunstable-options --report-time cargo +nightly test --release -p jf-rescue --features gadgets -- -Zunstable-options --report-time cargo +nightly test --release -p jf-signature --features "bls, schnorr, gadgets" -- -Zunstable-options --report-time -cargo +nightly test --release -p jf-vdf -- -Zunstable-options --report-time cargo +nightly test --release -p jf-vid --features test-srs -- -Zunstable-options --report-time cargo +nightly test --release -p jf-aead -- -Zunstable-options --report-time cargo +nightly test --release -p jf-elgamal --features gadgets -- -Zunstable-options --report-time diff --git a/vdf/CHAGELOG.md b/vdf/CHAGELOG.md deleted file mode 100644 index 89f2d9640..000000000 --- a/vdf/CHAGELOG.md +++ /dev/null @@ -1,18 +0,0 @@ -# CHANGELOG - -The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), -and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - -# 0.1.1 - -## Added - -- [#683](https://github.com/EspressoSystems/jellyfish/pull/683): add a simple hash chain delay function using Keccak. - -## Deprecated - -- [#683](https://github.com/EspressoSystems/jellyfish/pull/683): MinRoot delay function is now hidden under a feature flag. Add documentation about its security issue. - -# 0.1.0 - -- Initial release. VDF trait definition and (non-verifiable) MinRoot delay function implementation. diff --git a/vdf/Cargo.toml b/vdf/Cargo.toml deleted file mode 100644 index 2e8ae4674..000000000 --- a/vdf/Cargo.toml +++ /dev/null @@ -1,43 +0,0 @@ -[package] -name = "jf-vdf" -version = "0.1.1" -description = "Verifiable delay function." -authors = { workspace = true } -edition = { workspace = true } -license = { workspace = true } -rust-version = { workspace = true } -homepage = { workspace = true } -documentation = { workspace = true } -repository = { workspace = true } - -[dependencies] -ark-bls12-381 = { workspace = true, optional = true } -ark-bn254 = { workspace = true, optional = true } -ark-ec = { workspace = true, optional = true } -ark-ff = { workspace = true, optional = true } -ark-pallas = { version = "0.4.0", optional = true } -ark-serialize = { workspace = true } -ark-std = { workspace = true } -displaydoc = { workspace = true } -serde = { workspace = true } -sha3 = { workspace = true } - -[dev-dependencies] -criterion = "0.5.1" - -[[bench]] -name = "minroot" -harness = false -required-features = ["minroot"] - -[[bench]] -name = "hashchain" -harness = false - -[features] -default = [] -minroot = ["ark-bls12-381", "ark-bn254", "ark-ec", "ark-ff", "ark-pallas"] -std = [ - "ark-pallas/std", "ark-bls12-381/std", "ark-bn254/std", "ark-std/std", - "ark-ff/std", "ark-ec/std", -] diff --git a/vdf/benches/hashchain.rs b/vdf/benches/hashchain.rs deleted file mode 100644 index 2386c961f..000000000 --- a/vdf/benches/hashchain.rs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) 2022 Espresso Systems (espressosys.com) -// This file is part of the Jellyfish library. - -// You should have received a copy of the MIT License -// along with the Jellyfish library. If not, see . - -#[macro_use] -extern crate criterion; -use ark_std::rand::rngs::StdRng; -use criterion::{Criterion, Throughput}; -use jf_vdf::{hashchain::HashChain, VDF}; - -fn minroot_bench(c: &mut Criterion) { - let mut benchmark_group = c.benchmark_group("HashChain"); - benchmark_group.sample_size(10); - let iterations = 1u64 << 22; - - benchmark_group.throughput(Throughput::Elements(iterations)); - let pp = HashChain::setup::(iterations, None).unwrap(); - let input = [0u8; 32]; - benchmark_group.bench_function("HashChain_sha3_keccak", |b| { - b.iter(|| HashChain::eval(&pp, &input).unwrap()) - }); - - benchmark_group.finish(); -} - -fn bench(c: &mut Criterion) { - minroot_bench(c); -} - -criterion_group!(benches, bench); - -criterion_main!(benches); diff --git a/vdf/benches/minroot.rs b/vdf/benches/minroot.rs deleted file mode 100644 index 95748154a..000000000 --- a/vdf/benches/minroot.rs +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) 2022 Espresso Systems (espressosys.com) -// This file is part of the Jellyfish library. - -// You should have received a copy of the MIT License -// along with the Jellyfish library. If not, see . - -#[macro_use] -extern crate criterion; -use ark_bls12_381::Fr as Fr381; -use ark_bn254::Fr as Fr254; -use ark_pallas::Fr as PastaFr; -use ark_std::rand::rngs::StdRng; -use criterion::{Criterion, Throughput}; -use jf_vdf::{ - minroot::{MinRoot, MinRootElement}, - VDF, -}; - -fn minroot_bench(c: &mut Criterion) { - let mut benchmark_group = c.benchmark_group("MinRoot"); - benchmark_group.sample_size(10); - let iterations = 1u64 << 16; - - benchmark_group.throughput(Throughput::Elements(iterations)); - let pp = MinRoot::::setup::(iterations, None).unwrap(); - let input = MinRootElement::::default(); - benchmark_group.bench_function("MinRoot_BN254", |b| { - b.iter(|| MinRoot::::eval(&pp, &input).unwrap()) - }); - - let input = MinRootElement::::default(); - benchmark_group.bench_function("MinRoot_BLS381", |b| { - b.iter(|| MinRoot::::eval(&pp, &input).unwrap()) - }); - - let input = MinRootElement::::default(); - benchmark_group.bench_function("MinRoot_Pallas", |b| { - b.iter(|| MinRoot::::eval(&pp, &input).unwrap()) - }); - - benchmark_group.finish(); -} - -fn bench(c: &mut Criterion) { - minroot_bench(c); -} - -criterion_group!(benches, bench); - -criterion_main!(benches); diff --git a/vdf/src/hashchain.rs b/vdf/src/hashchain.rs deleted file mode 100644 index 443d7fb06..000000000 --- a/vdf/src/hashchain.rs +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright (c) 2022 Espresso Systems (espressosys.com) -// This file is part of the Jellyfish library. - -// You should have received a copy of the MIT License -// along with the Jellyfish library. If not, see . -//! Instantiation of the hash chain delay function. - -use crate::{VDFError, VDF}; -use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; -use ark_std::vec::Vec; -use core::marker::PhantomData; -use serde::{Deserialize, Serialize}; -use sha3::Digest; - -/// Glorified bool type -type VerificationResult = Result<(), ()>; - -#[derive( - Copy, - Clone, - Debug, - Default, - Eq, - PartialEq, - Ord, - PartialOrd, - CanonicalSerialize, - CanonicalDeserialize, - Serialize, - Deserialize, -)] -/// Public parameter for MinRoot DF, -pub struct HashChainParam { - /// Indicates the number of iterations - pub difficulty: u64, -} - -#[derive(Copy, Debug, Clone)] -/// Dummy struct for MinRoot delay function. -pub struct HashChain; - -impl VDF for HashChain { - type PublicParameter = HashChainParam; - type Proof = [u8; 32]; - type Input = [u8; 32]; - type Output = [u8; 32]; - - fn setup( - difficulty: u64, - prng: Option<&mut R>, - ) -> Result { - Ok(HashChainParam { difficulty }) - } - - fn eval( - pp: &Self::PublicParameter, - input: &Self::Input, - ) -> Result<(Self::Output, Self::Proof), VDFError> { - let mut output = *input; - for _ in 0..pp.difficulty { - output = sha3::Keccak256::digest(&output).into(); - } - Ok((output, output)) - } - - fn verify( - _pp: &Self::PublicParameter, - _input: &Self::Input, - output: &Self::Output, - proof: &Self::Proof, - ) -> Result { - Ok(if output == proof { Ok(()) } else { Err(()) }) - } -} - -#[cfg(test)] -mod test { - use crate::{hashchain::HashChain, VDF}; - use ark_std::rand::rngs::StdRng; - - #[test] - fn test_hashchain() { - let start = [0u8; 32]; - let pp = HashChain::setup::(100, None).unwrap(); - let (output, proof) = HashChain::eval(&pp, &start).unwrap(); - assert_eq!(output, proof); - assert!(HashChain::verify(&pp, &start, &output, &proof) - .unwrap() - .is_ok()); - } -} diff --git a/vdf/src/lib.rs b/vdf/src/lib.rs deleted file mode 100644 index a48261197..000000000 --- a/vdf/src/lib.rs +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright (c) 2022 Espresso Systems (espressosys.com) -// This file is part of the Jellyfish library. - -// You should have received a copy of the MIT License -// along with the Jellyfish library. If not, see . - -//! Trait and implementation for a Verifiable Delay Function (VDF) . - -#![cfg_attr(not(feature = "std"), no_std)] -// Temporarily allow warning for nightly compilation with [`displaydoc`]. -#![allow(warnings)] -#![deny(missing_docs)] -#[cfg(test)] -extern crate std; - -#[cfg(any(not(feature = "std"), target_has_atomic = "ptr"))] -#[doc(hidden)] -extern crate alloc; - -use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; -use ark_std::{ - fmt::Debug, - rand::{CryptoRng, RngCore}, - string::String, -}; -use displaydoc::Display; - -pub mod hashchain; -#[cfg(feature = "minroot")] -pub mod minroot; - -/// VDF error type -#[derive(Debug, Display, Eq, PartialEq)] -pub struct VDFError(String); - -impl ark_std::error::Error for VDFError {} - -/// Glorified bool type. -type VerificationResult = Result<(), ()>; - -/// A trait for VDF proof, evaluation and verification. -pub trait VDF { - /// Public parameters - type PublicParameter; - - /// VDF proof. - type Proof: Debug - + Clone - + Send - + Sync - + CanonicalSerialize - + CanonicalDeserialize - + PartialEq - + Eq; - - /// VDF input. - type Input: Debug - + Clone - + Send - + Sync - + CanonicalSerialize - + CanonicalDeserialize - + PartialEq - + Eq; - - /// VDF output. - type Output: Debug - + Clone - + Send - + Sync - + CanonicalSerialize - + CanonicalDeserialize - + PartialEq - + Eq; - - /// Generates a public parameter from RNG with given difficulty. - /// Concrete instantiations of VDF shall document properly about the - /// correspondence between the difficulty value and the time required - /// for evaluation/proof generation. - fn setup( - difficulty: u64, - prng: Option<&mut R>, - ) -> Result; - - /// Computes the VDF output and proof. - fn eval( - pp: &Self::PublicParameter, - input: &Self::Input, - ) -> Result<(Self::Output, Self::Proof), VDFError>; - - /// Verifies a VDF output given the proof. - fn verify( - pp: &Self::PublicParameter, - input: &Self::Input, - output: &Self::Output, - proof: &Self::Proof, - ) -> Result; -} diff --git a/vdf/src/minroot.rs b/vdf/src/minroot.rs deleted file mode 100644 index 3d0254326..000000000 --- a/vdf/src/minroot.rs +++ /dev/null @@ -1,188 +0,0 @@ -// Copyright (c) 2022 Espresso Systems (espressosys.com) -// This file is part of the Jellyfish library. - -// You should have received a copy of the MIT License -// along with the Jellyfish library. If not, see . -//! DEPRECATED! DO NOT USE UNLESS YOU CLEARLY UNDERSTAND THIS . -//! Instantiation of the MinRoot Delay function . - -use crate::{VDFError, VDF}; -use ark_ec::AffineRepr; -use ark_ff::PrimeField; -use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; -use ark_std::vec::Vec; -use core::marker::PhantomData; - -/// Glorified bool type -type VerificationResult = Result<(), ()>; - -/// MinRoot compatible field -pub trait MinRootField: PrimeField { - /// The MinRoot iteration is calculating the cubic root (or fifth-root if - /// modulus % 3 == 1) of a field element. E.g. `EXP_COEF` should be (2 * - /// modulus - 1) / 3 if modulus % 3 != 1. - const EXP_COEF: Self::BigInt; -} - -#[derive( - Copy, - Clone, - Debug, - Default, - Eq, - PartialEq, - Ord, - PartialOrd, - CanonicalSerialize, - CanonicalDeserialize, -)] - -/// Public parameter for MinRoot DF, -pub struct MinRootPP { - /// Indicates the number of iterations - pub difficulty: u64, -} - -/// A minroot element consists of a pair of field elements. -#[derive( - Copy, - Clone, - Debug, - Default, - Eq, - PartialEq, - Ord, - PartialOrd, - CanonicalSerialize, - CanonicalDeserialize, -)] -pub struct MinRootElement(F, F); - -impl From for MinRootElement -where - T: AffineRepr, - F: MinRootField, -{ - fn from(value: T) -> Self { - let (x, y) = value.xy().unwrap(); - MinRootElement(*x, *y) - } -} - -#[derive(Copy, Clone, Debug)] -/// Dummy struct for MinRoot delay function. -pub struct MinRoot { - _phantom: PhantomData, -} - -impl VDF for MinRoot { - type PublicParameter = MinRootPP; - type Proof = MinRootElement; - type Input = MinRootElement; - type Output = MinRootElement; - - fn setup( - difficulty: u64, - _prng: Option<&mut R>, - ) -> Result { - Ok(MinRootPP { difficulty }) - } - - fn eval( - pp: &Self::PublicParameter, - input: &Self::Input, - ) -> Result<(Self::Output, Self::Proof), VDFError> { - let mut output = *input; - for i in 0..pp.difficulty { - Self::iterate_in_place(&mut output, i)?; - } - Ok((output, output)) - } - - fn verify( - _pp: &Self::PublicParameter, - _input: &Self::Input, - output: &Self::Output, - proof: &Self::Proof, - ) -> Result { - if proof == output { - Ok(Ok(())) - } else { - Ok(Err(())) - } - } -} - -impl MinRoot { - #[inline] - fn iterate_in_place(elem: &mut MinRootElement, round: u64) -> Result<(), VDFError> { - let x = elem.0; - elem.0 = (x + elem.1).pow(F::EXP_COEF); - // assert_eq!(elem.0.pow([5u64]), x + elem.1); - elem.1 = x + F::from(round); - Ok(()) - } -} - -impl MinRootField for ark_bn254::Fr { - // modulus 21888242871839275222246405745257275088548364400416034343698204186575808495617 - // modulus % 3 == 1, modulus % 5 == 2 - // coef = (4 * modulus - 3) / 5 - // coef: 17510594297471420177797124596205820070838691520332827474958563349260646796493 - const EXP_COEF: Self::BigInt = ark_ff::BigInt::<4>([ - 14981214993055009997, - 6006880321387387405, - 10624953561019755799, - 2789598613442376532, - ]); -} - -impl MinRootField for ark_bls12_381::Fr { - // modulus 52435875175126190479447740508185965837690552500527637822603658699938581184513 - // modulus % 3 == 1, modulus % 5 == 3 - // coef = (2 * modulus - 1) / 5 - // coef: 20974350070050476191779096203274386335076221000211055129041463479975432473805 - const EXP_COEF: Self::BigInt = ark_ff::BigInt::<4>([ - 3689348813023923405, - 2413663763415232921, - 16233882818423549954, - 3341406743785779740, - ]); -} - -impl MinRootField for ark_pallas::Fr { - // modulus 28948022309329048855892746252171976963363056481941647379679742748393362948097 - // modulus % 3 == 1, modulus % 5 == 2 - // coef = (4 * modulus - 3) / 5 - // coef: 23158417847463239084714197001737581570690445185553317903743794198714690358477 - const EXP_COEF: Self::BigInt = ark_ff::BigInt::<4>([ - 15465117582000704717, - 5665212537877281354, - 3689348814741910323, - 3689348814741910323, - ]); -} - -#[cfg(test)] -mod test { - use super::{MinRoot, MinRootElement, MinRootField}; - use crate::VDF; - use ark_std::rand::rngs::StdRng; - - #[test] - fn test_minroot() { - test_minroot_helper::(); - test_minroot_helper::(); - test_minroot_helper::(); - } - - fn test_minroot_helper() { - let start = MinRootElement(F::one(), F::one()); - let pp = MinRoot::::setup::(100, None).unwrap(); - let (output, proof) = MinRoot::::eval(&pp, &start).unwrap(); - assert_eq!(output, proof); - assert!(MinRoot::::verify(&pp, &start, &output, &proof) - .unwrap() - .is_ok()); - } -}