aztec-nr - noir_aztec::utils::conversion::fields_as_bytes

Function encode_fields_as_bytes

pub fn encode_fields_as_bytes<let N: u32>(fields: [Field; N]) -> [u8; N * 32]

Encodes an array of fields as bytes.

Losslessly preserves any field value; use try_decode_fields_from_bytes to recover the original fields.

Encoding

Each field is written as 32 big-endian bytes and the chunks are concatenated. The field array [5, 42] becomes:

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,  // First field (32 bytes)
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,42] // Second field (32 bytes)

Privacy

The BN254 modulus is < 2^254, so every 32-byte chunk has its top bit at zero and the next bit biased. The output is therefore distinguishable from uniform random bytes; take this into account when feeding it into anything that assumes uniform randomness (e.g. ciphertexts meant to look random).