Skip to content

Commit

Permalink
perf(wip): remove extraneous functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpwang committed Dec 28, 2024
1 parent aa8e9a4 commit 2f99d1b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 140 deletions.
38 changes: 12 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion extensions/native/recursion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tempfile = "3.14.0"
bitcode = { workspace = true }

[features]
default = ["parallel", "mimalloc"]
default = ["parallel", "mimalloc", "static-verifier"]
parallel = ["openvm-stark-backend/parallel"]
static-verifier = [
"openvm-native-compiler/halo2-compiler",
Expand Down
3 changes: 3 additions & 0 deletions extensions/native/recursion/src/challenger/multi_field32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ impl<C: Config> MultiField32ChallengerVariable<C> {

pub fn sample_bits(&mut self, builder: &mut Builder<C>, bits: usize) -> Var<C::N> {
let rand_f = self.sample(builder);
builder.print_f(rand_f);
let rand_f_bits = builder.num2bits_f_circuit(rand_f);
builder.bits2num_v_circuit(&rand_f_bits[0..bits])
}

pub fn check_witness(&mut self, builder: &mut Builder<C>, bits: usize, witness: Felt<C::F>) {
self.observe(builder, witness);
let element = self.sample_bits(builder, bits);
builder.print_v(element);
builder.assert_var_eq(element, C::N::from_canonical_usize(0));
}
}
Expand Down Expand Up @@ -128,6 +130,7 @@ impl<C: Config> CanSampleBitsVariable<C> for MultiField32ChallengerVariable<C> {
nb_bits: RVar<C::N>,
) -> Array<C, Var<C::N>> {
let rand_f = self.sample(builder);
builder.print_f(rand_f);
let rand_f_bits = builder.num2bits_f_circuit(rand_f);
builder.vec(rand_f_bits[..nb_bits.value()].to_vec())
}
Expand Down
94 changes: 1 addition & 93 deletions extensions/native/recursion/src/fri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ use openvm_native_compiler::{
use openvm_stark_backend::p3_field::{Field, FieldAlgebra, TwoAdicField};
pub use two_adic_pcs::*;

use self::types::{
DimensionsVariable, FriChallengesVariable, FriConfigVariable, FriProofVariable,
FriQueryProofVariable,
};
use self::types::{DimensionsVariable, FriConfigVariable, FriQueryProofVariable};
use crate::{
challenger::ChallengerVariable,
digest::{CanPoseidon2Digest, DigestVariable},
outer_poseidon2::Poseidon2CircuitBuilder,
utils::cond_eval,
Expand All @@ -27,94 +23,6 @@ pub mod two_adic_pcs;
pub mod types;
pub mod witness;

/// Reference: https://github.com/Plonky3/Plonky3/blob/4809fa7bedd9ba8f6f5d3267b1592618e3776c57/fri/src/verifier.rs#L27
pub fn verify_shape_and_sample_challenges<C: Config>(
builder: &mut Builder<C>,
config: &FriConfigVariable<C>,
proof: &FriProofVariable<C>,
challenger: &mut impl ChallengerVariable<C>,
) -> FriChallengesVariable<C> {
let betas: Array<C, Ext<C::F, C::EF>> = builder.array(proof.commit_phase_commits.len());

builder
.range(0, proof.commit_phase_commits.len())
.for_each(|i, builder| {
let comm = builder.get(&proof.commit_phase_commits, i);
challenger.observe_digest(builder, comm);
let sample = challenger.sample_ext(builder);
builder.set(&betas, i, sample);
});

builder
.range(0, proof.final_poly.len())
.for_each(|i, builder| {
let final_poly_elem = builder.get(&proof.final_poly, i);
let final_poly_elem_felts = builder.ext2felt(final_poly_elem);
challenger.observe_slice(builder, final_poly_elem_felts);
});

let num_query_proofs = proof.query_proofs.len().clone();
builder
.if_ne(num_query_proofs, RVar::from(config.num_queries))
.then(|builder| {
builder.error();
});

challenger.check_witness(builder, config.proof_of_work_bits, proof.pow_witness);

let log_max_height =
builder.eval_expr(proof.commit_phase_commits.len() + RVar::from(config.log_blowup));
let query_indices = builder.array(config.num_queries);
builder.range(0, config.num_queries).for_each(|i, builder| {
let index_bits = challenger.sample_bits(builder, log_max_height);
builder.set(&query_indices, i, index_bits);
});

FriChallengesVariable {
query_indices,
betas,
}
}

/// Verifies a set of FRI challenges.
///
/// Reference: https://github.com/Plonky3/Plonky3/blob/4809fa7bedd9ba8f6f5d3267b1592618e3776c57/fri/src/verifier.rs#L67
#[allow(clippy::type_complexity)]
pub fn verify_challenges<C: Config>(
builder: &mut Builder<C>,
config: &FriConfigVariable<C>,
proof: &FriProofVariable<C>,
challenges: &FriChallengesVariable<C>,
reduced_openings: &Array<C, Array<C, Ext<C::F, C::EF>>>,
) where
C::F: TwoAdicField,
C::EF: TwoAdicField,
{
let log_max_height =
builder.eval_expr(proof.commit_phase_commits.len() + RVar::from(config.log_blowup));
builder
.range(0, challenges.query_indices.len())
.for_each(|i, builder| {
let index_bits = builder.get(&challenges.query_indices, i);
let query_proof = builder.get(&proof.query_proofs, i);
let ro = builder.get(reduced_openings, i);

let folded_eval = verify_query(
builder,
config,
&proof.commit_phase_commits,
&index_bits,
&query_proof,
&challenges.betas,
&ro,
log_max_height,
);

let final_poly_elem = builder.get(&proof.final_poly, 0);
builder.assert_ext_eq(folded_eval, final_poly_elem);
});
}

/// Verifies a FRI query.
///
/// Currently assumes the index that is accessed is constant.
Expand Down
Loading

0 comments on commit 2f99d1b

Please sign in to comment.