Hi all, it's been a while :)
What's the most efficient way to check for underflows / overflows?
E.g. If I have a = b + c
, what's the neatest way to ensure b + c
doesn't overflow the field modulus?
E.g. If I have a = b - c
, what's the neatest way to ensure b > c
?
E.g. If I have a = b * c
, what's the neatest way to ensure b * c
doesn't overflow the field modulus?
E.g. If I have a = b / c
, is there a way to ensure c 'divides' b in the 'integer' sense? Like passing a, b, c
as inputs and asserting that c * a == b
and ensuring c * a
doesn't overflow the field modulus (somehow)?
Presumably I'd need to do the calculations in bits, to check for bits 'carrying over' 2**253?
Does anyone have any neat implementations? :)
Hi all,
I’m working on a bit of an edge case where I need to check if a field is a positive number by doing either assert(field > 0)
or assert(field < (field_prime-1)/2)
. The problem here is that ZoKrates implements comparison operators such that the operands have to be strictly less than biggest power of 2 lower than p/2
. Let’s take the example of Bn128, the operands here will have to be lower than 2^252 even though the field elements can be greater than 2^252 and lower than bn128_prime of 2592827839077369332604021086610909215435616943586837466384278596745456903517. So assert(field > 0)
would fail during compute witness stage if field passed into witness is greater than 2^252 but less than bn128_prime. Andassert(field < (bn128_prime-1)/2)
would fail at compile stage because (bn128_prime-1)/2
is already greater than 2^252. Can this limitation be worked on in ZoKrates’s rust code ? Thank you
x < constant
check, the one we have now is x < y
for x and y both variables. We already use it internally (here https://github.com/Zokrates/ZoKrates/blob/master/zokrates_core/src/flatten/mod.rs#L204)
I made some small adjustments to get_hask.zok and please find attached my part of code, "import "hashes/sha256/512bit" as sha256
def main(u32[192] hashMe) -> u32[96] result:
for field i in 0..12 do
u32[8] h = sha256(hashMe[16i..16i+8], hashMe[16i+8..16i+16])
result[8i..8i+8] = h
endfor
return result"
I get an error message "zokrates compile -i get_hash.zok -o get_hash --light
Compiling get_hash.zok
Compilation failed:
get_hash.zok: --> 3:36
|
3 | def main(u32[192] hashMe) -> u32[96] result:␊
| ^---
|
= expected op_or, op_and, op_bit_xor, op_bit_and, op_bit_or, op_equal, op_not_equal, op_lt, op_lte, op_gt, op_gte, op_add, op_sub, op_mul, op_div, op_pow, op_left_shift, or op_right_shift"