Skip to content

Commit

Permalink
switching byte lookup to cubic combination
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirhemo committed Jan 11, 2024
1 parent e81ea9d commit a95b888
Show file tree
Hide file tree
Showing 14 changed files with 449 additions and 320 deletions.
4 changes: 2 additions & 2 deletions curta/src/chip/hash/blake/blake2b/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl<F: PrimeField64, E: CubicParameters<F>> AirParameters for BLAKE2BAirParamet

type Instruction = UintInstruction;

const NUM_FREE_COLUMNS: usize = 3545;
const EXTENDED_COLUMNS: usize = 1632;
const NUM_FREE_COLUMNS: usize = 2413;
const EXTENDED_COLUMNS: usize = 4758;
const NUM_ARITHMETIC_COLUMNS: usize = 0;
}

Expand Down
11 changes: 8 additions & 3 deletions curta/src/chip/uint/bytes/lookup_table/builder_operations.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use crate::chip::register::element::ElementRegister;
use crate::chip::uint::bytes::operations::value::ByteOperation;
use crate::chip::uint::bytes::register::ByteRegister;

#[derive(Debug, Clone)]
pub struct ByteLookupOperations {
pub values: Vec<ElementRegister>,
pub trace_operations: Vec<ByteOperation<ByteRegister>>,
pub public_operations: Vec<ByteOperation<ByteRegister>>,
}

impl ByteLookupOperations {
pub fn new() -> Self {
ByteLookupOperations { values: Vec::new() }
ByteLookupOperations {
trace_operations: Vec::new(),
public_operations: Vec::new(),
}
}
}
39 changes: 24 additions & 15 deletions curta/src/chip/uint/bytes/lookup_table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::air::parser::AirParser;
use crate::air::AirConstraint;
use crate::chip::builder::AirBuilder;
use crate::chip::instruction::Instruction;
use crate::chip::table::lookup::values::LogLookupValues;
use crate::chip::trace::writer::{AirWriter, TraceWriter};
use crate::chip::AirParameters;

Expand Down Expand Up @@ -56,24 +55,34 @@ impl<L: AirParameters> AirBuilder<L> {
table: &mut ByteLogLookupTable<L::Field, L::CubicParams>,
operations: ByteLookupOperations,
) -> ByteMultiplicityData {
let lookup_values = table
.lookup
.register_lookup_values(self, &operations.values);

let LogLookupValues {
trace_values,
public_values,
..
} = lookup_values;

ByteMultiplicityData::new(table.multiplicity_data.clone(), trace_values, public_values)
let trace_digest_values = operations
.trace_operations
.iter()
.map(|op| self.accumulate_expressions(&table.challenges, &op.expressions()))
.collect::<Vec<_>>();

let public_digest_values = operations
.public_operations
.iter()
.map(|op| self.accumulate_public_expressions(&table.challenges, &op.expressions()))
.collect::<Vec<_>>();

let values = [trace_digest_values, public_digest_values].concat();

let _ = table.lookup.register_lookup_values(self, &values);

ByteMultiplicityData::new(
table.multiplicity_data.clone(),
operations.trace_operations.clone(),
operations.public_operations.clone(),
)
}

pub fn constraint_byte_lookup_table(
&mut self,
table: &ByteLogLookupTable<L::Field, L::CubicParams>,
) {
self.constrain_element_lookup_table(table.lookup.clone())
self.constrain_cubic_lookup_table(table.lookup.clone())
}
}

Expand Down Expand Up @@ -172,8 +181,8 @@ mod tests {

type Instruction = ByteInstructionSet;

const NUM_FREE_COLUMNS: usize = 377;
const EXTENDED_COLUMNS: usize = 159;
const NUM_FREE_COLUMNS: usize = 195;
const EXTENDED_COLUMNS: usize = 453;
const NUM_ARITHMETIC_COLUMNS: usize = 0;
}

Expand Down
62 changes: 40 additions & 22 deletions curta/src/chip/uint/bytes/lookup_table/multiplicity_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,34 @@ use serde::{Deserialize, Serialize};

