-
Notifications
You must be signed in to change notification settings - Fork 57
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
base: main
Are you sure you want to change the base?
Builtin operators #197
Changes from 1 commit
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 |
---|---|---|
|
@@ -239,6 +239,33 @@ pub fn modulus<B: Backend>( | |
} | ||
} | ||
|
||
/// Left shift operation | ||
pub fn left_shift<B: Backend>( | ||
compiler: &mut CircuitWriter<B>, | ||
lhs: &ConstOrCell<B::Field, B::Var>, | ||
rhs: &ConstOrCell<B::Field, B::Var>, | ||
span: Span, | ||
) -> Var<B::Field, B::Var> { | ||
// to constrain lhs * (1 << rhs) = res | ||
|
||
match (lhs, rhs) { | ||
(ConstOrCell::Const(lhs), ConstOrCell::Const(rhs)) => { | ||
// convert to bigint | ||
let pow2 = B::Field::from(2u32).pow(rhs.into_repr()); | ||
let res = *lhs * pow2; | ||
Var::new_constant(res, span) | ||
} | ||
(ConstOrCell::Cell(lhs_), ConstOrCell::Const(rhs_)) => { | ||
let pow2 = B::Field::from(2u32).pow(rhs_.into_repr()); | ||
let res = compiler.backend.mul_const(lhs_, &pow2, span); | ||
Var::new_var(res, span) | ||
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 weird thing to me is that at this point there is no way to tell if there is an overflow, the modulus length also impacts that. Maybe that's fine though... But I'm not sure what people would expect. Is this operator implemented in any other DSL? How do they handle overflows? 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. yeah, overflow is probably something to take care of here. One of the compilers restricts the value type to be integer and the shift amount to be u8. I guess the reason it restrict to be integer is for range checking the resulted value. |
||
} | ||
// todo: wrap rhs in a symbolic value | ||
(ConstOrCell::Const(_), ConstOrCell::Cell(_)) => todo!(), | ||
(ConstOrCell::Cell(_), ConstOrCell::Cell(_)) => todo!(), | ||
} | ||
} | ||
|
||
/// This takes variables that can be anything, and returns a boolean | ||
// TODO: so perhaps it's not really relevant in this file? | ||
pub fn equal<B: Backend>( | ||
|
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.
We should return an error on overflow perhaps