From aea4eeaaae54e9628956040631f1b5f5259d175c Mon Sep 17 00:00:00 2001 From: Daniel Lubarov Date: Fri, 2 Apr 2021 14:00:26 -0700 Subject: [PATCH] Minor --- src/circuit_builder.rs | 4 ++++ src/field/crandall_field.rs | 14 ++++++++------ src/field/field.rs | 5 +++++ src/gates/gmimc.rs | 1 - src/main.rs | 16 ++++++++++++++++ src/plonk_challenger.rs | 8 +++++--- src/witness.rs | 6 ++++++ 7 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/circuit_builder.rs b/src/circuit_builder.rs index fce4c641..431e7427 100644 --- a/src/circuit_builder.rs +++ b/src/circuit_builder.rs @@ -109,6 +109,10 @@ impl CircuitBuilder { Target::Wire(Wire { gate, input: ConstantGate::WIRE_OUTPUT }) } + pub fn constants(&mut self, constants: &[F]) -> Vec { + constants.iter().map(|&c| self.constant(c)).collect() + } + pub fn permute(&mut self, inputs: [Target; 12]) -> [Target; 12] { todo!() } diff --git a/src/field/crandall_field.rs b/src/field/crandall_field.rs index f4f61699..662a1864 100644 --- a/src/field/crandall_field.rs +++ b/src/field/crandall_field.rs @@ -54,12 +54,12 @@ impl Field for CrandallField { const ORDER: u64 = 18446744071293632513; const MULTIPLICATIVE_SUBGROUP_GENERATOR: Self = Self(5); // TODO: Double check. - #[inline(always)] + #[inline] fn sq(&self) -> Self { *self * *self } - #[inline(always)] + #[inline] fn cube(&self) -> Self { *self * *self * *self } @@ -132,12 +132,12 @@ impl Field for CrandallField { subgroup } - #[inline(always)] + #[inline] fn to_canonical_u64(&self) -> u64 { self.0 } - #[inline(always)] + #[inline] fn from_canonical_u64(n: u64) -> Self { Self(n) } @@ -180,6 +180,7 @@ impl Sub for CrandallField { } impl SubAssign for CrandallField { + #[inline] fn sub_assign(&mut self, rhs: Self) { *self = *self - rhs; } @@ -195,6 +196,7 @@ impl Mul for CrandallField { } impl MulAssign for CrandallField { + #[inline] fn mul_assign(&mut self, rhs: Self) { *self = *self * rhs; } @@ -215,7 +217,7 @@ impl DivAssign for CrandallField { } /// no final reduction -#[inline(always)] +#[inline] fn reduce128(x: u128) -> CrandallField { // This is Crandall's algorithm. When we have some high-order bits (i.e. with a weight of 2^64), // we convert them to low-order bits by multiplying by EPSILON (the logic is a simple @@ -229,7 +231,7 @@ fn reduce128(x: u128) -> CrandallField { CrandallField(lo_2) + CrandallField(lo_3) } -#[inline(always)] +#[inline] fn split(x: u128) -> (u64, u64) { (x as u64, (x >> 64) as u64) } diff --git a/src/field/field.rs b/src/field/field.rs index 0037783e..59c94c64 100644 --- a/src/field/field.rs +++ b/src/field/field.rs @@ -1,6 +1,7 @@ use std::fmt::{Debug, Display}; use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use rand::Rng; +use rand::rngs::OsRng; /// A finite field with prime order less than 2^64. pub trait Field: 'static @@ -115,4 +116,8 @@ pub trait Field: 'static fn rand_from_rng(rng: &mut R) -> Self { Self::from_canonical_u64(rng.gen_range(0, Self::ORDER)) } + + fn rand() -> Self { + Self::rand_from_rng(&mut OsRng) + } } diff --git a/src/gates/gmimc.rs b/src/gates/gmimc.rs index 72b78e4b..f9992514 100644 --- a/src/gates/gmimc.rs +++ b/src/gates/gmimc.rs @@ -212,7 +212,6 @@ mod tests { use crate::circuit_data::CircuitConfig; use crate::field::crandall_field::CrandallField; use crate::field::field::Field; - use crate::gates::deterministic_gate::DeterministicGate; use crate::gates::gmimc::{GMiMCGate, W}; use crate::generator::generate_partial_witness; use crate::gmimc::gmimc_permute_naive; diff --git a/src/main.rs b/src/main.rs index c8823647..0043e77f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,6 +52,8 @@ fn main() { bench_prove::(); + // bench_field_mul::(); + // bench_fft(); println!(); // bench_gmimc::(); @@ -59,6 +61,20 @@ fn main() { // field_search() } +fn bench_field_mul() { + let m = F::from_canonical_u64(12345678901234567890); + let mut x = F::ONE; + let start = Instant::now(); + let num_muls = 2000000000; + for _ in 0..num_muls { + x *= m; + } + let duration = start.elapsed(); + println!("result {:?}", x); + println!("took {:?}", duration); + println!("avg {:?}ns", duration.as_secs_f64() * 1e9 / (num_muls as f64)); +} + fn bench_prove() { let mut gmimc_constants = [F::ZERO; GMIMC_ROUNDS]; for i in 0..GMIMC_ROUNDS { diff --git a/src/plonk_challenger.rs b/src/plonk_challenger.rs index 1c5ab14d..2bc6f3de 100644 --- a/src/plonk_challenger.rs +++ b/src/plonk_challenger.rs @@ -194,12 +194,14 @@ impl RecursiveChallenger { #[cfg(test)] mod tests { - use crate::{CircuitBuilder, Curve, Field, PartialWitness, Target, Tweedledum}; use crate::circuit_data::CircuitConfig; use crate::field::crandall_field::CrandallField; use crate::generator::generate_partial_witness; use crate::plonk_challenger::{Challenger, RecursiveChallenger}; use crate::target::Target; + use crate::circuit_builder::CircuitBuilder; + use crate::witness::PartialWitness; + use crate::field::field::Field; /// Tests for consistency between `Challenger` and `RecursiveChallenger`. #[test] @@ -217,7 +219,7 @@ mod tests { .map(|&n| (0..n).map(|_| F::rand()).collect::>()) .collect(); - let mut challenger = Challenger::new(128); + let mut challenger = Challenger::new(); let mut outputs_per_round: Vec> = Vec::new(); for (r, inputs) in inputs_per_round.iter().enumerate() { challenger.observe_elements(inputs); @@ -230,7 +232,7 @@ mod tests { let mut recursive_outputs_per_round: Vec> = Vec::new(); for (r, inputs) in inputs_per_round.iter().enumerate() { - recursive_challenger.observe_elements(&builder.constant_wires(inputs)); + recursive_challenger.observe_elements(&builder.constants(inputs)); recursive_outputs_per_round.push( recursive_challenger.get_n_challenges(&mut builder, num_outputs_per_round[r]), ); diff --git a/src/witness.rs b/src/witness.rs index 5f6e803b..ebd68797 100644 --- a/src/witness.rs +++ b/src/witness.rs @@ -28,6 +28,12 @@ impl PartialWitness { pub fn get_target(&self, target: Target) -> F { self.target_values[&target] +} + + pub fn get_targets(&self, targets: &[Target]) -> Vec { + targets.iter() + .map(|&t| self.get_target(t)) + .collect() } pub fn try_get_target(&self, target: Target) -> Option {