frontend/zk_iriscode_ss/
decode.rs

1//! Serialization and deserialization of the parameters of the iriscode circuit.
2
3/// Given a byte array representing the wirings from the image (2d) to the LH matrix multiplicand
4/// (2d), first a flattened u16 array, then serialized as bytes, return the wirings as a Vec of
5/// tuples of 4 u16s.
6pub fn decode_wirings(wirings_bytes: &[u8]) -> Vec<(u16, u16, u16, u16)> {
7    assert!(wirings_bytes.len().is_multiple_of(8)); // 4 u16s
8                                                    // Process the data in chunks of 8 bytes (one row)
9    wirings_bytes
10        .chunks_exact(8)
11        .map(|row_bytes| {
12            // Convert each pair of bytes to an u16 value
13            let a = u16::from_le_bytes(row_bytes[0..2].try_into().unwrap());
14            let b = u16::from_le_bytes(row_bytes[2..4].try_into().unwrap());
15            let c = u16::from_le_bytes(row_bytes[4..6].try_into().unwrap());
16            let d = u16::from_le_bytes(row_bytes[6..8].try_into().unwrap());
17            (a, b, c, d)
18        })
19        .collect()
20}
21
22/// Given a byte array representing a 1d i32 array, then serialized as bytes, return the i32 array.
23pub fn decode_i32_array(bytes: &[u8]) -> Vec<i32> {
24    assert!(bytes.len().is_multiple_of(4)); // 4 bytes per i32
25    bytes
26        .chunks_exact(4)
27        .map(|row_bytes| i32::from_le_bytes(row_bytes.try_into().unwrap()))
28        .collect()
29}
30
31/// Given a byte array representing a 1d i64 array, then serialized as bytes, return the i64 array.
32pub fn decode_i64_array(bytes: &[u8]) -> Vec<i64> {
33    assert!(bytes.len().is_multiple_of(8)); // 8 bytes per i64
34    bytes
35        .chunks_exact(8)
36        .map(|row_bytes| i64::from_le_bytes(row_bytes.try_into().unwrap()))
37        .collect()
38}