Skip to content

Commit 111f70f

Browse files
committed
diluted
1 parent e29bfbc commit 111f70f

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

Diff for: src/air.cairo

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ mod global_values;
33
mod constants;
44
mod public_input;
55
mod public_memory;
6+
mod diluted;

Diff for: src/air/composition.cairo

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use cairo_verifier::air::global_values::{EcPoint, InteractionElements, GlobalValues};
2-
use cairo_verifier::air::constants::{PUBLIC_MEMORY_STEP};
2+
use cairo_verifier::air::constants::{PUBLIC_MEMORY_STEP, DILUTED_N_BITS, DILUTED_SPACING};
33
use cairo_verifier::air::public_input::{PublicInput, PublicInputTrait};
4+
use cairo_verifier::air::diluted::get_diluted_product;
45
use cairo_verifier::common::felt252::{Felt252Div, Felt252PartialOrd};
56

67
fn eval_composition_polynomial(
@@ -22,6 +23,11 @@ fn eval_composition_polynomial(
2223
.get_public_memory_product_ratio(memory_z, memory_alpha, public_memory_column_size);
2324

2425
// TODO diluted
26+
let diluted_z = interaction_elements.diluted_check_interaction_z;
27+
let diluted_alpha = interaction_elements.diluted_check_interaction_alpha;
28+
let diluted_prod = get_diluted_product(
29+
DILUTED_N_BITS, DILUTED_SPACING, diluted_z, diluted_alpha
30+
);
2531

2632
// TODO periodic cols
2733

Diff for: src/air/diluted.cairo

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use cairo_verifier::common::felt252::pow;
2+
3+
// The cumulative value is defined using the next recursive formula:
4+
// r_1 = 1, r_{j+1} = r_j * (1 + z * u_j) + alpha * u_j^2
5+
// where u_j = Dilute(j, spacing, n_bits) - Dilute(j-1, spacing, n_bits)
6+
// and we want to compute the final value r_{2^n_bits}.
7+
// Note that u_j depends only on the number of trailing zeros in the binary representation of j.
8+
// Specifically, u_{(1+2k)*2^i} = u_{2^i} = u_{2^{i-1}} + 2^{i*spacing} - 2^{(i-1)*spacing + 1}.
9+
//
10+
// The recursive formula can be reduced to a nonrecursive form:
11+
// r_j = prod_{n=1..j-1}(1+z*u_n) + alpha*sum_{n=1..j-1}(u_n^2 * prod_{m=n+1..j-1}(1+z*u_m))
12+
//
13+
// We rewrite this equation to generate a recursive formula that converges in log(j) steps:
14+
// Denote:
15+
// p_i = prod_{n=1..2^i-1}(1+z*u_n)
16+
// q_i = sum_{n=1..2^i-1}(u_n^2 * prod_{m=n+1..2^i-1}(1+z*u_m))
17+
// x_i = u_{2^i}.
18+
//
19+
// Clearly
20+
// r_{2^i} = p_i + alpha * q_i.
21+
// Moreover,
22+
// p_i = p_{i-1} * (1 + z * x_{i-1}) * p_{i-1}
23+
// q_i = q_{i-1} * (1 + z * x_{i-1}) * p_{i-1} + x_{i-1}^2 * p_{i-1} + q_{i-1}
24+
//
25+
// Now we can compute p_{n_bits} and q_{n_bits} in just n_bits recursive steps and we are done.
26+
fn get_diluted_product(n_bits: felt252, spacing: felt252, z: felt252, alpha: felt252) -> felt252 {
27+
let diff_multiplier = pow(2, spacing);
28+
let mut diff_x = diff_multiplier - 2;
29+
let mut x = 1;
30+
let mut p = 1 + z;
31+
let mut q = 1;
32+
33+
let mut i = 0;
34+
loop {
35+
if i == n_bits {
36+
break p + q * alpha;
37+
}
38+
39+
x = x + diff_x;
40+
diff_x *= diff_multiplier;
41+
let x_p = x * p;
42+
let y = p + z * x_p;
43+
q = q * y + x * x_p + q;
44+
p *= y;
45+
46+
i += 1;
47+
}
48+
}

Diff for: src/air/public_memory.cairo

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl PageImpl of PageTrait {
4040
let current = self.at(i);
4141

4242
res *= z - (*current.address + alpha * *current.value);
43+
i += 1;
4344
}
4445
}
4546
}
@@ -56,6 +57,8 @@ fn get_continuous_pages_product(page_headers: Span<ContinuousPageHeader>) -> (fe
5657

5758
res *= *current.prod;
5859
total_length += *current.size;
60+
61+
i += 1;
5962
}
6063
}
6164

0 commit comments

Comments
 (0)