Skip to content

Commit

Permalink
feat: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-sun committed Jan 10, 2025
1 parent 8fc92db commit df89bb1
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

10 changes: 6 additions & 4 deletions extensions/native/compiler/src/ir/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ impl<C: Config> Builder<C> {
}
}

pub fn zipped_iter<'a>(
pub fn zip<'a>(
&'a mut self,
arrays: &'a [Box<dyn ArrayLike<C> + 'a>],
) -> ZippedPointerIteratorBuilder<'a, C> {
Expand Down Expand Up @@ -419,12 +419,14 @@ impl<C: Config> Builder<C> {
let end: Var<C::N> = self.eval(
array.ptr().address
+ len
* RVar::from_field(C::N::from_canonical_usize(array.size_of())),
* RVar::from_field(C::N::from_canonical_usize(
array.element_size_of(),
)),
);
end.into()
})
.collect(),
step_sizes: arrays.iter().map(|array| array.size_of()).collect(),
step_sizes: arrays.iter().map(|array| array.element_size_of()).collect(),
builder: self,
}
} else {
Expand Down Expand Up @@ -528,7 +530,7 @@ impl<C: Config> Builder<C> {
let arr = self.dyn_array(vlen);

// Write the content hints directly into the array memory.
self.zipped_iter(&[Box::new(arr.clone()) as Box<dyn ArrayLike<C>>])
self.zip(&[Box::new(arr.clone()) as Box<dyn ArrayLike<C>>])
.for_each(|ptr_vec, builder| {
let index = MemIndex {
index: 0.into(),
Expand Down
14 changes: 12 additions & 2 deletions extensions/native/compiler/src/ir/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ impl<C: Config> Builder<C> {
Ref::from_ptr(ptr)
}

/// Intended to be used with `ptr` from `zip`. Assumes that:
/// - if `slice` is `Array::Fixed`, then `ptr` is a constant index in [0, slice.len()).
/// - if `slice` is `Array::Dyn`, then `ptr` is a variable iterator over the entries of `slice`.
///
/// In both cases, loads and returns the corresponding element of `slice`.
pub fn iter_ptr_get<V: MemVariable<C>>(&mut self, slice: &Array<C, V>, ptr: RVar<C::N>) -> V {
match slice {
Array::Fixed(v) => {
Expand Down Expand Up @@ -305,6 +310,11 @@ impl<C: Config> Builder<C> {
}
}

/// Intended to be used with `ptr` from `zip`. Assumes that:
/// - if `slice` is `Array::Fixed`, then `ptr` is a constant index in [0, slice.len()).
/// - if `slice` is `Array::Dyn`, then `ptr` is a variable iterator over the entries of `slice`.
///
/// In both cases, stores the given `value` at the corresponding element of `slice`.
pub fn iter_ptr_set<V: MemVariable<C>, Expr: Into<V::Expression>>(
&mut self,
slice: &Array<C, V>,
Expand Down Expand Up @@ -566,7 +576,7 @@ pub trait ArrayLike<C: Config> {

fn is_fixed(&self) -> bool;

fn size_of(&self) -> usize;
fn element_size_of(&self) -> usize;
}

impl<C: Config, T: MemVariable<C>> ArrayLike<C> for Array<C, T> {
Expand All @@ -585,7 +595,7 @@ impl<C: Config, T: MemVariable<C>> ArrayLike<C> for Array<C, T> {
}
}

fn size_of(&self) -> usize {
fn element_size_of(&self) -> usize {
T::size_of()
}
}
4 changes: 2 additions & 2 deletions extensions/native/compiler/src/ir/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ impl<C: Config> Builder<C> {
result
}

/// Exponentiates a variable to a list of reversed bits with a given length.
/// Exponentiates a variable to a list of big endian bits with a given length.
///
/// Example: if power_bits = [1, 0, 1, 0], then the result should be x^8 * x^2 = x^10.
///
/// Reference: [`openvm_stark_backend::p3_util::reverse_bits_len`]
pub fn exp_reverse_bits_len<V>(&mut self, x: V, power_bits: &Array<C, Var<C::N>>) -> V
pub fn exp_bits_big_endian<V>(&mut self, x: V, power_bits: &Array<C, Var<C::N>>) -> V
where
V::Expression: FieldAlgebra,
V: Copy + Mul<Output = V::Expression> + Variable<C> + CanSelect<C>,
Expand Down
6 changes: 3 additions & 3 deletions extensions/native/recursion/src/fri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where
let two_adic_generator_ef: Ext<_, _> = builder.eval(two_adic_gen_ext);

let index_bits_truncated = index_bits.slice(builder, 0, log_max_height);
let x = builder.exp_reverse_bits_len(two_adic_generator_ef, &index_bits_truncated);
let x = builder.exp_bits_big_endian(two_adic_generator_ef, &index_bits_truncated);

builder
.range(0, commit_phase_commits.len())
Expand Down Expand Up @@ -195,7 +195,7 @@ pub fn verify_batch<C: Config>(
let left: Ptr<C::N> = builder.uninit();
let right: Ptr<C::N> = builder.uninit();
builder
.zipped_iter(&[
.zip(&[
Box::new(proof.clone()) as Box<dyn ArrayLike<C>>,
Box::new(index_bits.clone()) as Box<dyn ArrayLike<C>>,
])
Expand Down Expand Up @@ -365,7 +365,7 @@ where
let dims_shifted = dims.shift(builder, start_dim_idx.clone());
let opened_values_shifted = opened_values.shift(builder, start_dim_idx);
builder
.zipped_iter(&[
.zip(&[
Box::new(dims_shifted.clone()) as Box<dyn ArrayLike<C>>,
Box::new(opened_values_shifted.clone()) as Box<dyn ArrayLike<C>>,
])
Expand Down
10 changes: 5 additions & 5 deletions extensions/native/recursion/src/fri/two_adic_pcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn verify_two_adic_pcs<C: Config>(
builder.cycle_tracker_start("stage-d-verifier-verify");
let betas: Array<C, Ext<C::F, C::EF>> = builder.array(proof.commit_phase_commits.len());
builder
.zipped_iter(&[
.zip(&[
Box::new(proof.commit_phase_commits.clone()) as Box<dyn ArrayLike<C>>,
Box::new(betas.clone()) as Box<dyn ArrayLike<C>>,
])
Expand Down Expand Up @@ -106,7 +106,7 @@ pub fn verify_two_adic_pcs<C: Config>(
}

builder
.zipped_iter(&[
.zip(&[
Box::new(query_proof.input_proof.clone()) as Box<dyn ArrayLike<C>>,
Box::new(rounds.clone()) as Box<dyn ArrayLike<C>>,
])
Expand Down Expand Up @@ -179,7 +179,7 @@ pub fn verify_two_adic_pcs<C: Config>(
let opened_values = batch_opening.opened_values;

builder
.zipped_iter(&[
.zip(&[
Box::new(opened_values.clone()) as Box<dyn ArrayLike<C>>,
Box::new(mats.clone()) as Box<dyn ArrayLike<C>>,
])
Expand All @@ -206,15 +206,15 @@ pub fn verify_two_adic_pcs<C: Config>(

let index_bits_shifted_truncated =
index_bits_shifted.slice(builder, 0, log_height);
let two_adic_generator_exp = builder.exp_reverse_bits_len(
let two_adic_generator_exp = builder.exp_bits_big_endian(
two_adic_generator,
&index_bits_shifted_truncated,
);
builder.cycle_tracker_end("exp-reverse-bits-len");
let x: Felt<C::F> = builder.eval(two_adic_generator_exp * g);

builder
.zipped_iter(&[
.zip(&[
Box::new(mat_points.clone()) as Box<dyn ArrayLike<C>>,
Box::new(mat_values.clone()) as Box<dyn ArrayLike<C>>,
])
Expand Down
4 changes: 2 additions & 2 deletions extensions/native/recursion/src/hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<C: Config, I: VecAutoHintable + Hintable<C>> Hintable<C> for Vec<I> {
let len = builder.hint_var();
let arr = builder.dyn_array(len);
builder
.zipped_iter(&[Box::new(arr.clone()) as Box<dyn ArrayLike<C>>])
.zip(&[Box::new(arr.clone()) as Box<dyn ArrayLike<C>>])
.for_each(|idx_vec, builder| {
let hint = I::read(builder);
let ptr = idx_vec[0];
Expand Down Expand Up @@ -263,7 +263,7 @@ impl Hintable<InnerConfig> for Vec<Vec<InnerChallenge>> {
let len = builder.hint_var();
let arr = builder.dyn_array(len);
builder
.zipped_iter(&[Box::new(arr.clone()) as Box<dyn ArrayLike<InnerConfig>>])
.zip(&[Box::new(arr.clone()) as Box<dyn ArrayLike<InnerConfig>>])
.for_each(|idx_vec, builder| {
let hint = Vec::<InnerChallenge>::read(builder);
builder.iter_ptr_set(&arr, idx_vec[0], hint);
Expand Down

0 comments on commit df89bb1

Please sign in to comment.