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
This commit is contained in:
Daniel Lubarov 2021-08-20 08:44:28 -07:00 committed by GitHub
parent c1b8a4b4a7
commit 535c385643
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 8 deletions

View File

@ -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<R: 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<R: Rng>(rng: &mut R) -> Self {
Self::from_canonical_u64(rng.gen_range(0..FIELD_ORDER))
}
}
impl Neg for CrandallField {

View File

@ -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.<x> = GF(p)[]; assert (x^2 -3).is_irreducible()`.

View File

@ -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.<x> = GF(p)[]

View File

@ -31,6 +31,7 @@ pub trait Field:
+ Div<Self, Output = Self>
+ DivAssign<Self>
+ Debug
+ Default
+ Display
+ Send
+ Sync

View File

@ -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<F: Field> {
pub(crate) elements: [F; 4],
pub elements: [F; 4],
}
impl<F: Field> HashOut<F> {
pub const ZERO: Self = Self {
elements: [F::ZERO; 4],
};
pub(crate) fn from_vec(elements: Vec<F>) -> Self {
debug_assert!(elements.len() == 4);
Self {
@ -30,6 +35,17 @@ impl<F: Field> HashOut<F> {
}
}
pub fn rand_from_rng<R: 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<F: Field> HashOut<F> {
}
}
impl<F: Field> Default for HashOut<F> {
fn default() -> Self {
Self::ZERO
}
}
/// Represents a ~256 bit hash output.
#[derive(Copy, Clone, Debug)]
pub struct HashOutTarget {

View File

@ -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<F: Extendable<D>, const D: usize> ProverCircuitData<F, D> {
}
/// Circuit data required by the prover.
#[derive(Debug)]
pub struct VerifierCircuitData<F: Extendable<D>, const D: usize> {
pub(crate) verifier_only: VerifierOnlyCircuitData<F>,
pub(crate) common: CommonCircuitData<F, D>,
@ -160,12 +161,14 @@ pub(crate) struct ProverOnlyCircuitData<F: Extendable<D>, const D: usize> {
}
/// Circuit data required by the verifier, but not the prover.
#[derive(Debug)]
pub(crate) struct VerifierOnlyCircuitData<F: Field> {
/// A commitment to each constant polynomial and each permutation polynomial.
pub(crate) constants_sigmas_cap: MerkleCap<F>,
}
/// Circuit data required by both the prover and the verifier.
#[derive(Debug)]
pub struct CommonCircuitData<F: Extendable<D>, const D: usize> {
pub(crate) config: CircuitConfig,