mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-04 06:43:07 +00:00
feat: separate reading and writing to get infallible writers
Signed-off-by: Brandon H. Gomes <bhgomes@pm.me>
This commit is contained in:
parent
7a81c5d46a
commit
4aaf57e9a9
@ -11,27 +11,17 @@ edition = "2021"
|
|||||||
default-run = "generate_constants"
|
default-run = "generate_constants"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = [
|
default = ["gate_testing", "parallel", "rand", "rand_chacha", "std", "timing"]
|
||||||
"gate_testing",
|
rand = ["dep:rand", "num/rand", "plonky2_field/rand"]
|
||||||
"parallel",
|
|
||||||
"rand",
|
|
||||||
"rand_chacha",
|
|
||||||
"std",
|
|
||||||
"timing",
|
|
||||||
]
|
|
||||||
rand = [
|
|
||||||
"dep:rand",
|
|
||||||
"num/rand",
|
|
||||||
"plonky2_field/rand"
|
|
||||||
]
|
|
||||||
gate_testing = ["rand"]
|
gate_testing = ["rand"]
|
||||||
parallel = ["maybe_rayon/parallel"]
|
parallel = ["hashbrown/rayon", "maybe_rayon/parallel"]
|
||||||
std = ["anyhow/std"]
|
std = ["anyhow/std", "rand/std"]
|
||||||
timing = []
|
timing = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = { version = "1.0.40", default-features = false }
|
anyhow = { version = "1.0.40", default-features = false }
|
||||||
derivative = { version = "2.2.0", default-features = false, features = ["use_core"] }
|
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 }
|
itertools = { version = "0.10.0", default-features = false }
|
||||||
keccak-hash = { version = "0.8.0", default-features = false }
|
keccak-hash = { version = "0.8.0", default-features = false }
|
||||||
log = { version = "0.4.14", default-features = false }
|
log = { version = "0.4.14", default-features = false }
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
use alloc::vec;
|
use alloc::vec;
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
|
use hashbrown::HashMap;
|
||||||
use itertools::izip;
|
use itertools::izip;
|
||||||
use plonky2_field::extension::{flatten, unflatten, Extendable};
|
use plonky2_field::extension::{flatten, unflatten, Extendable};
|
||||||
use plonky2_field::polynomial::PolynomialCoeffs;
|
use plonky2_field::polynomial::PolynomialCoeffs;
|
||||||
|
|||||||
@ -6,8 +6,8 @@ use alloc::vec::Vec;
|
|||||||
use core::fmt::{Debug, Error, Formatter};
|
use core::fmt::{Debug, Error, Formatter};
|
||||||
use core::hash::{Hash, Hasher};
|
use core::hash::{Hash, Hasher};
|
||||||
use core::ops::Range;
|
use core::ops::Range;
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
|
use hashbrown::HashMap;
|
||||||
use plonky2_field::batch_util::batch_multiply_inplace;
|
use plonky2_field::batch_util::batch_multiply_inplace;
|
||||||
use plonky2_field::extension::{Extendable, FieldExtension};
|
use plonky2_field::extension::{Extendable, FieldExtension};
|
||||||
use plonky2_field::types::Field;
|
use plonky2_field::types::Field;
|
||||||
|
|||||||
@ -9,7 +9,7 @@ use keccak_hash::keccak;
|
|||||||
use crate::hash::hash_types::{BytesHash, RichField};
|
use crate::hash::hash_types::{BytesHash, RichField};
|
||||||
use crate::hash::hashing::{PlonkyPermutation, SPONGE_WIDTH};
|
use crate::hash::hashing::{PlonkyPermutation, SPONGE_WIDTH};
|
||||||
use crate::plonk::config::Hasher;
|
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.
|
/// 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)))`
|
/// 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.
|
/// Keccak-256 hash function.
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||||
pub struct KeccakHash<const N: usize>;
|
pub struct KeccakHash<const N: usize>;
|
||||||
|
|
||||||
impl<F: RichField, const N: usize> Hasher<F> for KeccakHash<N> {
|
impl<F: RichField, const N: usize> Hasher<F> for KeccakHash<N> {
|
||||||
const HASH_SIZE: usize = N;
|
const HASH_SIZE: usize = N;
|
||||||
type Hash = BytesHash<N>;
|
type Hash = BytesHash<N>;
|
||||||
type Permutation = KeccakPermutation;
|
type Permutation = KeccakPermutation;
|
||||||
|
|
||||||
fn hash_no_pad(input: &[F]) -> Self::Hash {
|
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();
|
buffer.write_field_vec(input).unwrap();
|
||||||
let mut arr = [0; N];
|
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]);
|
arr.copy_from_slice(&hash_bytes[..N]);
|
||||||
BytesHash(arr)
|
BytesHash(arr)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
use alloc::vec;
|
use alloc::vec;
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
|
use hashbrown::HashMap;
|
||||||
use num::Integer;
|
use num::Integer;
|
||||||
|
|
||||||
use crate::hash::hash_types::RichField;
|
use crate::hash::hash_types::RichField;
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
use alloc::vec;
|
use alloc::vec;
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
|
use hashbrown::HashMap;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use plonky2_field::extension::{Extendable, FieldExtension};
|
use plonky2_field::extension::{Extendable, FieldExtension};
|
||||||
use plonky2_field::types::Field;
|
use plonky2_field::types::Field;
|
||||||
|
|||||||
@ -3,9 +3,10 @@ use alloc::collections::BTreeMap;
|
|||||||
use alloc::vec;
|
use alloc::vec;
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use core::cmp::max;
|
use core::cmp::max;
|
||||||
use std::collections::{HashMap, HashSet};
|
#[cfg(feature = "std")]
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use hashbrown::{HashMap, HashSet};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use log::{debug, info, Level};
|
use log::{debug, info, Level};
|
||||||
use plonky2_field::cosets::get_unique_coset_shifts;
|
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.
|
/// Builds a "full circuit", with both prover and verifier data.
|
||||||
pub fn build<C: GenericConfig<D, F = F>>(mut self) -> CircuitData<F, C, D> {
|
pub fn build<C: GenericConfig<D, F = F>>(mut self) -> CircuitData<F, C, D> {
|
||||||
let mut timing = TimingTree::new("preprocess", Level::Trace);
|
let mut timing = TimingTree::new("preprocess", Level::Trace);
|
||||||
|
#[cfg(feature = "std")]
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let rate_bits = self.config.fri_config.rate_bits;
|
let rate_bits = self.config.fri_config.rate_bits;
|
||||||
let cap_height = self.config.fri_config.cap_height;
|
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();
|
timing.print();
|
||||||
|
#[cfg(feature = "std")]
|
||||||
debug!("Building circuit took {}s", start.elapsed().as_secs_f32());
|
debug!("Building circuit took {}s", start.elapsed().as_secs_f32());
|
||||||
CircuitData {
|
CircuitData {
|
||||||
prover_only,
|
prover_only,
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
use alloc::vec;
|
use alloc::vec;
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
|
use hashbrown::HashSet;
|
||||||
use plonky2_field::extension::Extendable;
|
use plonky2_field::extension::Extendable;
|
||||||
use plonky2_field::polynomial::PolynomialCoeffs;
|
use plonky2_field::polynomial::PolynomialCoeffs;
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
|
use hashbrown::HashMap;
|
||||||
use maybe_rayon::*;
|
use maybe_rayon::*;
|
||||||
use plonky2_field::polynomial::PolynomialValues;
|
use plonky2_field::polynomial::PolynomialValues;
|
||||||
use plonky2_field::types::Field;
|
use plonky2_field::types::Field;
|
||||||
|
|||||||
@ -21,7 +21,9 @@ use crate::iop::target::Target;
|
|||||||
use crate::plonk::circuit_data::{CommonCircuitData, VerifierOnlyCircuitData};
|
use crate::plonk::circuit_data::{CommonCircuitData, VerifierOnlyCircuitData};
|
||||||
use crate::plonk::config::{GenericConfig, Hasher};
|
use crate::plonk::config::{GenericConfig, Hasher};
|
||||||
use crate::plonk::verifier::verify_with_challenges;
|
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)]
|
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
|
||||||
#[serde(bound = "")]
|
#[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)
|
C::InnerHasher::hash_no_pad(&self.public_inputs)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_bytes(&self) -> anyhow::Result<Vec<u8>> {
|
pub fn to_bytes(&self) -> Vec<u8> {
|
||||||
let mut buffer = Buffer::new(Vec::new());
|
let mut buffer = Vec::new();
|
||||||
buffer.write_proof_with_public_inputs(self)?;
|
let _ = buffer.write_proof_with_public_inputs(self);
|
||||||
Ok(buffer.bytes())
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
pub fn from_bytes(
|
pub fn from_bytes(
|
||||||
bytes: Vec<u8>,
|
bytes: Vec<u8>,
|
||||||
common_data: &CommonCircuitData<F, D>,
|
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)
|
C::InnerHasher::hash_no_pad(&self.public_inputs)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
pub fn to_bytes(&self) -> Vec<u8> {
|
||||||
pub fn to_bytes(&self) -> anyhow::Result<Vec<u8>> {
|
let mut buffer = Vec::new();
|
||||||
let mut buffer = Buffer::new(Vec::new());
|
let _ = buffer.write_compressed_proof_with_public_inputs(self);
|
||||||
buffer.write_compressed_proof_with_public_inputs(self)?;
|
buffer
|
||||||
Ok(buffer.bytes())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use alloc::string::String;
|
use alloc::string::{String, ToString};
|
||||||
use alloc::vec;
|
use alloc::vec;
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
|
|||||||
@ -6,13 +6,12 @@ use plonky2_field::types::Field;
|
|||||||
|
|
||||||
pub(crate) mod context_tree;
|
pub(crate) mod context_tree;
|
||||||
pub(crate) mod partial_products;
|
pub(crate) mod partial_products;
|
||||||
|
|
||||||
pub mod reducing;
|
pub mod reducing;
|
||||||
|
pub mod serialization;
|
||||||
pub mod strided_view;
|
pub mod strided_view;
|
||||||
pub mod timing;
|
pub mod timing;
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub mod serialization;
|
|
||||||
|
|
||||||
pub(crate) fn transpose_poly_values<F: Field>(polys: Vec<PolynomialValues<F>>) -> Vec<Vec<F>> {
|
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<_>>();
|
let poly_values = polys.into_iter().map(|p| p.values).collect::<Vec<_>>();
|
||||||
transpose(&poly_values)
|
transpose(&poly_values)
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -10,6 +10,7 @@ extern crate alloc;
|
|||||||
|
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use core::arch::asm;
|
use core::arch::asm;
|
||||||
|
use core::convert::Infallible;
|
||||||
use core::hint::unreachable_unchecked;
|
use core::hint::unreachable_unchecked;
|
||||||
use core::mem::size_of;
|
use core::mem::size_of;
|
||||||
use core::ptr::{swap, swap_nonoverlapping};
|
use core::ptr::{swap, swap_nonoverlapping};
|
||||||
@ -18,6 +19,15 @@ use crate::transpose_util::transpose_in_place_square;
|
|||||||
|
|
||||||
mod transpose_util;
|
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 {
|
pub fn bits_u64(n: u64) -> usize {
|
||||||
(64 - n.leading_zeros()) as usize
|
(64 - n.leading_zeros()) as usize
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user