From 18611dc4ff59ac53f895cd45588d562f0c088758 Mon Sep 17 00:00:00 2001 From: puma314 Date: Fri, 8 Mar 2024 00:15:48 -0800 Subject: [PATCH] feat: Add must use to functions that return BoolVariable (#380) --- plonky2x/core/src/frontend/builder/mod.rs | 2 ++ plonky2x/core/src/frontend/hash/common/mod.rs | 2 +- plonky2x/core/src/frontend/hash/sha/sha256/pad.rs | 4 ++-- plonky2x/core/src/frontend/hash/sha/sha512/pad.rs | 4 ++-- plonky2x/core/src/frontend/ops/math.rs | 6 ++++++ plonky2x/core/src/frontend/uint/uint32.rs | 1 + plonky2x/core/src/frontend/uint/uint32_n.rs | 1 + 7 files changed, 15 insertions(+), 5 deletions(-) diff --git a/plonky2x/core/src/frontend/builder/mod.rs b/plonky2x/core/src/frontend/builder/mod.rs index 3a612487d..53f685ff5 100644 --- a/plonky2x/core/src/frontend/builder/mod.rs +++ b/plonky2x/core/src/frontend/builder/mod.rs @@ -358,6 +358,7 @@ impl, const D: usize> CircuitBuilder { } /// Returns 1 if i1 is zero, 0 otherwise as a boolean. + #[must_use] pub fn is_zero(&mut self, i1: Variable) -> BoolVariable { let zero = self.api.zero(); @@ -372,6 +373,7 @@ impl, const D: usize> CircuitBuilder { } /// Returns 1 if i1 == i2 and 0 otherwise as a BoolVariable. + #[must_use] pub fn is_equal(&mut self, i1: V, i2: V) -> BoolVariable { let mut result = self._true(); for (t1, t2) in i1.targets().iter().zip(i2.targets().iter()) { diff --git a/plonky2x/core/src/frontend/hash/common/mod.rs b/plonky2x/core/src/frontend/hash/common/mod.rs index 31a0bd905..8d2623c01 100644 --- a/plonky2x/core/src/frontend/hash/common/mod.rs +++ b/plonky2x/core/src/frontend/hash/common/mod.rs @@ -40,7 +40,7 @@ impl, const D: usize> CircuitBuilder { // = a*( 1 - 2*b - 2*c + 4*b*c ) + b + c - 2*b*c // = a*( 1 - 2*b -2*c + 4*m ) + b + c - 2*m // where m = b*c - // + #[must_use] pub fn xor3(&mut self, a: Variable, b: Variable, c: Variable) -> BoolVariable { let m = self.mul(b, c); let two_b = self.add(b, b); diff --git a/plonky2x/core/src/frontend/hash/sha/sha256/pad.rs b/plonky2x/core/src/frontend/hash/sha/sha256/pad.rs index 6cb66ea09..0684ae2b1 100644 --- a/plonky2x/core/src/frontend/hash/sha/sha256/pad.rs +++ b/plonky2x/core/src/frontend/hash/sha/sha256/pad.rs @@ -150,8 +150,8 @@ impl, const D: usize> CircuitBuilder { } } // These checks verify input_byte_length <= input.len(). - self.is_equal(message_byte_selector, false_t); - self.is_equal(reached_last_chunk, true_t); + self.assert_is_equal(message_byte_selector, false_t); + self.assert_is_equal(reached_last_chunk, true_t); padded_bytes } diff --git a/plonky2x/core/src/frontend/hash/sha/sha512/pad.rs b/plonky2x/core/src/frontend/hash/sha/sha512/pad.rs index 48eb45ae5..29b0803b6 100644 --- a/plonky2x/core/src/frontend/hash/sha/sha512/pad.rs +++ b/plonky2x/core/src/frontend/hash/sha/sha512/pad.rs @@ -147,8 +147,8 @@ impl, const D: usize> CircuitBuilder { } } // These checks verify input_byte_length <= input.len(). - self.is_equal(message_byte_selector, false_t); - self.is_equal(reached_last_chunk, true_t); + self.assert_is_equal(message_byte_selector, false_t); + self.assert_is_equal(reached_last_chunk, true_t); padded_bytes } diff --git a/plonky2x/core/src/frontend/ops/math.rs b/plonky2x/core/src/frontend/ops/math.rs index 579e6d369..e472faf31 100644 --- a/plonky2x/core/src/frontend/ops/math.rs +++ b/plonky2x/core/src/frontend/ops/math.rs @@ -153,11 +153,13 @@ impl, const D: usize> CircuitBuilder { /// /// Types implementing this trait can be used within the `builder.lte(lhs, rhs)` method. pub trait LessThanOrEqual, const D: usize, Rhs = Self> { + #[must_use] fn lte(self, rhs: Rhs, builder: &mut CircuitBuilder) -> BoolVariable; } impl, const D: usize> CircuitBuilder { /// The less than or equal to operation (<=). + #[must_use] pub fn lte(&mut self, lhs: Lhs, rhs: Rhs) -> BoolVariable where Lhs: LessThanOrEqual, @@ -166,6 +168,7 @@ impl, const D: usize> CircuitBuilder { } /// The less than operation (<). + #[must_use] pub fn lt(&mut self, lhs: Lhs, rhs: Rhs) -> BoolVariable where Rhs: LessThanOrEqual, @@ -175,6 +178,7 @@ impl, const D: usize> CircuitBuilder { } /// The greater than operation (>). + #[must_use] pub fn gt(&mut self, lhs: Lhs, rhs: Rhs) -> BoolVariable where Lhs: LessThanOrEqual, @@ -183,6 +187,7 @@ impl, const D: usize> CircuitBuilder { } /// The greater than or equal to operation (>=). + #[must_use] pub fn gte(&mut self, lhs: Lhs, rhs: Rhs) -> BoolVariable where Rhs: LessThanOrEqual, @@ -191,6 +196,7 @@ impl, const D: usize> CircuitBuilder { } /// The within range operation (lhs <= variable < rhs). + #[must_use] pub fn within_range(&mut self, variable: V, lhs: V, rhs: V) -> BoolVariable where V: LessThanOrEqual + Sub + One + Clone, diff --git a/plonky2x/core/src/frontend/uint/uint32.rs b/plonky2x/core/src/frontend/uint/uint32.rs index dcf211e3f..f6c5746b8 100644 --- a/plonky2x/core/src/frontend/uint/uint32.rs +++ b/plonky2x/core/src/frontend/uint/uint32.rs @@ -127,6 +127,7 @@ impl From for U32Variable { } impl, const D: usize> LessThanOrEqual for U32Variable { + #[must_use] fn lte(self, rhs: Self, builder: &mut CircuitBuilder) -> BoolVariable { list_lte_circuit(&mut builder.api, self.targets(), rhs.targets(), 32).into() } diff --git a/plonky2x/core/src/frontend/uint/uint32_n.rs b/plonky2x/core/src/frontend/uint/uint32_n.rs index c1be4538a..1c9ceb361 100644 --- a/plonky2x/core/src/frontend/uint/uint32_n.rs +++ b/plonky2x/core/src/frontend/uint/uint32_n.rs @@ -303,6 +303,7 @@ macro_rules! make_uint32_n { } impl, const D: usize> LessThanOrEqual for $a { + #[must_use] fn lte(self, rhs: Self, builder: &mut CircuitBuilder) -> BoolVariable { let mut lte_acc = builder.constant::(false); let mut equal_so_far = builder.constant::(true);