You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Good idea. We have for now postponed implementing such methods as in general the arithmetic in circuits are done on field elements and doing bit operations is quite expensive (as we need to decompose the field elements to bits etc.). But I do see that this kind of functionality is needed anyway, so it would be good to have idiomatic way. But if we implement it, then I think it makes more sense to have them as gadgets (e.g. in std/ package) instead of extending frontend.API.
ivokub
changed the title
More APIs of arithmetic operations for frontend.Variable
feat: binary operations for frontend.VariableSep 3, 2024
Thanks for the feedback especially shedding light on "bit operations is quite expensive". Another thought is the granuality of bitwise operations, which may potentially generate unexpected result.
Example: a is a fixed 8-bit value , left shift by 4 bits should result in ( 0xc5 << 4 ) & 0xff = 0x50. Following code shows the unexpected result, as Variable has no semantics of bit level granularity.
var a, b frontend.Variable
a = 0xc5
b = Lsh(a, 4) // b = 0xc5 << 4 = 0x0c50 (16bit integer)
We can again use another gadget to perform AND to get correct result, e.g., result := and(b, 0xff). It would be nice to provide an option bits in binary gadgets for convenient use:
It would be very useful to provide more arithmetic operations for
frontend.Variable
, including:The arithmetic operations can be user-friendly APIs to use. Example code:
gnark/std/math/bits
provide compatible types(e.g.,U8
), but direct APIs forVariable
are more convenient.I have implemented a demo:
The text was updated successfully, but these errors were encountered: