Skip to content

Commit

Permalink
Type def ColumnVec.
Browse files Browse the repository at this point in the history
  • Loading branch information
alonh5 committed Feb 21, 2024
1 parent 18c1867 commit c3ff7d6
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/commitment_scheme/merkle_decommitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ mod tests {
use crate::commitment_scheme::hasher::Hasher;
use crate::commitment_scheme::merkle_tree::MerkleTree;
use crate::commitment_scheme::utils::tests::generate_test_queries;
use crate::commitment_scheme::utils::ColumnArray;
use crate::core::fields::m31::M31;
use crate::core::ColumnVec;

#[test]
pub fn verify_test() {
let trace: ColumnArray<M31> = vec![(0..4096).map(M31::from_u32_unchecked).collect(); 7];
let trace: ColumnVec<M31> = vec![(0..4096).map(M31::from_u32_unchecked).collect(); 7];
let tree = MerkleTree::<M31, Blake3Hasher>::commit(trace);
let queries = generate_test_queries(100, 4096);
let decommitment = tree.generate_decommitment(queries.clone());
Expand All @@ -194,7 +194,7 @@ mod tests {
#[test]
pub fn verify_false_proof_test() {
let trace_column_length = 1 << 12;
let trace: ColumnArray<M31> = vec![
let trace: ColumnVec<M31> = vec![
(0..trace_column_length)
.map(M31::from_u32_unchecked)
.collect();
Expand Down Expand Up @@ -225,7 +225,7 @@ mod tests {
.map(M31::from_u32_unchecked)
.collect::<Vec<M31>>();
let reversed_trace_column = trace_column.iter().rev().cloned().collect::<Vec<M31>>();
let trace: ColumnArray<M31> = vec![trace_column, reversed_trace_column];
let trace: ColumnVec<M31> = vec![trace_column, reversed_trace_column];
let tree = MerkleTree::<M31, Blake3Hasher>::commit(trace.clone());
let random_queries = generate_test_queries(10, trace_column_length as usize);
let test_skip_queries = vec![17, 50];
Expand Down
7 changes: 4 additions & 3 deletions src/commitment_scheme/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use super::hasher::Hasher;
use super::merkle_decommitment::MerkleDecommitment;
use crate::commitment_scheme::utils::{
allocate_balanced_tree, column_to_row_major, hash_merkle_tree_from_bottom_layer,
tree_data_as_mut_ref, ColumnArray, TreeData,
tree_data_as_mut_ref, TreeData,
};
use crate::core::fields::{Field, IntoSlice};
use crate::core::ColumnVec;
use crate::math::utils::{prev_pow_two, usize_div_ceil};

pub struct MerkleTree<T: Field + Sized + Debug + Display, H: Hasher> {
Expand All @@ -26,7 +27,7 @@ where
T: IntoSlice<H::NativeType>,
{
/// Commits on a given trace(matrix).
pub fn commit(trace: ColumnArray<T>) -> Self {
pub fn commit(trace: ColumnVec<T>) -> Self {
let mut tree = Self::init_from_column_array(trace);

hash_merkle_tree_from_bottom_layer::<T, H>(
Expand All @@ -41,7 +42,7 @@ where
/// Builds the base layer of the tree from the given trace.
/// Allocates the rest of the tree.
// TODO(Ohad): add support for columns of different lengths.
fn init_from_column_array(trace: ColumnArray<T>) -> Self {
fn init_from_column_array(trace: ColumnVec<T>) -> Self {
assert!(!trace.is_empty());
assert!(trace[0].len().is_power_of_two());
trace.iter().for_each(|column| {
Expand Down
22 changes: 11 additions & 11 deletions src/commitment_scheme/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::slice::Iter;

use super::hasher::Hasher;
use crate::core::fields::{Field, IntoSlice};
use crate::core::ColumnVec;
use crate::math::utils::{log2_ceil, usize_safe_div};

pub type ColumnArray<T> = Vec<Vec<T>>;
pub type ColumnLengthMap<T> = BTreeMap<usize, ColumnArray<T>>;
pub type ColumnLengthMap<T> = BTreeMap<usize, ColumnVec<T>>;
pub type TreeLayer<T> = Box<[T]>;
pub type TreeData<T> = Box<[TreeLayer<T>]>;

Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn hash_merkle_tree_from_bottom_layer<'a, F: Field, H: Hasher>(

/// Maps columns by length.
/// Mappings are sorted by length. i.e the first entry is a matrix of the shortest columns.
pub fn map_columns_sorted<T: Sized>(cols: ColumnArray<T>) -> ColumnLengthMap<T> {
pub fn map_columns_sorted<T: Sized>(cols: ColumnVec<T>) -> ColumnLengthMap<T> {
let mut columns_length_map: ColumnLengthMap<T> = BTreeMap::new();
for c in cols {
let length_index_entry = columns_length_map.entry(c.len()).or_default();
Expand All @@ -104,7 +104,7 @@ pub fn map_columns_sorted<T: Sized>(cols: ColumnArray<T>) -> ColumnLengthMap<T>
/// Pointers in 'dst' should point to pre-allocated memory with enough space to store
/// column_array.len() amount of u32 elements.
// TODO(Ohad): Change tree impl and remove.
pub unsafe fn transpose_to_bytes<T: Sized>(column_array: &ColumnArray<T>, dst: &[*mut u8]) {
pub unsafe fn transpose_to_bytes<T: Sized>(column_array: &ColumnVec<T>, dst: &[*mut u8]) {
let column_length = column_array[0].len();

for (i, ptr) in dst.iter().enumerate().take(column_length) {
Expand Down Expand Up @@ -138,7 +138,7 @@ pub fn tree_data_as_mut_ref<T: Sized>(tree_data: &mut TreeData<T>) -> Vec<&mut [
/// offset*(n_rows/n_rows_in_node) amount of T elements.
// TODO(Ohad): Change tree impl and remove.
pub unsafe fn inject<T: Sized>(
column_array: &ColumnArray<T>,
column_array: &ColumnVec<T>,
dst: &mut [u8],
n_rows_in_node: usize,
gap_offset: usize,
Expand All @@ -154,7 +154,7 @@ pub unsafe fn inject<T: Sized>(
/// Given a matrix, returns a vector of the matrix elements in row-major order.
/// Assumes all columns are of the same length and non-zero.
// TODO(Ohad): Change tree impl and remove.
pub fn column_to_row_major<T>(mut mat: ColumnArray<T>) -> Vec<T> {
pub fn column_to_row_major<T>(mut mat: ColumnVec<T>) -> Vec<T> {
if mat.len() == 1 {
return mat.remove(0);
};
Expand Down Expand Up @@ -318,7 +318,7 @@ pub mod tests {

use super::{
allocate_balanced_tree, inject_and_hash_layer, inject_column_chunks, map_columns_sorted,
ColumnArray,
ColumnVec,
};
use crate::commitment_scheme::blake3_hash::Blake3Hasher;
use crate::commitment_scheme::hasher::Hasher;
Expand All @@ -339,22 +339,22 @@ pub mod tests {
queries
}

fn init_test_trace() -> ColumnArray<u32> {
fn init_test_trace() -> ColumnVec<u32> {
let col0 = std::iter::repeat(0).take(8).collect();
let col1 = vec![1, 2, 3, 4];
let col2 = vec![5, 6];
let col3 = vec![7, 8];
let col4 = vec![9];
let cols: ColumnArray<u32> = vec![col0, col1, col2, col3, col4];
let cols: ColumnVec<u32> = vec![col0, col1, col2, col3, col4];
cols
}

fn init_transpose_test_trace() -> ColumnArray<u32> {
fn init_transpose_test_trace() -> ColumnVec<u32> {
let col1 = vec![1, 2, 3, 4];
let col2 = vec![5, 6, 7, 8];
let col3 = vec![9, 10];
let col4 = vec![11];
let cols: ColumnArray<u32> = vec![col1, col2, col3, col4];
let cols: ColumnVec<u32> = vec![col1, col2, col3, col4];
cols
}

Expand Down
9 changes: 5 additions & 4 deletions src/core/air/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::circle::CirclePoint;
use super::fields::m31::BaseField;
use super::fields::qm31::SecureField;
use super::poly::circle::{CanonicCoset, CirclePoly};
use super::ColumnVec;

pub mod evaluation;

Expand All @@ -28,14 +29,14 @@ pub trait ComponentVisitor {
/// Holds the mask offsets at each column.
/// Holds a vector with an entry for each column. Each entry holds the offsets
/// of the mask at that column.
pub struct Mask(pub Vec<Vec<usize>>);
pub struct Mask(pub ColumnVec<usize>);

impl Mask {
pub fn to_points(
&self,
domains: Vec<CanonicCoset>,
point: CirclePoint<SecureField>,
) -> Vec<Vec<CirclePoint<SecureField>>> {
) -> ColumnVec<CirclePoint<SecureField>> {
self.iter()
.zip(domains.iter())
.map(|(col, domain)| {
Expand All @@ -48,7 +49,7 @@ impl Mask {
}

impl Deref for Mask {
type Target = Vec<Vec<usize>>;
type Target = ColumnVec<usize>;

fn deref(&self) -> &Self::Target {
&self.0
Expand Down Expand Up @@ -83,7 +84,7 @@ pub trait Component {
&self,
point: CirclePoint<SecureField>,
trace: &ComponentTrace<'_>,
) -> (Vec<Vec<CirclePoint<SecureField>>>, Vec<Vec<SecureField>>) {
) -> (ColumnVec<CirclePoint<SecureField>>, ColumnVec<SecureField>) {
let domains = trace
.columns
.iter()
Expand Down
2 changes: 2 additions & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ pub mod poly;
pub mod proof_of_work;
pub mod queries;
pub mod utils;

pub type ColumnVec<T> = Vec<Vec<T>>;
3 changes: 2 additions & 1 deletion src/fibonacci/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::core::oods::{get_oods_quotient, get_pair_oods_quotient, quotient_log_
use crate::core::poly::circle::{CanonicCoset, CircleEvaluation, CirclePoly};
use crate::core::poly::BitReversedOrder;
use crate::core::proof_of_work::{ProofOfWork, ProofOfWorkProof};
use crate::core::ColumnVec;

type Channel = Blake2sChannel;
type MerkleHasher = Blake2sHasher;
Expand Down Expand Up @@ -55,7 +56,7 @@ pub struct FibonacciProof {
pub trace_decommitments: Vec<MerkleDecommitment<BaseField, MerkleHasher>>,
pub composition_polynomial_commitment: <MerkleHasher as Hasher>::Hash,
pub composition_polynomial_decommitment: MerkleDecommitment<SecureField, MerkleHasher>,
pub trace_oods_values: Vec<Vec<SecureField>>,
pub trace_oods_values: ColumnVec<SecureField>,
pub composition_polynomial_opened_values: Vec<SecureField>,
pub trace_opened_values: Vec<BaseField>,
pub proof_of_work: ProofOfWorkProof,
Expand Down

0 comments on commit c3ff7d6

Please sign in to comment.