MCPcopy Create free account
hub / github.com/docknetwork/crypto / decompose

Function decompose

saver/src/utils.rs:17–45  ·  view source on GitHub ↗

Given an element `F`, break it into chunks where each chunk is of `chunk_bit_size` bits. This is essentially an n-ary representation where n is `chunk_bit_size`. Returns big-endian representation.

(message: &F, chunk_bit_size: u8)

Source from the content-addressed store, hash-verified

15/// Given an element `F`, break it into chunks where each chunk is of `chunk_bit_size` bits. This is
16/// essentially an n-ary representation where n is `chunk_bit_size`. Returns big-endian representation.
17pub fn decompose<F: PrimeField>(message: &F, chunk_bit_size: u8) -> crate::Result<Vec<CHUNK_TYPE>> {
18 let bytes = message.into_bigint().to_bytes_be();
19 let mut decomposition = Vec::<CHUNK_TYPE>::new();
20 match chunk_bit_size {
21 4 => {
22 for b in bytes {
23 decomposition.push((b >> 4) as CHUNK_TYPE);
24 decomposition.push((b & 15) as CHUNK_TYPE);
25 }
26 }
27 8 => {
28 for b in bytes {
29 decomposition.push(b as CHUNK_TYPE);
30 }
31 }
32 16 => {
33 // Process 2 bytes at a time
34 for bytes_2 in bytes.chunks(2) {
35 let mut b = (bytes_2[0] as CHUNK_TYPE) << (8 as CHUNK_TYPE);
36 if bytes_2.len() > 1 {
37 b += bytes_2[1] as CHUNK_TYPE;
38 }
39 decomposition.push(b);
40 }
41 }
42 b => return Err(SaverError::UnexpectedBase(b)),
43 }
44 Ok(decomposition)
45}
46
47/// Recreate a field element back from output of `decompose`. Assumes big-endian representation in `decomposed`
48pub fn compose<F: PrimeField>(decomposed: &[CHUNK_TYPE], chunk_bit_size: u8) -> crate::Result<F> {

Callers 10

compose_decomposeFunction · 0.70
get_values_to_commitMethod · 0.70
checkFunction · 0.70
encryptMethod · 0.70
encrypt_with_proofMethod · 0.70
encrypt_altMethod · 0.70
checkFunction · 0.70
checkFunction · 0.70

Calls 1

lenMethod · 0.45

Tested by 2

compose_decomposeFunction · 0.56
checkFunction · 0.56