-
Notifications
You must be signed in to change notification settings - Fork 59
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
Builtin operators #197
Open
katat
wants to merge
4
commits into
main
Choose a base branch
from
builtin-ops
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Builtin operators #197
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fn main(pub xx: Field, yy: Field) { | ||
let res = xx < yy; | ||
assert(res); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -254,6 +254,121 @@ pub fn not_equal<B: Backend>( | |
acc | ||
} | ||
|
||
/// Returns 1 if lhs < rhs, 0 otherwise | ||
pub fn less_than<B: Backend>( | ||
compiler: &mut CircuitWriter<B>, | ||
bitlen: Option<usize>, | ||
lhs: &ConstOrCell<B::Field, B::Var>, | ||
rhs: &ConstOrCell<B::Field, B::Var>, | ||
span: Span, | ||
) -> Var<B::Field, B::Var> { | ||
let one = B::Field::one(); | ||
let zero = B::Field::zero(); | ||
|
||
// Instead of comparing bit by bit, we check the carry bit: | ||
// lhs + (1 << LEN) - rhs | ||
// proof: | ||
// lhs + (1 << LEN) will add a carry bit, valued 1, to the bit array representing lhs, | ||
// resulted in a bit array of length LEN + 1, named as sum_bits. | ||
// if `lhs < rhs``, then `lhs - rhs < 0`, thus `(1 << LEN) + lhs - rhs < (1 << LEN)` | ||
// then, the carry bit of sum_bits is 0. | ||
// otherwise, the carry bit of sum_bits is 1. | ||
|
||
/* | ||
psuedo code: | ||
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. pseudo* |
||
let carry_bit_len = LEN + 1; | ||
|
||
# 1 << LEN | ||
let mut pow2 = 1; | ||
for ii in 0..LEN { | ||
pow2 = pow2 + pow2; | ||
} | ||
|
||
let sum = (pow2 + lhs) - rhs; | ||
let sum_bit = bits::to_bits(carry_bit_len, sum); | ||
|
||
let b1 = false; | ||
let b2 = true; | ||
let res = if sum_bit[LEN] { b1 } else { b2 }; | ||
|
||
*/ | ||
|
||
let modulus_bits: usize = B::Field::modulus_biguint() | ||
.bits() | ||
.try_into() | ||
.expect("can't determine the number of bits in the modulus"); | ||
|
||
let bitlen_upper_bound = modulus_bits - 2; | ||
let bit_len = bitlen.unwrap_or(bitlen_upper_bound); | ||
|
||
assert!(bit_len <= (bitlen_upper_bound)); | ||
|
||
|
||
let carry_bit_len = bit_len + 1; | ||
|
||
|
||
// let pow2 = (1 << bit_len) as u32; | ||
// let pow2 = B::Field::from(pow2); | ||
let two = B::Field::from(2u32); | ||
let pow2 = two.pow([bit_len as u64]); | ||
|
||
// let pow2_lhs = compiler.backend.add_const(lhs, &pow2, span); | ||
match (lhs, rhs) { | ||
(ConstOrCell::Const(lhs), ConstOrCell::Const(rhs)) => { | ||
let res = if lhs < rhs { one } else { zero }; | ||
|
||
Var::new_constant(res, span) | ||
} | ||
(_, _) => { | ||
let pow2_lhs = match lhs { | ||
// todo: we really should refactor the backend to handle ConstOrCell | ||
ConstOrCell::Const(lhs) => compiler.backend.add_constant( | ||
Some("wrap a constant as var"), | ||
*lhs + pow2, | ||
span, | ||
), | ||
ConstOrCell::Cell(lhs) => compiler.backend.add_const(lhs, &pow2, span), | ||
}; | ||
|
||
let rhs = match rhs { | ||
ConstOrCell::Const(rhs) => compiler.backend.add_constant( | ||
Some("wrap a constant as var"), | ||
*rhs, | ||
span, | ||
), | ||
ConstOrCell::Cell(rhs) => rhs.clone(), | ||
}; | ||
|
||
let sum = compiler.backend.sub(&pow2_lhs, &rhs, span); | ||
|
||
// todo: this api call is kind of weird here, maybe these bulitin shouldn't get inputs from the `GenericParameters` | ||
let generic_var_name = "LEN".to_string(); | ||
let mut gens = GenericParameters::default(); | ||
gens.add(generic_var_name.clone()); | ||
gens.assign(&generic_var_name, carry_bit_len as u32, span) | ||
.unwrap(); | ||
|
||
// construct var info for sum | ||
let cbl_var = Var::new_constant(B::Field::from(carry_bit_len as u32), span); | ||
let cbl_var = VarInfo::new(cbl_var, false, Some(TyKind::Field { constant: true })); | ||
|
||
let sum_var = Var::new_var(sum, span); | ||
let sum_var = VarInfo::new(sum_var, false, Some(TyKind::Field { constant: false })); | ||
|
||
let sum_bits = to_bits(compiler, &gens, &[cbl_var, sum_var], span).unwrap().unwrap(); | ||
// convert to cell vars | ||
let sum_bits: Vec<_> = sum_bits.cvars.into_iter().collect(); | ||
|
||
// if sum_bits[LEN] == 0, then lhs < rhs | ||
let res = &is_zero_cell(compiler, &sum_bits[bit_len], span)[0]; | ||
let res = res | ||
.cvar() | ||
.unwrap(); | ||
Var::new_var(res.clone(), span) | ||
} | ||
} | ||
} | ||
|
||
/// Returns 1 if var is zero, 0 otherwise | ||
fn is_zero_cell<B: Backend>( | ||
compiler: &mut CircuitWriter<B>, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Trying to understand this but intuitively this is not going to work when we are too close to the modulus