use crate::chip::register::array::ArrayRegister;
use crate::chip::register::element::ElementRegister;
use crate::chip::table::log_derivative::entry::LogEntry;
use crate::chip::trace::writer::TraceWriter;
use crate::chip::uint::bytes::operations::value::ByteOperation;
use crate::chip::uint::bytes::operations::{
NUM_BIT_OPPS, OPCODE_AND, OPCODE_INDICES, OPCODE_NOT, OPCODE_RANGE, OPCODE_ROT, OPCODE_SHR,
OPCODE_XOR,
OPCODE_SHR_CARRY, OPCODE_XOR,
};
use crate::chip::uint::bytes::register::ByteRegister;
use crate::math::prelude::*;
use crate::maybe_rayon::*;
use crate::trace::AirTrace;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiplicityData {
pub multiplicities: ArrayRegister<ElementRegister>,
operations_multipcitiy_dict: HashMap<ByteOperation<u8>, (usize, usize)>,
values_multiplicity_dict: HashMap<u32, (usize, usize)>,
pub operations_dict: HashMap<usize, Vec<ByteOperation<u8>>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ByteMultiplicityData {
data: MultiplicityData,
trace_values: Vec<LogEntry<ElementRegister>>,
public_values: Vec<LogEntry<ElementRegister>>,
trace_operations: Vec<ByteOperation<ByteRegister>>,
public_operations: Vec<ByteOperation<ByteRegister>>,
}

impl MultiplicityData {
pub fn new(multiplicities: ArrayRegister<ElementRegister>) -> Self {
let mut operations_multipcitiy_dict = HashMap::new();
let mut values_multiplicity_dict = HashMap::new();
let mut operations_dict = HashMap::new();
for (row_index, (a, b)) in (0..=u8::MAX).cartesian_product(0..=u8::MAX).enumerate() {
let mut operations = Vec::with_capacity(NUM_BIT_OPPS + 1);
Expand All @@ -42,14 +41,13 @@ impl MultiplicityData {
OPCODE_AND => ByteOperation::and(a, b),
OPCODE_XOR => ByteOperation::xor(a, b),
OPCODE_SHR => ByteOperation::shr(a, b),
OPCODE_SHR_CARRY => ByteOperation::shr_full(a, b),
OPCODE_ROT => ByteOperation::rot(a, b),
OPCODE_NOT => ByteOperation::not(a),
OPCODE_RANGE => ByteOperation::range(a),
_ => unreachable!("Invalid opcode: {}", opcode),
};
operations_multipcitiy_dict.insert(operation, (row_index, op_index));
let lookup_value = operation.lookup_digest_value();
values_multiplicity_dict.insert(lookup_value, (row_index, op_index));
operations.push(operation);
}
operations_dict.insert(row_index, operations);
Expand All @@ -58,7 +56,6 @@ impl MultiplicityData {
multiplicities,
operations_dict,
operations_multipcitiy_dict,
values_multiplicity_dict,
}
}

Expand All @@ -70,32 +67,53 @@ impl MultiplicityData {
pub fn multiplicities(&self) -> ArrayRegister<ElementRegister> {
self.multiplicities
}

pub fn table_index<F: PrimeField64>(&self, lookup_element: F) -> (usize, usize) {
self.values_multiplicity_dict[&(lookup_element.as_canonical_u64() as u32)]
}
}

impl ByteMultiplicityData {
pub fn new(
data: MultiplicityData,
trace_values: Vec<LogEntry<ElementRegister>>,
public_values: Vec<LogEntry<ElementRegister>>,
trace_operations: Vec<ByteOperation<ByteRegister>>,
public_operations: Vec<ByteOperation<ByteRegister>>,
) -> Self {
Self {
data,
trace_values,
public_values,
trace_operations,
public_operations,
}
}

pub fn get_multiplicities<F: PrimeField64>(&self, writer: &TraceWriter<F>) -> AirTrace<F> {
writer.get_multiplicities_from_fn(
let mut multiplicities_trace = AirTrace::new_with_value(NUM_BIT_OPPS + 1, 1 << 16, 0u32);

// Count the multiplicities in the trace
let trace = writer.read_trace().unwrap();
for row in trace.rows() {
for op in self.trace_operations.iter() {
let op_value = op.read_from_slice(row);
let (row_index, col_index) = self.data.operations_multipcitiy_dict[&op_value];
assert!(col_index < NUM_BIT_OPPS + 1);
assert!(row_index < 1 << 16);
multiplicities_trace.row_mut(row_index)[col_index] += 1;
}
}

// Count the multiplicities in public inputs
let public_slice = writer.public.read().unwrap();
for op in self.public_operations.iter() {
let op_value = op.read_from_slice(&public_slice);
let (row_index, col_index) = self.data.operations_multipcitiy_dict[&op_value];
assert!(col_index < NUM_BIT_OPPS + 1);
assert!(row_index < 1 << 16);
multiplicities_trace.row_mut(row_index)[col_index] += 1;
}

AirTrace::from_rows(
multiplicities_trace
.values
.into_par_iter()
.map(F::from_canonical_u32)
.collect(),
NUM_BIT_OPPS + 1,
1 << 16,
&self.trace_values,
&self.public_values,
|x| self.data.table_index(x),
)
}

Expand Down
Loading

0 comments on commit a95b888

Please sign in to comment.