Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor uint #90

Merged
merged 64 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
cba09be
confirm const across file impl
iammadab May 29, 2024
11949a4
Merge branch 'master' into feat/refactor-uint
iammadab May 29, 2024
210d714
Merge branch 'master' into feat/refactor-uint
iammadab May 29, 2024
a9093ff
Merge branch 'master' into feat/refactor-uint
iammadab May 29, 2024
7b6c70b
implement try from cell_id's to uint
iammadab May 29, 2024
18cfcac
add test for uint from cell ids
iammadab May 29, 2024
a2b39d6
add documentation
iammadab May 29, 2024
9bd42ed
document N_OPERAND_CELLS constant
iammadab May 29, 2024
3818103
wip
iammadab May 30, 2024
74516f3
test cell repacking
iammadab May 31, 2024
31a51c1
add documentation
iammadab May 31, 2024
c3e24e9
update packing test, ensure remaining values are 0s
iammadab May 31, 2024
e94e1e5
add more uint instantiation methods
iammadab Jun 3, 2024
97676c8
add test placeholders
iammadab Jun 3, 2024
a283f77
sketch base plan for cmp
iammadab Jun 3, 2024
65af7b5
implement and test pad cells
iammadab Jun 3, 2024
3a89440
modify from_range and from_bytes to use cell padding
iammadab Jun 3, 2024
eff16cf
extract logic for uint from arbitrary cell_width to make from_range a…
iammadab Jun 3, 2024
e9d2a41
add compile time evaluated min function
iammadab Jun 3, 2024
ffe140e
introduce new constant cleans up uint_from*
iammadab Jun 3, 2024
3bf521a
update uint from cell id test to test greater than N_OPERANDS
iammadab Jun 3, 2024
2c4a0ab
add test for uint from different sized cells
iammadab Jun 3, 2024
957ba24
implement add_unsafe
iammadab Jun 10, 2024
d49f31a
add functions sketch
iammadab Jun 11, 2024
632820f
wip
iammadab Jun 13, 2024
5b298c4
all func
iammadab Jun 13, 2024
e5403e4
building
iammadab Jun 13, 2024
5dce9db
test compiling
iammadab Jun 13, 2024
e810356
use range checker
iammadab Jun 13, 2024
17a63aa
refactor constants second pass
iammadab Jun 13, 2024
2033535
document cmp
iammadab Jun 13, 2024
9ca2e42
wip
iammadab Jun 13, 2024
14d0254
arithmetic second pass
iammadab Jun 13, 2024
c2ccb5e
delete old uint
iammadab Jun 13, 2024
4b0604f
Merge branch 'master' into feat/refactor-uint
iammadab Jun 13, 2024
8977106
refactor arithmetic
iammadab Jun 14, 2024
6dfaf71
fix documentation
iammadab Jun 14, 2024
08813e2
add test for add
iammadab Jun 14, 2024
d031363
test add constant unsafe
iammadab Jun 14, 2024
97f3945
add test for add_small_unsafe
iammadab Jun 14, 2024
8787912
implement handle borrow
iammadab Jun 14, 2024
8937ab0
test sub_unsafe
iammadab Jun 14, 2024
4c23282
merge with master
iammadab Jun 17, 2024
4d95615
rename add_small to add_cell
iammadab Jun 17, 2024
640541e
wip
iammadab Jun 17, 2024
aec94b2
change uint mod management
iammadab Jun 17, 2024
3c61078
setup structure for namespacing constants
iammadab Jun 17, 2024
02bbaee
refactor counter vector implementation
iammadab Jun 18, 2024
55519da
move add specific constants to AddSub block
iammadab Jun 25, 2024
d8aa67d
fix comment arithmetic precedence
iammadab Jun 26, 2024
834783f
wip
iammadab Jun 26, 2024
ca607d2
use range cells instead of range cells no overflow
iammadab Jun 27, 2024
d0e1d06
add optimization comment
iammadab Jun 27, 2024
fff6549
wip
iammadab Jun 27, 2024
3e74ec7
rename witness extractors
iammadab Jun 27, 2024
9b56a63
fix test building
iammadab Jun 28, 2024
b590de3
wip
iammadab Jun 28, 2024
6a07c47
wip
iammadab Jul 5, 2024
a81afef
fix bugs: correct convert_decomp endian and timestamp cmp witness size
dreamATD Jul 12, 2024
086cd96
Merge branch 'fix/tests' into feat/refactor-uint
iammadab Jul 12, 2024
eea2c28
rename uint module before merge
iammadab Jul 12, 2024
a349701
Merge branch 'master' into merge-refactor
iammadab Jul 12, 2024
189ad7f
rename uint_new back to uint
iammadab Jul 12, 2024
c718742
cleanup pr
iammadab Jul 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions singer-pro/src/instructions/mstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ 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},
constants::{OpcodeType, EVM_STACK_BYTE_WIDTH},
register_witness,
structs::{ChipChallenges, InstOutChipType, RAMHandler, ROMHandler, StackUInt, TSUInt},
uint::constants::AddSubConstants,
};
use std::{mem, sync::Arc};

