Begin to refactor gen.go

master
magical 2015-01-01 00:41:19 -08:00
parent 7b01515ff6
commit c8f826bc6a
1 changed files with 25 additions and 21 deletions

46
gen.go
View File

@ -47,16 +47,17 @@ func count(n int) []int {
return out return out
} }
func add(a, b, m int) int { func add(a, b int) int { return a + b }
return (a + b) % m func sub(a, b int) int { return a - b }
func mul(a, b int) int { return a * b }
func mod(a, b int) int { return a % b }
func afunc(x, y int) string {
return fmt.Sprintf("a[%d][%d]", y%5, x%5)
} }
func sub(a, b int) int { func bfunc(x, y int) string {
return a - b return fmt.Sprintf("a%d%d", x%5, y%5)
}
func mul(a, b int) int {
return a * b
} }
var funcs = template.FuncMap{ var funcs = template.FuncMap{
@ -64,6 +65,9 @@ var funcs = template.FuncMap{
"add": add, "add": add,
"sub": sub, "sub": sub,
"mul": mul, "mul": mul,
"mod": mod,
"a": afunc,
"b": bfunc,
} }
var tmpl = template.Must(template.New("keccak").Funcs(funcs).Parse(` var tmpl = template.Must(template.New("keccak").Funcs(funcs).Parse(`
@ -83,41 +87,41 @@ func roundGo(a *[5][5]uint64) {
{{ range $y := count 5 }} {{ range $y := count 5 }}
{{ range $x := count 5 }} {{ range $x := count 5 }}
{{ if eq $y 0 }} {{ if eq $y 0 }}
c{{$x}} = a[{{$y}}][{{$x}}] c{{$x}} = {{a $x $y}}
{{ else }} {{ else }}
c{{$x}} ^= a[{{$y}}][{{$x}}] c{{$x}} ^= {{a $x $y}}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}
var d uint64 var d uint64
{{ range $x := count 5 }} {{ range $x := count 5 }}
{{ $x0 := add $x 4 5 }} {{ $x0 := mod (add $x 4) 5 }}
{{ $x1 := add $x 1 5 }} {{ $x1 := mod (add $x 1) 5 }}
d = c{{$x0}} ^ (c{{$x1}}<<1 | c{{$x1}}>>63) d = c{{$x0}} ^ (c{{$x1}}<<1 | c{{$x1}}>>63)
{{ range $y := count 5 }} {{ range $y := count 5 }}
a{{$x}}{{$y}} = a[{{$y}}][{{$x}}] ^ d {{b $x $y}} = {{a $x $y}} ^ d
{{ end }} {{ end }}
{{ end }} {{ end }}
// Rho // Rho
{{ range $y := count 5 }} {{ range $y := count 5 }}
{{ range $x := count 5 }} {{ range $x := count 5 }}
{{ $a := printf "a%d%d" $x $y }} {{ $b := b $x $y }}
{{ $r := index $.Rotc $x $y }} {{ $r := index $.Rotc $x $y }}
{{$a}} = {{$a}}<<{{$r}} | {{$a}}>>{{sub 64 $r}} {{$b}} = {{$b}}<<{{$r}} | {{$b}}>>{{sub 64 $r}}
{{ end }} {{ end }}
{{ end }} {{ end }}
// Pi / Chi / output // Pi / Chi / output
{{ range $y := count 5 }} {{ range $y := count 5 }}
{{ range $x := count 5 }} {{ range $x := count 5 }}
{{ $x0 := add $x (mul $y 3) 5 }} {{ $x0 := add $x (mul $y 3) }}
{{ $y0 := $x }} {{ $y0 := $x }}
{{ $x1 := add (add $x 1 5) (mul $y 3) 5 }} {{ $x1 := add (add $x 1) (mul $y 3) }}
{{ $y1 := add $x 1 5 }} {{ $y1 := add $x 1 }}
{{ $x2 := add (add $x 2 5) (mul $y 3) 5 }} {{ $x2 := add (add $x 2) (mul $y 3) }}
{{ $y2 := add $x 2 5 }} {{ $y2 := add $x 2 }}
a[{{$y}}][{{$x}}] = a{{$x0}}{{$y0}} ^ (a{{$x2}}{{$y2}} &^ a{{$x1}}{{$y1}}) {{a $x $y}} = {{b $x0 $y0}} ^ ({{b $x2 $y2}} &^ {{b $x1 $y1}})
{{ end }} {{ end }}
{{ end }} {{ end }}
} }