feat: separate reading and writing to get infallible writers

Signed-off-by: Brandon H. Gomes <bhgomes@pm.me>
This commit is contained in:
Brandon H. Gomes 2022-11-02 21:07:51 -07:00
parent 7a81c5d46a
commit 4aaf57e9a9
No known key found for this signature in database
GPG Key ID: 773D44E6A904B222
14 changed files with 612 additions and 368 deletions

View File

@ -11,27 +11,17 @@ edition = "2021"
default-run = "generate_constants"
[features]
default = [
"gate_testing",
"parallel",
"rand",
"rand_chacha",
"std",
"timing",
]
rand = [
"dep:rand",
"num/rand",
"plonky2_field/rand"
]
default = ["gate_testing", "parallel", "rand", "rand_chacha", "std", "timing"]
rand = ["dep:rand", "num/rand", "plonky2_field/rand"]
gate_testing = ["rand"]
parallel = ["maybe_rayon/parallel"]
std = ["anyhow/std"]
parallel = ["hashbrown/rayon", "maybe_rayon/parallel"]
std = ["anyhow/std", "rand/std"]
timing = []
[dependencies]
anyhow = { version = "1.0.40", default-features = false }
derivative = { version = "2.2.0", default-features = false, features = ["use_core"] }
hashbrown = { version = "0.12.3", default-features = false, features = ["ahash", "serde"] }
itertools = { version = "0.10.0", default-features = false }
keccak-hash = { version = "0.8.0", default-features = false }
log = { version = "0.4.14", default-features = false }

View File

@ -1,7 +1,7 @@
use alloc::vec;
use alloc::vec::Vec;
use std::collections::HashMap;
use hashbrown::HashMap;
use itertools::izip;
use plonky2_field::extension::{flatten, unflatten, Extendable};
use plonky2_field::polynomial::PolynomialCoeffs;

View File

@ -6,8 +6,8 @@ use alloc::vec::Vec;
use core::fmt::{Debug, Error, Formatter};
use core::hash::{Hash, Hasher};
use core::ops::Range;
use std::collections::HashMap;
use hashbrown::HashMap;
use plonky2_field::batch_util::batch_multiply_inplace;
use plonky2_field::extension::{Extendable, FieldExtension};
use plonky2_field::types::Field;

View File

