From 4370950eb5a06c767140f4c27e1798487b01c063 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Wed, 20 Dec 2023 19:27:38 +0100 Subject: [PATCH] fri_commit --- src/channel/channel.cairo | 23 +++++++++++++++++ src/fri/fri.cairo | 54 ++++++++++++++++++++++++++++++++++++++- src/fri/fri_config.cairo | 4 +-- 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/src/channel/channel.cairo b/src/channel/channel.cairo index 120fbbeeb..7e3375ef7 100644 --- a/src/channel/channel.cairo +++ b/src/channel/channel.cairo @@ -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 { @@ -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 + ) -> Array { + let sent_felts = ArrayTrait::::new(); + sent_felts + } } diff --git a/src/fri/fri.cairo b/src/fri/fri.cairo index 6a7e95ead..2400653be 100644 --- a/src/fri/fri.cairo +++ b/src/fri/fri.cairo @@ -1,3 +1,4 @@ +use core::array::SpanTrait; use core::traits::Destruct; use cairo_verifier::channel::channel::ChannelTrait; use cairo_verifier::table_commitment::{ @@ -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, + // Array of size 2**log_last_layer_degree_bound containing coefficients for the last layer + // polynomial. + last_layer_coefficients: Span, +} + +#[derive(Drop, Copy)] +struct FriCommitment { + config: FriConfig, + // Array of size n_layers - 1 containing table commitments for each inner layer. + inner_layers: Span, + // Array of size n_layers, of one evaluation point for each layer. + eval_points: Span, + // Array of size 2**log_last_layer_degree_bound containing coefficients for the last layer + // polynomial. + last_layer_coefficients: Span, +} // 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. @@ -34,7 +61,6 @@ fn fri_commit_rounds( configs: Span, unsent_commitments: Span, step_sizes: Span, - commitments: Span, ) -> (Array, Array) { let mut commitments = ArrayTrait::::new(); let mut eval_points = ArrayTrait::::new(); @@ -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() + } +} diff --git a/src/fri/fri_config.cairo b/src/fri/fri_config.cairo index 4f4fb4e60..e101a5dd9 100644 --- a/src/fri/fri_config.cairo +++ b/src/fri/fri_config.cairo @@ -9,7 +9,7 @@ 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, @@ -17,7 +17,7 @@ struct FriConfig { 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, + inner_layers: Span, // 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,