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

refactor: Refactor namespace allocations using ns! macro #1132

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 6 additions & 11 deletions src/circuit/circuit_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,16 @@ mod tests {
let mut cs = TestConstraintSystem::<Fr>::new();

for x in 0..128 {
let alloc_a =
AllocatedNum::alloc(&mut cs.namespace(|| x.to_string()), || Ok(Fr::from(x)))
.unwrap();
let bits = alloc_a
.to_bits_le(&mut cs.namespace(|| format!("bits_{x}")))
.unwrap();
let alloc_a = AllocatedNum::alloc(ns!(cs, x.to_string()), || Ok(Fr::from(x))).unwrap();
let bits = alloc_a.to_bits_le(ns!(cs, format!("bits_{x}"))).unwrap();
let popcount_result =
AllocatedNum::alloc(&mut cs.namespace(|| format!("alloc popcount {x}")), || {
AllocatedNum::alloc(ns!(cs, format!("alloc popcount {x}")), || {
Ok(Fr::from(u64::from(x.count_ones())))
})
.unwrap();

popcount_equal(
&mut cs.namespace(|| format!("popcount {x}")),
ns!(cs, format!("popcount {x}")),
&bits,
popcount_result.get_variable(),
);
Expand All @@ -69,9 +65,8 @@ mod tests {
#[test]
fn test_enforce_pack() {
let mut cs = TestConstraintSystem::<Fr>::new();
let a_num =
AllocatedNum::alloc_infallible(&mut cs.namespace(|| "a num"), || Fr::from_u64(42));
let bits = a_num.to_bits_le(&mut cs.namespace(|| "bits")).unwrap();
let a_num = AllocatedNum::alloc_infallible(ns!(cs, "a num"), || Fr::from_u64(42));
let bits = a_num.to_bits_le(ns!(cs, "bits")).unwrap();
implies_pack(&mut cs, &Boolean::Constant(true), &bits, &a_num);
assert!(cs.is_satisfied());
}
Expand Down
35 changes: 15 additions & 20 deletions src/circuit/gadgets/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub(crate) fn implies_u64<F: LurkField, CS: ConstraintSystem<F>>(
for i in 0..64 {
let b = a_u64 & 1;
let b_bool = Boolean::Is(AllocatedBit::alloc(
&mut cs.namespace(|| format!("b.{i}")),
ns!(cs, format!("b.{i}")),
Some(b == 1),
)?);
bits.push(b_bool);
Expand All @@ -148,12 +148,7 @@ pub(crate) fn implies_u64<F: LurkField, CS: ConstraintSystem<F>>(
}

// premise -> a = sum(bits)
implies_pack(
&mut cs.namespace(|| "u64 bit decomposition check"),
premise,
&bits,
a,
);
implies_pack(ns!(cs, "u64 bit decomposition check"), premise, &bits, a);

Ok(())
}
Expand Down Expand Up @@ -881,7 +876,7 @@ mod tests {
let mut cs = TestConstraintSystem::<Fr>::new();
let num = AllocatedNum::alloc_infallible(cs.namespace(|| "num"), || Fr::from(n));
let pb = Boolean::constant(premise);
implies_equal_zero(&mut cs.namespace(|| "implies equal zero"), &pb, &num);
implies_equal_zero(ns!(cs, "implies equal zero"), &pb, &num);
cs.is_satisfied()
};

Expand All @@ -898,7 +893,7 @@ mod tests {
let a_num = AllocatedNum::alloc_infallible(cs.namespace(|| "a_num"), || a);
let b_num = AllocatedNum::alloc_infallible(cs.namespace(|| "b_num"), || b);
let pb = Boolean::constant(premise);
implies_equal(&mut cs.namespace(|| "implies equal"), &pb, &a_num, &b_num);
implies_equal(ns!(cs, "implies equal"), &pb, &a_num, &b_num);
cs.is_satisfied()
};

Expand All @@ -915,7 +910,7 @@ mod tests {
let a_num = AllocatedNum::alloc_infallible(cs.namespace(|| "a_num"), || a);
let b_num = AllocatedNum::alloc_infallible(cs.namespace(|| "b_num"), || b);
let pb = Boolean::constant(premise);
let _ = implies_unequal(&mut cs.namespace(|| "implies equal"), &pb, &a_num, &b_num);
let _ = implies_unequal(ns!(cs, "implies equal"), &pb, &a_num, &b_num);
cs.is_satisfied()
};

Expand All @@ -933,7 +928,7 @@ mod tests {
let mut cs = TestConstraintSystem::<Fr>::new();
let num = AllocatedNum::alloc_infallible(cs.namespace(|| "num"), || n);
let pb = Boolean::constant(premise);
let _ = implies_unequal_const(&mut cs.namespace(|| "implies equal zero"), &pb, &num, t);
let _ = implies_unequal_const(ns!(cs, "implies equal zero"), &pb, &num, t);
cs.is_satisfied()
};

Expand All @@ -952,7 +947,7 @@ mod tests {
let mut cs = TestConstraintSystem::<Fr>::new();
let num = AllocatedNum::alloc_infallible(cs.namespace(|| "num"), || n);
let pb = Boolean::constant(premise);
implies_equal_const(&mut cs.namespace(|| "implies equal zero"), &pb, &num, t);
implies_equal_const(ns!(cs, "implies equal zero"), &pb, &num, t);
cs.is_satisfied()
};

Expand All @@ -977,7 +972,7 @@ mod tests {
v[e] = Boolean::constant(true);
};
let alloc_sum = AllocatedNum::alloc(cs.namespace(|| "sum"), || Ok(Fr::from(sum)));
popcount_equal(&mut cs.namespace(|| "popcount equal"), &v, alloc_sum.unwrap().get_variable());
popcount_equal(ns!(cs, "popcount equal"), &v, alloc_sum.unwrap().get_variable());
assert_eq!(cs.is_satisfied(), result);
};

Expand Down Expand Up @@ -1028,7 +1023,7 @@ mod tests {
}
let mut cs = TestConstraintSystem::<Fr>::new();
let p = Boolean::Constant(premise);
enforce_selector_with_premise(&mut cs.namespace(|| "enforce selector with premise"), &p, &v);
enforce_selector_with_premise(ns!(cs, "enforce selector with premise"), &p, &v);
assert_eq!(cs.is_satisfied(), result);
};

Expand Down Expand Up @@ -1083,12 +1078,12 @@ mod tests {
fn prop_alloc_equal_const((x, y) in any::<(FWrap<Fr>, FWrap<Fr>)>()) {
let mut cs = TestConstraintSystem::<Fr>::new();

let a = AllocatedNum::alloc_infallible(&mut cs.namespace(|| "a"), || x.0);
let a = AllocatedNum::alloc_infallible(ns!(cs, "a"), || x.0);

let equal =
alloc_equal_const(&mut cs.namespace(|| "alloc_equal_const"), &a, x.0).unwrap();
alloc_equal_const(ns!(cs, "alloc_equal_const"), &a, x.0).unwrap();
let equal2 =
alloc_equal_const(&mut cs.namespace(|| "alloc_equal_const 2"), &a, y.0).unwrap();
alloc_equal_const(ns!(cs, "alloc_equal_const 2"), &a, y.0).unwrap();
// a must always equal x.
assert!(equal.get_value().unwrap());

Expand Down Expand Up @@ -1292,14 +1287,14 @@ mod tests {
fn test_implies_u64_negative_edge_case() {
let mut cs = TestConstraintSystem::<Fr>::new();

let alloc_num = AllocatedNum::alloc(&mut cs.namespace(|| "num"), || {
let alloc_num = AllocatedNum::alloc(ns!(cs, "num"), || {
// Edge case: 2ˆ64 = 18446744073709551616
Ok(Fr::from_str_vartime("18446744073709551616").unwrap())
})
.unwrap();

let t = Boolean::Constant(true);
implies_u64(&mut cs.namespace(|| "enforce u64"), &t, &alloc_num).unwrap();
implies_u64(ns!(cs, "enforce u64"), &t, &alloc_num).unwrap();
assert!(!cs.is_satisfied());
}

Expand All @@ -1311,7 +1306,7 @@ mod tests {
let num = AllocatedNum::alloc_infallible(cs.namespace(|| "num"), || f.0);

let t = Boolean::Constant(true);
implies_u64(&mut cs.namespace(|| "enforce u64"), &t, &num).unwrap();
implies_u64(ns!(cs, "enforce u64"), &t, &num).unwrap();

let f_u64_roundtrip: Fr = f.0.to_u64_unchecked().into();
let was_u64 = f_u64_roundtrip == f.0;
Expand Down
17 changes: 4 additions & 13 deletions src/circuit/gadgets/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ impl ExprTag {
&self,
cs: &mut CS,
) -> AllocatedNum<F> {
allocate_constant(
&mut cs.namespace(|| format!("{self:?} tag")),
self.to_field(),
)
allocate_constant(ns!(cs, format!("{self:?} tag")), self.to_field())
}
}

Expand All @@ -55,7 +52,7 @@ impl ContTag {
cs: &mut CS,
) -> AllocatedNum<F> {
allocate_constant(
&mut cs.namespace(|| format!("{self:?} base continuation tag")),
ns!(cs, format!("{self:?} base continuation tag")),
self.to_field(),
)
}
Expand All @@ -66,10 +63,7 @@ impl Op1 {
&self,
cs: &mut CS,
) -> AllocatedNum<F> {
allocate_constant(
&mut cs.namespace(|| format!("{self:?} tag")),
self.to_field(),
)
allocate_constant(ns!(cs, format!("{self:?} tag")), self.to_field())
}
}

Expand All @@ -78,9 +72,6 @@ impl Op2 {
&self,
cs: &mut CS,
) -> AllocatedNum<F> {
allocate_constant(
&mut cs.namespace(|| format!("{self:?} tag")),
self.to_field(),
)
allocate_constant(ns!(cs, format!("{self:?} tag")), self.to_field())
}
}
4 changes: 2 additions & 2 deletions src/circuit/gadgets/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ macro_rules! or {
)
};
($cs:expr, $a:expr, $b:expr, $c:expr, $($x:expr),+) => {{
let or_tmp_cs_ = &mut $cs.namespace(|| format!("or({})", stringify!(vec![$a, $b, $c, $($x),*])));
adr1anh marked this conversation as resolved.
Show resolved Hide resolved
let or_tmp_cs_ = ns!($cs, format!("or({})", stringify!(vec![$a, $b, $c, $($x),*])));
bellpepper::gadgets::boolean_utils::or_v(or_tmp_cs_, &[$a, $b, $c, $($x),*])
}};
($cs:expr, $a:expr, $($x:expr),+) => {{
let or_tmp_cs_ = &mut $cs.namespace(|| format!("or {}", stringify!(vec![$a, $($x),*])));
let or_tmp_cs_ = ns!($cs, format!("or {}", stringify!(vec![$a, $($x),*])));
let or_tmp_ = or!(or_tmp_cs_, $($x),*)?;
or!(or_tmp_cs_, $a, &or_tmp_)
}};
Expand Down
Loading