Expand Down Expand Up @@ -219,7 +219,7 @@ register_witness!(
},
phase0 {
old_memory_ts => TSUInt::N_OPERAND_CELLS,
old_memory_ts_lt => AddSubConstants::<TSUInt>::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,
old_memory_ts_lt => AddSubConstants::<TSUInt>::N_WITNESS_CELLS,

offset_add_delta => AddSubConstants::<StackUInt>::N_WITNESS_CELLS,
prev_mem_byte => 1
Expand Down
2 changes: 1 addition & 1 deletion singer-utils/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ pub struct ROMHandler<Ext: ExtensionField> {

pub type UInt64 = UInt<64, VALUE_BIT_WIDTH>;
pub type PCUInt = UInt64;
pub type TSUInt = UInt<56, 56>;
pub type TSUInt = UInt<48, 48>;
pub type StackUInt = UInt<{ EVM_STACK_BIT_WIDTH as usize }, { VALUE_BIT_WIDTH as usize }>;
7 changes: 2 additions & 5 deletions singer-utils/src/uint/arithmetic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::chip_handler::RangeChipOperations;
use crate::error::UtilError;
use crate::uint::uint::UInt;
use crate::{chip_handler::RangeChipOperations, error::UtilError, uint::uint::UInt};
use ff::Field;
use ff_ext::ExtensionField;
use simple_frontend::structs::{Cell, CellId, CircuitBuilder};
Expand Down Expand Up @@ -294,8 +292,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 crate::uint::{constants::AddSubConstants, UInt};
use gkr::structs::{Circuit, CircuitWitness};
use goldilocks::{Goldilocks, GoldilocksExt2};
use itertools::Itertools;
Expand Down
9 changes: 5 additions & 4 deletions singer-utils/src/uint/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::chip_handler::RangeChipOperations;
use crate::error::UtilError;
use crate::uint::constants::AddSubConstants;
use crate::uint::uint::UInt;
use crate::{
chip_handler::RangeChipOperations,
error::UtilError,
uint::{constants::AddSubConstants, uint::UInt},
};
use ff::Field;
use ff_ext::ExtensionField;
use simple_frontend::structs::{CellId, CircuitBuilder, MixedCell};
Expand Down
15 changes: 9 additions & 6 deletions singer-utils/src/uint/uint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::constants::{BYTE_BIT_WIDTH, RANGE_CHIP_BIT_WIDTH};
use crate::error::UtilError;
use crate::uint::util::{add_one_to_big_num, convert_decomp, pad_cells};
use crate::{
constants::{BYTE_BIT_WIDTH, RANGE_CHIP_BIT_WIDTH},
error::UtilError,
uint::util::{add_one_to_big_num, convert_decomp, pad_cells},
};
use ff_ext::ExtensionField;
use goldilocks::SmallField;
use itertools::Itertools;
Expand Down Expand Up @@ -80,6 +82,7 @@ impl<const M: usize, const C: usize> UInt<M, C> {
Self::MAX_CELL_BIT_WIDTH,
is_little_endian,
)?;
debug_assert!(values.len() <= Self::N_OPERAND_CELLS);
// TODO: is this safe, do we need to ensure that the padded cells are always 0?
iammadab marked this conversation as resolved.
Show resolved Hide resolved
pad_cells(circuit_builder, &mut values, Self::N_OPERAND_CELLS);
values.try_into()
Expand Down Expand Up @@ -170,8 +173,8 @@ mod tests {
// since total bit = 30 then expect 5 cells ( 30 / 6)
// since we have 3 cells, we need to pad with 2 more
// hence expected output:
// 110010 110110 010100 000000 000000(bit representation)
// 50 54 20 0 0
// 100011 100111 000101 000000 000000(bit representation)
// 35 39 5 0 0

let witness_values = vec![3, 0, 2, 3, 1, 2, 1, 1]
.into_iter()
Expand All @@ -188,7 +191,7 @@ mod tests {
let output = circuit_witness.output_layer_witness_ref().instances[0].to_vec();
assert_eq!(
&output[..5],
vec![50, 54, 20, 0, 0]
vec![35, 39, 5, 0, 0]
.into_iter()
.map(|v| Goldilocks::from(v))
.collect_vec()
Expand Down
27 changes: 13 additions & 14 deletions singer-utils/src/uint/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ pub fn convert_decomp<E: ExtensionField>(
for values in small_cells.chunks(small_cell_count_per_big_cell) {
let big_cell = circuit_builder.create_cell();
for (small_chunk_index, small_bit_cell) in values.iter().enumerate() {
let shift_size =
(small_cell_count_per_big_cell - small_chunk_index - 1) * small_cell_bit_width;
let shift_size = small_chunk_index * small_cell_bit_width;
circuit_builder.add(
big_cell,
*small_bit_cell,
Expand Down Expand Up @@ -153,7 +152,7 @@ mod tests {
let mut circuit_builder = CircuitBuilder::<GoldilocksExt2>::new();
let (_, small_values) = circuit_builder.create_witness_in(9);
let small_bit_width = 2;
let big_bit_width = 5;
let big_bit_width = 6;
let big_values = convert_decomp(
&mut circuit_builder,
&small_values,
Expand All @@ -162,7 +161,8 @@ mod tests {
true,
)
.unwrap();
assert_eq!(big_values.len(), 5);
assert_eq!(big_values.len(), 3);
circuit_builder.create_witness_out_from_cells(&big_values);

// verify construction against concrete witness values
circuit_builder.configure();
Expand All @@ -174,18 +174,17 @@ mod tests {
// 3 0 2 3 1 2 1 1 3 (field representation)
//
// expected output
// repacking into cells of bit width 5
// we can only fit two 2-bit cells into a 5 bit cell
// 1100 1011 0110 0101 1100 (bit representation)
// 12 11 6 5 12 (field representation)
// repacking into cells of bit width 6
// we can only fit three 2-bit cells into a 6 bit cell
// 100011 100111 110101 (bit representation)
// 35 39 53 (field representation)

let witness_values = vec![3, 0, 2, 3, 1, 2, 1, 1, 3]
.into_iter()
.map(|v| Goldilocks::from(v))
.collect::<Vec<_>>();
let circuit_witness = {
let challenges = vec![GoldilocksExt2::from(2)];
let mut circuit_witness = CircuitWitness::new(&circuit, challenges);
let mut circuit_witness = CircuitWitness::new(&circuit, vec![]);
circuit_witness.add_instance(&circuit, vec![witness_values]);
circuit_witness
};
Expand All @@ -195,17 +194,17 @@ mod tests {
let output = circuit_witness.output_layer_witness_ref().instances[0].to_vec();

assert_eq!(
&output[..5],
vec![12, 11, 6, 5, 12]
&output[..3],
vec![35, 39, 53]
.into_iter()
.map(|v| Goldilocks::from(v))
.collect::<Vec<_>>()
);

// padding to power of 2
assert_eq!(
&output[5..],
vec![0, 0, 0]
&output[3..],
vec![0]
.into_iter()
.map(|v| Goldilocks::from(v))
.collect_vec()
Expand Down
4 changes: 2 additions & 2 deletions singer-utils/src/uint/witness_extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl<const M: usize, const C: usize> UInt<M, C> {
}

pub fn extract_carry_no_overflow_add_sub(witness: &[CellId]) -> &[CellId] {
iammadab marked this conversation as resolved.
Show resolved Hide resolved
&witness[AddSubConstants::<Self>::N_RANGE_CELLS_IN_CARRY_NO_OVERFLOW..]
&witness[AddSubConstants::<Self>::N_RANGE_CELLS_NO_OVERFLOW..]
}

// TODO: why do we need this
iammadab marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -35,6 +35,6 @@ impl<const M: usize, const C: usize> UInt<M, C> {
}

pub fn extract_range_values_no_overflow(witness: &[CellId]) -> &[CellId] {
&witness[..AddSubConstants::<Self>::N_RANGE_CELLS_IN_CARRY_NO_OVERFLOW]
&witness[..AddSubConstants::<Self>::N_RANGE_CELLS_NO_OVERFLOW]
}
}
39 changes: 20 additions & 19 deletions singer/src/instructions/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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 All @@ -12,6 +11,7 @@ use singer_utils::{
constants::OpcodeType,
register_witness,
structs::{PCUInt, RAMHandler, ROMHandler, StackUInt, TSUInt},
uint::constants::AddSubConstants,
};
use std::sync::Arc;

Expand All @@ -38,9 +38,9 @@ register_witness!(
stack_ts_add => AddSubConstants::<TSUInt>::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,

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

addend_0 => StackUInt::N_OPERAND_CELLS,
addend_1 => StackUInt::N_OPERAND_CELLS,
Expand Down Expand Up @@ -187,19 +187,22 @@ mod test {
use goldilocks::{Goldilocks, GoldilocksExt2};
use itertools::Itertools;
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 singer_utils::{
constants::RANGE_CHIP_BIT_WIDTH,
structs::{StackUInt, TSUInt},
uint::constants::AddSubConstants,
};
use std::{collections::BTreeMap, time::Instant};
use transcript::Transcript;

use crate::instructions::{
AddInstruction, ChipChallenges, Instruction, InstructionGraph, SingerCircuitBuilder,
use crate::{
instructions::{
AddInstruction, ChipChallenges, Instruction, InstructionGraph, SingerCircuitBuilder,
},
scheme::GKRGraphProverState,
test::{get_uint_params, test_opcode_circuit, u2vec},
CircuitWiresIn, SingerGraphBuilder, SingerParams,
};
use crate::scheme::GKRGraphProverState;
use crate::test::{get_uint_params, test_opcode_circuit, u2vec};
use crate::{CircuitWiresIn, SingerGraphBuilder, SingerParams};

impl AddInstruction {
#[inline]
Expand Down Expand Up @@ -277,8 +280,8 @@ mod test {
phase0_values_map.insert(
"phase0_stack_ts_add".to_string(),
vec![
Goldilocks::from(4u64), // first TSUInt::N_RANGE_CELLS = 1*(56/16) = 4 cells are range values, stack_ts + 1 = 4
Goldilocks::from(0u64),
Goldilocks::from(4u64), /* first TSUInt::N_RANGE_CELLS = 1*(48/16) = 3 cells are
* range values, stack_ts + 1 = 4 */
Goldilocks::from(0u64),
Goldilocks::from(0u64),
// no place for carry
Expand All @@ -296,8 +299,7 @@ mod test {
Goldilocks::from(range_values[0]),
Goldilocks::from(range_values[1]),
Goldilocks::from(range_values[2]),
Goldilocks::from(range_values[3]),
Goldilocks::from(1u64), // borrow
Goldilocks::from(1u64),
],
);
phase0_values_map.insert(
Expand All @@ -312,8 +314,7 @@ mod test {
Goldilocks::from(range_values[0]),
Goldilocks::from(range_values[1]),
Goldilocks::from(range_values[2]),
Goldilocks::from(range_values[3]),
Goldilocks::from(1u64), // borrow
Goldilocks::from(1u64),
],
);
let m: u64 = (1 << get_uint_params::<StackUInt>().1) - 1;
Expand Down
2 changes: 1 addition & 1 deletion singer/src/instructions/calldataload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ mod test {
Goldilocks::from(range_values[0]),
Goldilocks::from(range_values[1]),
Goldilocks::from(range_values[2]),
Goldilocks::from(range_values[3]),
// Goldilocks::from(range_values[3]),
Goldilocks::from(1u64), // borrow
],
);
Expand Down
2 changes: 1 addition & 1 deletion singer/src/instructions/dup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ mod test {
Goldilocks::from(range_values[0]),
Goldilocks::from(range_values[1]),
Goldilocks::from(range_values[2]),
Goldilocks::from(range_values[3]),
// Goldilocks::from(range_values[3]),
Goldilocks::from(1u64),
],
);
Expand Down
4 changes: 2 additions & 2 deletions singer/src/instructions/gt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ mod test {
Goldilocks::from(range_values[0]),
Goldilocks::from(range_values[1]),
Goldilocks::from(range_values[2]),
Goldilocks::from(range_values[3]),
// Goldilocks::from(range_values[3]),
Goldilocks::from(1u64),
],
);
Expand All @@ -307,7 +307,7 @@ mod test {
Goldilocks::from(range_values[0]),
Goldilocks::from(range_values[1]),
Goldilocks::from(range_values[2]),
Goldilocks::from(range_values[3]),
// Goldilocks::from(range_values[3]),
Goldilocks::from(1u64),
],
);
Expand Down
2 changes: 1 addition & 1 deletion singer/src/instructions/jump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ mod test {
Goldilocks::from(range_values[0]),
Goldilocks::from(range_values[1]),
Goldilocks::from(range_values[2]),
Goldilocks::from(range_values[3]),
// Goldilocks::from(range_values[3]),
Goldilocks::from(1u64), // current length has no cells for borrow
],
);
Expand Down
6 changes: 3 additions & 3 deletions singer/src/instructions/jumpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use gkr::structs::Circuit;
use itertools::izip;
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 All @@ -13,6 +12,7 @@ use singer_utils::{
constants::OpcodeType,
register_witness,
structs::{PCUInt, RAMHandler, ROMHandler, StackUInt, TSUInt},
uint::constants::AddSubConstants,
};
use std::sync::Arc;

Expand All @@ -36,9 +36,9 @@ register_witness!(
clk => 1,

old_stack_ts_dest => TSUInt::N_OPERAND_CELLS,
old_stack_ts_dest_lt => AddSubConstants::<TSUInt>::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,
old_stack_ts_dest_lt => AddSubConstants::<TSUInt>::N_WITNESS_CELLS,
old_stack_ts_cond => TSUInt::N_OPERAND_CELLS,
old_stack_ts_cond_lt => AddSubConstants::<TSUInt>::N_WITNESS_CELLS_NO_CARRY_OVERFLOW,
old_stack_ts_cond_lt => AddSubConstants::<TSUInt>::N_WITNESS_CELLS,

dest_values => StackUInt::N_OPERAND_CELLS,
cond_values => StackUInt::N_OPERAND_CELLS,
Expand Down
Loading