From 535c385643e41e48ed218734e9ee3b8277119c47 Mon Sep 17 00:00:00 2001 From: Daniel Lubarov Date: Fri, 20 Aug 2021 08:44:28 -0700 Subject: [PATCH] Field: Default (#193) * Field: Default It's done for primitive types like `u64`, so seems conventional, and some code in mir-core expects it. * HashOut::ZERO * Default for HashOut * fmt * pub elements * Debug * rand_from_rng --- src/field/crandall_field.rs | 16 +++++++++++----- src/field/extension_field/quadratic.rs | 6 ++++++ src/field/extension_field/quartic.rs | 6 ++++++ src/field/field_types.rs | 1 + src/hash/hash_types.rs | 26 ++++++++++++++++++++++++-- src/plonk/circuit_data.rs | 5 ++++- 6 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/field/crandall_field.rs b/src/field/crandall_field.rs index fba11677..7e7e248b 100644 --- a/src/field/crandall_field.rs +++ b/src/field/crandall_field.rs @@ -114,6 +114,12 @@ const CAUCHY_MDS_8: [[CrandallField; 8]; 8] = [ #[derive(Copy, Clone, Serialize, Deserialize)] pub struct CrandallField(pub u64); +impl Default for CrandallField { + fn default() -> Self { + Self::ZERO + } +} + impl PartialEq for CrandallField { fn eq(&self, other: &Self) -> bool { self.to_canonical_u64() == other.to_canonical_u64() @@ -148,8 +154,8 @@ impl Field for CrandallField { const TWO: Self = Self(2); const NEG_ONE: Self = Self(FIELD_ORDER - 1); - const TWO_ADICITY: usize = 28; const CHARACTERISTIC: u64 = FIELD_ORDER; + const TWO_ADICITY: usize = 28; const MULTIPLICATIVE_GROUP_GENERATOR: Self = Self(5); const POWER_OF_TWO_GENERATOR: Self = Self(10281950781551402419); @@ -254,6 +260,10 @@ impl Field for CrandallField { Self(n.iter_u64_digits().next().unwrap_or(0)) } + fn rand_from_rng(rng: &mut R) -> Self { + Self::from_canonical_u64(rng.gen_range(0..FIELD_ORDER)) + } + fn cube_root(&self) -> Self { let x0 = *self; let x1 = x0.square(); @@ -341,10 +351,6 @@ impl Field for CrandallField { } result } - - fn rand_from_rng(rng: &mut R) -> Self { - Self::from_canonical_u64(rng.gen_range(0..FIELD_ORDER)) - } } impl Neg for CrandallField { diff --git a/src/field/extension_field/quadratic.rs b/src/field/extension_field/quadratic.rs index 1d31a0be..07491ad7 100644 --- a/src/field/extension_field/quadratic.rs +++ b/src/field/extension_field/quadratic.rs @@ -13,6 +13,12 @@ use crate::field::field_types::Field; #[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] pub struct QuadraticCrandallField([CrandallField; 2]); +impl Default for QuadraticCrandallField { + fn default() -> Self { + Self::ZERO + } +} + impl OEF<2> for QuadraticCrandallField { // Verifiable in Sage with // ``R. = GF(p)[]; assert (x^2 -3).is_irreducible()`. diff --git a/src/field/extension_field/quartic.rs b/src/field/extension_field/quartic.rs index 2c111627..e8f3a1f5 100644 --- a/src/field/extension_field/quartic.rs +++ b/src/field/extension_field/quartic.rs @@ -15,6 +15,12 @@ use crate::field::field_types::Field; #[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] pub struct QuarticCrandallField(pub(crate) [CrandallField; 4]); +impl Default for QuarticCrandallField { + fn default() -> Self { + Self::ZERO + } +} + impl OEF<4> for QuarticCrandallField { // Verifiable in Sage with // R. = GF(p)[] diff --git a/src/field/field_types.rs b/src/field/field_types.rs index 8d2f8872..cd528823 100644 --- a/src/field/field_types.rs +++ b/src/field/field_types.rs @@ -31,6 +31,7 @@ pub trait Field: + Div + DivAssign + Debug + + Default + Display + Send + Sync diff --git a/src/hash/hash_types.rs b/src/hash/hash_types.rs index 56cb371f..eb2f16b0 100644 --- a/src/hash/hash_types.rs +++ b/src/hash/hash_types.rs @@ -1,18 +1,23 @@ use std::convert::TryInto; +use rand::Rng; use serde::{Deserialize, Serialize}; use crate::field::field_types::Field; use crate::iop::target::Target; /// Represents a ~256 bit hash output. -#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] #[serde(bound = "")] pub struct HashOut { - pub(crate) elements: [F; 4], + pub elements: [F; 4], } impl HashOut { + pub const ZERO: Self = Self { + elements: [F::ZERO; 4], + }; + pub(crate) fn from_vec(elements: Vec) -> Self { debug_assert!(elements.len() == 4); Self { @@ -30,6 +35,17 @@ impl HashOut { } } + pub fn rand_from_rng(rng: &mut R) -> Self { + Self { + elements: [ + F::rand_from_rng(rng), + F::rand_from_rng(rng), + F::rand_from_rng(rng), + F::rand_from_rng(rng), + ], + } + } + pub fn rand() -> Self { Self { elements: [F::rand(), F::rand(), F::rand(), F::rand()], @@ -37,6 +53,12 @@ impl HashOut { } } +impl Default for HashOut { + fn default() -> Self { + Self::ZERO + } +} + /// Represents a ~256 bit hash output. #[derive(Copy, Clone, Debug)] pub struct HashOutTarget { diff --git a/src/plonk/circuit_data.rs b/src/plonk/circuit_data.rs index d711b45f..21ab39d6 100644 --- a/src/plonk/circuit_data.rs +++ b/src/plonk/circuit_data.rs @@ -18,7 +18,7 @@ use crate::plonk::prover::prove; use crate::plonk::verifier::verify; use crate::util::marking::MarkedTargets; -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct CircuitConfig { pub num_wires: usize, pub num_routed_wires: usize, @@ -127,6 +127,7 @@ impl, const D: usize> ProverCircuitData { } /// Circuit data required by the prover. +#[derive(Debug)] pub struct VerifierCircuitData, const D: usize> { pub(crate) verifier_only: VerifierOnlyCircuitData, pub(crate) common: CommonCircuitData, @@ -160,12 +161,14 @@ pub(crate) struct ProverOnlyCircuitData, const D: usize> { } /// Circuit data required by the verifier, but not the prover. +#[derive(Debug)] pub(crate) struct VerifierOnlyCircuitData { /// A commitment to each constant polynomial and each permutation polynomial. pub(crate) constants_sigmas_cap: MerkleCap, } /// Circuit data required by both the prover and the verifier. +#[derive(Debug)] pub struct CommonCircuitData, const D: usize> { pub(crate) config: CircuitConfig,