Skip to content

Commit

Permalink
move add specific constants to AddSub block
Browse files Browse the repository at this point in the history
  • Loading branch information
iammadab committed Jun 25, 2024
1 parent 02bbaee commit 55519da
Show file tree
Hide file tree
Showing 23 changed files with 96 additions and 76 deletions.
3 changes: 2 additions & 1 deletion singer-pro/src/basic_block/bb_final.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use gkr::structs::Circuit;
use itertools::Itertools;
use paste::paste;
use simple_frontend::structs::{CircuitBuilder, MixedCell};
use singer_utils::uint::constants::AddSubConstants;
use singer_utils::{
chip_handler::{
GlobalStateChipOperations, OAMOperations, ROMOperations, RangeChipOperations,
Expand All @@ -27,7 +28,7 @@ pub struct BasicBlockFinal;

register_witness!(BasicBlockFinal, phase0 {
// State in related
stack_ts_add => TSUInt::N_WITNESS_CELLS_NO_CARRY_OVERFLOW
stack_ts_add => AddSubConstants::<TSUInt>::N_WITNESS_CELLS_NO_CARRY_OVERFLOW
});

impl BasicBlockFinal {
Expand Down
3 changes: 2 additions & 1 deletion singer-pro/src/basic_block/bb_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ff_ext::ExtensionField;
use gkr::structs::Circuit;
use paste::paste;
use simple_frontend::structs::{CircuitBuilder, MixedCell};
use singer_utils::uint::constants::AddSubConstants;
use singer_utils::{
chip_handler::{
GlobalStateChipOperations, OAMOperations, ROMOperations, RangeChipOperations,
Expand Down Expand Up @@ -35,7 +36,7 @@ register_multi_witness!(BasicBlockStart, phase0(n_stack_items) {
// Stack values
old_stack_values(n_stack_items) => StackUInt::N_OPERAND_CELLS,
old_stack_ts(n_stack_items) => TSUInt::N_OPERAND_CELLS,
old_stack_ts_lt(n_stack_items) => TSUInt::N_WITNESS_CELLS_NO_CARRY_OVERFLOW
old_stack_ts_lt(n_stack_items) => AddSubConstants::<TSUInt>::N_WITNESS_CELLS_NO_CARRY_OVERFLOW
});

impl BasicBlockStart {
Expand Down
3 changes: 2 additions & 1 deletion singer-pro/src/instructions/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ff_ext::ExtensionField;
use gkr::structs::Circuit;
use paste::paste;
use simple_frontend::structs::CircuitBuilder;
use singer_utils::uint::constants::AddSubConstants;
use singer_utils::{
chip_handler::ROMOperations,
chips::IntoEnumIterator,
Expand Down Expand Up @@ -29,7 +30,7 @@ register_witness!(
AddInstruction,
phase0 {
// Witness for addend_0 + addend_1
instruction_add => StackUInt::N_WITNESS_CELLS
instruction_add => AddSubConstants::<StackUInt>::N_WITNESS_CELLS
}
);

Expand Down
3 changes: 2 additions & 1 deletion singer-pro/src/instructions/gt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ff_ext::ExtensionField;
use gkr::structs::Circuit;
use paste::paste;
use simple_frontend::structs::CircuitBuilder;
use singer_utils::uint::constants::AddSubConstants;
use singer_utils::{
chip_handler::ROMOperations,
chips::IntoEnumIterator,
Expand All @@ -27,7 +28,7 @@ register_witness!(
GtInstruction,
phase0 {
// Witness for operand_0 > operand_1
instruction_gt => StackUInt::N_WITNESS_CELLS
instruction_gt => AddSubConstants::<StackUInt>::N_WITNESS_CELLS
}
);

Expand Down
7 changes: 4 additions & 3 deletions singer-pro/src/instructions/mstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use gkr_graph::structs::{CircuitGraphBuilder, NodeOutputType, PredType};
use itertools::Itertools;
use paste::paste;
use simple_frontend::structs::CircuitBuilder;
use singer_utils::uint::constants::AddSubConstants;
use singer_utils::{
chip_handler::{MemoryChipOperations, ROMOperations, RangeChipOperations},
chips::{IntoEnumIterator, SingerChipBuilder},
Expand Down Expand Up @@ -110,7 +111,7 @@ impl<E: ExtensionField> InstructionGraph<E> for MstoreInstruction {
register_witness!(
MstoreInstruction,
phase0 {
memory_ts_add => TSUInt::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,
memory_ts_add => AddSubConstants::<TSUInt>::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,
mem_bytes => EVM_STACK_BYTE_WIDTH
}
);
Expand Down Expand Up @@ -218,9 +219,9 @@ register_witness!(
},
phase0 {
old_memory_ts => TSUInt::N_OPERAND_CELLS,
old_memory_ts_lt => TSUInt::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,
old_memory_ts_lt => AddSubConstants::<TSUInt>::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,

offset_add_delta => StackUInt::N_WITNESS_CELLS,
offset_add_delta => AddSubConstants::<StackUInt>::N_WITNESS_CELLS,
prev_mem_byte => 1
}
);
Expand Down
3 changes: 2 additions & 1 deletion singer-pro/src/instructions/ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use gkr::structs::Circuit;
use gkr_graph::structs::{CircuitGraphBuilder, NodeOutputType, PredType};
use paste::paste;
use simple_frontend::structs::CircuitBuilder;
use singer_utils::uint::constants::AddSubConstants;
use singer_utils::{
chip_handler::{OAMOperations, ROMOperations},
chips::{IntoEnumIterator, SingerChipBuilder},
Expand Down Expand Up @@ -110,7 +111,7 @@ register_witness!(
},
phase0 {
old_memory_ts => TSUInt::N_OPERAND_CELLS,
offset_add => StackUInt::N_WITNESS_CELLS
offset_add => AddSubConstants::<StackUInt>::N_WITNESS_CELLS
}
);

Expand Down
2 changes: 1 addition & 1 deletion singer-utils/src/uint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod arithmetic;
mod cmp;
mod constants;
pub mod constants;
mod uint;
pub use uint::UInt;
pub mod util;
Expand Down
13 changes: 9 additions & 4 deletions singer-utils/src/uint/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ impl<const M: usize, const C: usize> UInt<M, C> {

#[cfg(test)]
mod tests {
use crate::uint::constants::AddSubConstants;
use crate::uint::UInt;
use gkr::structs::{Circuit, CircuitWitness};
use goldilocks::{Goldilocks, GoldilocksExt2};
Expand Down Expand Up @@ -327,7 +328,8 @@ mod tests {
circuit_builder.create_witness_in(UInt20::N_OPERAND_CELLS);
let (addend_1_id, addend_1_cells) =
circuit_builder.create_witness_in(UInt20::N_OPERAND_CELLS);
let (carry_id, carry_cells) = circuit_builder.create_witness_in(UInt20::N_CARRY_CELLS);
let (carry_id, carry_cells) =
circuit_builder.create_witness_in(AddSubConstants::<UInt20>::N_CARRY_CELLS);

let addend_0 = UInt20::try_from(addend_0_cells).expect("should build uint");
let addend_1 = UInt20::try_from(addend_1_cells).expect("should build uint");
Expand Down Expand Up @@ -401,7 +403,8 @@ mod tests {
// addend_0, carry, constant
let (addend_0_id, addend_0_cells) =
circuit_builder.create_witness_in(UInt20::N_OPERAND_CELLS);
let (carry_id, carry_cells) = circuit_builder.create_witness_in(UInt20::N_CARRY_CELLS);
let (carry_id, carry_cells) =
circuit_builder.create_witness_in(AddSubConstants::<UInt20>::N_CARRY_CELLS);

let addend_0 = UInt20::try_from(addend_0_cells).expect("should build uint");

Expand Down Expand Up @@ -474,7 +477,8 @@ mod tests {
let (addend_0_id, addend_0_cells) =
circuit_builder.create_witness_in(UInt20::N_OPERAND_CELLS);
let (small_value_id, small_value_cell) = circuit_builder.create_witness_in(1);
let (carry_id, carry_cells) = circuit_builder.create_witness_in(UInt20::N_CARRY_CELLS);
let (carry_id, carry_cells) =
circuit_builder.create_witness_in(AddSubConstants::<UInt20>::N_CARRY_CELLS);

let addend_0 = UInt20::try_from(addend_0_cells).expect("should build uint");

Expand Down Expand Up @@ -551,7 +555,8 @@ mod tests {
let (subtrahend_id, subtrahend_cells) =
circuit_builder.create_witness_in(UInt20::N_OPERAND_CELLS);
// |Carry| == |Borrow|
let (borrow_id, borrow_cells) = circuit_builder.create_witness_in(UInt20::N_CARRY_CELLS);
let (borrow_id, borrow_cells) =
circuit_builder.create_witness_in(AddSubConstants::<UInt20>::N_CARRY_CELLS);

let minuend = UInt20::try_from(minuend_cells).expect("should build uint");
let subtrahend = UInt20::try_from(subtrahend_cells).expect("should build uint");
Expand Down
5 changes: 3 additions & 2 deletions singer-utils/src/uint/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::chip_handler::RangeChipOperations;
use crate::error::UtilError;
use crate::uint::constants::AddSubConstants;
use crate::uint::uint::UInt;
use ff::Field;
use ff_ext::ExtensionField;
Expand All @@ -25,8 +26,8 @@ impl<const M: usize, const C: usize> UInt<M, C> {
)?;

// if operand_0 < operand_1, the last borrow should equal 1
if borrow.len() == Self::N_CARRY_CELLS {
Ok((borrow[Self::N_CARRY_CELLS - 1], diff))
if borrow.len() == AddSubConstants::<Self>::N_CARRY_CELLS {
Ok((borrow[AddSubConstants::<Self>::N_CARRY_CELLS - 1], diff))
} else {
Ok((circuit_builder.create_cell(), diff))
}
Expand Down
38 changes: 16 additions & 22 deletions singer-utils/src/uint/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,27 @@ impl<const M: usize, const C: usize> UInt<M, C> {

/// The number of `RANGE_CHIP_BIT_WIDTH` cells needed to represent the entire `UInt<M, C>`
pub const N_RANGE_CELLS: usize = Self::N_OPERAND_CELLS * Self::N_RANGE_CELLS_PER_CELL;
}

/// The number of `RANGE_CHIP_BIT_WIDTH` cells needed to represent the carry cells, assuming
/// no overflow.
pub const N_RANGE_CELLS_IN_CARRY_NO_OVERFLOW: usize =
Self::N_CARRY_CELLS_NO_OVERFLOW * Self::N_RANGE_CELLS_PER_CELL;
/// Holds addition specific constants
pub struct AddSubConstants<UInt> {
_marker: PhantomData<UInt>,
}

impl<const M: usize, const C: usize> AddSubConstants<UInt<M, C>> {
/// The size of the witness
pub const N_WITNESS_CELLS: usize = Self::N_RANGE_CELLS + Self::N_CARRY_CELLS;

/// The size of the witness assuming carry has no overflow
/// |Range_values| + |Carry - 1|
pub const N_WITNESS_CELLS_NO_CARRY_OVERFLOW: usize =
Self::N_RANGE_CELLS + Self::N_CARRY_CELLS_NO_OVERFLOW;
pub const N_WITNESS_CELLS: usize = UInt::<M, C>::N_RANGE_CELLS + Self::N_CARRY_CELLS;

// TODO: add documentation
// TODO: why this?
pub const N_NO_OVERFLOW_WITNESS_UNSAFE_CELLS: usize = Self::N_CARRY_CELLS_NO_OVERFLOW;

// Arithmetic Constants

/// Number of cells required to track carry information for the addition operation.
/// operand_0 = a b c
/// operand_1 = e f g
/// ----------
/// result = h i j
/// carry = k l m -
/// |Carry| = |Cells|
pub const N_CARRY_CELLS: usize = Self::N_OPERAND_CELLS;
pub const N_CARRY_CELLS: usize = UInt::<M, C>::N_OPERAND_CELLS;

/// Number of cells required to track carry information if we assume the addition
/// operation cannot lead to overflow.
Expand All @@ -58,13 +51,14 @@ impl<const M: usize, const C: usize> UInt<M, C> {
/// carry = l m -
/// |Carry| = |Cells - 1|
const N_CARRY_CELLS_NO_OVERFLOW: usize = Self::N_CARRY_CELLS - 1;
}

/// Holds addition specific constants
struct AddSubConstants<UInt> {
_marker: PhantomData<UInt>,
}
/// The number of `RANGE_CHIP_BIT_WIDTH` cells needed to represent the carry cells, assuming
/// no overflow.
pub const N_RANGE_CELLS_IN_CARRY_NO_OVERFLOW: usize =
Self::N_CARRY_CELLS_NO_OVERFLOW * UInt::<M, C>::N_RANGE_CELLS_PER_CELL;

impl<const M: usize, const C: usize> AddSubConstants<UInt<M, C>> {
// TODO: fill this as specific constants become clearer
/// The size of the witness assuming carry has no overflow
/// |Range_values| + |Carry - 1|
pub const N_WITNESS_CELLS_NO_CARRY_OVERFLOW: usize =
UInt::<M, C>::N_RANGE_CELLS + Self::N_CARRY_CELLS_NO_OVERFLOW;
}
5 changes: 3 additions & 2 deletions singer-utils/src/uint/witness_extractors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::uint::constants::AddSubConstants;
use crate::uint::uint::UInt;
use simple_frontend::structs::CellId;

Expand All @@ -10,7 +11,7 @@ impl<const M: usize, const C: usize> UInt<M, C> {
}

pub fn extract_carry_no_overflow(witness: &[CellId]) -> &[CellId] {
&witness[Self::N_RANGE_CELLS_IN_CARRY_NO_OVERFLOW..]
&witness[AddSubConstants::<Self>::N_RANGE_CELLS_IN_CARRY_NO_OVERFLOW..]
}

// TODO: why do we need this
Expand All @@ -32,6 +33,6 @@ impl<const M: usize, const C: usize> UInt<M, C> {
}

pub fn extract_range_values_no_overflow(witness: &[CellId]) -> &[CellId] {
&witness[..Self::N_RANGE_CELLS_IN_CARRY_NO_OVERFLOW]
&witness[..AddSubConstants::<Self>::N_RANGE_CELLS_IN_CARRY_NO_OVERFLOW]
}
}
12 changes: 7 additions & 5 deletions singer/src/instructions/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ff_ext::ExtensionField;
use gkr::structs::Circuit;
use paste::paste;
use simple_frontend::structs::{CircuitBuilder, MixedCell};
use singer_utils::uint::constants::AddSubConstants;
use singer_utils::{
chip_handler::{
BytecodeChipOperations, GlobalStateChipOperations, OAMOperations, ROMOperations,
Expand Down Expand Up @@ -33,17 +34,17 @@ register_witness!(
stack_top => 1,
clk => 1,

pc_add => PCUInt::N_NO_OVERFLOW_WITNESS_UNSAFE_CELLS,
stack_ts_add => TSUInt::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,
pc_add => AddSubConstants::<PCUInt>::N_NO_OVERFLOW_WITNESS_UNSAFE_CELLS,
stack_ts_add => AddSubConstants::<TSUInt>::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,

old_stack_ts0 => TSUInt::N_OPERAND_CELLS,
old_stack_ts_lt0 => TSUInt::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,
old_stack_ts_lt0 => AddSubConstants::<TSUInt>::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,
old_stack_ts1 => TSUInt::N_OPERAND_CELLS,
old_stack_ts_lt1 => TSUInt::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,
old_stack_ts_lt1 => AddSubConstants::<TSUInt>::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,

addend_0 => StackUInt::N_OPERAND_CELLS,
addend_1 => StackUInt::N_OPERAND_CELLS,
instruction_add => StackUInt::N_WITNESS_CELLS
instruction_add => AddSubConstants::<StackUInt>::N_WITNESS_CELLS
}
);

Expand Down Expand Up @@ -188,6 +189,7 @@ mod test {
use simple_frontend::structs::CellId;
use singer_utils::constants::RANGE_CHIP_BIT_WIDTH;
use singer_utils::structs::{StackUInt, TSUInt};
use singer_utils::uint::constants::AddSubConstants;
use std::collections::BTreeMap;
use std::time::Instant;
use transcript::Transcript;
Expand Down
7 changes: 4 additions & 3 deletions singer/src/instructions/calldataload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ff_ext::ExtensionField;
use gkr::structs::Circuit;
use paste::paste;
use simple_frontend::structs::{CircuitBuilder, MixedCell};
use singer_utils::uint::constants::AddSubConstants;
use singer_utils::{
chip_handler::{
BytecodeChipOperations, CalldataChipOperations, GlobalStateChipOperations, OAMOperations,
Expand Down Expand Up @@ -34,13 +35,13 @@ register_witness!(
stack_top => 1,
clk => 1,

pc_add => PCUInt::N_NO_OVERFLOW_WITNESS_UNSAFE_CELLS,
stack_ts_add => TSUInt::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,
pc_add => AddSubConstants::<PCUInt>::N_NO_OVERFLOW_WITNESS_UNSAFE_CELLS,
stack_ts_add => AddSubConstants::<TSUInt>::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,

data => StackUInt::N_OPERAND_CELLS,
offset => UInt64::N_OPERAND_CELLS,
old_stack_ts => TSUInt::N_OPERAND_CELLS,
old_stack_ts_lt => TSUInt::N_WITNESS_CELLS
old_stack_ts_lt => AddSubConstants::<TSUInt>::N_WITNESS_CELLS
}
);

Expand Down
7 changes: 4 additions & 3 deletions singer/src/instructions/dup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ff_ext::ExtensionField;
use gkr::structs::Circuit;
use paste::paste;
use simple_frontend::structs::{CircuitBuilder, MixedCell};
use singer_utils::uint::constants::AddSubConstants;
use singer_utils::{
chip_handler::{
BytecodeChipOperations, GlobalStateChipOperations, OAMOperations, ROMOperations,
Expand Down Expand Up @@ -33,12 +34,12 @@ register_witness!(
stack_top => 1,
clk => 1,

pc_add => PCUInt::N_NO_OVERFLOW_WITNESS_UNSAFE_CELLS,
stack_ts_add => TSUInt::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,
pc_add => AddSubConstants::<PCUInt>::N_NO_OVERFLOW_WITNESS_UNSAFE_CELLS,
stack_ts_add => AddSubConstants::<TSUInt>::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,

stack_values => StackUInt::N_OPERAND_CELLS,
old_stack_ts => TSUInt::N_OPERAND_CELLS,
old_stack_ts_lt => TSUInt::N_WITNESS_CELLS
old_stack_ts_lt => AddSubConstants::<TSUInt>::N_WITNESS_CELLS
}
);

Expand Down
11 changes: 6 additions & 5 deletions singer/src/instructions/gt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ff_ext::ExtensionField;
use gkr::structs::Circuit;
use paste::paste;
use simple_frontend::structs::{CircuitBuilder, MixedCell};
use singer_utils::uint::constants::AddSubConstants;
use singer_utils::{
chip_handler::{
BytecodeChipOperations, GlobalStateChipOperations, OAMOperations, ROMOperations,
Expand Down Expand Up @@ -33,17 +34,17 @@ register_witness!(
stack_top => 1,
clk => 1,

pc_add => PCUInt::N_NO_OVERFLOW_WITNESS_UNSAFE_CELLS,
stack_ts_add => TSUInt::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,
pc_add => AddSubConstants::<PCUInt>::N_NO_OVERFLOW_WITNESS_UNSAFE_CELLS,
stack_ts_add => AddSubConstants::<TSUInt>::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,

old_stack_ts0 => TSUInt::N_OPERAND_CELLS,
old_stack_ts_lt0 => TSUInt::N_WITNESS_CELLS,
old_stack_ts_lt0 => AddSubConstants::<TSUInt>::N_WITNESS_CELLS,
old_stack_ts1 => TSUInt::N_OPERAND_CELLS,
old_stack_ts_lt1 => TSUInt::N_WITNESS_CELLS,
old_stack_ts_lt1 => AddSubConstants::<TSUInt>::N_WITNESS_CELLS,

oprand_0 => StackUInt::N_OPERAND_CELLS,
oprand_1 => StackUInt::N_OPERAND_CELLS,
instruction_gt => StackUInt::N_WITNESS_CELLS
instruction_gt => AddSubConstants::<StackUInt>::N_WITNESS_CELLS
}
);

Expand Down
Loading

0 comments on commit 55519da

Please sign in to comment.