This commit is contained in:
Daniel Lubarov 2021-04-02 14:00:26 -07:00
parent 8565e5015d
commit aea4eeaaae
7 changed files with 44 additions and 10 deletions

View File

@ -109,6 +109,10 @@ impl<F: Field> CircuitBuilder<F> {
Target::Wire(Wire { gate, input: ConstantGate::WIRE_OUTPUT })
}
pub fn constants(&mut self, constants: &[F]) -> Vec<Target> {
constants.iter().map(|&c| self.constant(c)).collect()
}
pub fn permute(&mut self, inputs: [Target; 12]) -> [Target; 12] {
todo!()
}

View File

@ -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)
}

View File

@ -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<R: Rng>(rng: &mut R) -> Self {
Self::from_canonical_u64(rng.gen_range(0, Self::ORDER))
}
fn rand() -> Self {
Self::rand_from_rng(&mut OsRng)
}
}

View File

@ -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;

View File

@ -52,6 +52,8 @@ fn main() {
bench_prove::<CrandallField>();
// bench_field_mul::<CrandallField>();
// bench_fft();
println!();
// bench_gmimc::<CrandallField>();
@ -59,6 +61,20 @@ fn main() {
// field_search()
}
fn bench_field_mul<F: Field>() {
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<F: Field>() {
let mut gmimc_constants = [F::ZERO; GMIMC_ROUNDS];
for i in 0..GMIMC_ROUNDS {

View File

@ -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::<Vec<_>>())
.collect();
let mut challenger = Challenger::new(128);
let mut challenger = Challenger::new();
let mut outputs_per_round: Vec<Vec<F>> = 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<Target>> =
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]),
);

View File

@ -28,6 +28,12 @@ impl<F: Field> PartialWitness<F> {
pub fn get_target(&self, target: Target) -> F {
self.target_values[&target]
}
pub fn get_targets(&self, targets: &[Target]) -> Vec<F> {
targets.iter()
.map(|&t| self.get_target(t))
.collect()
}
pub fn try_get_target(&self, target: Target) -> Option<F> {