Skip to main content

vyre_reference/dual_impls/bitwise/xor/
reference_b.rs

1//! Bit-by-bit XOR reference for `primitive.bitwise.xor`.
2
3/// Compute XOR by reconstructing each output bit independently.
4pub fn reference(input: &[u8]) -> Vec<u8> {
5    if input.len() < 8 {
6        return vec![0; 4];
7    }
8
9    let mut output = [0_u8; 4];
10    for bit_index in 0..32 {
11        let left = bit_at(input, bit_index);
12        let right = bit_at(input, bit_index + 32);
13        if left != right {
14            output[bit_index / 8] |= 1 << (bit_index % 8);
15        }
16    }
17    output.to_vec()
18}
19
20fn bit_at(input: &[u8], bit_index: usize) -> bool {
21    let byte = input[bit_index / 8];
22    let mask = 1_u8 << (bit_index % 8);
23    byte & mask != 0
24}