1use ark_ff::Field;
4use o1_utils::FieldHelpers;
5
6pub trait CairoFieldHelpers<F> {
10 fn lsb(&self) -> u8;
12
13 fn u16_chunk(&self, pos: usize) -> F;
15
16 fn to_u64(&self) -> u64;
18
19 fn to_hex_be(&self) -> String;
21}
22
23impl<F: Field> CairoFieldHelpers<F> for F {
24 fn lsb(&self) -> u8 {
25 self.to_bytes()[0]
26 }
27
28 fn u16_chunk(&self, pos: usize) -> F {
29 let bytes = self.to_bytes();
30 let chunk = u16::from(bytes[2 * pos]) + u16::from(bytes[2 * pos + 1]) * 2u16.pow(8);
31 F::from(chunk)
32 }
33
34 fn to_u64(&self) -> u64 {
35 let bytes = self.to_bytes();
36 let mut acc: u64 = 0;
37 for i in 0..8 {
38 acc += 2u64.pow(i * 8) * (bytes[i as usize] as u64);
39 }
40 acc
41 }
42
43 fn to_hex_be(&self) -> String {
44 let mut bytes = self.to_bytes();
45 bytes.reverse();
46 hex::encode(bytes)
47 }
48}