authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-08-31 18:45:45+12:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-08-31 18:45:45+12:00
log763845f95c33140859aa0297c9495d5f08f9afea
treead2fc6f4b8dcbb7f67d306209e2dfaf144fca8e0
parent38399941d41f8edba9c297a7c0e3da6deb44766c

std/crypto: zig fmt


2 files changed, 39 insertions(+), 109 deletions(-)

std/crypto/chacha20.zig+30-27
......@@ -32,24 +32,28 @@ fn salsa20_wordtobyte(out: []u8, input: [16]u32) void {
3232 x[i] = input[i];
3333
3434 const rounds = comptime []QuarterRound{
35 Rp( 0, 4, 8,12),
36 Rp( 1, 5, 9,13),
37 Rp( 2, 6,10,14),
38 Rp( 3, 7,11,15),
39 Rp( 0, 5,10,15),
40 Rp( 1, 6,11,12),
41 Rp( 2, 7, 8,13),
42 Rp( 3, 4, 9,14),
35 Rp(0, 4, 8, 12),
36 Rp(1, 5, 9, 13),
37 Rp(2, 6, 10, 14),
38 Rp(3, 7, 11, 15),
39 Rp(0, 5, 10, 15),
40 Rp(1, 6, 11, 12),
41 Rp(2, 7, 8, 13),
42 Rp(3, 4, 9, 14),
4343 };
4444
4545 comptime var j: usize = 0;
4646 inline while (j < 20) : (j += 2) {
4747 // two-round cycles
4848 inline for (rounds) |r| {
49 x[r.a] +%= x[r.b]; x[r.d] = std.math.rotl(u32, x[r.d] ^ x[r.a], u32(16));
50 x[r.c] +%= x[r.d]; x[r.b] = std.math.rotl(u32, x[r.b] ^ x[r.c], u32(12));
51 x[r.a] +%= x[r.b]; x[r.d] = std.math.rotl(u32, x[r.d] ^ x[r.a], u32(8));
52 x[r.c] +%= x[r.d]; x[r.b] = std.math.rotl(u32, x[r.b] ^ x[r.c], u32(7));
49 x[r.a] +%= x[r.b];
50 x[r.d] = std.math.rotl(u32, x[r.d] ^ x[r.a], u32(16));
51 x[r.c] +%= x[r.d];
52 x[r.b] = std.math.rotl(u32, x[r.b] ^ x[r.c], u32(12));
53 x[r.a] +%= x[r.b];
54 x[r.d] = std.math.rotl(u32, x[r.d] ^ x[r.a], u32(8));
55 x[r.c] +%= x[r.d];
56 x[r.b] = std.math.rotl(u32, x[r.b] ^ x[r.c], u32(7));
5357 }
5458 }
5559
......@@ -166,9 +170,8 @@ pub fn chaCha20With64BitNonce(out: []u8, in: []const u8, counter: u64, key: [32]
166170 var remaining_blocks: u32 = @intCast(u32, (in.len / big_block));
167171 var i: u32 = 0;
168172 while (remaining_blocks > 0) : (remaining_blocks -= 1) {
169 chaCha20_internal(out[cursor..cursor + big_block], in[cursor..cursor + big_block], k, c);
170 c[1] += 1; // upper 32-bit of counter, generic chaCha20_internal() doesn't
171 // know about this.
173 chaCha20_internal(out[cursor .. cursor + big_block], in[cursor .. cursor + big_block], k, c);
174 c[1] += 1; // upper 32-bit of counter, generic chaCha20_internal() doesn't know about this.
172175 cursor += big_block;
173176 }
174177 }
......@@ -199,16 +202,16 @@ test "crypto.chacha20 test vector sunscreen" {
199202 const input = "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it.";
200203 var result: [114]u8 = undefined;
201204 const key = []u8{
202 0, 1, 2, 3, 4, 5, 6, 7,
203 8, 9,10,11,12,13,14,15,
204 16,17,18,19,20,21,22,23,
205 24,25,26,27,28,29,30,31,
205 0, 1, 2, 3, 4, 5, 6, 7,
206 8, 9, 10, 11, 12, 13, 14, 15,
207 16, 17, 18, 19, 20, 21, 22, 23,
208 24, 25, 26, 27, 28, 29, 30, 31,
206209 };
207210 const nonce = []u8{
208 0, 0, 0, 0,
209 0, 0, 0, 0x4a,
210 0, 0, 0, 0,
211 };
211 0, 0, 0, 0,
212 0, 0, 0, 0x4a,
213 0, 0, 0, 0,
214 };
212215
213216 chaCha20IETF(result[0..], input[0..], 1, key, nonce);
214217 assert(mem.eql(u8, expected_result, result));
......@@ -248,7 +251,7 @@ test "crypto.chacha20 test vector 1" {
248251 0, 0, 0, 0, 0, 0, 0, 0,
249252 0, 0, 0, 0, 0, 0, 0, 0,
250253 };
251 const nonce = []u8{0, 0, 0, 0, 0, 0, 0, 0};
254 const nonce = []u8{ 0, 0, 0, 0, 0, 0, 0, 0 };
252255
253256 chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce);
254257 assert(mem.eql(u8, expected_result, result));
......@@ -282,7 +285,7 @@ test "crypto.chacha20 test vector 2" {
282285 0, 0, 0, 0, 0, 0, 0, 0,
283286 0, 0, 0, 0, 0, 0, 0, 1,
284287 };
285 const nonce = []u8{0, 0, 0, 0, 0, 0, 0, 0};
288 const nonce = []u8{ 0, 0, 0, 0, 0, 0, 0, 0 };
286289
287290 chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce);
288291 assert(mem.eql(u8, expected_result, result));
......@@ -316,7 +319,7 @@ test "crypto.chacha20 test vector 3" {
316319 0, 0, 0, 0, 0, 0, 0, 0,
317320 0, 0, 0, 0, 0, 0, 0, 0,
318321 };
319 const nonce = []u8{0, 0, 0, 0, 0, 0, 0, 1};
322 const nonce = []u8{ 0, 0, 0, 0, 0, 0, 0, 1 };
320323
321324 chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce);
322325 assert(mem.eql(u8, expected_result, result));
......@@ -350,7 +353,7 @@ test "crypto.chacha20 test vector 4" {
350353 0, 0, 0, 0, 0, 0, 0, 0,
351354 0, 0, 0, 0, 0, 0, 0, 0,
352355 };
353 const nonce = []u8{1, 0, 0, 0, 0, 0, 0, 0};
356 const nonce = []u8{ 1, 0, 0, 0, 0, 0, 0, 0 };
354357
355358 chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce);
356359 assert(mem.eql(u8, expected_result, result));
std/crypto/sha3.zig+9-82
......@@ -87,97 +87,24 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {
8787}
8888
8989const RC = []const u64{
90 0x0000000000000001,
91 0x0000000000008082,
92 0x800000000000808a,
93 0x8000000080008000,
94 0x000000000000808b,
95 0x0000000080000001,
96 0x8000000080008081,
97 0x8000000000008009,
98 0x000000000000008a,
99 0x0000000000000088,
100 0x0000000080008009,
101 0x000000008000000a,
102 0x000000008000808b,
103 0x800000000000008b,
104 0x8000000000008089,
105 0x8000000000008003,
106 0x8000000000008002,
107 0x8000000000000080,
108 0x000000000000800a,
109 0x800000008000000a,
110 0x8000000080008081,
111 0x8000000000008080,
112 0x0000000080000001,
113 0x8000000080008008,
90 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000,
91 0x000000000000808b, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009,
92 0x000000000000008a, 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
93 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 0x8000000000008003,
94 0x8000000000008002, 0x8000000000000080, 0x000000000000800a, 0x800000008000000a,
95 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008,
11496};
11597
11698const ROTC = []const usize{
117 1,
118 3,
119 6,
120 10,
121 15,
122 21,
123 28,
124 36,
125 45,
126 55,
127 2,
128 14,
129 27,
130 41,
131 56,
132 8,
133 25,
134 43,
135 62,
136 18,
137 39,
138 61,
139 20,
140 44,
99 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44,
141100};
142101
143102const PIL = []const usize{
144 10,
145 7,
146 11,
147 17,
148 18,
149 3,
150 5,
151 16,
152 8,
153 21,
154 24,
155 4,
156 15,
157 23,
158 19,
159 13,
160 12,
161 2,
162 20,
163 14,
164 22,
165 9,
166 6,
167 1,
103 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1,
168104};
169105
170106const M5 = []const usize{
171 0,
172 1,
173 2,
174 3,
175 4,
176 0,
177 1,
178 2,
179 3,
180 4,
107 0, 1, 2, 3, 4, 0, 1, 2, 3, 4,
181108};
182109
183110fn keccak_f(comptime F: usize, d: []u8) void {