Documentation
¶
Overview ¶
Package conversion provides methods for converting between different primitive types.
gnark provides different wrappers for extending the usage beyond native field elements (bytes, bits, non-native elements etc.). This package implements some conversion methods between these types.
It is still work in progress and interfaces may change in the future. Currently we have implemented:
- convert from bytes to native field element ✅
- convert from bytes to emulated field element ✅
- convert from bytes to emulated field element, but allow for overflow with an option ✅
- convert from native field element to bytes ✅
- convert from emulated field element to bytes ✅
Still work in progress (open issue if you need the functionality):
- convert from native field element to emulated field element
- convert from emulated field element to another emulated field element (ECDSA)
- convert from bits to native field element (duplicate existing method, for completeness)
- convert from bits to emulated field element (? duplicate existing method, for completeness)
Index ¶
- func BytesToEmulated[T emulated.FieldParams](api frontend.API, b []uints.U8, opts ...Option) (*emulated.Element[T], error)
- func BytesToNative(api frontend.API, b []uints.U8, opts ...Option) (frontend.Variable, error)
- func EmulatedToBytes[T emulated.FieldParams](api frontend.API, v *emulated.Element[T], opts ...Option) ([]uints.U8, error)
- func GetHints() []solver.Hint
- func NativeToBytes(api frontend.API, v frontend.Variable, opts ...Option) ([]uints.U8, error)
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesToEmulated ¶
func BytesToEmulated[T emulated.FieldParams](api frontend.API, b []uints.U8, opts ...Option) (*emulated.Element[T], error)
BytesToEmulated converts the bytes in MSB order to an emulated field element. If the option WithAllowOverflow is set, then the method does not check that the input is strictly less than the modulus of the field. Otherwise, it checks that the input is strictly less than the modulus of the field.
It errors when the provided bytes slice is too large to fit into an emulated field element.
NB! Currently it supports only the case when the emulated field element limb width is divisible by 8 bits. If the limb width is not divisible by 8 bits, then the method panics. Please open an issue if you need this functionality. Otherwise, we will implement it when needed.
func BytesToNative ¶
BytesToNative converts the bytes in MSB order to a native field element. If the option WithAllowOverflow is set, then the method does not check that the input is strictly less than the modulus of the field. Otherwise, it checks that the input is strictly less than the modulus of the field.
It errors when the the provided bytes slice is too large to fit into a native field element.
func EmulatedToBytes ¶
func EmulatedToBytes[T emulated.FieldParams](api frontend.API, v *emulated.Element[T], opts ...Option) ([]uints.U8, error)
EmulatedToBytes converts an emulated field element to a slice of bytes in MSB order. The number of bytes is determined by the emulated field element bit length, rounded up to the nearest byte. The method returns a slice of uints.U8 values, which represent the bytes of the emulated field element.
If the option WithAllowOverflow is set, then the method does not check that the input is strictly less than the modulus of the field. This may happen in case of malicious hint execution. The user could bypass the overflow checking if it is done later, i.e. when composing the bytes back to a native field element using BytesToEmulated.
NB! Currently it supports only the case when the emulated field element limb width is divisible by 8 bits. If the limb width is not divisible by 8 bits, then the method panics. Please open an issue if you need this functionality.
func NativeToBytes ¶
NativeToBytes converts a native field element to a slice of bytes in MSB order. The number of bytes is determined by the field bit length, rounded up to the nearest byte. The method returns a slice of uints.U8 values, which represent the bytes of the native field element.
If the option WithAllowOverflow is set, then the method does not check that the input is strictly less than the modulus of the field. This may happen in case of malicious hint execution. The user could bypass the overflow checking if it is done later, i.e. when composing the bytes back to a native field element using BytesToNative.
Types ¶
type Option ¶
type Option func(*config) error
Option allows to configure the conversion functions behavior.
func WithAllowOverflow ¶
func WithAllowOverflow() Option
WithAllowOverflow allows for overflowing the modulus when converting bytes to emulated field element. When not set, then we assert that the constructed element is strictly less than the modulus.