Skip to content

Commit

Permalink
use add method for AllocateNum
Browse files Browse the repository at this point in the history
  • Loading branch information
jobez committed Nov 1, 2023
1 parent 519b4a7 commit f32d3c0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 32 deletions.
21 changes: 7 additions & 14 deletions src/circuit/circuit_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use super::gadgets::constraints::{
pick, pick_const, sub,
};
use crate::circuit::circuit_frame::constraints::{
add, allocate_is_negative, boolean_to_num, enforce_pack, enforce_product_and_sum, mul,
allocate_is_negative, boolean_to_num, enforce_pack, enforce_product_and_sum, mul,
};
use crate::circuit::gadgets::hashes::{AllocatedConsWitness, AllocatedContWitness};
use crate::circuit::ToInputs;
Expand Down Expand Up @@ -4304,7 +4304,7 @@ fn apply_continuation<F: LurkField, CS: ConstraintSystem<F>>(
ContTag::Binop2.to_field(),
)?;

let sum = add(&mut cs.namespace(|| "sum"), a, b)?;
let sum = a.add(&mut cs.namespace(|| "sum"), b)?;
let diff = sub(&mut cs.namespace(|| "difference"), a, b)?;
let product = mul(&mut cs.namespace(|| "product"), a, b)?;

Expand Down Expand Up @@ -4486,9 +4486,8 @@ fn apply_continuation<F: LurkField, CS: ConstraintSystem<F>>(

// Subtraction in U64 is almost the same as subtraction in the field.
// If the difference is negative, we need to add 2^64 to get back to U64 domain.
let field_arithmetic_result_plus_2p64 = add(
let field_arithmetic_result_plus_2p64 = field_arithmetic_result.hash().add(
&mut cs.namespace(|| "field arithmetic result plus 2^64"),
field_arithmetic_result.hash(),
&g.power2_64_num,
)?;

Expand Down Expand Up @@ -5383,11 +5382,8 @@ fn enforce_u64_div_mod<F: LurkField, CS: ConstraintSystem<F>>(
&alloc_q_num,
&alloc_arg2_num,
)?;
let sum_u64mod = add(
&mut cs.namespace(|| "sum remainder mod u64"),
&product_u64mod,
&alloc_r_num,
)?;
let sum_u64mod =
product_u64mod.add(&mut cs.namespace(|| "sum remainder mod u64"), &alloc_r_num)?;
let u64mod_decomp = alloc_equal(
&mut cs.namespace(|| "check u64 mod decomposition"),
&sum_u64mod,
Expand Down Expand Up @@ -5657,11 +5653,8 @@ fn destructure_list_aux<F: LurkField, CS: ConstraintSystem<F>>(
let is_cons = alloc_equal(&mut cs.namespace(|| "is_cons"), list.tag(), &g.cons_tag)?;
let increment = boolean_to_num(&mut cs.namespace(|| "increment"), &is_cons)?;

let new_length_so_far = add(
&mut cs.namespace(|| "new_length_so_far"),
&increment,
length_so_far,
)?;
let new_length_so_far =
increment.add(&mut cs.namespace(|| "new_length_so_far"), length_so_far)?;

Check warning on line 5657 in src/circuit/circuit_frame.rs

View check run for this annotation

Codecov / codecov/patch

src/circuit/circuit_frame.rs#L5656-L5657

Added lines #L5656 - L5657 were not covered by tests

if n == 0 {
return Ok(new_length_so_far.clone());
Expand Down
13 changes: 2 additions & 11 deletions src/circuit/gadgets/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,6 @@ pub(crate) fn enforce_equal_const<F: PrimeField, A, AR, CS: ConstraintSystem<F>>
);
}

/// Compute sum and enforce it.
pub(crate) fn add<F: PrimeField, CS: ConstraintSystem<F>>(
cs: CS,
a: &AllocatedNum<F>,
b: &AllocatedNum<F>,
) -> Result<AllocatedNum<F>, SynthesisError> {
a.add(cs, b)
}

/// Creates a linear combination representing the popcount (sum of one bits) of `v`.
pub(crate) fn popcount_lc<F: PrimeField, CS: ConstraintSystem<F>>(
v: &[Boolean],
Expand Down Expand Up @@ -900,7 +891,7 @@ pub(crate) fn allocate_is_negative<F: LurkField, CS: ConstraintSystem<F>>(
mut cs: CS,
num: &AllocatedNum<F>,
) -> Result<Boolean, SynthesisError> {
let double_num = add(&mut cs.namespace(|| "double num"), num, num)?;
let double_num = num.add(&mut cs.namespace(|| "double num"), num)?;
let double_num_bits = double_num
.to_bits_le_strict(&mut cs.namespace(|| "double num bits"))
.unwrap();
Expand Down Expand Up @@ -1145,7 +1136,7 @@ mod tests {
let a = AllocatedNum::alloc_infallible(cs.namespace(|| "a"), || x.0);
let b = AllocatedNum::alloc_infallible(cs.namespace(|| "b"), || y.0);

let res = add(cs.namespace(|| "a+b"), &a, &b).expect("add failed");
let res = a.add(cs.namespace(|| "a+b"), &b).expect("add failed");

Check warning on line 1139 in src/circuit/gadgets/constraints.rs

View check run for this annotation

Codecov / codecov/patch

src/circuit/gadgets/constraints.rs#L1139

Added line #L1139 was not covered by tests

let mut tmp = a.get_value().expect("get_value failed");
tmp.add_assign(&b.get_value().expect("get_value failed"));
Expand Down
4 changes: 2 additions & 2 deletions src/coprocessor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ pub(crate) mod test {
use serde::{Deserialize, Serialize};

use super::*;
use crate::circuit::gadgets::constraints::{add, alloc_equal, and, mul};
use crate::circuit::gadgets::constraints::{alloc_equal, and, mul};
use crate::lem::Tag as LEMTag;
use crate::tag::{ExprTag, Tag};
use std::marker::PhantomData;
Expand Down Expand Up @@ -278,7 +278,7 @@ pub(crate) mod test {

// a^2 + b = c
let a2 = mul(&mut cs.namespace(|| "square"), a.hash(), a.hash())?;
let c = add(&mut cs.namespace(|| "add"), &a2, b.hash())?;
let c = a2.add(&mut cs.namespace(|| "add"), b.hash())?;

Check warning on line 281 in src/coprocessor/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/coprocessor/mod.rs#L281

Added line #L281 was not covered by tests
let c_ptr = AllocatedPtr::alloc_tag(cs, ExprTag::Num.to_field(), c)?;

let result_expr0 =
Expand Down
10 changes: 5 additions & 5 deletions src/lem/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use std::{
use crate::{
circuit::gadgets::{
constraints::{
add, alloc_equal, alloc_is_zero, and, div, enforce_product_and_sum,
alloc_equal, alloc_is_zero, and, div, enforce_product_and_sum,
enforce_selector_with_premise, implies_equal, implies_equal_const, implies_pack,
implies_u64, implies_unequal_const, mul, or, pick, sub,
},
Expand Down Expand Up @@ -789,7 +789,7 @@ fn synthesize_block<F: LurkField, CS: ConstraintSystem<F>, C: Coprocessor<F>>(
let b = bound_allocations.get_ptr(b)?;
let a_num = a.hash();
let b_num = b.hash();
let c_num = add(cs.namespace(|| "add"), a_num, b_num)?;
let c_num = a_num.add(cs.namespace(|| "add"), b_num)?;
let tag = ctx.global_allocator.get_tag_cloned(&Num)?;
let c = AllocatedPtr::from_parts(tag, c_num);
bound_allocations.insert_ptr(tgt.clone(), c);
Expand Down Expand Up @@ -848,9 +848,9 @@ fn synthesize_block<F: LurkField, CS: ConstraintSystem<F>, C: Coprocessor<F>>(
let b_num = bound_allocations.get_ptr(b)?.hash();
let diff = sub(cs.namespace(|| "diff"), a_num, b_num)?;
// Double a, b, a-b
let double_a = add(cs.namespace(|| "double_a"), a_num, a_num)?;
let double_b = add(cs.namespace(|| "double_b"), b_num, b_num)?;
let double_diff = add(cs.namespace(|| "double_diff"), &diff, &diff)?;
let double_a = a_num.add(cs.namespace(|| "double_a"), a_num)?;
let double_b = b_num.add(cs.namespace(|| "double_b"), b_num)?;
let double_diff = diff.add(cs.namespace(|| "double_diff"), &diff)?;
// Get slot allocated preimages/bits for the double of a, b, a-b
let (double_a_preimg, double_a_bits) =
&ctx.bit_decomp_slots[next_slot.consume_bit_decomp()];
Expand Down

0 comments on commit f32d3c0

Please sign in to comment.