Skip to content

Commit

Permalink
refactor&fri development
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Dec 21, 2023
1 parent 4370950 commit c20d5d3
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/channel/channel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ struct Channel {

// A wrapper around felt with a guarantee that the felt must be read from the channel before
// use.
#[derive(Drop)]
#[derive(Drop, Copy)]
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)]
#[derive(Drop, Copy)]
struct ChannelSentFelt {
value: felt252,
}
Expand Down
76 changes: 76 additions & 0 deletions src/fri/fri.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::option::OptionTrait;
use core::traits::TryInto;
use core::array::SpanTrait;
use core::traits::Destruct;
use cairo_verifier::channel::channel::ChannelTrait;
Expand All @@ -10,6 +12,10 @@ 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;
use cairo_verifier::table_commitment::TableCommitmentWitness;
use cairo_verifier::fri::fri_first_layer::gather_first_layer_queries;
use cairo_verifier::fri::fri_group::get_fri_group;
use cairo_verifier::fri::fri_layer::FriLayerQuery;

// Commitment values for FRI. Used to generate a commitment by "reading" these values
// from the channel.
Expand All @@ -34,6 +40,35 @@ struct FriCommitment {
last_layer_coefficients: Span<ChannelSentFelt>,
}

#[derive(Drop, Copy)]
struct FriDecommitment {
// Number of queries.
n_values: felt252,
// Array of size n_values, containing the values of the input layer at query indices.
values: Span<felt252>,
// Array of size n_values, containing the field elements that correspond to the query indices
// (See queries_to_points).
points: Span<felt252>,
}

// A witness for the decommitment of the FRI layers over queries.
#[derive(Drop, Copy)]
struct FriWitness {
// An array of size n_layers - 1, containing a witness for each inner layer.
layers: Span<FriLayerWitness>,
}

// A witness for a single FRI layer. This witness is required to verify the transition from an
// inner layer to the following layer.
#[derive(Drop, Copy)]
struct FriLayerWitness {
// Values for the sibling leaves required for decommitment.
n_leaves: felt252,
leaves: Span<felt252>,
// Table commitment witnesses for decommiting all the leaves.
table_witness: TableCommitmentWitness,
}

// 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.
// Each such layer has a separate table commitment, for a total of N - 1 commitments.
Expand Down Expand Up @@ -106,3 +141,44 @@ fn fri_commit(
last_layer_coefficients: coefficients.span()
}
}

fn fri_decommit_layers(
fri_group: Span<felt252>,
n_layers: felt252,
commitment: Span<TableCommitment>,
layer_witness: Span<FriLayerWitness>,
eval_points: Span<felt252>,
step_sizes: Span<felt252>,
queries: Span<felt252>,
) -> Array<FriLayerQuery> {
let last_queries = ArrayTrait::<FriLayerQuery>::new();
let len: u32 = n_layers.try_into().unwrap();
let mut i: u32 = 0;

loop {
if i == len {
break;
}
//

};

last_queries
}

