use num_bigint::BigUint;
use std::cmp::{max, Ordering};
use crate::BigUintHelpers;
pub trait BitwiseOps<Rhs = Self> {
fn bitwise_xor(input1: &Rhs, input: &Rhs) -> Rhs;
fn bitwise_and(input1: &Rhs, input: &Rhs, bytes: usize) -> Rhs;
fn bitwise_not(input: &Rhs, bits: Option<usize>) -> Rhs;
}
impl BitwiseOps for BigUint {
fn bitwise_xor(input1: &BigUint, input2: &BigUint) -> BigUint {
let bytes1 = input1.to_bytes_le().len();
let bytes2 = input2.to_bytes_le().len();
let in1 = to_padded_bytes(input1, bytes2);
let in2 = to_padded_bytes(input2, bytes1);
BigUint::from_bytes_le(
&in1.iter()
.zip(in2.iter())
.map(|(b1, b2)| b1 ^ b2)
.collect::<Vec<u8>>(),
)
}
fn bitwise_not(input: &BigUint, bits: Option<usize>) -> BigUint {
let in_bits = input.bitlen();
let bits = max(in_bits, bits.unwrap_or(0));
let mut bit_vec = vec![];
(0..bits).for_each(|i| bit_vec.push(!bit_at(input, i as u32)));
ToBigUint::to_biguint(&bit_vec)
}
fn bitwise_and(input1: &BigUint, input2: &BigUint, bytes: usize) -> BigUint {
let in1 = to_padded_bytes(input1, bytes);
let in2 = to_padded_bytes(input2, bytes);
BigUint::from_bytes_le(
&in1.iter()
.zip(in2.iter())
.map(|(b1, b2)| b1 & b2)
.collect::<Vec<u8>>(),
)
}
}
fn to_padded_bytes(input: &BigUint, bytes: usize) -> Vec<u8> {
let bytes_inp = input.to_bytes_le().len();
match bytes.cmp(&bytes_inp) {
Ordering::Greater => pad(input, bytes - bytes_inp),
Ordering::Equal | Ordering::Less => input.to_bytes_le(),
}
}
fn pad(input: &BigUint, bytes: usize) -> Vec<u8> {
let mut padded = input.to_bytes_le().to_vec();
padded.resize(bytes + padded.len(), 0u8);
padded
}
fn bit_at(input: &BigUint, index: u32) -> bool {
if input.bit(index as u64) {
((input / BigUint::from(2u8).pow(index)) % BigUint::from(2u32)) == BigUint::from(1u32)
} else {
false
}
}
trait ToBigUint {
fn to_biguint(&self) -> BigUint;
}
impl ToBigUint for Vec<bool> {
fn to_biguint(&self) -> BigUint {
let mut bigvalue = BigUint::from(0u8);
let mut power = BigUint::from(1u8);
for bit in self {
bigvalue += power.clone() * BigUint::from(*bit as u8);
power *= BigUint::from(2u8);
}
bigvalue
}
}