Skip to content

Commit

Permalink
Implement shift_queries for vector commitment
Browse files Browse the repository at this point in the history
  • Loading branch information
fmkra committed Jan 2, 2024
1 parent 7ca9bd9 commit 4b58d0c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/vector_commitment/tests/test_vector_commitment.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ fn test_truncated_blake2s() {
assert(out == 642191007116032514313255519742888271333651019057, 'invalid truncated_blake2s');
}

#[test]
#[available_gas(9999999999)]
fn test_vector_commitment_decommit() {
let n_queries = 15;
let queries = array![
Expand Down
46 changes: 43 additions & 3 deletions src/vector_commitment/vector_commitment.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use cairo_verifier::common::flip_endianness::FlipEndiannessTrait;
use cairo_verifier::common::{array_append::ArrayAppendTrait, blake2s::blake2s,};
use cairo_verifier::common::{array_append::ArrayAppendTrait, blake2s::blake2s, math::pow};
use poseidon::hades_permutation;

// TODO: remove
use core::debug::PrintTrait;

// Commitment for a vector of field elements.
#[derive(Drop, Copy)]
Expand All @@ -23,6 +24,14 @@ struct VectorQuery {
value: felt252,
}

// A query to the vector commitment that contains also the depth of the query in the Merkle tree.
#[derive(Drop, Copy)]
struct VectorQueryWithDepth {
index: felt252,
value: felt252,
depth: felt252,
}

// Witness for a decommitment over queries.
#[derive(Drop, Copy)]
struct VectorCommitmentWitness {
Expand All @@ -45,7 +54,38 @@ fn vector_commitment_decommit(
queries: Array<VectorQuery>,
witness: VectorCommitmentWitness,
) {
assert(false, 'not implemented');
let shift = pow(2, commitment.config.height);
let shifted_queries = shift_queries(queries.span(), shift, commitment.config.height).span();

let mut i: u32 = 0;
loop {
if i == shifted_queries.len() {
break;
}
let q: VectorQueryWithDepth = *shifted_queries.at(i);
q.index.print();
q.value.print();
q.depth.print();
i += 1;
};
}

fn shift_queries(queries: Span<VectorQuery>, shift: felt252, height: felt252) -> Array<VectorQueryWithDepth> {
let mut shifted_queries = ArrayTrait::new();
let mut i = 0;
loop {
if i == queries.len() {
break;
};
let q = *queries[i];
shifted_queries.append(VectorQueryWithDepth {
index: q.index + shift,
value: q.value,
depth: height,
});
i += 1;
};
shifted_queries
}

fn hash_blake_or_poseidon(x: felt252, y: felt252, is_verifier_friendly: bool) -> felt252 {
Expand Down

0 comments on commit 4b58d0c

Please sign in to comment.