Debian Codegraph @

examples/sha1/asm.go

raw symbols SCIP
1 //go:build ignore
2
3 package main
4
5 import (
6 . "github.com/mmcloughlin/avo/build"
7 . "github.com/mmcloughlin/avo/operand"
8 . "github.com/mmcloughlin/avo/reg"
9 )
10
11 func main() {
12 TEXT("block", 0, "func(h *[5]uint32, m []byte)")
13 Doc("block SHA-1 hashes the 64-byte message m into the running state h.")
14 h := Mem{Base: Load(Param("h"), GP64())}
15 m := Mem{Base: Load(Param("m").Base(), GP64())}
16
17 // Store message values on the stack.
18 w := AllocLocal(64)
19 W := func(r int) Mem { return w.Offset((r % 16) * 4) }
20
21 Comment("Load initial hash.")
22 hash := [5]Register{GP32(), GP32(), GP32(), GP32(), GP32()}
23 for i, r := range hash {
24 MOVL(h.Offset(4*i), r)
25 }
26
27 Comment("Initialize registers.")
28 a, b, c, d, e := GP32(), GP32(), GP32(), GP32(), GP32()
29 for i, r := range []Register{a, b, c, d, e} {
30 MOVL(hash[i], r)
31 }
32
33 // Generate round updates.
34 quarter := []struct {
35 F func(Register, Register, Register) Register
36 K uint32
37 }{
38 {choose, 0x5a827999},
39 {xor, 0x6ed9eba1},
40 {majority, 0x8f1bbcdc},
41 {xor, 0xca62c1d6},
42 }
43
44 for r := 0; r < 80; r++ {
45 Commentf("Round %d.", r)
46 q := quarter[r/20]
47
48 // Load message value.
49 u := GP32()
50 if r < 16 {
51 MOVL(m.Offset(4*r), u)
52 BSWAPL(u)
53 } else {
54 MOVL(W(r-3), u)
55 XORL(W(r-8), u)
56 XORL(W(r-14), u)
57 XORL(W(r-16), u)
58 ROLL(U8(1), u)
59 }
60 MOVL(u, W(r))
61
62 // Compute the next state register.
63 t := GP32()
64 MOVL(a, t)
65 ROLL(U8(5), t)
66 ADDL(q.F(b, c, d), t)
67 ADDL(e, t)
68 ADDL(U32(q.K), t)
69 ADDL(u, t)
70
71 // Update registers.
72 ROLL(Imm(30), b)
73 a, b, c, d, e = t, a, b, c, d
74 }
75
76 Comment("Final add.")
77 for i, r := range []Register{a, b, c, d, e} {
78 ADDL(r, hash[i])
79 }
80
81 Comment("Store results back.")
82 for i, r := range hash {
83 MOVL(r, h.Offset(4*i))
84 }
85 RET()
86
87 Generate()
88 }
89
90 func choose(b, c, d Register) Register {
91 r := GP32()
92 MOVL(d, r)
93 XORL(c, r)
94 ANDL(b, r)
95 XORL(d, r)
96 return r
97 }
98
99 func xor(b, c, d Register) Register {
100 r := GP32()
101 MOVL(b, r)
102 XORL(c, r)
103 XORL(d, r)
104 return r
105 }
106
107 func majority(b, c, d Register) Register {
108 t, r := GP32(), GP32()
109 MOVL(b, t)
110 ORL(c, t)
111 ANDL(d, t)
112 MOVL(b, r)
113 ANDL(c, r)
114 ORL(t, r)
115 return r
116 }