Combine keccakf steps.

master
magical 2014-12-31 15:11:40 -08:00
parent ed04711f60
commit 0ed98686b8
2 changed files with 19 additions and 39 deletions

View File

@ -2,17 +2,7 @@ package keccak
// roundGeneric implements one round of the keccak-f[1600] permutation.
func roundGeneric(a [5][5]uint64) [5][5]uint64 {
a = theta(a)
a = rho(a)
a = pi(a)
a = chi(a)
// Iota
//a[0][0] ^= RC[i]
return a
}
// Theta
func theta(a [5][5]uint64) [5][5]uint64 {
var c [5]uint64
for x := range a {
c[x] = a[x][0] ^ a[x][1] ^ a[x][2] ^ a[x][3] ^ a[x][4]
@ -25,45 +15,31 @@ func theta(a [5][5]uint64) [5][5]uint64 {
a[x][3] ^= c[x0] ^ rotl(c[x1], 1)
a[x][4] ^= c[x0] ^ rotl(c[x1], 1)
}
return a
}
// Rho
func rho(a [5][5]uint64) [5][5]uint64 {
for x := range a {
for y := range a[0] {
a[x][y] = rotl(a[x][y], rotc[x][y])
}
}
return a
}
// Pi
func pi(a [5][5]uint64) [5][5]uint64 {
// Rho and pi
var b [5][5]uint64
for x := range a {
for y := range a[0] {
x0 := y
y0 := (x*2 + y*3) % 5
b[x0][y0] = a[x][y]
b[x0][y0] = rotl(a[x][y], rotc[x][y])
}
}
return b
}
// Chi
func chi(a [5][5]uint64) [5][5]uint64 {
for y := range a[0] {
c := [5]uint64{a[0][y], a[1][y], a[2][y], a[3][y], a[4][y]}
a[0][y] ^= ^c[1] & c[2]
a[1][y] ^= ^c[2] & c[3]
a[2][y] ^= ^c[3] & c[4]
a[3][y] ^= ^c[4] & c[0]
a[4][y] ^= ^c[0] & c[1]
c := [5]uint64{b[0][y], b[1][y], b[2][y], b[3][y], b[4][y]}
a[0][y] = b[0][y] ^ ^c[1] & c[2]
a[1][y] = b[1][y] ^ ^c[2] & c[3]
a[2][y] = b[2][y] ^ ^c[3] & c[4]
a[3][y] = b[3][y] ^ ^c[4] & c[0]
a[4][y] = b[4][y] ^ ^c[0] & c[1]
}
return a
}
func rotl(a uint64, r uint) uint64 {
return a<<r | a>>(64-r)
}

View File

@ -45,15 +45,15 @@ loop:
if len(b) == 0 {
break loop
}
d.a[x][y] ^= uint64(b[0]) + uint64(b[1])<<8 + uint64(b[2])<<16 + uint64(b[3])<<24 + uint64(b[4])<<32 + uint64(b[5])<<40 + uint64(b[6])<<48 + uint64(b[7])<<56
d.a[x][y] ^= le64dec(b)
b = b[8:]
}
}
d.a = keccak(d.a)
d.a = keccakf(d.a)
d.len = 0
}
func keccak(a [5][5]uint64) [5][5]uint64 {
func keccakf(a [5][5]uint64) [5][5]uint64 {
for i := 0; i < 24; i++ {
a = roundGeneric(a)
a[0][0] ^= RC[i]
@ -77,6 +77,10 @@ func (d0 *digest) Sum(b []byte) []byte {
return b
}
func le64dec(b []byte) uint64 {
return uint64(b[0]) << 0 | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
}
func le64enc(b []byte, x uint64) []byte {
return append(b, byte(x), byte(x>>8), byte(x>>16), byte(x>>24), byte(x>>32), byte(x>>40), byte(x>>48), byte(x>>56))
}