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

feat: Add must use to functions that return BoolVariable #380

Merged
merged 2 commits into from
Mar 8, 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
2 changes: 2 additions & 0 deletions plonky2x/core/src/frontend/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
}

/// 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();

Expand All @@ -372,6 +373,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
}

/// Returns 1 if i1 == i2 and 0 otherwise as a BoolVariable.
#[must_use]
pub fn is_equal<V: CircuitVariable>(&mut self, i1: V, i2: V) -> BoolVariable {
let mut result = self._true();
for (t1, t2) in i1.targets().iter().zip(i2.targets().iter()) {
Expand Down
2 changes: 1 addition & 1 deletion plonky2x/core/src/frontend/hash/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
// = 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);
Expand Down
4 changes: 2 additions & 2 deletions plonky2x/core/src/frontend/hash/sha/sha256/pad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
}
}
// 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
}
Expand Down
4 changes: 2 additions & 2 deletions plonky2x/core/src/frontend/hash/sha/sha512/pad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
}
}
// 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
}
Expand Down
6 changes: 6 additions & 0 deletions plonky2x/core/src/frontend/ops/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,13 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
///
/// Types implementing this trait can be used within the `builder.lte(lhs, rhs)` method.
pub trait LessThanOrEqual<L: PlonkParameters<D>, const D: usize, Rhs = Self> {
#[must_use]
fn lte(self, rhs: Rhs, builder: &mut CircuitBuilder<L, D>) -> BoolVariable;
}

impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
/// The less than or equal to operation (<=).
#[must_use]
pub fn lte<Lhs, Rhs>(&mut self, lhs: Lhs, rhs: Rhs) -> BoolVariable
where
Lhs: LessThanOrEqual<L, D, Rhs>,
Expand All @@ -166,6 +168,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
}

/// The less than operation (<).
#[must_use]
pub fn lt<Lhs, Rhs>(&mut self, lhs: Lhs, rhs: Rhs) -> BoolVariable
where
Rhs: LessThanOrEqual<L, D, Lhs>,
Expand All @@ -175,6 +178,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
}

/// The greater than operation (>).
#[must_use]
pub fn gt<Lhs, Rhs>(&mut self, lhs: Lhs, rhs: Rhs) -> BoolVariable
where
Lhs: LessThanOrEqual<L, D, Rhs>,
Expand All @@ -183,6 +187,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
}

/// The greater than or equal to operation (>=).
#[must_use]
pub fn gte<Lhs, Rhs>(&mut self, lhs: Lhs, rhs: Rhs) -> BoolVariable
where
Rhs: LessThanOrEqual<L, D, Lhs>,
Expand All @@ -191,6 +196,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
}

/// The within range operation (lhs <= variable < rhs).
#[must_use]
pub fn within_range<V>(&mut self, variable: V, lhs: V, rhs: V) -> BoolVariable
where
V: LessThanOrEqual<L, D, V> + Sub<L, D, V, Output = V> + One<L, D> + Clone,
Expand Down
1 change: 1 addition & 0 deletions plonky2x/core/src/frontend/uint/uint32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl From<U32Target> for U32Variable {
}

impl<L: PlonkParameters<D>, const D: usize> LessThanOrEqual<L, D> for U32Variable {
#[must_use]
fn lte(self, rhs: Self, builder: &mut CircuitBuilder<L, D>) -> BoolVariable {
list_lte_circuit(&mut builder.api, self.targets(), rhs.targets(), 32).into()
}
Expand Down
1 change: 1 addition & 0 deletions plonky2x/core/src/frontend/uint/uint32_n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ macro_rules! make_uint32_n {
}

impl<L: PlonkParameters<D>, const D: usize> LessThanOrEqual<L, D> for $a {
#[must_use]
fn lte(self, rhs: Self, builder: &mut CircuitBuilder<L, D>) -> BoolVariable {
let mut lte_acc = builder.constant::<BoolVariable>(false);
let mut equal_so_far = builder.constant::<BoolVariable>(true);
Expand Down
Loading