Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmilson committed Nov 5, 2024
1 parent f24730b commit c7df8ae
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 61 deletions.
16 changes: 0 additions & 16 deletions stwo_cairo_verifier/Scarb.lock
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "snforge_scarb_plugin"
version = "0.32.0"
source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.32.0#3817c903b640201c72e743b9bbe70a97149828a2"

[[package]]
name = "snforge_std"
version = "0.32.0"
source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.32.0#3817c903b640201c72e743b9bbe70a97149828a2"
dependencies = [
"snforge_scarb_plugin",
]

[[package]]
name = "stwo_cairo_verifier"
version = "0.1.0"
dependencies = [
"snforge_std",
]
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
25 changes: 17 additions & 8 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 Expand Up @@ -557,7 +564,9 @@ mod test {

#[test]
fn test_fold_line_2() {
let domain = LineDomainImpl::new(CosetImpl::new(CirclePointIndexImpl::new(553648128), 1));
let domain = LineDomainImpl::new_unchecked(
CosetImpl::new(CirclePointIndexImpl::new(553648128), 1)
);
let values = array![
qm31(730692421, 1363821003, 2146256633, 106012305),
qm31(1387266930, 149259209, 1148988082, 1930518101)
Expand Down
Loading

0 comments on commit c7df8ae

Please sign in to comment.