-
Notifications
You must be signed in to change notification settings - Fork 12
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: added support for comparison operators #86
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ use crate::fns::{ | |
expressions::evaluate_quadratic_expression, | ||
unconstrained_helpers::{ | ||
__add_with_flags, __neg_with_flags, __sub_with_flags, __validate_gt_remainder, | ||
__validate_in_field_compute_borrow_flags, | ||
__validate_in_field_compute_borrow_flags, __cmp_remainder, | ||
}, unconstrained_ops::{__div, __mul, __udiv_mod}, | ||
}; | ||
|
||
|
@@ -294,6 +294,58 @@ pub(crate) fn validate_gt<let N: u32, let MOD_BITS: u32>(lhs: [Field; N], rhs: [ | |
assert(result_limb == 0); | ||
} | ||
|
||
pub(crate) fn cmp<let N: u32, let MOD_BITS: u32>(lhs: [Field; N], rhs: [Field; N]) -> bool { | ||
// so we do... p - x - r = 0 and there might be borrow flags | ||
// a - b = r | ||
// p + a - b - r = 0 | ||
let (comp_result, result, carry_flags, borrow_flags) = unsafe { __cmp_remainder(lhs, rhs) }; | ||
validate_in_range::<_, MOD_BITS>(result); | ||
|
||
let borrow_shift = 0x1000000000000000000000000000000; | ||
let carry_shift = 0x1000000000000000000000000000000; | ||
Comment on lines
+340
to
+341
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are the same value so why not have a single constant? Also this should be named to make it clear how big they are. |
||
|
||
let mut addend: [Field; N] = [0; N]; | ||
if comp_result == true { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was first thinking about skipping the if statement by casting the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you just want to swap the lhs and rhs then you can do let (lhs, rhs) = if comp_result {
(rhs, lhs)
} else {
(lhs, rhs)
} This would end up with needing to merge two There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The best thing to do in this situation however would be to integrate the benchmarking system I added to https://github.com/noir-lang/noir-library-starter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks I was wondering if there's a way to do a swap in a single line :D There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We still need to reduce this copy-pasting. |
||
let result_limb = rhs[0] - lhs[0] + addend[0] - result[0] - 1 | ||
+ (borrow_flags[0] as Field * borrow_shift) | ||
- (carry_flags[0] as Field * carry_shift); | ||
assert(result_limb == 0); | ||
|
||
for i in 1..N - 1 { | ||
let result_limb = rhs[i] - lhs[i] + addend[i] - result[i] - borrow_flags[i - 1] as Field | ||
+ carry_flags[i - 1] as Field | ||
+ ((borrow_flags[i] as Field - carry_flags[i] as Field) * borrow_shift); | ||
assert(result_limb == 0); | ||
} | ||
|
||
let result_limb = rhs[N - 1] - lhs[N - 1] + addend[N - 1] | ||
- result[N - 1] | ||
- borrow_flags[N - 2] as Field | ||
+ carry_flags[N - 2] as Field; | ||
assert(result_limb == 0); | ||
true | ||
} else { | ||
let result_limb = lhs[0] - rhs[0] + addend[0] - result[0] - 1 | ||
+ (borrow_flags[0] as Field * borrow_shift) | ||
- (carry_flags[0] as Field * carry_shift); | ||
assert(result_limb == 0); | ||
|
||
for i in 1..N - 1 { | ||
let result_limb = lhs[i] - rhs[i] + addend[i] - result[i] - borrow_flags[i - 1] as Field | ||
+ carry_flags[i - 1] as Field | ||
+ ((borrow_flags[i] as Field - carry_flags[i] as Field) * borrow_shift); | ||
assert(result_limb == 0); | ||
} | ||
|
||
let result_limb = lhs[N - 1] - rhs[N - 1] + addend[N - 1] | ||
- result[N - 1] | ||
- borrow_flags[N - 2] as Field | ||
+ carry_flags[N - 2] as Field; | ||
assert(result_limb == 0); | ||
false | ||
} | ||
} | ||
|
||
pub(crate) fn neg<let N: u32, let MOD_BITS: u32>( | ||
params: P<N, MOD_BITS>, | ||
val: [Field; N], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not return an
Ordering
from this function?