Skip to content

Commit

Permalink
feat: use square-and-multiply for exp_reverse_bits_len
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-sun committed Jan 10, 2025
1 parent 76b1771 commit c9ce9e8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
24 changes: 8 additions & 16 deletions extensions/native/compiler/src/ir/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,30 +145,22 @@ impl<C: Config> Builder<C> {
/// 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>>,
bit_len: impl Into<RVar<C::N>>,
) -> V
pub fn exp_reverse_bits_len<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>,
{
let result: V = self.eval(V::Expression::ONE);
let power_f: V = self.eval(x);
let bit_len = bit_len.into();
let bit_len_plus_one = self.eval_expr(bit_len + C::N::ONE);
let one_var: V = self.eval(V::Expression::ONE);

self.range(RVar::one(), bit_len_plus_one)
.for_each(|i, builder| {
let index = builder.eval_expr(bit_len - i);
let bit = builder.get(power_bits, index);
let mul = V::select(builder, bit, power_f, one_var);
builder.assign(&result, result * mul);
builder.assign(&power_f, power_f * power_f);
});
// Implements a square-and-multiply algorithm.
self.iter(power_bits).for_each(|bit, builder| {
builder.assign(&result, result * result);
let mul = V::select(builder, bit, power_f, one_var);
builder.assign(&result, result * mul);
});

result
}

Expand Down
2 changes: 1 addition & 1 deletion extensions/native/recursion/src/fri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ where
let two_adic_gen_ext = two_adic_generator_f.to_operand().symbolic();
let two_adic_generator_ef: Ext<_, _> = builder.eval(two_adic_gen_ext);

let x = builder.exp_reverse_bits_len(two_adic_generator_ef, index_bits, log_max_height);
let x = builder.exp_reverse_bits_len(two_adic_generator_ef, index_bits);

builder
.range(0, commit_phase_commits.len())
Expand Down
7 changes: 2 additions & 5 deletions extensions/native/recursion/src/fri/two_adic_pcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,8 @@ pub fn verify_two_adic_pcs<C: Config>(
let two_adic_generator =
config.get_two_adic_generator(builder, log_height);
builder.cycle_tracker_start("exp-reverse-bits-len");
let two_adic_generator_exp = builder.exp_reverse_bits_len(
two_adic_generator,
&index_bits_shifted,
log_height,
);
let two_adic_generator_exp = builder
.exp_reverse_bits_len(two_adic_generator, &index_bits_shifted);
builder.cycle_tracker_end("exp-reverse-bits-len");
let x: Felt<C::F> = builder.eval(two_adic_generator_exp * g);

Expand Down

0 comments on commit c9ce9e8

Please sign in to comment.