diff --git a/CHANGELOG.md b/CHANGELOG.md index ca39f03713..79a203f8fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.2.3] - 2024-04-16 + +- Code refactoring ([#1558](https://github.com/0xPolygonZero/plonky2/pull/1558)) +- Simplify types: remove option from CTL filters ([#1567](https://github.com/0xPolygonZero/plonky2/pull/1567)) +- Add stdarch_x86_avx512 feature ([#1566](https://github.com/0xPolygonZero/plonky2/pull/1566)) + ## [0.2.2] - 2024-03-21 ### Changed diff --git a/field/Cargo.toml b/field/Cargo.toml index acea27f89d..8c155bd8c6 100644 --- a/field/Cargo.toml +++ b/field/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "plonky2_field" description = "Finite field arithmetic" -version = "0.2.1" +version = "0.2.2" authors = ["Daniel Lubarov ", "William Borgeaud ", "Jacqueline Nabaglo ", "Hamish Ivey-Law "] edition.workspace = true license.workspace = true diff --git a/field/src/fft.rs b/field/src/fft.rs index 912c94da5e..09c9104422 100644 --- a/field/src/fft.rs +++ b/field/src/fft.rs @@ -53,6 +53,7 @@ fn fft_dispatch_cpu( zero_factor: Option, root_table: Option<&FftRootTable>, ) { + /* if root_table.is_some() { return fft_classic(input, zero_factor.unwrap_or(0), root_table.unwrap()); } else { @@ -68,6 +69,11 @@ fn fft_dispatch_cpu( return fft_classic(input, zero_factor.unwrap_or(0), computed.as_ref()); }; + */ + let computed_root_table = root_table.is_none().then(|| fft_root_table(input.len())); + let used_root_table = root_table.or(computed_root_table.as_ref()).unwrap(); + + fft_classic(input, zero_factor.unwrap_or(0), used_root_table); } #[inline] diff --git a/field/src/lib.rs b/field/src/lib.rs index 76d366b0fb..1b9c3afe0b 100644 --- a/field/src/lib.rs +++ b/field/src/lib.rs @@ -4,6 +4,7 @@ #![deny(rustdoc::broken_intra_doc_links)] #![deny(missing_debug_implementations)] #![feature(specialization)] +#![cfg_attr(target_arch = "x86_64", feature(stdarch_x86_avx512))] #![cfg_attr(not(test), no_std)] #![cfg(not(test))] extern crate alloc; diff --git a/plonky2/Cargo.toml b/plonky2/Cargo.toml index d8ba58279e..94715189b3 100644 --- a/plonky2/Cargo.toml +++ b/plonky2/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "plonky2" description = "Recursive SNARKs based on PLONK and FRI" -version = "0.2.1" +version = "0.2.2" authors = ["Daniel Lubarov ", "William Borgeaud ", "Nicholas Ward "] readme = "README.md" edition.workspace = true @@ -41,7 +41,7 @@ once_cell = { version = "1.18.0" } papi-bindings = { version = "0.5.2" } # Local dependencies -plonky2_field = { version = "0.2.1", path = "../field", default-features = false } +plonky2_field = { version = "0.2.2", path = "../field", default-features = false } plonky2_maybe_rayon = { version = "0.2.0", path = "../maybe_rayon", default-features = false } plonky2_util = { version = "0.2.0", path = "../util", default-features = false } cryptography_cuda = { workspace = true, optional = true } diff --git a/plonky2/src/gates/lookup.rs b/plonky2/src/gates/lookup.rs index 87d289304b..9c18cee3af 100644 --- a/plonky2/src/gates/lookup.rs +++ b/plonky2/src/gates/lookup.rs @@ -5,7 +5,6 @@ use alloc::{ vec, vec::Vec, }; -use core::usize; use itertools::Itertools; use keccak_hash::keccak; diff --git a/plonky2/src/gates/lookup_table.rs b/plonky2/src/gates/lookup_table.rs index b6b7977690..5e13cfce31 100644 --- a/plonky2/src/gates/lookup_table.rs +++ b/plonky2/src/gates/lookup_table.rs @@ -6,7 +6,6 @@ use alloc::{ vec, vec::Vec, }; -use core::usize; #[cfg(feature = "std")] use std::sync::Arc; diff --git a/plonky2/src/plonk/copy_constraint.rs b/plonky2/src/plonk/copy_constraint.rs index 6868650dfe..f5b6f19345 100644 --- a/plonky2/src/plonk/copy_constraint.rs +++ b/plonky2/src/plonk/copy_constraint.rs @@ -7,6 +7,7 @@ use crate::iop::target::Target; #[derive(Debug, Clone)] pub struct CopyConstraint { pub pair: (Target, Target), + #[allow(dead_code)] pub name: String, } diff --git a/starky/Cargo.toml b/starky/Cargo.toml index 92aaaa1aed..33d90d5b4d 100644 --- a/starky/Cargo.toml +++ b/starky/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "starky" description = "Implementation of STARKs" -version = "0.3.0" +version = "0.4.0" authors = ["Daniel Lubarov ", "William Borgeaud "] readme = "README.md" edition.workspace = true @@ -26,7 +26,7 @@ log = { workspace = true } num-bigint = { version = "0.4.3", default-features = false } # Local dependencies -plonky2 = { version = "0.2.1", path = "../plonky2", default-features = false } +plonky2 = { version = "0.2.2", path = "../plonky2", default-features = false } plonky2_maybe_rayon = { version = "0.2.0", path = "../maybe_rayon", default-features = false } plonky2_util = { version = "0.2.0", path = "../util", default-features = false } diff --git a/starky/src/cross_table_lookup.rs b/starky/src/cross_table_lookup.rs index da50c24c17..d0b020a6a0 100644 --- a/starky/src/cross_table_lookup.rs +++ b/starky/src/cross_table_lookup.rs @@ -67,12 +67,12 @@ pub type TableIdx = usize; pub struct TableWithColumns { table: TableIdx, columns: Vec>, - filter: Option>, + filter: Filter, } impl TableWithColumns { /// Generates a new `TableWithColumns` given a `table` index, a linear combination of columns `columns` and a `filter`. - pub fn new(table: TableIdx, columns: Vec>, filter: Option>) -> Self { + pub fn new(table: TableIdx, columns: Vec>, filter: Filter) -> Self { Self { table, columns, @@ -163,7 +163,7 @@ pub struct CtlZData<'a, F: Field> { pub(crate) columns: Vec<&'a [Column]>, /// Vector of filter columns for the current table. /// Each filter evaluates to either 1 or 0. - pub(crate) filter: Vec>>, + pub(crate) filter: Vec>, } impl<'a, F: Field> CtlZData<'a, F> { @@ -173,7 +173,7 @@ impl<'a, F: Field> CtlZData<'a, F> { z: PolynomialValues, challenge: GrandProductChallenge, columns: Vec<&'a [Column]>, - filter: Vec>>, + filter: Vec>, ) -> Self { Self { helper_columns, @@ -404,7 +404,7 @@ fn ctl_helper_zs_cols( .map(|(table, group)| { let columns_filters = group .map(|table| (&table.columns[..], &table.filter)) - .collect::], &Option>)>>(); + .collect::], &Filter)>>(); ( table, partial_sums( @@ -484,7 +484,7 @@ where /// Column linear combinations of the `CrossTableLookup`s. pub(crate) columns: Vec<&'a [Column]>, /// Filter that evaluates to either 1 or 0. - pub(crate) filter: Vec>>, + pub(crate) filter: Vec>, } impl<'a, F: RichField + Extendable, const D: usize> @@ -682,16 +682,8 @@ pub(crate) fn eval_cross_table_lookup_checks { /// Column linear combinations of the `CrossTableLookup`s. pub(crate) columns: Vec>>, /// Filter that evaluates to either 1 or 0. - pub(crate) filter: Vec>>, + pub(crate) filter: Vec>, } impl<'a, F: Field, const D: usize> CtlCheckVarsTarget { @@ -856,8 +844,6 @@ pub(crate) fn eval_cross_table_lookup_checks_circuit< let local_values = vars.get_local_values(); let next_values = vars.get_next_values(); - let one = builder.one_extension(); - for lookup_vars in ctl_vars { let CtlCheckVarsTarget { helper_columns, @@ -906,16 +892,8 @@ pub(crate) fn eval_cross_table_lookup_checks_circuit< let combin0 = challenges.combine_circuit(builder, &evals[0]); let combin1 = challenges.combine_circuit(builder, &evals[1]); - let f0 = if let Some(filter0) = &filter[0] { - filter0.eval_filter_circuit(builder, local_values, next_values) - } else { - one - }; - let f1 = if let Some(filter1) = &filter[1] { - filter1.eval_filter_circuit(builder, local_values, next_values) - } else { - one - }; + let f0 = filter[0].eval_filter_circuit(builder, local_values, next_values); + let f1 = filter[1].eval_filter_circuit(builder, local_values, next_values); let combined = builder.mul_sub_extension(combin1, *local_z, f1); let combined = builder.mul_extension(combined, combin0); @@ -928,11 +906,7 @@ pub(crate) fn eval_cross_table_lookup_checks_circuit< consumer.constraint_last_row(builder, constr); } else { let combin0 = challenges.combine_circuit(builder, &evals[0]); - let f0 = if let Some(filter0) = &filter[0] { - filter0.eval_filter_circuit(builder, local_values, next_values) - } else { - one - }; + let f0 = filter[0].eval_filter_circuit(builder, local_values, next_values); let constr = builder.mul_sub_extension(combin0, *local_z, f0); consumer.constraint_last_row(builder, constr); @@ -1121,11 +1095,7 @@ pub mod debug_utils { ) { let trace = &trace_poly_values[table.table]; for i in 0..trace[0].len() { - let filter = if let Some(combin) = &table.filter { - combin.eval_table(trace, i) - } else { - F::ONE - }; + let filter = table.filter.eval_table(trace, i); if filter.is_one() { let row = table .columns diff --git a/starky/src/lookup.rs b/starky/src/lookup.rs index 16383cd690..f63c4bd76c 100644 --- a/starky/src/lookup.rs +++ b/starky/src/lookup.rs @@ -39,6 +39,16 @@ pub struct Filter { constants: Vec>, } +/// The default filter is always on. +impl Default for Filter { + fn default() -> Self { + Self { + products: vec![], + constants: vec![Column::constant(F::ONE)], + } + } +} + impl Filter { /// Returns a filter from the provided `products` and `constants` vectors. pub fn new(products: Vec<(Column, Column)>, constants: Vec>) -> Self { @@ -396,7 +406,7 @@ impl Column { } } -pub(crate) type ColumnFilter<'a, F> = (&'a [Column], &'a Option>); +pub(crate) type ColumnFilter<'a, F> = (&'a [Column], &'a Filter); /// A [`Lookup`] defines a set of `columns`` whose values should appear in a /// `table_column` (i.e. the lookup table associated to these looking columns), @@ -423,7 +433,7 @@ pub struct Lookup { /// Columns to filter some elements. There is at most one filter /// column per column to lookup. - pub filter_columns: Vec>>, + pub filter_columns: Vec>, } impl Lookup { @@ -650,7 +660,7 @@ pub(crate) fn lookup_helper_columns( /// Given data associated to a lookup, check the associated helper polynomials. pub(crate) fn eval_helper_columns( - filter: &[Option>], + filter: &[Filter], columns: &[Vec

], local_values: &[P], next_values: &[P], @@ -674,26 +684,14 @@ pub(crate) fn eval_helper_columns( let combin0 = challenges.combine(&chunk[0]); let combin1 = challenges.combine(chunk[1].iter()); - let f0 = if let Some(filter0) = &fs[0] { - filter0.eval_filter(local_values, next_values) - } else { - P::ONES - }; - let f1 = if let Some(filter1) = &fs[1] { - filter1.eval_filter(local_values, next_values) - } else { - P::ONES - }; + let f0 = fs[0].eval_filter(local_values, next_values); + let f1 = fs[1].eval_filter(local_values, next_values); consumer.constraint(combin1 * combin0 * h - f0 * combin1 - f1 * combin0); } 1 => { let combin = challenges.combine(&chunk[0]); - let f0 = if let Some(filter1) = &fs[0] { - filter1.eval_filter(local_values, next_values) - } else { - P::ONES - }; + let f0 = fs[0].eval_filter(local_values, next_values); consumer.constraint(combin * h - f0); } @@ -707,7 +705,7 @@ pub(crate) fn eval_helper_columns( /// Given data associated to a lookup (either a CTL or a range-check), check the associated helper polynomials. pub(crate) fn eval_helper_columns_circuit, const D: usize>( builder: &mut CircuitBuilder, - filter: &[Option>], + filter: &[Filter], columns: &[Vec>], local_values: &[ExtensionTarget], next_values: &[ExtensionTarget], @@ -722,22 +720,13 @@ pub(crate) fn eval_helper_columns_circuit, const D: .chunks(chunk_size) .zip(filter.chunks(chunk_size).zip(helper_columns)) { - let one = builder.one_extension(); match chunk.len() { 2 => { let combin0 = challenges.combine_circuit(builder, &chunk[0]); let combin1 = challenges.combine_circuit(builder, &chunk[1]); - let f0 = if let Some(filter0) = &fs[0] { - filter0.eval_filter_circuit(builder, local_values, next_values) - } else { - one - }; - let f1 = if let Some(filter1) = &fs[1] { - filter1.eval_filter_circuit(builder, local_values, next_values) - } else { - one - }; + let f0 = fs[0].eval_filter_circuit(builder, local_values, next_values); + let f1 = fs[1].eval_filter_circuit(builder, local_values, next_values); let constr = builder.mul_sub_extension(combin0, h, f0); let constr = builder.mul_extension(constr, combin1); @@ -748,11 +737,7 @@ pub(crate) fn eval_helper_columns_circuit, const D: } 1 => { let combin = challenges.combine_circuit(builder, &chunk[0]); - let f0 = if let Some(filter1) = &fs[0] { - filter1.eval_filter_circuit(builder, local_values, next_values) - } else { - one - }; + let f0 = fs[0].eval_filter_circuit(builder, local_values, next_values); let constr = builder.mul_sub_extension(combin, h, f0); consumer.constraint(builder, constr); } @@ -788,13 +773,10 @@ pub(crate) fn get_helper_cols( let mut filter_col = Vec::with_capacity(degree); let first_combined = (0..degree) .map(|d| { - let f = if let Some(filter) = first_filter { - let f = filter.eval_table(trace, d); + let f = { + let f = first_filter.eval_table(trace, d); filter_col.push(f); f - } else { - filter_col.push(F::ONE); - F::ONE }; if f.is_one() { let evals = first_col @@ -821,13 +803,10 @@ pub(crate) fn get_helper_cols( let mut filter_col = Vec::with_capacity(degree); let mut combined = (0..degree) .map(|d| { - let f = if let Some(filter) = filt { - let f = filter.eval_table(trace, d); + let f = { + let f = filt.eval_table(trace, d); filter_col.push(f); f - } else { - filter_col.push(F::ONE); - F::ONE }; if f.is_one() { let evals = col diff --git a/starky/src/permutation_stark.rs b/starky/src/permutation_stark.rs index bddf420279..62290b658d 100644 --- a/starky/src/permutation_stark.rs +++ b/starky/src/permutation_stark.rs @@ -72,7 +72,7 @@ impl, const D: usize> Stark for PermutationSt columns: vec![Column::single(0)], table_column: Column::single(1), frequencies_column: Column::single(2), - filter_columns: vec![None; 1], + filter_columns: vec![Default::default()], }] } diff --git a/starky/src/prover.rs b/starky/src/prover.rs index c7b77b9336..29b72521b0 100644 --- a/starky/src/prover.rs +++ b/starky/src/prover.rs @@ -126,8 +126,6 @@ where ); // Permutation arguments. - - let constraint_degree = stark.constraint_degree(); let lookup_challenges = stark.uses_lookups().then(|| { if let Some(c) = ctl_challenges { c.challenges.iter().map(|ch| ch.beta).collect::>()