diff --git a/crates/compiler/Cargo.toml b/crates/compiler/Cargo.toml index 4d094034..7212f530 100644 --- a/crates/compiler/Cargo.toml +++ b/crates/compiler/Cargo.toml @@ -31,6 +31,7 @@ p3-poseidon2-air.workspace = true lookup.workspace = true sumcheck.workspace = true vm.workspace = true +thiserror.workspace = true [dev-dependencies] hashsig.workspace = true diff --git a/crates/compiler/src/parser.rs b/crates/compiler/src/parser.rs index b58630ac..57ed1660 100644 --- a/crates/compiler/src/parser.rs +++ b/crates/compiler/src/parser.rs @@ -3,6 +3,7 @@ use std::collections::BTreeMap; use p3_field::PrimeCharacteristicRing; use pest::{Parser, iterators::Pair}; use pest_derive::Parser; +use thiserror::Error; use utils::ToUsize; use vm::F; @@ -18,29 +19,15 @@ use crate::{ #[grammar = "grammar.pest"] pub struct LangParser; -#[derive(Debug)] +#[derive(Debug, Error)] pub(crate) enum ParseError { - PestError(pest::error::Error), - SemanticError(String), -} - -impl From> for ParseError { - fn from(error: pest::error::Error) -> Self { - Self::PestError(error) - } -} + #[error(transparent)] + PestError(#[from] pest::error::Error), -impl std::fmt::Display for ParseError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::PestError(e) => write!(f, "Parse error: {e}"), - Self::SemanticError(e) => write!(f, "Semantic error: {e}"), - } - } + #[error("Semantic error: {0}")] + SemanticError(String), } -impl std::error::Error for ParseError {} - pub(crate) fn parse_program(input: &str) -> Result { let input = remove_comments(input); let mut pairs = LangParser::parse(Rule::program, &input)?; diff --git a/crates/pcs/src/combinatorics.rs b/crates/pcs/src/combinatorics.rs index de62f86e..935ce78c 100644 --- a/crates/pcs/src/combinatorics.rs +++ b/crates/pcs/src/combinatorics.rs @@ -54,8 +54,7 @@ impl TreeOfVariablesInner { impl TreeOfVariables { pub fn compute_optimal(vars_per_polynomial: Vec) -> Self { - let n = vars_per_polynomial.len(); - assert!(n > 0); + assert!(!vars_per_polynomial.is_empty()); let root = Self::compute_greedy(&vars_per_polynomial); diff --git a/crates/utils/src/constraints_checker.rs b/crates/utils/src/constraints_checker.rs index 6af334d8..3e25abfc 100644 --- a/crates/utils/src/constraints_checker.rs +++ b/crates/utils/src/constraints_checker.rs @@ -49,7 +49,7 @@ impl<'a, EF: ExtensionField> + ExtensionField, IF: ExtensionField>(&mut self, x: I) { - let x: IF = x.into(); + let x = x.into(); if !x.is_zero() { self.errors.push(self.constraint_index); } diff --git a/crates/utils/src/constraints_folder.rs b/crates/utils/src/constraints_folder.rs index 19a83f97..fec9bb8f 100644 --- a/crates/utils/src/constraints_folder.rs +++ b/crates/utils/src/constraints_folder.rs @@ -49,7 +49,7 @@ where #[inline] fn assert_zero>(&mut self, x: I) { - let x: NF = x.into(); + let x = x.into(); let alpha_power = self.alpha_powers[self.constraint_index]; self.accumulator += alpha_power * x; self.constraint_index += 1; diff --git a/crates/utils/src/misc.rs b/crates/utils/src/misc.rs index 3525bad4..71b81afe 100644 --- a/crates/utils/src/misc.rs +++ b/crates/utils/src/misc.rs @@ -1,6 +1,4 @@ -use std::ops::Range; - -use p3_field::{BasedVectorSpace, ExtensionField, Field}; +use p3_field::{ExtensionField, Field}; use rayon::prelude::*; use crate::PF; @@ -15,31 +13,6 @@ pub fn transmute_slice(slice: &[Before]) -> &[After] { unsafe { std::slice::from_raw_parts(slice.as_ptr().cast::(), new_len) } } -#[must_use] -pub const fn shift_range(range: Range, shift: usize) -> Range { - Range { - start: range.start + shift, - end: range.end + shift, - } -} - -#[must_use] -pub const fn diff_to_next_power_of_two(n: usize) -> usize { - n.next_power_of_two() - n -} - -pub fn left_mut(slice: &mut [A]) -> &mut [A] { - assert!(slice.len().is_multiple_of(2)); - let mid = slice.len() / 2; - &mut slice[..mid] -} - -pub fn right_mut(slice: &mut [A]) -> &mut [A] { - assert!(slice.len().is_multiple_of(2)); - let mid = slice.len() / 2; - &mut slice[mid..] -} - pub fn left_ref(slice: &[A]) -> &[A] { assert!(slice.len().is_multiple_of(2)); let mid = slice.len() / 2; @@ -71,9 +44,9 @@ pub fn field_slice_as_base>(slice: &[EF]) -> Opt } pub fn dot_product_with_base>>(slice: &[EF]) -> EF { - assert_eq!(slice.len(), >>::DIMENSION); + assert_eq!(slice.len(), EF::DIMENSION); (0..EF::DIMENSION) - .map(|i| slice[i] * >>::ith_basis_element(i).unwrap()) + .map(|i| slice[i] * EF::ith_basis_element(i).unwrap()) .sum::() } diff --git a/crates/utils/src/multilinear.rs b/crates/utils/src/multilinear.rs index a8f63a81..6b637d3f 100644 --- a/crates/utils/src/multilinear.rs +++ b/crates/utils/src/multilinear.rs @@ -1,75 +1,12 @@ use std::borrow::Borrow; -use p3_field::{BasedVectorSpace, ExtensionField, Field, PackedValue, dot_product}; +use p3_field::{ExtensionField, Field, dot_product}; use rayon::prelude::*; use tracing::instrument; use whir_p3::poly::evals::EvaluationsList; use crate::{EFPacking, PF}; -pub fn fold_multilinear_in_small_field, D>( - m: &[D], - scalars: &[F], -) -> Vec { - // TODO ... - assert!(scalars.len().is_power_of_two() && scalars.len() <= m.len()); - let new_size = m.len() / scalars.len(); - - let dim = >::DIMENSION; - - let m_transmuted: &[F] = - unsafe { std::slice::from_raw_parts(m.as_ptr().cast::(), m.len() * dim) }; - let res_transmuted = { - let new_size = m.len() * dim / scalars.len(); - - if new_size < F::Packing::WIDTH { - (0..new_size) - .into_par_iter() - .map(|i| { - scalars - .iter() - .enumerate() - .map(|(j, s)| *s * m_transmuted[i + j * new_size]) - .sum() - }) - .collect() - } else { - let inners = (0..scalars.len()) - .map(|i| &m_transmuted[i * new_size..(i + 1) * new_size]) - .collect::>(); - let inners_packed = inners - .iter() - .map(|&inner| F::Packing::pack_slice(inner)) - .collect::>(); - - let packed_res = (0..new_size / F::Packing::WIDTH) - .into_par_iter() - .map(|i| { - scalars - .iter() - .enumerate() - .map(|(j, s)| inners_packed[j][i] * *s) - .sum::() - }) - .collect::>(); - - let mut unpacked: Vec = unsafe { std::mem::transmute(packed_res) }; - unsafe { - unpacked.set_len(new_size); - } - - unpacked - } - }; - let res: Vec = unsafe { - let mut res: Vec = std::mem::transmute(res_transmuted); - res.set_len(new_size); - res - }; - - res -} - pub fn fold_multilinear_in_large_field>( m: &[F], scalars: &[EF], @@ -145,40 +82,6 @@ pub fn batch_fold_multilinear_in_large_field_packed>>( .collect() } -pub fn batch_fold_multilinear_in_small_field>( - polys: &[&[EF]], - scalars: &[F], -) -> Vec> { - polys - .par_iter() - .map(|poly| fold_multilinear_in_small_field(poly, scalars)) - .collect() -} - -pub fn batch_fold_multilinear_in_small_field_packed>>( - polys: &[&[EFPacking]], - scalars: &[PF], -) -> Vec> { - polys - .par_iter() - .map(|poly| fold_multilinear_in_small_field(poly, scalars)) - .collect() -} - -// pub fn packed_multilinear(pols: &[Vec]) -> Vec { -// let n_vars = pols[0].num_variables(); -// assert!(pols.iter().all(|p| p.num_variables() == n_vars)); -// let packed_len = (pols.len() << n_vars).next_power_of_two(); -// let mut dst = F::zero_vec(packed_len); -// let mut offset = 0; -// // TODO parallelize -// for pol in pols { -// dst[offset..offset + pol.num_evals()].copy_from_slice(pol); -// offset += pol.num_evals(); -// } -// dst -// } - #[instrument(name = "add_multilinears", skip_all)] pub fn add_multilinears(pol1: &[F], pol2: &[F]) -> Vec { assert_eq!(pol1.len(), pol2.len()); diff --git a/crates/utils/src/packed_constraints_folder.rs b/crates/utils/src/packed_constraints_folder.rs index b83c339d..8a0c105f 100644 --- a/crates/utils/src/packed_constraints_folder.rs +++ b/crates/utils/src/packed_constraints_folder.rs @@ -46,7 +46,7 @@ impl<'a, EF: ExtensionField>> AirBuilder for ConstraintFolderPackedBase<' #[inline] fn assert_zero>(&mut self, x: I) { let alpha_power = self.alpha_powers[self.constraint_index]; - let x: PFPacking = x.into(); + let x = x.into(); self.accumulator += Into::>::into(alpha_power) * x; self.constraint_index += 1; } @@ -102,7 +102,7 @@ impl<'a, EF: ExtensionField>> AirBuilder for ConstraintFolderPackedExtens #[inline] fn assert_zero>(&mut self, x: I) { let alpha_power = self.alpha_powers[self.constraint_index]; - let x: EFPacking = x.into(); + let x = x.into(); self.accumulator += x * alpha_power; self.constraint_index += 1; } diff --git a/crates/utils/src/wrappers.rs b/crates/utils/src/wrappers.rs index 2be7d40f..4045bfc7 100644 --- a/crates/utils/src/wrappers.rs +++ b/crates/utils/src/wrappers.rs @@ -65,7 +65,7 @@ pub fn pack_extension>>(slice: &[EF]) -> Vec()) .map(EFPacking::::from_ext_slice) - .collect::>() + .collect() } pub fn unpack_extension>>(vec: &[EFPacking]) -> Vec { diff --git a/crates/vm/src/bytecode/hint.rs b/crates/vm/src/bytecode/hint.rs index 4a44dcdf..3755c1bb 100644 --- a/crates/vm/src/bytecode/hint.rs +++ b/crates/vm/src/bytecode/hint.rs @@ -107,7 +107,7 @@ impl Hint { let values = content .iter() .map(|m| Ok(m.read_value(memory, fp)?.to_string())) - .collect::, RunnerError>>()?; + .collect::, _>>()?; // Logs for performance analysis: if values.first().is_some_and(|s| s == "123456789") { diff --git a/crates/xmss/src/lib.rs b/crates/xmss/src/lib.rs index 540c8d8b..1fd3b092 100644 --- a/crates/xmss/src/lib.rs +++ b/crates/xmss/src/lib.rs @@ -34,13 +34,12 @@ impl WotsSecretKey { #[must_use] pub fn new(pre_images: [Digest; N_CHAINS], poseidon16: &Poseidon16) -> Self { - let mut public_key = [Default::default(); N_CHAINS]; - for i in 0..N_CHAINS { - public_key[i] = iterate_hash(&pre_images[i], CHAIN_LENGTH, poseidon16); - } + let public_key = WotsPublicKey(std::array::from_fn(|i| { + iterate_hash(&pre_images[i], CHAIN_LENGTH, poseidon16) + })); Self { pre_images, - public_key: WotsPublicKey(public_key), + public_key, } }