Skip to content

Commit 4370950

Browse files
committed
fri_commit
1 parent 20a720d commit 4370950

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

Diff for: src/channel/channel.cairo

+23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ struct Channel {
1717
counter: u256,
1818
}
1919

20+
// A wrapper around felt with a guarantee that the felt must be read from the channel before
21+
// use.
22+
#[derive(Drop)]
23+
struct ChannelUnsentFelt {
24+
value: felt252,
25+
}
26+
27+
// A wrapper around felt with a guarantee that the felt was read from the channel as data from the
28+
// prover.
29+
#[derive(Drop)]
30+
struct ChannelSentFelt {
31+
value: felt252,
32+
}
33+
2034
#[generate_trait]
2135
impl ChannelImpl of ChannelTrait {
2236
fn new(digest: u256) -> Channel {
@@ -61,4 +75,13 @@ impl ChannelImpl of ChannelTrait {
6175
};
6276
res
6377
}
78+
79+
// Reads a field element vector from the prover. Unlike read_felts_from_prover, this hashes all the
80+
// field elements at once. See Channel.
81+
fn read_felt_vector_from_prover(
82+
ref self: Channel, values: Span<ChannelUnsentFelt>
83+
) -> Array<ChannelSentFelt> {
84+
let sent_felts = ArrayTrait::<ChannelSentFelt>::new();
85+
sent_felts
86+
}
6487
}

Diff for: src/fri/fri.cairo

+53-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::array::SpanTrait;
12
use core::traits::Destruct;
23
use cairo_verifier::channel::channel::ChannelTrait;
34
use cairo_verifier::table_commitment::{
@@ -6,6 +7,32 @@ use cairo_verifier::table_commitment::{
67
use core::array::ArrayTrait;
78
use cairo_verifier::table_commitment::table_commit;
89
use cairo_verifier::channel::channel::Channel;
10+
use cairo_verifier::channel::channel::{ChannelUnsentFelt, ChannelSentFelt};
11+
use cairo_verifier::fri::fri_config::FriConfig;
12+
use cairo_verifier::common::math;
13+
14+
// Commitment values for FRI. Used to generate a commitment by "reading" these values
15+
// from the channel.
16+
#[derive(Drop, Copy)]
17+
struct FriUnsentCommitment {
18+
// Array of size n_layers - 1 containing unsent table commitments for each inner layer.
19+
inner_layers: Span<TableUnsentCommitment>,
20+
// Array of size 2**log_last_layer_degree_bound containing coefficients for the last layer
21+
// polynomial.
22+
last_layer_coefficients: Span<ChannelUnsentFelt>,
23+
}
24+
25+
#[derive(Drop, Copy)]
26+
struct FriCommitment {
27+
config: FriConfig,
28+
// Array of size n_layers - 1 containing table commitments for each inner layer.
29+
inner_layers: Span<TableCommitment>,
30+
// Array of size n_layers, of one evaluation point for each layer.
31+
eval_points: Span<felt252>,
32+
// Array of size 2**log_last_layer_degree_bound containing coefficients for the last layer
33+
// polynomial.
34+
last_layer_coefficients: Span<ChannelSentFelt>,
35+
}
936

1037
// A FRI phase with N layers starts with a single input layer.
1138
// Afterwards, there are N - 1 inner layers resulting from FRI-folding each preceding layer.
@@ -34,7 +61,6 @@ fn fri_commit_rounds(
3461
configs: Span<TableCommitmentConfig>,
3562
unsent_commitments: Span<TableUnsentCommitment>,
3663
step_sizes: Span<felt252>,
37-
commitments: Span<TableCommitment>,
3864
) -> (Array<TableCommitment>, Array<felt252>) {
3965
let mut commitments = ArrayTrait::<TableCommitment>::new();
4066
let mut eval_points = ArrayTrait::<felt252>::new();
@@ -54,3 +80,29 @@ fn fri_commit_rounds(
5480

5581
(commitments, eval_points)
5682
}
83+
84+
fn fri_commit(
85+
ref channel: Channel, unsent_commitment: FriUnsentCommitment, config: FriConfig
86+
) -> FriCommitment {
87+
assert((*config.fri_step_sizes.at(0)) == 0, 'Invalid value');
88+
89+
let (commitments, eval_points) = fri_commit_rounds(
90+
ref channel,
91+
config.n_layers,
92+
config.inner_layers,
93+
unsent_commitment.inner_layers,
94+
config.fri_step_sizes,
95+
);
96+
97+
// Read last layer coefficients.
98+
let n_coefficients = math::pow(2, config.log_last_layer_degree_bound);
99+
let coefficients = channel
100+
.read_felt_vector_from_prover(unsent_commitment.last_layer_coefficients);
101+
102+
FriCommitment {
103+
config: config,
104+
inner_layers: commitments.span(),
105+
eval_points: eval_points.span(),
106+
last_layer_coefficients: coefficients.span()
107+
}
108+
}

Diff for: src/fri/fri_config.cairo

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ const MAX_LAST_LAYER_LOG_DEGREE_BOUND: u32 = 15;
99
const MAX_FRI_LAYERS: u32 = 15;
1010
const MAX_FRI_STEP: u32 = 4;
1111

12-
#[derive(Drop)]
12+
#[derive(Drop, Copy)]
1313
struct FriConfig {
1414
// Log2 of the size of the input layer to FRI.
1515
log_input_size: felt252,
1616
// Number of layers in the FRI. Inner + last layer.
1717
n_layers: felt252,
1818
// Array of size n_layers - 1, each entry is a configuration of a table commitment for the
1919
// corresponding inner layer.
20-
inner_layers: Array<TableCommitmentConfig>,
20+
inner_layers: Span<TableCommitmentConfig>,
2121
// Array of size n_layers, each entry represents the FRI step size,
2222
// i.e. the number of FRI-foldings between layer i and i+1.
2323
fri_step_sizes: Span<felt252>,

0 commit comments

Comments
 (0)