// FRI protocol component decommitment.
fn fri_decommit(
queries: Span<felt252>,
commitment: FriCommitment,
decommitment: FriDecommitment,
witness: FriWitness,
) {
assert(queries.len().into() == decommitment.n_values, 'Invalid value');
let fri_first_layer_evaluations = decommitment.values;

let fri_queries = gather_first_layer_queries(
queries, decommitment.values, decommitment.points,
);

let fri_group = get_fri_group();
}
2 changes: 1 addition & 1 deletion src/fri/fri_config.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn fri_config_validate(
let fri_step_u32: u32 = fri_step.try_into().unwrap();
assert(1_u32 <= fri_step_u32, 'Invalid value');
assert(fri_step_u32 <= MAX_FRI_STEP + 1, 'Invalid value');
assert(table_commitment.columns == fri_step * fri_step, 'Invalid value');
assert(table_commitment.n_columns == fri_step * fri_step, 'Invalid value');

i += 1;
log_input_size -= fri_step;
Expand Down
10 changes: 6 additions & 4 deletions src/fri/fri_first_layer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use cairo_verifier::fri::fri_layer::FriLayerQuery;
use cairo_verifier::FIELD_GENERATOR_INV;

fn gather_first_layer_queries(
n_queries: felt252, queries: Span<felt252>, evaluations: Span<felt252>, x_values: Span<felt252>
) {
queries: Span<felt252>, evaluations: Span<felt252>, x_values: Span<felt252>
) -> Array<FriLayerQuery> {
let mut fri_queries = ArrayTrait::<FriLayerQuery>::new();

let len: u32 = n_queries.try_into().unwrap();
let len: u32 = queries.len();
let mut i: u32 = 0;
loop {
if i == len {
Expand All @@ -32,5 +32,7 @@ fn gather_first_layer_queries(
);

i += 1;
}
};

fri_queries
}
67 changes: 58 additions & 9 deletions src/table_commitment.cairo
Original file line number Diff line number Diff line change
@@ -1,29 +1,78 @@
use cairo_verifier::vector_commitment::VectorCommitmentConfig;
use cairo_verifier::vector_commitment::{VectorCommitmentConfig, VectorUnsentCommitment, VectorCommitment, VectorCommitmentWitness};
use cairo_verifier::channel::channel::ChannelSentFelt;

// Commitment values for a table commitment protocol. Used to generate a commitment by "reading"
// these values from the channel.
#[derive(Drop, Copy)]
struct TableUnsentCommitment {
a: felt252, // dummy
// vector: VectorUnsentCommitment,
vector: VectorUnsentCommitment,
}

// Commitment for a table (n_rows x n_columns) of field elements in montgomery form.
#[derive(Drop, Copy)]
struct TableCommitment {
a: felt252, // dummy
// config: TableCommitmentConfig*,
// vector_commitment: VectorCommitment*,
config: TableCommitmentConfig,
vector_commitment: VectorCommitment,
}

#[derive(Drop, Copy)]
struct TableCommitmentConfig {
columns: felt252,
vector: VectorCommitmentConfig
n_columns: felt252,
vector: VectorCommitmentConfig,
}

// Responses for queries to the table commitment.
// Each query corresponds to a full row of the table.
#[derive(Drop, Copy)]
struct TableDecommitment {
// n_columns * n_queries values to decommit.
n_values: felt252,
values: Span<felt252>,
}

// Witness for a decommitment over queries.
#[derive(Drop, Copy)]
struct TableCommitmentWitness {
vector: VectorCommitmentWitness,
}

fn table_commit(
unsent_commitment: TableUnsentCommitment, config: TableCommitmentConfig
) -> TableCommitment {
TableCommitment { a: 0 }
TableCommitment {
config: TableCommitmentConfig {
n_columns: 0,
vector: VectorCommitmentConfig {
height: 0,
n_verifier_friendly_commitment_layers: 0,
}
},
vector_commitment: VectorCommitment {
config: VectorCommitmentConfig {
height: 0,
n_verifier_friendly_commitment_layers: 0,
},
commitment_hash: ChannelSentFelt {
value: 0,
}
}
}
}

// Decommits a TableCommitment at multiple indices.
// rows must be sorted and unique.
// Args:
// commitment - the table commitment.
// n_queries - number of queries to decommit.
// queries - the claimed indices.
// decommitment - the claimed values at those indices.
// witness - the decommitment witness.
fn table_decommit(
commitment: TableCommitment,
n_queries: felt252,
queries: felt252,
decommitment: TableDecommitment,
witness: TableCommitmentWitness,
) {

}
21 changes: 15 additions & 6 deletions src/vector_commitment.cairo
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
use cairo_verifier::channel::channel::{ChannelSentFelt, ChannelUnsentFelt};

// Commitment values for a vector commitment. Used to generate a commitment by "reading" these
// values from the channel.
#[derive(Drop, Copy)]
struct VectorUnsentCommitment {
a: felt252, // dummy
// commitment_hash: ChannelUnsentFelt,
commitment_hash: ChannelUnsentFelt,
}

// Commitment for a vector of field elements.
#[derive(Drop, Copy)]
struct VectorCommitment {
a: felt252, // dummy
// config: VectorCommitmentConfig*,
// commitment_hash: ChannelSentFelt,
config: VectorCommitmentConfig,
commitment_hash: ChannelSentFelt,
}

#[derive(Drop, Copy)]
struct VectorCommitmentConfig {
height: felt252,
verifier_friendly_commitment_layers: felt252,
n_verifier_friendly_commitment_layers: felt252,
}

// Witness for a decommitment over queries.
#[derive(Drop, Copy)]
struct VectorCommitmentWitness {
// The authentication values: all the siblings of the subtree generated by the queried indices,
// bottom layer up, left to right.
n_authentications: felt252,
authentications: Span<felt252>,
}

fn validate_vector_commitment(
Expand Down

0 comments on commit c20d5d3

Please sign in to comment.