0xPolygonMiden / crypto

Cryptographic primitives used in Polygon Miden rollup
MIT License
101 stars 35 forks source link

Add `ToFieldElements` and `FromFieldElements` traits #312

Open hackaugusto opened 6 months ago

hackaugusto commented 6 months ago

Proposal

Allow to easily covert a type to from field elements. This is useful to convert values so that they can be hashed, and to pass/receive values from the VM.

Outline

pub trait ToFieldElements<E: FieldElement> {
    fn to_elements(&self) -> Vec<E>;
}

pub trait FromFieldElements<E: FieldElement> {
    fn from_elements(elements: &[E]) -> Self;
}

Alternatives

Issues

It is unclear if the implementation should use the minimum number of field elements, or add padding as expected by the VM. I feel like this should be the minimum number of felts, and the VM crate should have another trait, say ToVMInput, which performs the encoding for the VM. The implementation for ToVMInput can be custom or be on top of ToFieldElements.

Related

https://discord.com/channels/893151281848406016/1148602449247080478/1234523004650917898