Skip to content

Commit

Permalink
rename witness extractors
Browse files Browse the repository at this point in the history
  • Loading branch information
iammadab committed Jun 27, 2024
1 parent fff6549 commit 3e74ec7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion singer-utils/src/chip_handler/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl<Ext: ExtensionField> ROMHandler<Ext> {
witness: &[CellId],
) -> Result<PCUInt, UtilError> {
// TODO: why unsafe here?
let carry = PCUInt::extract_unsafe_carry(witness);
let carry = PCUInt::extract_unsafe_carry_add_sub(witness);
PCUInt::add_const_unsafe(
circuit_builder,
&pc,
Expand Down
12 changes: 6 additions & 6 deletions singer-utils/src/uint/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<const M: usize, const C: usize> UInt<M, C> {
addend_1: &UInt<M, C>,
witness: &[CellId],
) -> Result<UInt<M, C>, UtilError> {
let carry = Self::extract_carry(witness);
let carry = Self::extract_carry_add_sub(witness);
let range_values = Self::extract_range_values(witness);
let computed_result = Self::add_unsafe(circuit_builder, addend_0, addend_1, carry)?;
range_chip_handler.range_check_uint(circuit_builder, &computed_result, Some(range_values))
Expand Down Expand Up @@ -98,7 +98,7 @@ impl<const M: usize, const C: usize> UInt<M, C> {
constant: E::BaseField,
witness: &[CellId],
) -> Result<UInt<M, C>, UtilError> {
let carry = Self::extract_carry(witness);
let carry = Self::extract_carry_add_sub(witness);
let range_values = Self::extract_range_values(witness);
let computed_result = Self::add_const_unsafe(circuit_builder, addend_0, constant, carry)?;
range_chip_handler.range_check_uint(circuit_builder, &computed_result, Some(range_values))
Expand All @@ -113,7 +113,7 @@ impl<const M: usize, const C: usize> UInt<M, C> {
constant: E::BaseField,
witness: &[CellId],
) -> Result<UInt<M, C>, UtilError> {
let carry = Self::extract_carry_no_overflow(witness);
let carry = Self::extract_carry_no_overflow_add_sub(witness);
let range_values = Self::extract_range_values_no_overflow(witness);
let computed_result = Self::add_const_unsafe(circuit_builder, addend_0, constant, carry)?;
range_chip_handler.range_check_uint(circuit_builder, &computed_result, Some(range_values))
Expand Down Expand Up @@ -153,7 +153,7 @@ impl<const M: usize, const C: usize> UInt<M, C> {
addend_1: CellId,
witness: &[CellId],
) -> Result<UInt<M, C>, UtilError> {
let carry = Self::extract_carry(witness);
let carry = Self::extract_carry_add_sub(witness);
let range_values = Self::extract_range_values(witness);
let computed_result = Self::add_cell_unsafe(circuit_builder, addend_0, addend_1, carry)?;
range_chip_handler.range_check_uint(circuit_builder, &computed_result, Some(range_values))
Expand All @@ -168,7 +168,7 @@ impl<const M: usize, const C: usize> UInt<M, C> {
addend_1: CellId,
witness: &[CellId],
) -> Result<UInt<M, C>, UtilError> {
let carry = Self::extract_carry_no_overflow(witness);
let carry = Self::extract_carry_no_overflow_add_sub(witness);
let range_values = Self::extract_range_values_no_overflow(witness);
let computed_result = Self::add_cell_unsafe(circuit_builder, addend_0, addend_1, carry)?;
range_chip_handler.range_check_uint(circuit_builder, &computed_result, Some(range_values))
Expand Down Expand Up @@ -207,7 +207,7 @@ impl<const M: usize, const C: usize> UInt<M, C> {
subtrahend: &UInt<M, C>,
witness: &[CellId],
) -> Result<UInt<M, C>, UtilError> {
let borrow = Self::extract_borrow(witness);
let borrow = Self::extract_borrow_add_sub(witness);
let range_values = Self::extract_range_values(witness);
let computed_result = Self::sub_unsafe(circuit_builder, minuend, subtrahend, borrow)?;
range_chip_handler.range_check_uint(circuit_builder, &computed_result, Some(range_values))
Expand Down
2 changes: 1 addition & 1 deletion singer-utils/src/uint/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<const M: usize, const C: usize> UInt<M, C> {
operand_1: &UInt<M, C>,
witness: &[CellId],
) -> Result<(CellId, UInt<M, C>), UtilError> {
let borrow = Self::extract_borrow(witness);
let borrow = Self::extract_borrow_add_sub(witness);
let range_values = Self::extract_range_values(witness);
let computed_diff = Self::sub_unsafe(circuit_builder, operand_0, operand_1, borrow)?;

Expand Down
12 changes: 7 additions & 5 deletions singer-utils/src/uint/witness_extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,31 @@ use crate::uint::constants::AddSubConstants;
use crate::uint::uint::UInt;
use simple_frontend::structs::CellId;

// TODO: split this into different impls, constrained by specific contexts
// e.g add_sub, mul, ...
impl<const M: usize, const C: usize> UInt<M, C> {
// witness_structure
// [...range_values..., ...carry_witness...]

pub fn extract_carry(witness: &[CellId]) -> &[CellId] {
pub fn extract_carry_add_sub(witness: &[CellId]) -> &[CellId] {
&witness[Self::N_RANGE_CELLS..]
}

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

Check failure on line 16 in singer-utils/src/uint/witness_extractors.rs

View workflow job for this annotation

GitHub Actions / Run Tests

no associated item named `N_RANGE_CELLS_IN_CARRY_NO_OVERFLOW` found for struct `AddSubConstants` in the current scope
}

// TODO: why do we need this
pub fn extract_unsafe_carry(witness: &[CellId]) -> &[CellId] {
pub fn extract_unsafe_carry_add_sub(witness: &[CellId]) -> &[CellId] {
witness
}

pub fn extract_borrow(witness: &[CellId]) -> &[CellId] {
pub fn extract_borrow_add_sub(witness: &[CellId]) -> &[CellId] {
&witness[Self::N_RANGE_CELLS..]
}

// TODO: why do we need this
pub fn extract_unsafe_borrow(witness: &[CellId]) -> &[CellId] {
pub fn extract_unsafe_borrow_add_sub(witness: &[CellId]) -> &[CellId] {
witness
}

Expand Down

0 comments on commit 3e74ec7

Please sign in to comment.