Abstract away the binary mapping from xor/notand.

This commit is contained in:
Sean Bowe 2015-12-28 02:21:52 -07:00
parent 774c4375ee
commit 75533e5e97
1 changed files with 9 additions and 8 deletions

View File

@ -71,22 +71,23 @@ fn keccakf(st: &mut [Byte], rounds: usize)
} }
} }
fn xor<B: Borrow<Bit>>(&self, other: &State<B>) -> State<Bit> { fn binary_map<F, B>(&self, other: &State<B>, f: F) -> State<Bit>
where F: Fn(&Bit, &Bit) -> Bit, B: Borrow<Bit>
{
State { State {
bits: self.bits.iter().map(|a| a.borrow()) bits: self.bits.iter().map(|a| a.borrow())
.zip(other.bits.iter().map(|a| a.borrow())) .zip(other.bits.iter().map(|a| a.borrow()))
.map(|(a, b)| a.xor(b)) .map(|(a, b)| f(a, b))
.collect() .collect()
} }
} }
fn xor<B: Borrow<Bit>>(&self, other: &State<B>) -> State<Bit> {
self.binary_map(other, Bit::xor)
}
fn notand<B: Borrow<Bit>>(&self, other: &State<B>) -> State<Bit> { fn notand<B: Borrow<Bit>>(&self, other: &State<B>) -> State<Bit> {
State { self.binary_map(other, Bit::notand)
bits: self.bits.iter().map(|a| a.borrow())
.zip(other.bits.iter().map(|a| a.borrow()))
.map(|(a, b)| a.notand(b))
.collect()
}
} }
fn rotl(&self, by: usize) -> State<Bit> { fn rotl(&self, by: usize) -> State<Bit> {