Recreate a field element back from output of `decompose`. Assumes big-endian representation in `decomposed`
(decomposed: &[CHUNK_TYPE], chunk_bit_size: u8)
| 46 | |
| 47 | /// Recreate a field element back from output of `decompose`. Assumes big-endian representation in `decomposed` |
| 48 | pub fn compose<F: PrimeField>(decomposed: &[CHUNK_TYPE], chunk_bit_size: u8) -> crate::Result<F> { |
| 49 | match chunk_bit_size { |
| 50 | 4 => { |
| 51 | if (decomposed.len() % 2) == 1 { |
| 52 | return Err(SaverError::InvalidDecomposition); |
| 53 | } |
| 54 | let mut bytes = Vec::<u8>::with_capacity(decomposed.len() / 2); |
| 55 | for nibbles in decomposed.chunks(2) { |
| 56 | bytes.push(((nibbles[0] << 4) + nibbles[1]) as u8); |
| 57 | } |
| 58 | Ok(F::from_be_bytes_mod_order(&bytes)) |
| 59 | } |
| 60 | 8 => Ok(F::from_be_bytes_mod_order( |
| 61 | &decomposed.iter().map(|b| *b as u8).collect::<Vec<u8>>(), |
| 62 | )), |
| 63 | 16 => { |
| 64 | let mut bytes = Vec::<u8>::with_capacity(decomposed.len() * 2); |
| 65 | for byte_2 in decomposed { |
| 66 | bytes.push((byte_2 >> 8) as u8); |
| 67 | bytes.push((byte_2 & 255) as u8); |
| 68 | } |
| 69 | Ok(F::from_be_bytes_mod_order(&bytes)) |
| 70 | } |
| 71 | b => Err(SaverError::UnexpectedBase(b)), |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | #[cfg(test)] |
| 76 | #[macro_export] |
no test coverage detected