Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmilson committed Nov 17, 2024
1 parent 97b4758 commit c229d88
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 42 deletions.
1 change: 1 addition & 0 deletions stwo_cairo_verifier/src/circle.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub trait CirclePointTrait<

impl CirclePointAdd<F, +Add<F>, +Sub<F>, +Mul<F>, +Drop<F>, +Copy<F>> of Add<CirclePoint<F>> {
/// Performs the operation of the circle as a group with additive notation.
#[inline]
fn add(lhs: CirclePoint<F>, rhs: CirclePoint<F>) -> CirclePoint<F> {
CirclePoint { x: lhs.x * rhs.x - lhs.y * rhs.y, y: lhs.x * rhs.y + lhs.y * rhs.x }
}
Expand Down
21 changes: 14 additions & 7 deletions stwo_cairo_verifier/src/fri.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ impl FriLayerVerifierImpl of FriLayerVerifierTrait {

let subline_initial_index = bit_reverse_index(subline_start, self.domain.log_size());
let subline_initial = self.domain.coset.index_at(subline_initial_index);
let subline_domain = LineDomainImpl::new(CosetImpl::new(subline_initial, FOLD_STEP));
let subline_domain = LineDomainImpl::new_unchecked(
CosetImpl::new(subline_initial, FOLD_STEP)
);

all_subline_evals.append(LineEvaluationImpl::new(subline_domain, subline_evals));
};
Expand Down Expand Up @@ -238,7 +240,7 @@ pub impl FriVerifierImpl of FriVerifierTrait {

let mut inner_layers = array![];
let mut layer_bound = *max_column_bound - CIRCLE_TO_LINE_FOLD_STEP;
let mut layer_domain = LineDomainImpl::new(
let mut layer_domain = LineDomainImpl::new_unchecked(
CosetImpl::half_odds(layer_bound + config.log_blowup_factor)
);

Expand Down Expand Up @@ -316,7 +318,6 @@ pub impl FriVerifierImpl of FriVerifierTrait {
self: @FriVerifier, queries: @Queries, decommitted_values: Array<SparseCircleEvaluation>
) -> Result<(), FriVerificationError> {
assert!(queries.log_domain_size == self.expected_query_log_domain_size);

let (last_layer_queries, last_layer_query_evals) = self
.decommit_inner_layers(queries, @decommitted_values)?;
self.decommit_last_layer(last_layer_queries, last_layer_query_evals)
Expand Down Expand Up @@ -397,17 +398,23 @@ pub impl FriVerifierImpl of FriVerifierTrait {
) -> Result<(), FriVerificationError> {
let FriVerifier { last_layer_domain, last_layer_poly, .. } = self;

let domain_log_size = last_layer_domain.log_size();
// TODO(andrew): Note depending on the proof parameters, doing FFT on the last layer poly vs
// pointwize evaluation is less efficient.
let last_layer_evals = last_layer_poly.evaluate(*last_layer_domain).values;

let mut i = 0;
loop {
if i == queries.positions.len() {
break Result::Ok(());
}

let query = *queries.positions[i];
let query_eval = *query_evals[i];
let x = last_layer_domain.at(bit_reverse_index(query, last_layer_domain.log_size()));
// TODO(andrew): Makes more sense for the proof to provide coeffs in natural order and
// the FFT return evals in bit-reversed order to prevent this unnessesary bit-reverse.
let last_layer_eval_i = bit_reverse_index(query, domain_log_size);

if query_eval != last_layer_poly.eval_at_point(x.into()) {
if query_evals[i] != last_layer_evals[last_layer_eval_i] {
break Result::Err(FriVerificationError::LastLayerEvaluationsInvalid);
}

Expand Down Expand Up @@ -514,7 +521,7 @@ pub fn fold_circle_into_line(eval: CircleEvaluation, alpha: QM31) -> LineEvaluat
let (f0, f1) = ibutterfly(*f_p, *f_neg_p, p.y.inverse());
values.append(f0 + alpha * f1);
};
LineEvaluation { values, domain: LineDomainImpl::new(domain.half_coset) }
LineEvaluation { values, domain: LineDomainImpl::new_unchecked(domain.half_coset) }
}

pub fn ibutterfly(v0: QM31, v1: QM31, itwid: M31) -> (QM31, QM31) {
Expand Down
Loading

0 comments on commit c229d88

Please sign in to comment.