-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhchacha20.wat
138 lines (130 loc) · 8 KB
/
hchacha20.wat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
(module
;; 0-64 reserved for param block
(memory (export "memory") 10 1000)
;; Reference https://github.com/jedisct1/libsodium/blob/41c7e47efd879e31504dbe9b2a46426f4551ac60/src/libsodium/crypto_core/hchacha20/core_hchacha20.c#L17
(func $core_hchacha20 (param $out_ptr i32) (param $in_ptr i32) (param $key_ptr i32)
(local $i i32)
(local $x0 i32)
(local $x1 i32)
(local $x2 i32)
(local $x3 i32)
(local $x4 i32)
(local $x5 i32)
(local $x6 i32)
(local $x7 i32)
(local $x8 i32)
(local $x9 i32)
(local $x10 i32)
(local $x11 i32)
(local $x12 i32)
(local $x13 i32)
(local $x14 i32)
(local $x15 i32)
(set_local $x0 (i32.const 0x61707865))
(set_local $x1 (i32.const 0x3320646e))
(set_local $x2 (i32.const 0x79622d32))
(set_local $x3 (i32.const 0x6b206574))
(set_local $x4 (i32.load (get_local $key_ptr)))
(set_local $x5 (i32.load (i32.add (get_local $key_ptr) (i32.const 4))))
(set_local $x6 (i32.load (i32.add (get_local $key_ptr) (i32.const 8))))
(set_local $x7 (i32.load (i32.add (get_local $key_ptr) (i32.const 12))))
(set_local $x8 (i32.load (i32.add (get_local $key_ptr) (i32.const 16))))
(set_local $x9 (i32.load (i32.add (get_local $key_ptr) (i32.const 20))))
(set_local $x10 (i32.load (i32.add (get_local $key_ptr) (i32.const 24))))
(set_local $x11 (i32.load (i32.add (get_local $key_ptr) (i32.const 28))))
(set_local $x12 (i32.load (get_local $in_ptr)))
(set_local $x13 (i32.load (i32.add (get_local $in_ptr) (i32.const 4))))
(set_local $x14 (i32.load (i32.add (get_local $in_ptr) (i32.const 8))))
(set_local $x15 (i32.load (i32.add (get_local $in_ptr) (i32.const 12))))
(set_local $i (i32.const 0))
(block $end_loop
(loop $start_loop
(br_if $end_loop (i32.le_u (get_local $i) (i32.const 10)))
;; QUARTERROUND(x0, x4, x8, x12);
(set_local $x0 (i32.add (get_local $x0) (get_local $x4)))
(set_local $x12 (i32.rotl (i32.xor (get_local $x12) (get_local $x0)) (i32.const 16)))
(set_local $x8 (i32.add (get_local $x8) (get_local $x12)))
(set_local $x4 (i32.rotl (i32.xor (get_local $x4) (get_local $x8)) (i32.const 12)))
(set_local $x0 (i32.add (get_local $x0) (get_local $x4)))
(set_local $x12 (i32.rotl (i32.xor (get_local $x12) (get_local $x0)) (i32.const 8)))
(set_local $x8 (i32.add (get_local $x8) (get_local $x12)))
(set_local $x4 (i32.rotl (i32.xor (get_local $x4) (get_local $x8)) (i32.const 7)))
;; QUARTERROUND(x1, x5, x9, x13);
(set_local $x1 (i32.add (get_local $x1) (get_local $x5)))
(set_local $x13 (i32.rotl (i32.xor (get_local $x13) (get_local $x1)) (i32.const 16)))
(set_local $x9 (i32.add (get_local $x9) (get_local $x13)))
(set_local $x5 (i32.rotl (i32.xor (get_local $x5) (get_local $x9)) (i32.const 12)))
(set_local $x1 (i32.add (get_local $x1) (get_local $x5)))
(set_local $x13 (i32.rotl (i32.xor (get_local $x13) (get_local $x1)) (i32.const 8)))
(set_local $x9 (i32.add (get_local $x9) (get_local $x13)))
(set_local $x5 (i32.rotl (i32.xor (get_local $x5) (get_local $x9)) (i32.const 7)))
;; QUARTERROUND(x2, x6, x10, x14);
(set_local $x2 (i32.add (get_local $x2) (get_local $x6)))
(set_local $x14 (i32.rotl (i32.xor (get_local $x14) (get_local $x2)) (i32.const 16)))
(set_local $x10 (i32.add (get_local $x10) (get_local $x14)))
(set_local $x6 (i32.rotl (i32.xor (get_local $x6) (get_local $x10)) (i32.const 12)))
(set_local $x2 (i32.add (get_local $x2) (get_local $x6)))
(set_local $x14 (i32.rotl (i32.xor (get_local $x14) (get_local $x2)) (i32.const 8)))
(set_local $x10 (i32.add (get_local $x10) (get_local $x14)))
(set_local $x6 (i32.rotl (i32.xor (get_local $x6) (get_local $x10)) (i32.const 7)))
;; QUARTERROUND(x3, x7, x11, x15);
(set_local $x3 (i32.add (get_local $x3) (get_local $x7)))
(set_local $x15 (i32.rotl (i32.xor (get_local $x15) (get_local $x3)) (i32.const 16)))
(set_local $x11 (i32.add (get_local $x11) (get_local $x15)))
(set_local $x7 (i32.rotl (i32.xor (get_local $x7) (get_local $x11)) (i32.const 12)))
(set_local $x3 (i32.add (get_local $x3) (get_local $x7)))
(set_local $x15 (i32.rotl (i32.xor (get_local $x15) (get_local $x3)) (i32.const 8)))
(set_local $x11 (i32.add (get_local $x11) (get_local $x15)))
(set_local $x7 (i32.rotl (i32.xor (get_local $x7) (get_local $x11)) (i32.const 7)))
;; QUARTERROUND(x0, x5, x10, x15);
(set_local $x0 (i32.add (get_local $x0) (get_local $x5)))
(set_local $x15 (i32.rotl (i32.xor (get_local $x15) (get_local $x0)) (i32.const 16)))
(set_local $x10 (i32.add (get_local $x10) (get_local $x15)))
(set_local $x5 (i32.rotl (i32.xor (get_local $x5) (get_local $x10)) (i32.const 12)))
(set_local $x0 (i32.add (get_local $x0) (get_local $x5)))
(set_local $x15 (i32.rotl (i32.xor (get_local $x15) (get_local $x0)) (i32.const 8)))
(set_local $x10 (i32.add (get_local $x10) (get_local $x15)))
(set_local $x5 (i32.rotl (i32.xor (get_local $x5) (get_local $x10)) (i32.const 7)))
;; QUARTERROUND(x1, x6, x11, x12);
(set_local $x1 (i32.add (get_local $x1) (get_local $x6)))
(set_local $x12 (i32.rotl (i32.xor (get_local $x12) (get_local $x1)) (i32.const 16)))
(set_local $x11 (i32.add (get_local $x11) (get_local $x12)))
(set_local $x6 (i32.rotl (i32.xor (get_local $x6) (get_local $x11)) (i32.const 12)))
(set_local $x1 (i32.add (get_local $x1) (get_local $x6)))
(set_local $x12 (i32.rotl (i32.xor (get_local $x12) (get_local $x1)) (i32.const 8)))
(set_local $x11 (i32.add (get_local $x11) (get_local $x12)))
(set_local $x6 (i32.rotl (i32.xor (get_local $x6) (get_local $x11)) (i32.const 7)))
;; QUARTERROUND(x2, x7, x8, x13);
(set_local $x2 (i32.add (get_local $x2) (get_local $x7)))
(set_local $x13 (i32.rotl (i32.xor (get_local $x13) (get_local $x2)) (i32.const 16)))
(set_local $x8 (i32.add (get_local $x8) (get_local $x13)))
(set_local $x7 (i32.rotl (i32.xor (get_local $x7) (get_local $x8)) (i32.const 12)))
(set_local $x2 (i32.add (get_local $x2) (get_local $x7)))
(set_local $x13 (i32.rotl (i32.xor (get_local $x13) (get_local $x2)) (i32.const 8)))
(set_local $x8 (i32.add (get_local $x8) (get_local $x13)))
(set_local $x7 (i32.rotl (i32.xor (get_local $x7) (get_local $x8)) (i32.const 7)))
;; QUARTERROUND(x3, x4, x9, x14);
(set_local $x3 (i32.add (get_local $x3) (get_local $x4)))
(set_local $x14 (i32.rotl (i32.xor (get_local $x14) (get_local $x3)) (i32.const 16)))
(set_local $x9 (i32.add (get_local $x9) (get_local $x14)))
(set_local $x4 (i32.rotl (i32.xor (get_local $x4) (get_local $x9)) (i32.const 12)))
(set_local $x3 (i32.add (get_local $x3) (get_local $x4)))
(set_local $x14 (i32.rotl (i32.xor (get_local $x14) (get_local $x3)) (i32.const 8)))
(set_local $x9 (i32.add (get_local $x9) (get_local $x14)))
(set_local $x4 (i32.rotl (i32.xor (get_local $x4) (get_local $x9)) (i32.const 7)))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
)
)
(i32.store (get_local $out_ptr) (get_local $x0))
(i32.store (i32.add (get_local $out_ptr) (i32.const 4)) (get_local $x1))
(i32.store (i32.add (get_local $out_ptr) (i32.const 8)) (get_local $x2))
(i32.store (i32.add (get_local $out_ptr) (i32.const 12)) (get_local $x3))
(i32.store (i32.add (get_local $out_ptr) (i32.const 16)) (get_local $x12))
(i32.store (i32.add (get_local $out_ptr) (i32.const 20)) (get_local $x13))
(i32.store (i32.add (get_local $out_ptr) (i32.const 24)) (get_local $x14))
(i32.store (i32.add (get_local $out_ptr) (i32.const 28)) (get_local $x15))
)
(func (export "core_hchacha20") (param $out_ptr i32) (param $in_ptr i32) (param $key_ptr i32)
(call $core_hchacha20 (get_local $out_ptr) (get_local $in_ptr) (get_local $key_ptr))
)
)