Skip to content

Commit

Permalink
fri_commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Dec 20, 2023
1 parent 20a720d commit 4370950
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/channel/channel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ struct Channel {
counter: u256,
}

// A wrapper around felt with a guarantee that the felt must be read from the channel before
// use.
#[derive(Drop)]
struct ChannelUnsentFelt {
value: felt252,
}

// A wrapper around felt with a guarantee that the felt was read from the channel as data from the
// prover.
#[derive(Drop)]
struct ChannelSentFelt {
value: felt252,
}

#[generate_trait]
impl ChannelImpl of ChannelTrait {
fn new(digest: u256) -> Channel {
Expand Down Expand Up @@ -61,4 +75,13 @@ impl ChannelImpl of ChannelTrait {
};
res
}

// Reads a field element vector from the prover. Unlike read_felts_from_prover, this hashes all the
// field elements at once. See Channel.
fn read_felt_vector_from_prover(
ref self: Channel, values: Span<ChannelUnsentFelt>
) -> Array<ChannelSentFelt> {
let sent_felts = ArrayTrait::<ChannelSentFelt>::new();
sent_felts
}
}
54 changes: 53 additions & 1 deletion src/fri/fri.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::array::SpanTrait;
use core::traits::Destruct;
use cairo_verifier::channel::channel::ChannelTrait;
use cairo_verifier::table_commitment::{
Expand All @@ -6,6 +7,32 @@ use cairo_verifier::table_commitment::{
use core::array::ArrayTrait;
use cairo_verifier::table_commitment::table_commit;
use cairo_verifier::channel::channel::Channel;
use cairo_verifier::channel::channel::{ChannelUnsentFelt, ChannelSentFelt};
use cairo_verifier::fri::fri_config::FriConfig;
use cairo_verifier::common::math;

// Commitment values for FRI. Used to generate a commitment by "reading" these values
// from the channel.
#[derive(Drop, Copy)]
struct FriUnsentCommitment {
// Array of size n_layers - 1 containing unsent table commitments for each inner layer.
inner_layers: Span<TableUnsentCommitment>,
// Array of size 2**log_last_layer_degree_bound containing coefficients for the last layer
// polynomial.
last_layer_coefficients: Span<ChannelUnsentFelt>,
}

#[derive(Drop, Copy)]
struct FriCommitment {
config: FriConfig,
// Array of size n_layers - 1 containing table commitments for each inner layer.
inner_layers: Span<TableCommitment>,
// Array of size n_layers, of one evaluation point for each layer.
eval_points: Span<felt252>,
// Array of size 2**log_last_layer_degree_bound containing coefficients for the last layer
// polynomial.
last_layer_coefficients: Span<ChannelSentFelt>,
}

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

(commitments, eval_points)
}

fn fri_commit(
ref channel: Channel, unsent_commitment: FriUnsentCommitment, config: FriConfig
) -> FriCommitment {
assert((*config.fri_step_sizes.at(0)) == 0, 'Invalid value');

let (commitments, eval_points) = fri_commit_rounds(
ref channel,
config.n_layers,
config.inner_layers,
unsent_commitment.inner_layers,
config.fri_step_sizes,
);

// Read last layer coefficients.
let n_coefficients = math::pow(2, config.log_last_layer_degree_bound);
let coefficients = channel
.read_felt_vector_from_prover(unsent_commitment.last_layer_coefficients);

FriCommitment {
config: config,
inner_layers: commitments.span(),
eval_points: eval_points.span(),
last_layer_coefficients: coefficients.span()
}
}
4 changes: 2 additions & 2 deletions src/fri/fri_config.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ const MAX_LAST_LAYER_LOG_DEGREE_BOUND: u32 = 15;
const MAX_FRI_LAYERS: u32 = 15;
const MAX_FRI_STEP: u32 = 4;

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

0 comments on commit 4370950

Please sign in to comment.