@ -9,7 +9,7 @@ use keccak_hash::keccak;
use crate::hash::hash_types::{BytesHash, RichField};
use crate::hash::hashing::{PlonkyPermutation, SPONGE_WIDTH};
use crate::plonk::config::Hasher;
use crate::util::serialization::Buffer;
use crate::util::serialization::Write;
/// Keccak-256 pseudo-permutation (not necessarily one-to-one) used in the challenger.
/// A state `input: [F; 12]` is sent to the field representation of `H(input) || H(H(input)) || H(H(H(input)))`
@ -53,16 +53,17 @@ impl<F: RichField> PlonkyPermutation<F> for KeccakPermutation {
/// Keccak-256 hash function.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct KeccakHash<const N: usize>;
impl<F: RichField, const N: usize> Hasher<F> for KeccakHash<N> {
const HASH_SIZE: usize = N;
type Hash = BytesHash<N>;
type Permutation = KeccakPermutation;
fn hash_no_pad(input: &[F]) -> Self::Hash {
let mut buffer = Buffer::new(Vec::new());
let mut buffer = Vec::new();
buffer.write_field_vec(input).unwrap();
let mut arr = [0; N];
let hash_bytes = keccak(buffer.bytes()).0;
let hash_bytes = keccak(buffer).0;
arr.copy_from_slice(&hash_bytes[..N]);
BytesHash(arr)
}

View File

@ -1,7 +1,7 @@
use alloc::vec;
use alloc::vec::Vec;
use std::collections::HashMap;
use hashbrown::HashMap;
use num::Integer;
use crate::hash::hash_types::RichField;

View File

@ -1,7 +1,7 @@
use alloc::vec;
use alloc::vec::Vec;
use std::collections::HashMap;
use hashbrown::HashMap;
use itertools::Itertools;
use plonky2_field::extension::{Extendable, FieldExtension};
use plonky2_field::types::Field;

View File

@ -3,9 +3,10 @@ use alloc::collections::BTreeMap;
use alloc::vec;
use alloc::vec::Vec;
use core::cmp::max;
use std::collections::{HashMap, HashSet};
#[cfg(feature = "std")]
use std::time::Instant;
use hashbrown::{HashMap, HashSet};
use itertools::Itertools;
use log::{debug, info, Level};
use plonky2_field::cosets::get_unique_coset_shifts;
@ -692,6 +693,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
/// Builds a "full circuit", with both prover and verifier data.
pub fn build<C: GenericConfig<D, F = F>>(mut self) -> CircuitData<F, C, D> {
let mut timing = TimingTree::new("preprocess", Level::Trace);
#[cfg(feature = "std")]
let start = Instant::now();
let rate_bits = self.config.fri_config.rate_bits;
let cap_height = self.config.fri_config.cap_height;
@ -879,6 +881,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
};
timing.print();
#[cfg(feature = "std")]
debug!("Building circuit took {}s", start.elapsed().as_secs_f32());
CircuitData {
prover_only,

View File

@ -1,7 +1,7 @@
use alloc::vec;
use alloc::vec::Vec;
use std::collections::HashSet;
use hashbrown::HashSet;
use plonky2_field::extension::Extendable;
use plonky2_field::polynomial::PolynomialCoeffs;

View File

@ -1,6 +1,6 @@
use alloc::vec::Vec;
use std::collections::HashMap;
use hashbrown::HashMap;
use maybe_rayon::*;
use plonky2_field::polynomial::PolynomialValues;
use plonky2_field::types::Field;

View File

@ -21,7 +21,9 @@ use crate::iop::target::Target;
use crate::plonk::circuit_data::{CommonCircuitData, VerifierOnlyCircuitData};
use crate::plonk::config::{GenericConfig, Hasher};
use crate::plonk::verifier::verify_with_challenges;
use crate::util::serialization::Buffer;
use crate::util::serialization::Write;
#[cfg(feature = "std")]
use crate::util::serialization::{Buffer, Read};
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
#[serde(bound = "")]
@ -101,12 +103,13 @@ impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
C::InnerHasher::hash_no_pad(&self.public_inputs)
}
pub fn to_bytes(&self) -> anyhow::Result<Vec<u8>> {
let mut buffer = Buffer::new(Vec::new());
buffer.write_proof_with_public_inputs(self)?;
Ok(buffer.bytes())
pub fn to_bytes(&self) -> Vec<u8> {
let mut buffer = Vec::new();
let _ = buffer.write_proof_with_public_inputs(self);
buffer
}
#[cfg(feature = "std")]
pub fn from_bytes(
bytes: Vec<u8>,
common_data: &CommonCircuitData<F, D>,
@ -226,11 +229,10 @@ impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
C::InnerHasher::hash_no_pad(&self.public_inputs)
}
#[cfg(feature = "std")]
pub fn to_bytes(&self) -> anyhow::Result<Vec<u8>> {
let mut buffer = Buffer::new(Vec::new());
buffer.write_compressed_proof_with_public_inputs(self)?;
Ok(buffer.bytes())
pub fn to_bytes(&self) -> Vec<u8> {
let mut buffer = Vec::new();
let _ = buffer.write_compressed_proof_with_public_inputs(self);
buffer
}
#[cfg(feature = "std")]

View File

@ -1,4 +1,4 @@
use alloc::string::String;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;

View File

@ -6,13 +6,12 @@ use plonky2_field::types::Field;
pub(crate) mod context_tree;
pub(crate) mod partial_products;
pub mod reducing;
pub mod serialization;
pub mod strided_view;
pub mod timing;
#[cfg(feature = "std")]
pub mod serialization;
pub(crate) fn transpose_poly_values<F: Field>(polys: Vec<PolynomialValues<F>>) -> Vec<Vec<F>> {
let poly_values = polys.into_iter().map(|p| p.values).collect::<Vec<_>>();
transpose(&poly_values)

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,7 @@ extern crate alloc;
use alloc::vec::Vec;
use core::arch::asm;
use core::convert::Infallible;
use core::hint::unreachable_unchecked;
use core::mem::size_of;
use core::ptr::{swap, swap_nonoverlapping};
@ -18,6 +19,15 @@ use crate::transpose_util::transpose_in_place_square;
mod transpose_util;
/// Converts `result` into the [`Ok`] variant of [`Result`].
#[inline]
pub fn into_ok<T>(result: Result<T, Infallible>) -> T {
match result {
Ok(value) => value,
_ => unreachable!("The `Infallible` value cannot be constructed."),
}
}
pub fn bits_u64(n: u64) -> usize {
(64 - n.leading_zeros()) as usize
}