| 52 | } |
| 53 | |
| 54 | pub(crate) fn extern_hash_nibbles(msgs: Vec<[u8; 64]>) -> [u8; 64] { |
| 55 | assert!( |
| 56 | msgs.len() == 4 |
| 57 | || msgs.len() == 2 |
| 58 | || msgs.len() == 12 |
| 59 | || msgs.len() == 14 |
| 60 | || msgs.len() == 6 |
| 61 | || msgs.len() == 8 |
| 62 | ); |
| 63 | |
| 64 | fn hex_string_to_nibble_array(hex_string: &str) -> Vec<u8> { |
| 65 | hex_string |
| 66 | .chars() |
| 67 | .map(|c| c.to_digit(16).expect("Invalid hex character") as u8) // Convert each char to a nibble |
| 68 | .collect() |
| 69 | } |
| 70 | |
| 71 | fn nib_to_byte_array(digits: &[u8]) -> Vec<u8> { |
| 72 | let mut msg_bytes = Vec::with_capacity(digits.len() / 2); |
| 73 | |
| 74 | for nibble_pair in digits.chunks(2) { |
| 75 | let byte = (nibble_pair[0] << 4) | (nibble_pair[1] & 0b00001111); |
| 76 | msg_bytes.push(byte); |
| 77 | } |
| 78 | |
| 79 | fn le_to_be_byte_array(byte_array: Vec<u8>) -> Vec<u8> { |
| 80 | assert!( |
| 81 | byte_array.len() % 4 == 0, |
| 82 | "Byte array length must be a multiple of 4" |
| 83 | ); |
| 84 | byte_array |
| 85 | .chunks(4) // Process each group of 4 bytes (one u32) |
| 86 | .flat_map(|chunk| chunk.iter().rev().cloned()) // Reverse each chunk |
| 87 | .collect() |
| 88 | } |
| 89 | le_to_be_byte_array(msg_bytes) |
| 90 | } |
| 91 | |
| 92 | fn replace_first_n_with_zero(hex_string: &str, n: usize) -> String { |
| 93 | let mut result = String::new(); |
| 94 | |
| 95 | if hex_string.len() <= n { |
| 96 | result.push_str(&"0".repeat(hex_string.len())); // If n >= string length, replace all |
| 97 | } else { |
| 98 | result.push_str(&"0".repeat(n)); // Replace first n characters |
| 99 | result.push_str(&hex_string[0..(hex_string.len() - n)]); // Keep the rest of the string |
| 100 | } |
| 101 | result |
| 102 | } |
| 103 | |
| 104 | fn extern_hash_fp_var(fqs: Vec<[u8; 64]>) -> [u8; 64] { |
| 105 | let mut vs = Vec::new(); |
| 106 | for fq in fqs { |
| 107 | let v = fq.to_vec(); |
| 108 | vs.extend_from_slice(&v); |
| 109 | } |
| 110 | let nib_arr: Vec<u8> = vs.clone().into_iter().collect(); |
| 111 | let p_bytes: Vec<u8> = nib_to_byte_array(&nib_arr); |