Combine Rho with Pi and Chi.

Starting to switch to plane-wise processing.
master
magical 2015-01-01 03:06:32 -08:00
parent f67abd3a9d
commit 0b361a2be7
3 changed files with 78 additions and 132 deletions

52
gen.go
View File

@ -28,14 +28,9 @@ func main() {
x, y = y, (2*x+3*y)%5
}
var ctx = struct {
Rotc [5][5]int
}{
rotc,
}
err := tmpl.Execute(os.Stdout, &ctx)
err := tmpl.Execute(os.Stdout, nil)
if err != nil {
fmt.Println(err)
fmt.Fprintln(os.Stderr, err)
}
}
@ -60,6 +55,10 @@ func bfunc(x, y int) string {
return fmt.Sprintf("b%d%d", x%5, y%5)
}
func rotcfunc(x, y int) int {
return rotc[x%5][y%5]
}
var funcs = template.FuncMap{
"count": count,
"add": add,
@ -68,6 +67,7 @@ var funcs = template.FuncMap{
"mod": mod,
"a": afunc,
"b": bfunc,
"rotc": rotcfunc,
}
var tmpl = template.Must(template.New("keccak").Funcs(funcs).Parse(`
@ -84,44 +84,32 @@ func roundGo(a *[25]uint64) {
// Theta
var c0, c1, c2, c3, c4 uint64
{{ range $y := count 5 }}
{{ range $x := count 5 }}
{{ if eq $y 0 }}
c{{$x}} = {{a $x $y}}
{{ else }}
c{{$x}} ^= {{a $x $y}}
{{ end }}
{{ end }}
{{ range $x := count 5 }}
c{{$x}} = {{a $x 0}} ^ {{a $x 1}} ^ {{a $x 2}} ^ {{a $x 3}} ^ {{a $x 4}}
{{ end }}
var d uint64
{{ range $x := count 5 }}
{{ $x0 := mod (add $x 4) 5 }}
{{ $x4 := mod (add $x 4) 5 }}
{{ $x1 := mod (add $x 1) 5 }}
d = c{{$x0}} ^ (c{{$x1}}<<1 | c{{$x1}}>>63)
d = c{{$x4}} ^ (c{{$x1}}<<1 | c{{$x1}}>>63)
{{ range $y := count 5 }}
{{b $x $y}} = {{a $x $y}} ^ d
{{ end }}
{{ end }}
// Rho
{{ range $y := count 5 }}
{{ range $x := count 5 }}
{{ $b := b $x $y }}
{{ $r := index $.Rotc $x $y }}
{{$b}} = {{$b}}<<{{$r}} | {{$b}}>>{{sub 64 $r}}
{{ end }}
{{ end }}
// Pi / Chi / output
// Rho / Pi / Chi / output
{{ range $y := count 5 }}
{{ range $x := count 5 }}
{{ $x0 := add $x (mul $y 3) }}
{{ $y0 := $x }}
{{ $x1 := add (add $x 1) (mul $y 3) }}
{{ $y1 := add $x 1 }}
{{ $x2 := add (add $x 2) (mul $y 3) }}
{{ $y2 := add $x 2 }}
{{a $x $y}} = {{b $x0 $y0}} ^ ({{b $x2 $y2}} &^ {{b $x1 $y1}})
{{ $b := b $x0 $y0 }}
{{ $r := rotc $x0 $y0 }}
c{{$x}} = {{$b}}<<{{$r}} | {{$b}}>>{{sub 64 $r}}
{{ end }}
{{ range $x := count 5 }}
{{ $x1 := mod (add $x 1) 5 }}
{{ $x2 := mod (add $x 2) 5 }}
{{a $x $y}} = c{{$x}} ^ (c{{$x2}} &^ c{{$x1}})
{{ end }}
{{ end }}
}

View File

@ -19,55 +19,15 @@ func roundGo(a *[25]uint64) {
// Theta
var c0, c1, c2, c3, c4 uint64
c0 = a[0]
c0 = a[0] ^ a[5] ^ a[10] ^ a[15] ^ a[20]
c1 = a[1]
c1 = a[1] ^ a[6] ^ a[11] ^ a[16] ^ a[21]
c2 = a[2]
c2 = a[2] ^ a[7] ^ a[12] ^ a[17] ^ a[22]
c3 = a[3]
c3 = a[3] ^ a[8] ^ a[13] ^ a[18] ^ a[23]
c4 = a[4]
c0 ^= a[5]
c1 ^= a[6]
c2 ^= a[7]
c3 ^= a[8]
c4 ^= a[9]
c0 ^= a[10]
c1 ^= a[11]
c2 ^= a[12]
c3 ^= a[13]
c4 ^= a[14]
c0 ^= a[15]
c1 ^= a[16]
c2 ^= a[17]
c3 ^= a[18]
c4 ^= a[19]
c0 ^= a[20]
c1 ^= a[21]
c2 ^= a[22]
c3 ^= a[23]
c4 ^= a[24]
c4 = a[4] ^ a[9] ^ a[14] ^ a[19] ^ a[24]
var d uint64
@ -131,108 +91,106 @@ func roundGo(a *[25]uint64) {
b44 = a[24] ^ d
// Rho
// Rho / Pi / Chi / output
b00 = b00<<0 | b00>>64
c0 = b00<<0 | b00>>64
b10 = b10<<1 | b10>>63
c1 = b11<<44 | b11>>20
b20 = b20<<62 | b20>>2
c2 = b22<<43 | b22>>21
b30 = b30<<28 | b30>>36
c3 = b33<<21 | b33>>43
b40 = b40<<27 | b40>>37
c4 = b44<<14 | b44>>50
b01 = b01<<36 | b01>>28
a[0] = c0 ^ (c2 &^ c1)
b11 = b11<<44 | b11>>20
a[1] = c1 ^ (c3 &^ c2)
b21 = b21<<6 | b21>>58
a[2] = c2 ^ (c4 &^ c3)
b31 = b31<<55 | b31>>9
a[3] = c3 ^ (c0 &^ c4)
b41 = b41<<20 | b41>>44
a[4] = c4 ^ (c1 &^ c0)
b02 = b02<<3 | b02>>61
c0 = b30<<28 | b30>>36
b12 = b12<<10 | b12>>54
c1 = b41<<20 | b41>>44
b22 = b22<<43 | b22>>21
c2 = b02<<3 | b02>>61
b32 = b32<<25 | b32>>39
c3 = b13<<45 | b13>>19
b42 = b42<<39 | b42>>25
c4 = b24<<61 | b24>>3
b03 = b03<<41 | b03>>23
a[5] = c0 ^ (c2 &^ c1)
b13 = b13<<45 | b13>>19
a[6] = c1 ^ (c3 &^ c2)
b23 = b23<<15 | b23>>49
a[7] = c2 ^ (c4 &^ c3)
b33 = b33<<21 | b33>>43
a[8] = c3 ^ (c0 &^ c4)
b43 = b43<<8 | b43>>56
a[9] = c4 ^ (c1 &^ c0)
b04 = b04<<18 | b04>>46
c0 = b10<<1 | b10>>63
b14 = b14<<2 | b14>>62
c1 = b21<<6 | b21>>58
b24 = b24<<61 | b24>>3
c2 = b32<<25 | b32>>39
b34 = b34<<56 | b34>>8
c3 = b43<<8 | b43>>56
b44 = b44<<14 | b44>>50
c4 = b04<<18 | b04>>46
// Pi / Chi / output
a[10] = c0 ^ (c2 &^ c1)
a[0] = b00 ^ (b22 &^ b11)
a[11] = c1 ^ (c3 &^ c2)
a[1] = b11 ^ (b33 &^ b22)
a[12] = c2 ^ (c4 &^ c3)
a[2] = b22 ^ (b44 &^ b33)
a[13] = c3 ^ (c0 &^ c4)
a[3] = b33 ^ (b00 &^ b44)
a[14] = c4 ^ (c1 &^ c0)
a[4] = b44 ^ (b11 &^ b00)
c0 = b40<<27 | b40>>37
a[5] = b30 ^ (b02 &^ b41)
c1 = b01<<36 | b01>>28
a[6] = b41 ^ (b13 &^ b02)
c2 = b12<<10 | b12>>54
a[7] = b02 ^ (b24 &^ b13)
c3 = b23<<15 | b23>>49
a[8] = b13 ^ (b30 &^ b24)
c4 = b34<<56 | b34>>8
a[9] = b24 ^ (b41 &^ b30)
a[15] = c0 ^ (c2 &^ c1)
a[10] = b10 ^ (b32 &^ b21)
a[16] = c1 ^ (c3 &^ c2)
a[11] = b21 ^ (b43 &^ b32)
a[17] = c2 ^ (c4 &^ c3)
a[12] = b32 ^ (b04 &^ b43)
a[18] = c3 ^ (c0 &^ c4)
a[13] = b43 ^ (b10 &^ b04)
a[19] = c4 ^ (c1 &^ c0)
a[14] = b04 ^ (b21 &^ b10)
c0 = b20<<62 | b20>>2
a[15] = b40 ^ (b12 &^ b01)
c1 = b31<<55 | b31>>9
a[16] = b01 ^ (b23 &^ b12)
c2 = b42<<39 | b42>>25
a[17] = b12 ^ (b34 &^ b23)
c3 = b03<<41 | b03>>23
a[18] = b23 ^ (b40 &^ b34)
c4 = b14<<2 | b14>>62
a[19] = b34 ^ (b01 &^ b40)
a[20] = c0 ^ (c2 &^ c1)
a[20] = b20 ^ (b42 &^ b31)
a[21] = c1 ^ (c3 &^ c2)
a[21] = b31 ^ (b03 &^ b42)
a[22] = c2 ^ (c4 &^ c3)
a[22] = b42 ^ (b14 &^ b03)
a[23] = c3 ^ (c0 &^ c4)
a[23] = b03 ^ (b20 &^ b14)
a[24] = b14 ^ (b31 &^ b20)
a[24] = c4 ^ (c1 &^ c0)
}

View File

@ -27,7 +27,7 @@ func TestKeccak256(t *testing.T) {
h := newKeccak256()
sum := h.Sum(nil)
if !reflect.DeepEqual(sum, vector256) {
t.Errorf("\"\": want % x, got % x", vector256, sum)
t.Errorf("Keccak-256(\"\"): want % x, got % x", vector256, sum)
}
}
@ -35,7 +35,7 @@ func TestKeccak512(t *testing.T) {
h := newKeccak512()
sum := h.Sum(nil)
if !reflect.DeepEqual(sum, vector512) {
t.Errorf("\"\": want % x, got % x", vector512, sum)
t.Errorf("Keccak-512(\"\"): want % x, got % x", vector512, sum)
}
}