From cdb0656f54b877ad9bf4d1fc45ab7255bfd893ff Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Sun, 25 Feb 2018 16:51:17 -0800 Subject: [PATCH] Forgot xor. How could I? --- src/cryptonum.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/cryptonum.rs b/src/cryptonum.rs index 124d1dd..99ff208 100644 --- a/src/cryptonum.rs +++ b/src/cryptonum.rs @@ -256,6 +256,66 @@ impl<'a> BitAnd<&'a U512> for &'a U512 { //------------------------------------------------------------------------------ +impl BitXorAssign for U512 { + fn bitxor_assign(&mut self, other: U512) { + self.bitxor_assign(&other) + } +} + +impl<'a> BitXorAssign<&'a U512> for U512 { + fn bitxor_assign(&mut self, other: &U512) { + let mut oback = other.contents.iter(); + for x in self.contents.iter_mut() { + match oback.next() { + None => panic!("Internal error in cryptonum (&=)."), + Some(v) => *x = *x ^ *v + } + } + } +} + +impl BitXor for U512 { + type Output = U512; + + fn bitxor(self, rhs: U512) -> U512 { + let mut copy = self.clone(); + copy ^= rhs; + copy + } +} + +impl<'a> BitXor<&'a U512> for U512 { + type Output = U512; + + fn bitxor(self, rhs: &U512) -> U512 { + let mut copy = self.clone(); + copy ^= rhs; + copy + } +} + +impl<'a> BitXor for &'a U512 { + type Output = U512; + + fn bitxor(self, rhs: U512) -> U512 { + let mut copy = self.clone(); + copy ^= rhs; + copy + } +} + +impl<'a> BitXor<&'a U512> for &'a U512 { + type Output = U512; + + fn bitxor(self, rhs: &U512) -> U512 { + let mut copy = self.clone(); + copy ^= rhs; + copy + } +} + +//------------------------------------------------------------------------------ + #[cfg(test)] mod test { use quickcheck::{Arbitrary,Gen}; @@ -388,6 +448,9 @@ mod test { fn and_associative(a: U512, b: U512, c: U512) -> bool { (&a & (&b & &c)) == ((&a & &b) & &c) } + fn xor_as_defined(a: U512, b: U512) -> bool { + (&a ^ &b) == ((&a | &b) & !(&a & &b)) + } fn small_or_check(x: u64, y: u64) -> bool { let x512 = U512::from_u64(x); let y512 = U512::from_u64(y); @@ -400,6 +463,12 @@ mod test { let z512 = x512 & y512; z512.to_u64() == (x & y) } + fn small_xor_check(x: u64, y: u64) -> bool { + let x512 = U512::from_u64(x); + let y512 = U512::from_u64(y); + let z512 = x512 ^ y512; + z512.to_u64() == (x ^ y) + } fn small_neg_check(x: u64) -> bool { let x512 = U512::from_u64(x); let z512 = !x512;