mirror of https://github.com/poanetwork/vdf.git
Add benchmarks, reformat, and reduce Miller-Rabin rounds
This commit is contained in:
parent
59f011b7ec
commit
45a02bfd4f
4
ci.sh
4
ci.sh
|
@ -51,9 +51,9 @@ test_output () {
|
|||
for proof_type in wesolowski pietrzak; do
|
||||
while read challenge iterations correct_proof; do
|
||||
printf "Checking proof of type %q on input %d... " "$proof_type" "$((count += 1))"
|
||||
test_output "$correct_proof" ./target/release/vdf-cli "-t$proof_type" prove -- "$challenge" "$iterations"
|
||||
test_output "$correct_proof" ./target/release/vdf-cli "-t$proof_type" -- "$challenge" "$iterations"
|
||||
printf "Checking verification of input %d... " "$count"
|
||||
test_output 'Proof is valid' ./target/release/vdf-cli "-t$proof_type" prove -- "$challenge" "$iterations" "$correct_proof"
|
||||
test_output 'Proof is valid' ./target/release/vdf-cli "-t$proof_type" -- "$challenge" "$iterations" "$correct_proof"
|
||||
done < <(grep -E '^[a-f0-9]{64},[0-9]{2,4},[0-9a-f]+$' "$proof_type.csv")
|
||||
done
|
||||
"$k"
|
||||
|
|
|
@ -23,3 +23,11 @@ license = "Apache-2.0"
|
|||
num-traits = "0.2"
|
||||
rust-gmp = "0.5"
|
||||
libc = "0.2"
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = ">=0.2"
|
||||
|
||||
[[bench]]
|
||||
name = "classgroup-bench"
|
||||
harness = false
|
||||
path = "bench/bench.rs"
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
// Copyright 2018 POA Networks Ltd.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#[macro_use]
|
||||
extern crate criterion;
|
||||
extern crate classgroup;
|
||||
extern crate gmp;
|
||||
use classgroup::{gmp_classgroup::GmpClassGroup, ClassGroup};
|
||||
use criterion::Criterion;
|
||||
use gmp::mpz::Mpz;
|
||||
use std::str::FromStr;
|
||||
|
||||
fn bench_square(c: &mut Criterion) {
|
||||
for _ in 0..2 {
|
||||
let m_2048 = -Mpz::from_str(
|
||||
"201493927071865251625903550712920535753645598483515670853547009\
|
||||
878440933309489362800393797428711071833308081461824159206915864\
|
||||
150805748296170245037221957772328044276705571745811271212292422\
|
||||
075849739248257870371300001313586036515879618764093772248760562\
|
||||
386804073478433157526816295216137723803793411828867470089409596\
|
||||
238958950007370719325959579892866588928887249912429688364409867\
|
||||
895510817680171869190054122881274299350947669820596157115994418\
|
||||
034091728887584373727555384075665624624856766441009974642693066\
|
||||
751400054217209981490667208950669417773785631693879782993019167\
|
||||
69407006303085854796535778826115224633447713584423",
|
||||
).unwrap();
|
||||
|
||||
let m_1024 = -Mpz::from_str(
|
||||
"-11208471744389096429663063172516742066731683613191418514476174383781\
|
||||
682509882427394963852743081347678693241523614532942268295868231081182\
|
||||
819214054220080323345750407342623884342617809879459211722505867733607\
|
||||
400509994975706778681543998242335468203860240586171413971485860382901\
|
||||
6409314686266660248501773529803183",
|
||||
).unwrap();
|
||||
let group_1024 = GmpClassGroup::generator_for_discriminant(m_1024);
|
||||
let group_2048 = GmpClassGroup::generator_for_discriminant(m_2048);
|
||||
let (group_1024_clone, group_2048_clone) = (group_1024.clone(), group_2048.clone());
|
||||
c.bench_function("square 1024", move |b| {
|
||||
b.iter(|| group_1024_clone.clone().square())
|
||||
});
|
||||
c.bench_function("multiply 1024", move |b| {
|
||||
b.iter(|| &group_1024 * &group_1024)
|
||||
});
|
||||
c.bench_function("square 2048", move |b| {
|
||||
b.iter(|| group_2048_clone.clone().square())
|
||||
});
|
||||
c.bench_function("multiply 2048", move |b| {
|
||||
b.iter(|| &group_2048 * &group_2048)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_square);
|
||||
criterion_main!(benches);
|
|
@ -16,10 +16,12 @@
|
|||
use super::ClassGroup;
|
||||
use gmp::mpz::Mpz;
|
||||
use num_traits::{One, Zero};
|
||||
use std::borrow::Borrow;
|
||||
use std::cell::RefCell;
|
||||
use std::fmt;
|
||||
use std::ops::{Mul, MulAssign};
|
||||
use std::{
|
||||
borrow::Borrow,
|
||||
cell::RefCell,
|
||||
fmt,
|
||||
ops::{Mul, MulAssign},
|
||||
};
|
||||
mod congruence;
|
||||
pub(super) mod ffi;
|
||||
|
||||
|
|
|
@ -172,14 +172,15 @@ pub trait ClassGroup:
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::PathBuf;
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{BufRead, BufReader},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
extern crate gmp;
|
||||
extern crate num_traits;
|
||||
use super::gmp_classgroup::GmpClassGroup;
|
||||
use super::ClassGroup;
|
||||
use super::{gmp_classgroup::GmpClassGroup, ClassGroup};
|
||||
use gmp::mpz::Mpz;
|
||||
|
||||
fn split_into_three_pieces(line: &str, c: char) -> [&str; 3] {
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
# make_backup = false
|
||||
# match_arm_blocks = true
|
||||
# match_block_trailing_comma = false
|
||||
# merge_imports = false
|
||||
# merge_imports = true
|
||||
# normalize_comments = true
|
||||
# normalize_doc_attributes = true
|
||||
# reorder_impl_items = false
|
||||
|
@ -61,6 +61,6 @@ remove_nested_parens = true
|
|||
reorder_imports = true
|
||||
reorder_modules = true
|
||||
tab_spaces = 4
|
||||
use_field_init_shorthand = false
|
||||
use_field_init_shorthand = true
|
||||
use_small_heuristics = "Default"
|
||||
use_try_shorthand = false
|
||||
use_try_shorthand = true
|
||||
|
|
|
@ -20,11 +20,7 @@ extern crate vdf;
|
|||
extern crate clap;
|
||||
extern crate classgroup;
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::rc::Rc;
|
||||
use std::u64;
|
||||
use std::{cell::RefCell, fs::File, io::Read, rc::Rc, u64};
|
||||
use vdf::{InvalidProof, PietrzakVDFParams, VDFParams, WesolowskiVDFParams, VDF};
|
||||
|
||||
macro_rules! gen_validator {
|
||||
|
|
|
@ -11,11 +11,7 @@
|
|||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::u16;
|
||||
use std::{env, fs::File, io::Write, path::PathBuf, u16};
|
||||
|
||||
/// The number of odd primes less than 65536.
|
||||
const PRIMES_LEN: usize = 6541;
|
||||
|
|
|
@ -108,7 +108,7 @@ pub fn create_discriminant<T: BigNumExt>(seed: &[u8], length: u16) -> T {
|
|||
if !x {
|
||||
let q = u64::from(M) * u64::from(i);
|
||||
n = n + q;
|
||||
if n.probab_prime(13) {
|
||||
if n.probab_prime(2) {
|
||||
return -n;
|
||||
}
|
||||
n = n - q;
|
||||
|
@ -135,6 +135,21 @@ mod test {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_discriminant_3() {
|
||||
assert_eq!(
|
||||
create_discriminant::<Mpz>(b"\xaa", 1024),
|
||||
Mpz::from_str(
|
||||
"-112084717443890964296630631725167420667316836131914185144761\
|
||||
7438378168250988242739496385274308134767869324152361453294226\
|
||||
8295868231081182819214054220080323345750407342623884342617809\
|
||||
8794592117225058677336074005099949757067786815439982423354682\
|
||||
0386024058617141397148586038290164093146862666602485017735298\
|
||||
03183"
|
||||
).unwrap()
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_discriminant_2() {
|
||||
assert_eq!(
|
||||
|
|
|
@ -43,31 +43,30 @@
|
|||
//! ### To use the VDF library
|
||||
//!
|
||||
//! ```rust
|
||||
//! extern crate vdf;
|
||||
//! use vdf::{InvalidProof, PietrzakVDFParams, VDFParams, WesolowskiVDFParams, VDF};
|
||||
//!
|
||||
//! fn main() {
|
||||
//! const CORRECT_SOLUTION: &[u8] =
|
||||
//! b"\x00\x52\x71\xe8\xf9\xab\x2e\xb8\xa2\x90\x6e\x85\x1d\xfc\xb5\x54\x2e\x41\x73\xf0\x16\
|
||||
//! \xb8\x5e\x29\xd4\x81\xa1\x08\xdc\x82\xed\x3b\x3f\x97\x93\x7b\x7a\xa8\x24\x80\x11\x38\
|
||||
//! \xd1\x77\x1d\xea\x8d\xae\x2f\x63\x97\xe7\x6a\x80\x61\x3a\xfd\xa3\x0f\x2c\x30\xa3\x4b\
|
||||
//! \x04\x0b\xaa\xaf\xe7\x6d\x57\x07\xd6\x86\x89\x19\x3e\x5d\x21\x18\x33\xb3\x72\xa6\xa4\
|
||||
//! \x59\x1a\xbb\x88\xe2\xe7\xf2\xf5\xa5\xec\x81\x8b\x57\x07\xb8\x6b\x8b\x2c\x49\x5c\xa1\
|
||||
//! \x58\x1c\x17\x91\x68\x50\x9e\x35\x93\xf9\xa1\x68\x79\x62\x0a\x4d\xc4\xe9\x07\xdf\x45\
|
||||
//! \x2e\x8d\xd0\xff\xc4\xf1\x99\x82\x5f\x54\xec\x70\x47\x2c\xc0\x61\xf2\x2e\xb5\x4c\x48\
|
||||
//! \xd6\xaa\x5a\xf3\xea\x37\x5a\x39\x2a\xc7\x72\x94\xe2\xd9\x55\xdd\xe1\xd1\x02\xae\x2a\
|
||||
//! \xce\x49\x42\x93\x49\x2d\x31\xcf\xf2\x19\x44\xa8\xbc\xb4\x60\x89\x93\x06\x5c\x9a\x00\
|
||||
//! \x29\x2e\x8d\x3f\x46\x04\xe7\x46\x5b\x4e\xee\xfb\x49\x4f\x5b\xea\x10\x2d\xb3\x43\xbb\
|
||||
//! \x61\xc5\xa1\x5c\x7b\xdf\x28\x82\x06\x88\x5c\x13\x0f\xa1\xf2\xd8\x6b\xf5\xe4\x63\x4f\
|
||||
//! \xdc\x42\x16\xbc\x16\xef\x7d\xac\x97\x0b\x0e\xe4\x6d\x69\x41\x6f\x9a\x9a\xce\xe6\x51\
|
||||
//! \xd1\x58\xac\x64\x91\x5b";
|
||||
//! let pietrzak_vdf = PietrzakVDFParams(2048).new();
|
||||
//! assert_eq!(
|
||||
//! &pietrzak_vdf.solve(b"\xaa", 100).unwrap()[..],
|
||||
//! CORRECT_SOLUTION
|
||||
//! );
|
||||
//! assert!(pietrzak_vdf.verify(b"\xaa", 100, CORRECT_SOLUTION).is_ok());
|
||||
//! }
|
||||
//! # extern crate vdf;
|
||||
//! # use vdf::{InvalidProof, PietrzakVDFParams, VDFParams, WesolowskiVDFParams, VDF};
|
||||
//! # fn main() {
|
||||
//! const CORRECT_SOLUTION: &[u8] =
|
||||
//! b"\x00\x52\x71\xe8\xf9\xab\x2e\xb8\xa2\x90\x6e\x85\x1d\xfc\xb5\x54\x2e\x41\x73\xf0\x16\
|
||||
//! \xb8\x5e\x29\xd4\x81\xa1\x08\xdc\x82\xed\x3b\x3f\x97\x93\x7b\x7a\xa8\x24\x80\x11\x38\
|
||||
//! \xd1\x77\x1d\xea\x8d\xae\x2f\x63\x97\xe7\x6a\x80\x61\x3a\xfd\xa3\x0f\x2c\x30\xa3\x4b\
|
||||
//! \x04\x0b\xaa\xaf\xe7\x6d\x57\x07\xd6\x86\x89\x19\x3e\x5d\x21\x18\x33\xb3\x72\xa6\xa4\
|
||||
//! \x59\x1a\xbb\x88\xe2\xe7\xf2\xf5\xa5\xec\x81\x8b\x57\x07\xb8\x6b\x8b\x2c\x49\x5c\xa1\
|
||||
//! \x58\x1c\x17\x91\x68\x50\x9e\x35\x93\xf9\xa1\x68\x79\x62\x0a\x4d\xc4\xe9\x07\xdf\x45\
|
||||
//! \x2e\x8d\xd0\xff\xc4\xf1\x99\x82\x5f\x54\xec\x70\x47\x2c\xc0\x61\xf2\x2e\xb5\x4c\x48\
|
||||
//! \xd6\xaa\x5a\xf3\xea\x37\x5a\x39\x2a\xc7\x72\x94\xe2\xd9\x55\xdd\xe1\xd1\x02\xae\x2a\
|
||||
//! \xce\x49\x42\x93\x49\x2d\x31\xcf\xf2\x19\x44\xa8\xbc\xb4\x60\x89\x93\x06\x5c\x9a\x00\
|
||||
//! \x29\x2e\x8d\x3f\x46\x04\xe7\x46\x5b\x4e\xee\xfb\x49\x4f\x5b\xea\x10\x2d\xb3\x43\xbb\
|
||||
//! \x61\xc5\xa1\x5c\x7b\xdf\x28\x82\x06\x88\x5c\x13\x0f\xa1\xf2\xd8\x6b\xf5\xe4\x63\x4f\
|
||||
//! \xdc\x42\x16\xbc\x16\xef\x7d\xac\x97\x0b\x0e\xe4\x6d\x69\x41\x6f\x9a\x9a\xce\xe6\x51\
|
||||
//! \xd1\x58\xac\x64\x91\x5b";
|
||||
//! let pietrzak_vdf = PietrzakVDFParams(2048).new();
|
||||
//! assert_eq!(
|
||||
//! &pietrzak_vdf.solve(b"\xaa", 100).unwrap()[..],
|
||||
//! CORRECT_SOLUTION
|
||||
//! );
|
||||
//! assert!(pietrzak_vdf.verify(b"\xaa", 100, CORRECT_SOLUTION).is_ok());
|
||||
//! # }
|
||||
//! ```
|
||||
extern crate classgroup;
|
||||
extern crate num_traits;
|
||||
|
@ -75,8 +74,10 @@ extern crate sha2;
|
|||
mod create_discriminant;
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub use self::proof_pietrzak::{PietrzakVDF, PietrzakVDFParams};
|
||||
pub use self::proof_wesolowski::{WesolowskiVDF, WesolowskiVDFParams};
|
||||
pub use self::{
|
||||
proof_pietrzak::{PietrzakVDF, PietrzakVDFParams},
|
||||
proof_wesolowski::{WesolowskiVDF, WesolowskiVDFParams},
|
||||
};
|
||||
|
||||
/// Message used to report an internal miscalculation of serialization buffer
|
||||
/// sizes.
|
||||
|
|
|
@ -12,15 +12,9 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
use super::proof_of_time::{deserialize_proof, iterate_squarings, serialize};
|
||||
use classgroup::gmp_classgroup::GmpClassGroup;
|
||||
use classgroup::{BigNumExt, ClassGroup};
|
||||
use classgroup::{gmp_classgroup::GmpClassGroup, BigNumExt, ClassGroup};
|
||||
use num_traits::{One, Zero};
|
||||
use std::fmt;
|
||||
use std::num::ParseIntError;
|
||||
use std::ops::Index;
|
||||
use std::str::FromStr;
|
||||
use std::u64;
|
||||
use std::usize;
|
||||
use std::{fmt, num::ParseIntError, ops::Index, str::FromStr, u64, usize};
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone, Debug)]
|
||||
pub struct Iterations(u64);
|
||||
|
|
|
@ -13,8 +13,7 @@
|
|||
// limitations under the License.
|
||||
|
||||
use super::proof_of_time::{iterate_squarings, serialize};
|
||||
use classgroup::gmp_classgroup::GmpClassGroup;
|
||||
use classgroup::{BigNum, BigNumExt, ClassGroup};
|
||||
use classgroup::{gmp_classgroup::GmpClassGroup, BigNum, BigNumExt, ClassGroup};
|
||||
use sha2::{digest::FixedOutput, Digest, Sha256};
|
||||
use std::{cmp::Eq, collections::HashMap, hash::Hash, mem, u64, usize};
|
||||
|
||||
|
@ -119,7 +118,7 @@ fn hash_prime<T: BigNum>(seed: &[&[u8]]) -> T {
|
|||
hasher.input(i);
|
||||
}
|
||||
let n = T::from(&hasher.fixed_result()[..16]);
|
||||
if n.probab_prime(13) {
|
||||
if n.probab_prime(2) {
|
||||
break n;
|
||||
}
|
||||
j += 1;
|
||||
|
|
Loading…
Reference in New Issue