From 6867167098937a0b6f9893c4c0755c234b755a3d Mon Sep 17 00:00:00 2001 From: MercysJest Date: Mon, 18 May 2026 11:25:52 -0700 Subject: [PATCH 1/2] Updates gadgets to make namespace pushing more consistent --- Cargo.toml | 2 +- src/frontend/gadgets/num.rs | 5 ++- src/frontend/gadgets/uint32.rs | 63 ++++++++++++++++------------------ 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index be97417d3..dc5cd073a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nova-snark" -version = "0.71.0" +version = "0.71.1" authors = ["Srinath Setty "] edition = "2021" description = "High-speed recursive arguments from folding schemes" diff --git a/src/frontend/gadgets/num.rs b/src/frontend/gadgets/num.rs index 3641b3a9f..f6e598c82 100644 --- a/src/frontend/gadgets/num.rs +++ b/src/frontend/gadgets/num.rs @@ -279,7 +279,10 @@ impl AllocatedNum { CS: ConstraintSystem, Scalar: PrimeFieldBits, { - let bits = boolean::field_into_allocated_bits_le(&mut cs, self.value)?; + let bits = boolean::field_into_allocated_bits_le( + cs.namespace(|| "field_into_allocated_bits_le"), + self.value, + )?; let mut lc = LinearCombination::zero(); let mut coeff = Scalar::ONE; diff --git a/src/frontend/gadgets/uint32.rs b/src/frontend/gadgets/uint32.rs index e0117b83e..008e7639c 100644 --- a/src/frontend/gadgets/uint32.rs +++ b/src/frontend/gadgets/uint32.rs @@ -117,22 +117,20 @@ impl UInt32 { } } - fn triop( + /// Compute the `maj` value (a and b) xor (a and c) xor (b and c) + /// during SHA256. + pub fn sha256_maj( mut cs: CS, a: &Self, b: &Self, c: &Self, - tri_fn: F, - circuit_fn: U, ) -> Result where Scalar: PrimeField, CS: ConstraintSystem, - F: Fn(u32, u32, u32) -> u32, - U: Fn(&mut CS, usize, &Boolean, &Boolean, &Boolean) -> Result, { let new_value = match (a.value, b.value, c.value) { - (Some(a), Some(b), Some(c)) => Some(tri_fn(a, b, c)), + (Some(a), Some(b), Some(c)) => Some((a & b) ^ (a & c) ^ (b & c)), _ => None, }; @@ -142,7 +140,9 @@ impl UInt32 { .zip(b.bits.iter()) .zip(c.bits.iter()) .enumerate() - .map(|(i, ((a, b), c))| circuit_fn(&mut cs, i, a, b, c)) + .map(|(i, ((a, b), c))| { + Boolean::sha256_maj(cs.namespace(|| format!("maj {i}")), a, b, c) + }) .collect::>()?; Ok(UInt32 { @@ -151,10 +151,10 @@ impl UInt32 { }) } - /// Compute the `maj` value (a and b) xor (a and c) xor (b and c) + /// Compute the `ch` value `(a and b) xor ((not a) and c)` /// during SHA256. - pub fn sha256_maj( - cs: CS, + pub fn sha256_ch( + mut cs: CS, a: &Self, b: &Self, c: &Self, @@ -163,31 +163,26 @@ impl UInt32 { Scalar: PrimeField, CS: ConstraintSystem, { - Self::triop( - cs, - a, - b, - c, - |a, b, c| (a & b) ^ (a & c) ^ (b & c), - |cs, i, a, b, c| Boolean::sha256_maj(cs.namespace(|| format!("maj {i}")), a, b, c), - ) - } + let new_value = match (a.value, b.value, c.value) { + (Some(a), Some(b), Some(c)) => Some((a & b) ^ ((!a) & c)), + _ => None, + }; - /// Compute the `ch` value `(a and b) xor ((not a) and c)` - /// during SHA256. - pub fn sha256_ch(cs: CS, a: &Self, b: &Self, c: &Self) -> Result - where - Scalar: PrimeField, - CS: ConstraintSystem, - { - Self::triop( - cs, - a, - b, - c, - |a, b, c| (a & b) ^ ((!a) & c), - |cs, i, a, b, c| Boolean::sha256_ch(cs.namespace(|| format!("ch {i}")), a, b, c), - ) + let bits = a + .bits + .iter() + .zip(b.bits.iter()) + .zip(c.bits.iter()) + .enumerate() + .map(|(i, ((a, b), c))| { + Boolean::sha256_ch(cs.namespace(|| format!("ch {i}")), a, b, c) + }) + .collect::>()?; + + Ok(UInt32 { + bits, + value: new_value, + }) } /// XOR this `UInt32` with another `UInt32` From 943e8b1cbd01db934ca64ff707525e574474f05b Mon Sep 17 00:00:00 2001 From: MercysJest Date: Mon, 18 May 2026 12:29:06 -0700 Subject: [PATCH 2/2] cargo fmt --- src/frontend/gadgets/uint32.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/frontend/gadgets/uint32.rs b/src/frontend/gadgets/uint32.rs index 008e7639c..2e598e5c6 100644 --- a/src/frontend/gadgets/uint32.rs +++ b/src/frontend/gadgets/uint32.rs @@ -140,9 +140,7 @@ impl UInt32 { .zip(b.bits.iter()) .zip(c.bits.iter()) .enumerate() - .map(|(i, ((a, b), c))| { - Boolean::sha256_maj(cs.namespace(|| format!("maj {i}")), a, b, c) - }) + .map(|(i, ((a, b), c))| Boolean::sha256_maj(cs.namespace(|| format!("maj {i}")), a, b, c)) .collect::>()?; Ok(UInt32 { @@ -174,9 +172,7 @@ impl UInt32 { .zip(b.bits.iter()) .zip(c.bits.iter()) .enumerate() - .map(|(i, ((a, b), c))| { - Boolean::sha256_ch(cs.namespace(|| format!("ch {i}")), a, b, c) - }) + .map(|(i, ((a, b), c))| Boolean::sha256_ch(cs.namespace(|| format!("ch {i}")), a, b, c)) .collect::>()?; Ok(UInt32 {