keccak/keccak.go

38 lines
722 B
Go
Raw Permalink Normal View History

2014-12-31 22:59:00 +00:00
package keccak
// roundGeneric implements one round of the keccak-f[1600] permutation.
func roundGeneric(a *[25]uint64) {
2014-12-31 23:11:40 +00:00
// Theta
2014-12-31 22:59:00 +00:00
var c [5]uint64
for x := range c {
c[x] = a[0+x] ^ a[5+x] ^ a[10+x] ^ a[15+x] ^ a[20+x]
2014-12-31 22:59:00 +00:00
}
for x := 0; x < 5; x++ {
d := c[(x+4)%5] ^ rotl(c[(x+1)%5], 1)
for y := 0; y < 5; y++ {
a[y*5+x] ^= d
}
2014-12-31 22:59:00 +00:00
}
2014-12-31 23:11:40 +00:00
// Rho and pi
2014-12-31 22:59:00 +00:00
var b [5][5]uint64
for y := 0; y < 5; y++ {
for x := 0; x < 5; x++ {
2014-12-31 22:59:00 +00:00
x0 := y
y0 := (x*2 + y*3) % 5
b[y0][x0] = rotl(a[y*5+x], rotc[y][x])
2014-12-31 22:59:00 +00:00
}
}
2014-12-31 23:11:40 +00:00
// Chi
for y := 0; y < 5; y++ {
for x := 0; x < 5; x++ {
a[y*5+x] = b[y][x] ^ (^b[y][(x+1)%5] & b[y][(x+2)%5])
}
2014-12-31 22:59:00 +00:00
}
}
func rotl(a uint64, r uint) uint64 {
2015-01-01 00:40:56 +00:00
return a<<(r%64) | a>>(64-(r%64))
2014-12-31 22:59:00 +00:00
}