Skip to content

Commit

Permalink
Merge pull request #30 from victorkstarkware/main
Browse files Browse the repository at this point in the history
removed duplicated code from stwo
  • Loading branch information
victorkstarkware authored Jun 2, 2024
2 parents e99ff23 + 9178db3 commit 10b8369
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 78 deletions.
51 changes: 26 additions & 25 deletions src/fibonacci/bitcoin_script.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -25,13 +24,16 @@ impl FibonacciCompositionGadget {
#[allow(dead_code)]
fn step_constraint_eval_quotient_by_mask_hint(
log_size: u32,
claim: M31,
z: CirclePoint<QM31>,
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]) }
}
}

Expand Down Expand Up @@ -104,12 +106,12 @@ impl FibonacciCompositionGadget {
z: CirclePoint<QM31>,
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 }
Expand Down Expand Up @@ -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) }
}
}

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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 }
Expand Down
53 changes: 0 additions & 53 deletions src/fibonacci/mod.rs
Original file line number Diff line number Diff line change
@@ -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<F: ExtensionOf<BaseField>>(
log_size: u32,
point: CirclePoint<F>,
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<F: ExtensionOf<BaseField>>(
log_size: u32,
claim: M31,
point: CirclePoint<F>,
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 {
Expand Down

0 comments on commit 10b8369

Please sign in to comment.