diff --git a/stwo_cairo_verifier/Scarb.toml b/stwo_cairo_verifier/Scarb.toml index ab88f3ec..204f4d8e 100644 --- a/stwo_cairo_verifier/Scarb.toml +++ b/stwo_cairo_verifier/Scarb.toml @@ -6,6 +6,9 @@ edition = "2024_07" [lib] casm = true +[tool.fmt] +sort-module-level-items = true + [dependencies] [dev-dependencies] diff --git a/stwo_cairo_verifier/src/channel.cairo b/stwo_cairo_verifier/src/channel.cairo index 8a098770..4fb25ed9 100644 --- a/stwo_cairo_verifier/src/channel.cairo +++ b/stwo_cairo_verifier/src/channel.cairo @@ -1,11 +1,11 @@ use core::array::SpanTrait; use core::poseidon::{poseidon_hash_span, hades_permutation}; use core::traits::DivRem; - -use stwo_cairo_verifier::{BaseField, SecureField}; use stwo_cairo_verifier::fields::qm31::QM31Trait; use stwo_cairo_verifier::utils::pack4; +use stwo_cairo_verifier::{BaseField, SecureField}; + const M31_SHIFT: felt252 = 0x80000000; // 2**31. const M31_SHIFT_NZ_U256: NonZero = 0x80000000; // 2**31. pub const EXTENSION_FELTS_PER_HASH: usize = 2; @@ -155,8 +155,8 @@ fn extract_m31(ref num: u256) -> BaseField { #[cfg(test)] mod tests { - use super::{Channel, ChannelTrait}; use stwo_cairo_verifier::fields::qm31::qm31; + use super::ChannelTrait; #[test] fn test_initialize_channel() { diff --git a/stwo_cairo_verifier/src/circle.cairo b/stwo_cairo_verifier/src/circle.cairo index d29bf1ce..7040da72 100644 --- a/stwo_cairo_verifier/src/circle.cairo +++ b/stwo_cairo_verifier/src/circle.cairo @@ -1,9 +1,10 @@ -use stwo_cairo_verifier::fields::m31::{M31, M31Impl}; +use core::num::traits::one::One; +use core::num::traits::zero::Zero; +use core::num::traits::{WrappingAdd, WideMul}; use stwo_cairo_verifier::fields::cm31::CM31; +use stwo_cairo_verifier::fields::m31::{M31, M31Impl}; use stwo_cairo_verifier::fields::qm31::{QM31Impl, QM31, QM31Trait}; use super::utils::pow; -use core::num::traits::zero::Zero; -use core::num::traits::one::One; pub const M31_CIRCLE_GEN: CirclePoint = CirclePoint { x: M31 { inner: 2 }, y: M31 { inner: 1268011823 }, }; @@ -123,10 +124,10 @@ pub impl CosetImpl of CosetTrait { } fn index_at(self: @Coset, index: usize) -> usize { - let index_times_step = (core::integer::u32_wide_mul(*self.step_size, index) & U32_BIT_MASK) + let index_times_step = ((*self.step_size).wide_mul(index) & U32_BIT_MASK) .try_into() .unwrap(); - let result = core::integer::u32_wrapping_add(*self.initial_index, index_times_step); + let result = (*self.initial_index).wrapping_add(index_times_step); result & CIRCLE_ORDER_BIT_MASK } @@ -178,14 +179,14 @@ pub impl CosetImpl of CosetTrait { #[cfg(test)] mod tests { - use super::{M31_CIRCLE_GEN, CIRCLE_ORDER, CirclePoint, CirclePointM31Impl, Coset, CosetImpl}; - use core::option::OptionTrait; use core::array::ArrayTrait; + use core::option::OptionTrait; use core::traits::TryInto; - use super::{CirclePointQM31Impl, QM31_CIRCLE_GEN}; - use stwo_cairo_verifier::fields::m31::{m31, M31}; - use stwo_cairo_verifier::fields::qm31::{qm31, QM31, QM31One}; + use stwo_cairo_verifier::fields::m31::m31; + use stwo_cairo_verifier::fields::qm31::QM31One; use stwo_cairo_verifier::utils::pow; + use super::{CirclePointQM31Impl, QM31_CIRCLE_GEN}; + use super::{M31_CIRCLE_GEN, CIRCLE_ORDER, CirclePoint, CirclePointM31Impl, Coset, CosetImpl}; #[test] fn test_add_1() { diff --git a/stwo_cairo_verifier/src/fields.cairo b/stwo_cairo_verifier/src/fields.cairo index 8c1737e7..32c0ba18 100644 --- a/stwo_cairo_verifier/src/fields.cairo +++ b/stwo_cairo_verifier/src/fields.cairo @@ -1,5 +1,5 @@ -pub mod m31; pub mod cm31; +pub mod m31; pub mod qm31; pub type BaseField = m31::M31; diff --git a/stwo_cairo_verifier/src/fields/cm31.cairo b/stwo_cairo_verifier/src/fields/cm31.cairo index 3363d3a0..fef2aa65 100644 --- a/stwo_cairo_verifier/src/fields/cm31.cairo +++ b/stwo_cairo_verifier/src/fields/cm31.cairo @@ -71,8 +71,8 @@ pub fn cm31(a: u32, b: u32) -> CM31 { #[cfg(test)] mod tests { - use super::{cm31}; use super::super::m31::{m31, P}; + use super::{cm31}; #[test] fn test_cm31() { diff --git a/stwo_cairo_verifier/src/fields/m31.cairo b/stwo_cairo_verifier/src/fields/m31.cairo index 69491fe5..b01ba972 100644 --- a/stwo_cairo_verifier/src/fields/m31.cairo +++ b/stwo_cairo_verifier/src/fields/m31.cairo @@ -1,6 +1,6 @@ +use core::num::traits::{WideMul, CheckedSub}; use core::option::OptionTrait; use core::traits::TryInto; -use core::result::ResultTrait; pub const P: u32 = 0x7fffffff; const P32NZ: NonZero = 0x7fffffff; @@ -45,7 +45,7 @@ pub impl M31Impl of M31Trait { pub impl M31Add of core::traits::Add { fn add(lhs: M31, rhs: M31) -> M31 { let res = lhs.inner + rhs.inner; - let res = core::integer::u32_overflowing_sub(res, P).unwrap_or(res); + let res = res.checked_sub(P).unwrap_or(res); M31 { inner: res } } } @@ -56,7 +56,7 @@ pub impl M31Sub of core::traits::Sub { } pub impl M31Mul of core::traits::Mul { fn mul(lhs: M31, rhs: M31) -> M31 { - M31Impl::reduce_u64(core::integer::u32_wide_mul(lhs.inner, rhs.inner)) + M31Impl::reduce_u64(lhs.inner.wide_mul(rhs.inner)) } } pub impl M31Zero of core::num::traits::Zero { @@ -102,7 +102,7 @@ pub fn m31(val: u32) -> M31 { #[cfg(test)] mod tests { - use super::{m31, P, M31, M31Trait}; + use super::{m31, P, M31Trait}; const POW2_15: u32 = 0b1000000000000000; const POW2_16: u32 = 0b10000000000000000; diff --git a/stwo_cairo_verifier/src/fields/qm31.cairo b/stwo_cairo_verifier/src/fields/qm31.cairo index f8ba678a..f34dd3ba 100644 --- a/stwo_cairo_verifier/src/fields/qm31.cairo +++ b/stwo_cairo_verifier/src/fields/qm31.cairo @@ -1,7 +1,7 @@ -use super::m31::{M31, m31, M31Impl}; -use super::cm31::{CM31, cm31, CM31Trait}; -use core::num::traits::zero::Zero; use core::num::traits::one::One; +use core::num::traits::zero::Zero; +use super::cm31::{CM31, cm31, CM31Trait}; +use super::m31::{M31, M31Impl}; pub const R: CM31 = CM31 { a: M31 { inner: 2 }, b: M31 { inner: 1 } }; @@ -113,9 +113,9 @@ pub fn qm31(a: u32, b: u32, c: u32, d: u32) -> QM31 { #[cfg(test)] mod tests { - use super::{QM31, qm31, QM31Trait, QM31Impl}; - use super::{CM31, cm31}; + use super::CM31; use super::super::m31::{m31, P, M31Trait}; + use super::{QM31, qm31, QM31Trait, QM31Impl}; #[test] fn test_QM31() { diff --git a/stwo_cairo_verifier/src/fri.cairo b/stwo_cairo_verifier/src/fri.cairo index 23fe23b8..d2ee8b0c 100644 --- a/stwo_cairo_verifier/src/fri.cairo +++ b/stwo_cairo_verifier/src/fri.cairo @@ -1,23 +1,23 @@ use core::dict::Felt252Dict; -use stwo_cairo_verifier::fields::m31::M31Trait; -use stwo_cairo_verifier::circle::{Coset, CosetImpl}; -use stwo_cairo_verifier::poly::line::{LineDomain, LineDomainImpl}; -use stwo_cairo_verifier::fields::qm31::{QM31, qm31, QM31Trait}; -use stwo_cairo_verifier::queries::SparseSubCircleDomain; +use stwo_cairo_verifier::channel::{Channel, ChannelTrait}; +use stwo_cairo_verifier::circle::CosetImpl; use stwo_cairo_verifier::fields::m31::M31; -use stwo_cairo_verifier::utils::{bit_reverse_index, pow, pow_qm31, qm31_zero_array, find}; -use stwo_cairo_verifier::poly::line::{ - LineEvaluation, LineEvaluationImpl, SparseLineEvaluation, SparseLineEvaluationImpl -}; +use stwo_cairo_verifier::fields::m31::M31Trait; +use stwo_cairo_verifier::fields::qm31::{QM31, QM31Trait}; +use stwo_cairo_verifier::poly::circle::CircleDomainImpl; use stwo_cairo_verifier::poly::circle::{ CircleEvaluation, SparseCircleEvaluation, SparseCircleEvaluationImpl }; -use stwo_cairo_verifier::poly::circle::{CircleDomain, CircleDomainImpl}; +use stwo_cairo_verifier::poly::line::{ + LineEvaluation, LineEvaluationImpl, SparseLineEvaluation, SparseLineEvaluationImpl +}; +use stwo_cairo_verifier::poly::line::{LineDomain, LineDomainImpl}; +use stwo_cairo_verifier::poly::line::{LinePoly, LinePolyImpl}; +use stwo_cairo_verifier::queries::SparseSubCircleDomain; use stwo_cairo_verifier::queries::{Queries, QueriesImpl}; -use stwo_cairo_verifier::vcs::verifier::{MerkleDecommitment, MerkleVerifier, MerkleVerifierTrait}; +use stwo_cairo_verifier::utils::{bit_reverse_index, pow, pow_qm31, qm31_zero_array, find}; use stwo_cairo_verifier::vcs::hasher::PoseidonMerkleHasher; -use stwo_cairo_verifier::poly::line::{LinePoly, LinePolyImpl}; -use stwo_cairo_verifier::channel::{Channel, ChannelTrait}; +use stwo_cairo_verifier::vcs::verifier::{MerkleDecommitment, MerkleVerifier, MerkleVerifierTrait}; pub const CIRCLE_TO_LINE_FOLD_STEP: u32 = 1; pub const FOLD_STEP: u32 = 1; @@ -531,27 +531,20 @@ pub fn ibutterfly(v0: QM31, v1: QM31, itwid: M31) -> (QM31, QM31) { #[cfg(test)] mod test { - use stwo_cairo_verifier::poly::line::{ - LineEvaluation, SparseLineEvaluation, SparseLineEvaluationImpl - }; - use stwo_cairo_verifier::fields::m31::M31Trait; + use stwo_cairo_verifier::channel::ChannelTrait; use stwo_cairo_verifier::circle::{Coset, CosetImpl}; - use stwo_cairo_verifier::poly::line::{LineDomain, LineDomainImpl}; - use stwo_cairo_verifier::fields::qm31::{QM31, qm31}; - use stwo_cairo_verifier::fields::m31::M31; - use stwo_cairo_verifier::utils::{bit_reverse_index, pow}; + use stwo_cairo_verifier::fields::qm31::qm31; use stwo_cairo_verifier::poly::circle::{ - CircleEvaluation, CircleEvaluationTrait, CircleDomain, CircleEvaluationImpl, - SparseCircleEvaluation, SparseCircleEvaluationImpl + CircleDomain, CircleEvaluationImpl, SparseCircleEvaluation, SparseCircleEvaluationImpl }; - use stwo_cairo_verifier::queries::{Queries, QueriesImpl}; - use stwo_cairo_verifier::channel::ChannelTrait; + use stwo_cairo_verifier::poly::line::LineDomainImpl; use stwo_cairo_verifier::poly::line::LinePoly; - use stwo_cairo_verifier::vcs::verifier::MerkleDecommitment; - use super::{ - FOLD_STEP, CIRCLE_TO_LINE_FOLD_STEP, FriConfig, FriProof, FriVerifierImpl, - FriVerificationError, FriLayerProof + use stwo_cairo_verifier::poly::line::{ + LineEvaluation, SparseLineEvaluation, SparseLineEvaluationImpl }; + use stwo_cairo_verifier::queries::{Queries, QueriesImpl}; + use stwo_cairo_verifier::vcs::verifier::MerkleDecommitment; + use super::{FriConfig, FriProof, FriVerifierImpl, FriVerificationError, FriLayerProof}; #[test] fn test_fold_line_1() { diff --git a/stwo_cairo_verifier/src/lib.cairo b/stwo_cairo_verifier/src/lib.cairo index 9dbe333c..1f422bbc 100644 --- a/stwo_cairo_verifier/src/lib.cairo +++ b/stwo_cairo_verifier/src/lib.cairo @@ -1,12 +1,12 @@ mod channel; mod circle; mod fields; +mod fri; mod poly; +mod queries; +mod sort; mod utils; mod vcs; -mod fri; -mod sort; -mod queries; pub use fields::{BaseField, SecureField}; diff --git a/stwo_cairo_verifier/src/poly/circle.cairo b/stwo_cairo_verifier/src/poly/circle.cairo index 34668fd4..65e63d9f 100644 --- a/stwo_cairo_verifier/src/poly/circle.cairo +++ b/stwo_cairo_verifier/src/poly/circle.cairo @@ -1,15 +1,12 @@ +use core::num::traits::ops::wrapping::WrappingSub; use stwo_cairo_verifier::circle::CirclePointTrait; -use core::option::OptionTrait; -use core::clone::Clone; -use core::result::ResultTrait; -use stwo_cairo_verifier::fields::qm31::{QM31, qm31}; -use stwo_cairo_verifier::fields::m31::{M31, m31}; -use stwo_cairo_verifier::utils::pow; use stwo_cairo_verifier::circle::{ Coset, CosetImpl, CirclePoint, CirclePointM31Impl, M31_CIRCLE_GEN, CIRCLE_ORDER }; +use stwo_cairo_verifier::fields::m31::M31; +use stwo_cairo_verifier::fields::qm31::QM31; use stwo_cairo_verifier::fri::fold_circle_into_line; -use core::num::traits::ops::wrapping::WrappingSub; +use stwo_cairo_verifier::utils::pow; /// A valid domain for circle polynomial interpolation and evaluation. /// @@ -127,13 +124,13 @@ pub impl SparseCircleEvaluationImpl of SparseCircleEvaluationImplTrait { #[cfg(test)] mod tests { - use super::{ - CircleDomain, CircleDomainTrait, CircleEvaluation, CircleEvaluationImpl, - SparseCircleEvaluation, SparseCircleEvaluationImplTrait - }; use stwo_cairo_verifier::circle::{Coset, CosetImpl, CirclePoint}; use stwo_cairo_verifier::fields::m31::m31; use stwo_cairo_verifier::fields::qm31::qm31; + use super::{ + CircleDomain, CircleDomainTrait, CircleEvaluationImpl, SparseCircleEvaluation, + SparseCircleEvaluationImplTrait + }; #[test] fn test_circle_domain_at_1() { diff --git a/stwo_cairo_verifier/src/poly/line.cairo b/stwo_cairo_verifier/src/poly/line.cairo index 83af45b6..7f55d511 100644 --- a/stwo_cairo_verifier/src/poly/line.cairo +++ b/stwo_cairo_verifier/src/poly/line.cairo @@ -1,13 +1,9 @@ -use core::option::OptionTrait; -use core::clone::Clone; -use core::result::ResultTrait; -use stwo_cairo_verifier::poly::utils::fold; -use stwo_cairo_verifier::fields::SecureField; -use stwo_cairo_verifier::fields::m31::{M31, m31, M31Trait}; -use stwo_cairo_verifier::fields::qm31::{QM31, qm31, QM31Zero}; -use stwo_cairo_verifier::utils::pow; use stwo_cairo_verifier::circle::{Coset, CosetImpl, CirclePointTrait, M31_CIRCLE_GEN}; +use stwo_cairo_verifier::fields::SecureField; +use stwo_cairo_verifier::fields::m31::{M31, m31}; +use stwo_cairo_verifier::fields::qm31::{QM31, QM31Zero}; use stwo_cairo_verifier::fri::fold_line; +use stwo_cairo_verifier::poly::utils::fold; /// A univariate polynomial defined on a [LineDomain]. #[derive(Debug, Drop, Clone)] @@ -138,11 +134,11 @@ pub impl SparseLineEvaluationImpl of SparseLineEvaluationTrait { #[cfg(test)] mod tests { - use super::{LinePoly, LinePolyTrait, LineDomain, LineDomainImpl}; - use stwo_cairo_verifier::fields::qm31::qm31; + use stwo_cairo_verifier::circle::{CosetImpl, CIRCLE_LOG_ORDER}; use stwo_cairo_verifier::fields::m31::m31; - use stwo_cairo_verifier::circle::{Coset, CosetImpl, CIRCLE_LOG_ORDER}; + use stwo_cairo_verifier::fields::qm31::qm31; use stwo_cairo_verifier::utils::pow; + use super::{LinePoly, LinePolyTrait, LineDomainImpl}; #[test] #[should_panic] diff --git a/stwo_cairo_verifier/src/queries.cairo b/stwo_cairo_verifier/src/queries.cairo index 279b701f..67d64bff 100644 --- a/stwo_cairo_verifier/src/queries.cairo +++ b/stwo_cairo_verifier/src/queries.cairo @@ -1,11 +1,8 @@ -use super::utils::{pow, bit_reverse_index, find}; -use core::traits::Copy; -use core::nullable::{NullableTrait, match_nullable, FromNullableResult}; -use core::dict::Felt252DictEntryTrait; -use stwo_cairo_verifier::poly::circle::{CircleDomain, CircleDomainImpl}; -use stwo_cairo_verifier::circle::{Coset, CosetImpl}; use stwo_cairo_verifier::channel::{Channel, ChannelTrait}; +use stwo_cairo_verifier::circle::CosetImpl; +use stwo_cairo_verifier::poly::circle::{CircleDomain, CircleDomainImpl}; use stwo_cairo_verifier::sort::MinimumToMaximumSortedIterator; +use super::utils::{pow, bit_reverse_index, find}; /// An ordered set of query indices over a bit reversed [CircleDomain]. @@ -161,8 +158,8 @@ pub impl SparseSubCircleDomainImpl of SparseSubCircleDomainTrait { #[cfg(test)] mod test { + use stwo_cairo_verifier::channel::ChannelTrait; use super::{Queries, QueriesImpl}; - use stwo_cairo_verifier::channel::{Channel, ChannelTrait}; #[test] fn test_fold_1() { diff --git a/stwo_cairo_verifier/src/sort.cairo b/stwo_cairo_verifier/src/sort.cairo index 0e873fc5..9f4851c8 100644 --- a/stwo_cairo_verifier/src/sort.cairo +++ b/stwo_cairo_verifier/src/sort.cairo @@ -1,5 +1,5 @@ -use core::array::ToSpanTrait; use core::array::ArrayTrait; +use core::array::ToSpanTrait; use core::option::OptionTrait; trait Compare { diff --git a/stwo_cairo_verifier/src/utils.cairo b/stwo_cairo_verifier/src/utils.cairo index 3b5e32f3..ea791d39 100644 --- a/stwo_cairo_verifier/src/utils.cairo +++ b/stwo_cairo_verifier/src/utils.cairo @@ -1,15 +1,13 @@ use core::array::SpanTrait; -use core::dict::Felt252Dict; -use core::traits::PanicDestruct; -use core::option::OptionTrait; use core::box::BoxTrait; +use core::dict::Felt252Dict; use core::dict::Felt252DictEntryTrait; use core::dict::Felt252DictTrait; -use core::iter::Iterator; use core::num::traits::BitSize; -use stwo_cairo_verifier::fields::qm31::{QM31, qm31}; -use stwo_cairo_verifier::BaseField; use core::traits::DivRem; +use core::traits::PanicDestruct; +use stwo_cairo_verifier::BaseField; +use stwo_cairo_verifier::fields::qm31::{QM31, qm31}; #[generate_trait] pub impl DictImpl, +PanicDestruct> of DictTrait { diff --git a/stwo_cairo_verifier/src/vcs/hasher.cairo b/stwo_cairo_verifier/src/vcs/hasher.cairo index 6f5f2f89..3050d81d 100644 --- a/stwo_cairo_verifier/src/vcs/hasher.cairo +++ b/stwo_cairo_verifier/src/vcs/hasher.cairo @@ -2,8 +2,6 @@ use core::array::ArrayTrait; use core::option::OptionTrait; use core::poseidon::poseidon_hash_span; use stwo_cairo_verifier::BaseField; -use core::hash::HashStateTrait; -use core::poseidon::{hades_permutation, HashState}; // A Merkle node hash is a hash of: // [left_child_hash, right_child_hash], column0_value, column1_value, ... @@ -65,8 +63,8 @@ pub impl PoseidonMerkleHasher of MerkleHasher { #[cfg(test)] mod tests { - use super::PoseidonMerkleHasher; use stwo_cairo_verifier::fields::m31::{m31}; + use super::PoseidonMerkleHasher; #[test] fn test_m31() { diff --git a/stwo_cairo_verifier/src/vcs/verifier.cairo b/stwo_cairo_verifier/src/vcs/verifier.cairo index d252e793..43cd1477 100644 --- a/stwo_cairo_verifier/src/vcs/verifier.cairo +++ b/stwo_cairo_verifier/src/vcs/verifier.cairo @@ -1,17 +1,17 @@ -use core::dict::Felt252DictTrait; -use core::result::ResultTrait; -use stwo_cairo_verifier::utils::SpanExTrait; -use core::option::OptionTrait; use core::array::ArrayTrait; use core::array::SpanTrait; use core::array::ToSpanTrait; +use core::cmp::min; use core::dict::Felt252Dict; use core::dict::Felt252DictEntryTrait; -use core::nullable::NullableTrait; -use core::cmp::min; +use core::dict::Felt252DictTrait; use core::fmt::{Debug, Formatter, Error}; +use core::nullable::NullableTrait; +use core::option::OptionTrait; +use core::result::ResultTrait; use stwo_cairo_verifier::BaseField; use stwo_cairo_verifier::fields::m31::m31; +use stwo_cairo_verifier::utils::SpanExTrait; use stwo_cairo_verifier::utils::{ArrayExTrait, DictTrait, OptBoxTrait}; use stwo_cairo_verifier::vcs::hasher::MerkleHasher;