diff --git a/src/fibonacci/bitcoin_script.rs b/src/fibonacci/bitcoin_script.rs index 7fa491f..823b5bd 100644 --- a/src/fibonacci/bitcoin_script.rs +++ b/src/fibonacci/bitcoin_script.rs @@ -1,6 +1,4 @@ -use core::slice; - -use crate::{constraints::ConstraintsGadget, fibonacci::FibonacciComposition, treepp::*}; +use crate::{constraints::ConstraintsGadget, treepp::*}; use num_traits::One; use rust_bitcoin_m31::qm31_add; use rust_bitcoin_m31::qm31_copy; @@ -16,6 +14,7 @@ use stwo_prover::core::{ circle::{CirclePoint, Coset}, fields::{m31::M31, qm31::QM31, FieldExpOps}, }; +use stwo_prover::examples::fibonacci::Fibonacci; /// Gadget for Fibonacci composition polynomial-related operations. pub struct FibonacciCompositionGadget; @@ -25,13 +24,16 @@ impl FibonacciCompositionGadget { #[allow(dead_code)] fn step_constraint_eval_quotient_by_mask_hint( log_size: u32, + claim: M31, z: CirclePoint, fz: QM31, fgz: QM31, fggz: QM31, ) -> Script { + let fib = Fibonacci::new(log_size, claim); + script! { - { FibonacciComposition::step_constraint_eval_quotient_by_mask(log_size, z, &[fz,fgz,fggz]) } + { fib.air.component.step_constraint_eval_quotient_by_mask(z, &[fz,fgz,fggz]) } } } @@ -104,12 +106,12 @@ impl FibonacciCompositionGadget { z: CirclePoint, fz: QM31, ) -> Script { - let res = FibonacciComposition::boundary_constraint_eval_quotient_by_mask( - log_size, - claim, - z, - slice::from_ref(&fz).try_into().unwrap(), - ); + let fib = Fibonacci::new(log_size, claim); + + let res = fib + .air + .component + .boundary_constraint_eval_quotient_by_mask(z, &[fz]); script! { { res } @@ -172,7 +174,7 @@ impl FibonacciCompositionGadget { ) -> Script { script! { { Self::boundary_constraint_eval_quotient_by_mask_hint(log_size, claim, z, fz) } - { Self::step_constraint_eval_quotient_by_mask_hint(log_size, z, fz, fgz, fggz) } + { Self::step_constraint_eval_quotient_by_mask_hint(log_size, claim, z, fz, fgz, fggz) } } } @@ -229,8 +231,7 @@ mod test { use crate::treepp::*; use crate::{ - fibonacci::{FibonacciComposition, FibonacciCompositionGadget}, - tests_utils::report::report_bitcoin_script_size, + fibonacci::FibonacciCompositionGadget, tests_utils::report::report_bitcoin_script_size, }; #[test] @@ -323,6 +324,7 @@ mod test { fn test_boundary_constraint_eval_quotient_by_mask() { let log_size = 5; let claim = m31::M31::from_u32_unchecked(443693538); + let fib = Fibonacci::new(log_size, claim); let mut prng = ChaCha20Rng::seed_from_u64(0); @@ -361,12 +363,10 @@ mod test { M31::reduce(prng.next_u64()), ); - let res = FibonacciComposition::boundary_constraint_eval_quotient_by_mask( - log_size, - claim, - z, - &[fz], - ); + let res = fib + .air + .component + .boundary_constraint_eval_quotient_by_mask(z, &[fz]); let script = script! { { FibonacciCompositionGadget::boundary_constraint_eval_quotient_by_mask_hint(log_size, claim, z, fz) } //hint @@ -386,6 +386,8 @@ mod test { #[test] fn test_step_constraint_eval_quotient_by_mask() { let log_size = 5; + let claim = m31::M31::from_u32_unchecked(443693538); + let fib = Fibonacci::new(log_size, claim); let mut prng = ChaCha20Rng::seed_from_u64(0); @@ -438,14 +440,13 @@ mod test { M31::reduce(prng.next_u64()), ); - let res = FibonacciComposition::step_constraint_eval_quotient_by_mask( - log_size, - z, - &[fz, fgz, fggz], - ); + let res = fib + .air + .component + .step_constraint_eval_quotient_by_mask(z, &[fz, fgz, fggz]); let script = script! { - { FibonacciCompositionGadget::step_constraint_eval_quotient_by_mask_hint(log_size, z, fz, fgz, fggz) } //hint + { FibonacciCompositionGadget::step_constraint_eval_quotient_by_mask_hint(log_size, claim, z, fz, fgz, fggz) } //hint { fggz } { fgz } { fz } diff --git a/src/fibonacci/mod.rs b/src/fibonacci/mod.rs index b40a028..78b63ca 100644 --- a/src/fibonacci/mod.rs +++ b/src/fibonacci/mod.rs @@ -1,58 +1,5 @@ mod bitcoin_script; pub use bitcoin_script::*; -use num_traits::One; -use stwo_prover::core::{ - circle::{CirclePoint, Coset}, - constraints::{coset_vanishing, pair_vanishing}, - fields::{ - m31::{BaseField, M31}, - ExtensionOf, FieldExpOps, - }, -}; - -///Fibonacci composition polynomial-related methods are PRIVATE, so we need to copy-paste them from stwo! -/// TODO: solve this problem -pub struct FibonacciComposition; - -impl FibonacciComposition { - ///step - fn step_constraint_eval_quotient_by_mask>( - log_size: u32, - point: CirclePoint, - mask: &[F; 3], - ) -> F { - let constraint_zero_domain = Coset::subgroup(log_size); - let constraint_value = mask[0].square() + mask[1].square() - mask[2]; - let selector = pair_vanishing( - constraint_zero_domain - .at(constraint_zero_domain.size() - 2) - .into_ef(), - constraint_zero_domain - .at(constraint_zero_domain.size() - 1) - .into_ef(), - point, - ); - let num = constraint_value * selector; - let denom = coset_vanishing(constraint_zero_domain, point); - num / denom - } - - ///boundary - pub fn boundary_constraint_eval_quotient_by_mask>( - log_size: u32, - claim: M31, - point: CirclePoint, - mask: &[F; 1], - ) -> F { - let constraint_zero_domain = Coset::subgroup(log_size); - let p = constraint_zero_domain.at(constraint_zero_domain.size() - 1); - let linear = F::one() + point.y * (claim - BaseField::one()) * p.y.inverse(); - - let num = mask[0] - linear; - let denom = pair_vanishing(p.into_ef(), CirclePoint::zero(), point); - num / denom - } -} #[cfg(test)] mod test {