This commit is contained in:
Sean Bowe 2015-12-28 04:02:22 -07:00
parent 75533e5e97
commit 8445bb4bb7
1 changed files with 11 additions and 18 deletions

View File

@ -32,8 +32,6 @@ fn keccakf(st: &mut [Byte], rounds: usize)
impl<'a> State<&'a mut Bit> { impl<'a> State<&'a mut Bit> {
fn new(bytes: &'a mut [Byte]) -> State<&'a mut Bit> { fn new(bytes: &'a mut [Byte]) -> State<&'a mut Bit> {
assert_eq!(bytes.len(), 8); // 64 bit lanes
State { State {
bits: bytes.iter_mut() bits: bytes.iter_mut()
.rev() // Endianness .rev() // Endianness
@ -52,8 +50,8 @@ fn keccakf(st: &mut [Byte], rounds: usize)
impl From<u64> for State<Bit> { impl From<u64> for State<Bit> {
fn from(num: u64) -> State<Bit> { fn from(num: u64) -> State<Bit> {
fn bit_at(num: u64, i: usize) -> u8 { fn bit_at(num: u64, i: usize) -> bool {
((num << i) >> 63) as u8 ((num << i) >> 63) == 1
} }
State { State {
@ -104,8 +102,6 @@ fn keccakf(st: &mut [Byte], rounds: usize)
let mut st: Vec<_> = st.chunks_mut(8).map(|c| State::new(c)).collect(); let mut st: Vec<_> = st.chunks_mut(8).map(|c| State::new(c)).collect();
assert_eq!(st.len(), 25);
for round in 0..rounds { for round in 0..rounds {
/* /*
// Theta // Theta
@ -255,7 +251,7 @@ fn keccak(rate: usize, capacity: usize, mut input: &[Byte], delimited_suffix: u8
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
enum Bit { enum Bit {
Constant(u8) Constant(bool)
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@ -270,11 +266,11 @@ impl Byte {
for bit in &self.bits { for bit in &self.bits {
match bit { match bit {
&Bit::Constant(1) => { &Bit::Constant(true) => {
acc |= 0b00000001 << cur; acc |= 1 << cur;
}, },
&Bit::Constant(0) => {}, &Bit::Constant(false) => {},
_ => panic!("Tried to unwrap a constant from a non-constant") //_ => panic!("Tried to unwrap a constant from a non-constant")
} }
cur -= 1; cur -= 1;
} }
@ -295,16 +291,13 @@ impl Byte {
impl Bit { impl Bit {
fn byte(byte: u8) -> Byte { fn byte(byte: u8) -> Byte {
Byte { Byte {
bits: (0..8).map(|i| byte & (0b00000001 << i) != 0) bits: (0..8).map(|i| Bit::constant(byte & (1 << i) != 0))
.map(|b| Bit::constant(if b { 1 } else { 0 }))
.rev() .rev()
.collect() .collect()
} }
} }
fn constant(num: u8) -> Bit { fn constant(num: bool) -> Bit {
assert_eq!((1 - num) * num, 0); // haha
Bit::Constant(num) Bit::Constant(num)
} }
@ -312,7 +305,7 @@ impl Bit {
fn xor(&self, other: &Bit) -> Bit { fn xor(&self, other: &Bit) -> Bit {
match (self, other) { match (self, other) {
(&Bit::Constant(a), &Bit::Constant(b)) => { (&Bit::Constant(a), &Bit::Constant(b)) => {
Bit::constant(a ^ b) Bit::constant(a != b)
}, },
//_ => unimplemented!() //_ => unimplemented!()
} }
@ -322,7 +315,7 @@ impl Bit {
fn notand(&self, other: &Bit) -> Bit { fn notand(&self, other: &Bit) -> Bit {
match (self, other) { match (self, other) {
(&Bit::Constant(a), &Bit::Constant(b)) => { (&Bit::Constant(a), &Bit::Constant(b)) => {
Bit::constant((a ^ 1) & b) Bit::constant((!a) && b)
}, },
//_ => unimplemented!() //_ => unimplemented!()
} }