authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-19 14:48:47-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-19 14:48:47-04:00
logf614d94faa3b2a259c3a82cd66f167029f20d224
tree1ecaaeda6c54d626ff3898d109c99d30152483fd
parent1d7861a36e1bcd8f7bfdb53716ef53467704922b
signaturelock-open Commit is signed but in an unrecognized format.

update std lib to take advantage of slicing with comptime indexes


13 files changed, 108 insertions(+), 119 deletions(-)

lib/std/crypto/aes.zig+19-19
...@@ -15,10 +15,10 @@ fn rotw(w: u32) u32 {...@@ -15,10 +15,10 @@ fn rotw(w: u32) u32 {
1515
16// Encrypt one block from src into dst, using the expanded key xk.16// Encrypt one block from src into dst, using the expanded key xk.
17fn encryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {17fn encryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {
18 var s0 = mem.readIntSliceBig(u32, src[0..4]);18 var s0 = mem.readIntBig(u32, src[0..4]);
19 var s1 = mem.readIntSliceBig(u32, src[4..8]);19 var s1 = mem.readIntBig(u32, src[4..8]);
20 var s2 = mem.readIntSliceBig(u32, src[8..12]);20 var s2 = mem.readIntBig(u32, src[8..12]);
21 var s3 = mem.readIntSliceBig(u32, src[12..16]);21 var s3 = mem.readIntBig(u32, src[12..16]);
2222
23 // First round just XORs input with key.23 // First round just XORs input with key.
24 s0 ^= xk[0];24 s0 ^= xk[0];
...@@ -58,18 +58,18 @@ fn encryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {...@@ -58,18 +58,18 @@ fn encryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {
58 s2 ^= xk[k + 2];58 s2 ^= xk[k + 2];
59 s3 ^= xk[k + 3];59 s3 ^= xk[k + 3];
6060
61 mem.writeIntSliceBig(u32, dst[0..4], s0);61 mem.writeIntBig(u32, dst[0..4], s0);
62 mem.writeIntSliceBig(u32, dst[4..8], s1);62 mem.writeIntBig(u32, dst[4..8], s1);
63 mem.writeIntSliceBig(u32, dst[8..12], s2);63 mem.writeIntBig(u32, dst[8..12], s2);
64 mem.writeIntSliceBig(u32, dst[12..16], s3);64 mem.writeIntBig(u32, dst[12..16], s3);
65}65}
6666
67// Decrypt one block from src into dst, using the expanded key xk.67// Decrypt one block from src into dst, using the expanded key xk.
68pub fn decryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {68pub fn decryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {
69 var s0 = mem.readIntSliceBig(u32, src[0..4]);69 var s0 = mem.readIntBig(u32, src[0..4]);
70 var s1 = mem.readIntSliceBig(u32, src[4..8]);70 var s1 = mem.readIntBig(u32, src[4..8]);
71 var s2 = mem.readIntSliceBig(u32, src[8..12]);71 var s2 = mem.readIntBig(u32, src[8..12]);
72 var s3 = mem.readIntSliceBig(u32, src[12..16]);72 var s3 = mem.readIntBig(u32, src[12..16]);
7373
74 // First round just XORs input with key.74 // First round just XORs input with key.
75 s0 ^= xk[0];75 s0 ^= xk[0];
...@@ -109,10 +109,10 @@ pub fn decryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {...@@ -109,10 +109,10 @@ pub fn decryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {
109 s2 ^= xk[k + 2];109 s2 ^= xk[k + 2];
110 s3 ^= xk[k + 3];110 s3 ^= xk[k + 3];
111111
112 mem.writeIntSliceBig(u32, dst[0..4], s0);112 mem.writeIntBig(u32, dst[0..4], s0);
113 mem.writeIntSliceBig(u32, dst[4..8], s1);113 mem.writeIntBig(u32, dst[4..8], s1);
114 mem.writeIntSliceBig(u32, dst[8..12], s2);114 mem.writeIntBig(u32, dst[8..12], s2);
115 mem.writeIntSliceBig(u32, dst[12..16], s3);115 mem.writeIntBig(u32, dst[12..16], s3);
116}116}
117117
118fn xorBytes(dst: []u8, a: []const u8, b: []const u8) usize {118fn xorBytes(dst: []u8, a: []const u8, b: []const u8) usize {
...@@ -154,8 +154,8 @@ fn AES(comptime keysize: usize) type {...@@ -154,8 +154,8 @@ fn AES(comptime keysize: usize) type {
154 var n: usize = 0;154 var n: usize = 0;
155 while (n < src.len) {155 while (n < src.len) {
156 ctx.encrypt(keystream[0..], ctrbuf[0..]);156 ctx.encrypt(keystream[0..], ctrbuf[0..]);
157 var ctr_i = std.mem.readIntSliceBig(u128, ctrbuf[0..]);157 var ctr_i = std.mem.readIntBig(u128, ctrbuf[0..]);
158 std.mem.writeIntSliceBig(u128, ctrbuf[0..], ctr_i +% 1);158 std.mem.writeIntBig(u128, ctrbuf[0..], ctr_i +% 1);
159159
160 n += xorBytes(dst[n..], src[n..], &keystream);160 n += xorBytes(dst[n..], src[n..], &keystream);
161 }161 }
...@@ -251,7 +251,7 @@ fn expandKey(key: []const u8, enc: []u32, dec: []u32) void {...@@ -251,7 +251,7 @@ fn expandKey(key: []const u8, enc: []u32, dec: []u32) void {
251 var i: usize = 0;251 var i: usize = 0;
252 var nk = key.len / 4;252 var nk = key.len / 4;
253 while (i < nk) : (i += 1) {253 while (i < nk) : (i += 1) {
254 enc[i] = mem.readIntSliceBig(u32, key[4 * i .. 4 * i + 4]);254 enc[i] = mem.readIntBig(u32, key[4 * i ..][0..4]);
255 }255 }
256 while (i < enc.len) : (i += 1) {256 while (i < enc.len) : (i += 1) {
257 var t = enc[i - 1];257 var t = enc[i - 1];
lib/std/crypto/blake2.zig+4-7
...@@ -123,8 +123,7 @@ fn Blake2s(comptime out_len: usize) type {...@@ -123,8 +123,7 @@ fn Blake2s(comptime out_len: usize) type {
123 const rr = d.h[0 .. out_len / 32];123 const rr = d.h[0 .. out_len / 32];
124124
125 for (rr) |s, j| {125 for (rr) |s, j| {
126 // TODO https://github.com/ziglang/zig/issues/863126 mem.writeIntLittle(u32, out[4 * j ..][0..4], s);
127 mem.writeIntSliceLittle(u32, out[4 * j .. 4 * j + 4], s);
128 }127 }
129 }128 }
130129
...@@ -135,8 +134,7 @@ fn Blake2s(comptime out_len: usize) type {...@@ -135,8 +134,7 @@ fn Blake2s(comptime out_len: usize) type {
135 var v: [16]u32 = undefined;134 var v: [16]u32 = undefined;
136135
137 for (m) |*r, i| {136 for (m) |*r, i| {
138 // TODO https://github.com/ziglang/zig/issues/863137 r.* = mem.readIntLittle(u32, b[4 * i ..][0..4]);
139 r.* = mem.readIntSliceLittle(u32, b[4 * i .. 4 * i + 4]);
140 }138 }
141139
142 var k: usize = 0;140 var k: usize = 0;
...@@ -358,8 +356,7 @@ fn Blake2b(comptime out_len: usize) type {...@@ -358,8 +356,7 @@ fn Blake2b(comptime out_len: usize) type {
358 const rr = d.h[0 .. out_len / 64];356 const rr = d.h[0 .. out_len / 64];
359357
360 for (rr) |s, j| {358 for (rr) |s, j| {
361 // TODO https://github.com/ziglang/zig/issues/863359 mem.writeIntLittle(u64, out[8 * j ..][0..8], s);
362 mem.writeIntSliceLittle(u64, out[8 * j .. 8 * j + 8], s);
363 }360 }
364 }361 }
365362
...@@ -370,7 +367,7 @@ fn Blake2b(comptime out_len: usize) type {...@@ -370,7 +367,7 @@ fn Blake2b(comptime out_len: usize) type {
370 var v: [16]u64 = undefined;367 var v: [16]u64 = undefined;
371368
372 for (m) |*r, i| {369 for (m) |*r, i| {
373 r.* = mem.readIntSliceLittle(u64, b[8 * i .. 8 * i + 8]);370 r.* = mem.readIntLittle(u64, b[8 * i ..][0..8]);
374 }371 }
375372
376 var k: usize = 0;373 var k: usize = 0;
lib/std/crypto/chacha20.zig+30-31
...@@ -61,8 +61,7 @@ fn salsa20_wordtobyte(out: []u8, input: [16]u32) void {...@@ -61,8 +61,7 @@ fn salsa20_wordtobyte(out: []u8, input: [16]u32) void {
61 }61 }
6262
63 for (x) |_, i| {63 for (x) |_, i| {
64 // TODO https://github.com/ziglang/zig/issues/86364 mem.writeIntLittle(u32, out[4 * i ..][0..4], x[i] +% input[i]);
65 mem.writeIntSliceLittle(u32, out[4 * i .. 4 * i + 4], x[i] +% input[i]);
66 }65 }
67}66}
6867
...@@ -73,10 +72,10 @@ fn chaCha20_internal(out: []u8, in: []const u8, key: [8]u32, counter: [4]u32) vo...@@ -73,10 +72,10 @@ fn chaCha20_internal(out: []u8, in: []const u8, key: [8]u32, counter: [4]u32) vo
7372
74 const c = "expand 32-byte k";73 const c = "expand 32-byte k";
75 const constant_le = [_]u32{74 const constant_le = [_]u32{
76 mem.readIntSliceLittle(u32, c[0..4]),75 mem.readIntLittle(u32, c[0..4]),
77 mem.readIntSliceLittle(u32, c[4..8]),76 mem.readIntLittle(u32, c[4..8]),
78 mem.readIntSliceLittle(u32, c[8..12]),77 mem.readIntLittle(u32, c[8..12]),
79 mem.readIntSliceLittle(u32, c[12..16]),78 mem.readIntLittle(u32, c[12..16]),
80 };79 };
8180
82 mem.copy(u32, ctx[0..], constant_le[0..4]);81 mem.copy(u32, ctx[0..], constant_le[0..4]);
...@@ -120,19 +119,19 @@ pub fn chaCha20IETF(out: []u8, in: []const u8, counter: u32, key: [32]u8, nonce:...@@ -120,19 +119,19 @@ pub fn chaCha20IETF(out: []u8, in: []const u8, counter: u32, key: [32]u8, nonce:
120 var k: [8]u32 = undefined;119 var k: [8]u32 = undefined;
121 var c: [4]u32 = undefined;120 var c: [4]u32 = undefined;
122121
123 k[0] = mem.readIntSliceLittle(u32, key[0..4]);122 k[0] = mem.readIntLittle(u32, key[0..4]);
124 k[1] = mem.readIntSliceLittle(u32, key[4..8]);123 k[1] = mem.readIntLittle(u32, key[4..8]);
125 k[2] = mem.readIntSliceLittle(u32, key[8..12]);124 k[2] = mem.readIntLittle(u32, key[8..12]);
126 k[3] = mem.readIntSliceLittle(u32, key[12..16]);125 k[3] = mem.readIntLittle(u32, key[12..16]);
127 k[4] = mem.readIntSliceLittle(u32, key[16..20]);126 k[4] = mem.readIntLittle(u32, key[16..20]);
128 k[5] = mem.readIntSliceLittle(u32, key[20..24]);127 k[5] = mem.readIntLittle(u32, key[20..24]);
129 k[6] = mem.readIntSliceLittle(u32, key[24..28]);128 k[6] = mem.readIntLittle(u32, key[24..28]);
130 k[7] = mem.readIntSliceLittle(u32, key[28..32]);129 k[7] = mem.readIntLittle(u32, key[28..32]);
131130
132 c[0] = counter;131 c[0] = counter;
133 c[1] = mem.readIntSliceLittle(u32, nonce[0..4]);132 c[1] = mem.readIntLittle(u32, nonce[0..4]);
134 c[2] = mem.readIntSliceLittle(u32, nonce[4..8]);133 c[2] = mem.readIntLittle(u32, nonce[4..8]);
135 c[3] = mem.readIntSliceLittle(u32, nonce[8..12]);134 c[3] = mem.readIntLittle(u32, nonce[8..12]);
136 chaCha20_internal(out, in, k, c);135 chaCha20_internal(out, in, k, c);
137}136}
138137
...@@ -147,19 +146,19 @@ pub fn chaCha20With64BitNonce(out: []u8, in: []const u8, counter: u64, key: [32]...@@ -147,19 +146,19 @@ pub fn chaCha20With64BitNonce(out: []u8, in: []const u8, counter: u64, key: [32]
147 var k: [8]u32 = undefined;146 var k: [8]u32 = undefined;
148 var c: [4]u32 = undefined;147 var c: [4]u32 = undefined;
149148
150 k[0] = mem.readIntSliceLittle(u32, key[0..4]);149 k[0] = mem.readIntLittle(u32, key[0..4]);
151 k[1] = mem.readIntSliceLittle(u32, key[4..8]);150 k[1] = mem.readIntLittle(u32, key[4..8]);
152 k[2] = mem.readIntSliceLittle(u32, key[8..12]);151 k[2] = mem.readIntLittle(u32, key[8..12]);
153 k[3] = mem.readIntSliceLittle(u32, key[12..16]);152 k[3] = mem.readIntLittle(u32, key[12..16]);
154 k[4] = mem.readIntSliceLittle(u32, key[16..20]);153 k[4] = mem.readIntLittle(u32, key[16..20]);
155 k[5] = mem.readIntSliceLittle(u32, key[20..24]);154 k[5] = mem.readIntLittle(u32, key[20..24]);
156 k[6] = mem.readIntSliceLittle(u32, key[24..28]);155 k[6] = mem.readIntLittle(u32, key[24..28]);
157 k[7] = mem.readIntSliceLittle(u32, key[28..32]);156 k[7] = mem.readIntLittle(u32, key[28..32]);
158157
159 c[0] = @truncate(u32, counter);158 c[0] = @truncate(u32, counter);
160 c[1] = @truncate(u32, counter >> 32);159 c[1] = @truncate(u32, counter >> 32);
161 c[2] = mem.readIntSliceLittle(u32, nonce[0..4]);160 c[2] = mem.readIntLittle(u32, nonce[0..4]);
162 c[3] = mem.readIntSliceLittle(u32, nonce[4..8]);161 c[3] = mem.readIntLittle(u32, nonce[4..8]);
163162
164 const block_size = (1 << 6);163 const block_size = (1 << 6);
165 // The full block size is greater than the address space on a 32bit machine164 // The full block size is greater than the address space on a 32bit machine
...@@ -463,8 +462,8 @@ pub fn chacha20poly1305Seal(dst: []u8, plaintext: []const u8, data: []const u8,...@@ -463,8 +462,8 @@ pub fn chacha20poly1305Seal(dst: []u8, plaintext: []const u8, data: []const u8,
463 mac.update(zeros[0..padding]);462 mac.update(zeros[0..padding]);
464 }463 }
465 var lens: [16]u8 = undefined;464 var lens: [16]u8 = undefined;
466 mem.writeIntSliceLittle(u64, lens[0..8], data.len);465 mem.writeIntLittle(u64, lens[0..8], data.len);
467 mem.writeIntSliceLittle(u64, lens[8..16], plaintext.len);466 mem.writeIntLittle(u64, lens[8..16], plaintext.len);
468 mac.update(lens[0..]);467 mac.update(lens[0..]);
469 mac.final(dst[plaintext.len..]);468 mac.final(dst[plaintext.len..]);
470}469}
...@@ -500,8 +499,8 @@ pub fn chacha20poly1305Open(dst: []u8, msgAndTag: []const u8, data: []const u8,...@@ -500,8 +499,8 @@ pub fn chacha20poly1305Open(dst: []u8, msgAndTag: []const u8, data: []const u8,
500 mac.update(zeros[0..padding]);499 mac.update(zeros[0..padding]);
501 }500 }
502 var lens: [16]u8 = undefined;501 var lens: [16]u8 = undefined;
503 mem.writeIntSliceLittle(u64, lens[0..8], data.len);502 mem.writeIntLittle(u64, lens[0..8], data.len);
504 mem.writeIntSliceLittle(u64, lens[8..16], ciphertext.len);503 mem.writeIntLittle(u64, lens[8..16], ciphertext.len);
505 mac.update(lens[0..]);504 mac.update(lens[0..]);
506 var computedTag: [16]u8 = undefined;505 var computedTag: [16]u8 = undefined;
507 mac.final(computedTag[0..]);506 mac.final(computedTag[0..]);
lib/std/crypto/md5.zig+1-2
...@@ -112,8 +112,7 @@ pub const Md5 = struct {...@@ -112,8 +112,7 @@ pub const Md5 = struct {
112 d.round(d.buf[0..]);112 d.round(d.buf[0..]);
113113
114 for (d.s) |s, j| {114 for (d.s) |s, j| {
115 // TODO https://github.com/ziglang/zig/issues/863115 mem.writeIntLittle(u32, out[4 * j ..][0..4], s);
116 mem.writeIntSliceLittle(u32, out[4 * j .. 4 * j + 4], s);
117 }116 }
118 }117 }
119118
lib/std/crypto/poly1305.zig+14-15
...@@ -3,11 +3,11 @@...@@ -3,11 +3,11 @@
3// https://monocypher.org/3// https://monocypher.org/
44
5const std = @import("../std.zig");5const std = @import("../std.zig");
6const builtin = @import("builtin");6const builtin = std.builtin;
77
8const Endian = builtin.Endian;8const Endian = builtin.Endian;
9const readIntSliceLittle = std.mem.readIntSliceLittle;9const readIntLittle = std.mem.readIntLittle;
10const writeIntSliceLittle = std.mem.writeIntSliceLittle;10const writeIntLittle = std.mem.writeIntLittle;
1111
12pub const Poly1305 = struct {12pub const Poly1305 = struct {
13 const Self = @This();13 const Self = @This();
...@@ -59,19 +59,19 @@ pub const Poly1305 = struct {...@@ -59,19 +59,19 @@ pub const Poly1305 = struct {
59 {59 {
60 var i: usize = 0;60 var i: usize = 0;
61 while (i < 1) : (i += 1) {61 while (i < 1) : (i += 1) {
62 ctx.r[0] = readIntSliceLittle(u32, key[0..4]) & 0x0fffffff;62 ctx.r[0] = readIntLittle(u32, key[0..4]) & 0x0fffffff;
63 }63 }
64 }64 }
65 {65 {
66 var i: usize = 1;66 var i: usize = 1;
67 while (i < 4) : (i += 1) {67 while (i < 4) : (i += 1) {
68 ctx.r[i] = readIntSliceLittle(u32, key[i * 4 .. i * 4 + 4]) & 0x0ffffffc;68 ctx.r[i] = readIntLittle(u32, key[i * 4 ..][0..4]) & 0x0ffffffc;
69 }69 }
70 }70 }
71 {71 {
72 var i: usize = 0;72 var i: usize = 0;
73 while (i < 4) : (i += 1) {73 while (i < 4) : (i += 1) {
74 ctx.pad[i] = readIntSliceLittle(u32, key[i * 4 + 16 .. i * 4 + 16 + 4]);74 ctx.pad[i] = readIntLittle(u32, key[i * 4 + 16 ..][0..4]);
75 }75 }
76 }76 }
7777
...@@ -168,10 +168,10 @@ pub const Poly1305 = struct {...@@ -168,10 +168,10 @@ pub const Poly1305 = struct {
168 const nb_blocks = nmsg.len >> 4;168 const nb_blocks = nmsg.len >> 4;
169 var i: usize = 0;169 var i: usize = 0;
170 while (i < nb_blocks) : (i += 1) {170 while (i < nb_blocks) : (i += 1) {
171 ctx.c[0] = readIntSliceLittle(u32, nmsg[0..4]);171 ctx.c[0] = readIntLittle(u32, nmsg[0..4]);
172 ctx.c[1] = readIntSliceLittle(u32, nmsg[4..8]);172 ctx.c[1] = readIntLittle(u32, nmsg[4..8]);
173 ctx.c[2] = readIntSliceLittle(u32, nmsg[8..12]);173 ctx.c[2] = readIntLittle(u32, nmsg[8..12]);
174 ctx.c[3] = readIntSliceLittle(u32, nmsg[12..16]);174 ctx.c[3] = readIntLittle(u32, nmsg[12..16]);
175 polyBlock(ctx);175 polyBlock(ctx);
176 nmsg = nmsg[16..];176 nmsg = nmsg[16..];
177 }177 }
...@@ -210,11 +210,10 @@ pub const Poly1305 = struct {...@@ -210,11 +210,10 @@ pub const Poly1305 = struct {
210 const uu2 = (uu1 >> 32) + ctx.h[2] + ctx.pad[2]; // <= 2_00000000210 const uu2 = (uu1 >> 32) + ctx.h[2] + ctx.pad[2]; // <= 2_00000000
211 const uu3 = (uu2 >> 32) + ctx.h[3] + ctx.pad[3]; // <= 2_00000000211 const uu3 = (uu2 >> 32) + ctx.h[3] + ctx.pad[3]; // <= 2_00000000
212212
213 // TODO https://github.com/ziglang/zig/issues/863213 writeIntLittle(u32, out[0..4], @truncate(u32, uu0));
214 writeIntSliceLittle(u32, out[0..], @truncate(u32, uu0));214 writeIntLittle(u32, out[4..8], @truncate(u32, uu1));
215 writeIntSliceLittle(u32, out[4..], @truncate(u32, uu1));215 writeIntLittle(u32, out[8..12], @truncate(u32, uu2));
216 writeIntSliceLittle(u32, out[8..], @truncate(u32, uu2));216 writeIntLittle(u32, out[12..16], @truncate(u32, uu3));
217 writeIntSliceLittle(u32, out[12..], @truncate(u32, uu3));
218217
219 ctx.secureZero();218 ctx.secureZero();
220 }219 }
lib/std/crypto/sha1.zig+1-2
...@@ -109,8 +109,7 @@ pub const Sha1 = struct {...@@ -109,8 +109,7 @@ pub const Sha1 = struct {
109 d.round(d.buf[0..]);109 d.round(d.buf[0..]);
110110
111 for (d.s) |s, j| {111 for (d.s) |s, j| {
112 // TODO https://github.com/ziglang/zig/issues/863112 mem.writeIntBig(u32, out[4 * j ..][0..4], s);
113 mem.writeIntSliceBig(u32, out[4 * j .. 4 * j + 4], s);
114 }113 }
115 }114 }
116115
lib/std/crypto/sha3.zig+2-3
...@@ -120,7 +120,7 @@ fn keccak_f(comptime F: usize, d: []u8) void {...@@ -120,7 +120,7 @@ fn keccak_f(comptime F: usize, d: []u8) void {
120 var c = [_]u64{0} ** 5;120 var c = [_]u64{0} ** 5;
121121
122 for (s) |*r, i| {122 for (s) |*r, i| {
123 r.* = mem.readIntSliceLittle(u64, d[8 * i .. 8 * i + 8]);123 r.* = mem.readIntLittle(u64, d[8 * i ..][0..8]);
124 }124 }
125125
126 comptime var x: usize = 0;126 comptime var x: usize = 0;
...@@ -167,8 +167,7 @@ fn keccak_f(comptime F: usize, d: []u8) void {...@@ -167,8 +167,7 @@ fn keccak_f(comptime F: usize, d: []u8) void {
167 }167 }
168168
169 for (s) |r, i| {169 for (s) |r, i| {
170 // TODO https://github.com/ziglang/zig/issues/863170 mem.writeIntLittle(u64, d[8 * i ..][0..8], r);
171 mem.writeIntSliceLittle(u64, d[8 * i .. 8 * i + 8], r);
172 }171 }
173}172}
174173
lib/std/crypto/x25519.zig+20-21
...@@ -7,8 +7,8 @@ const builtin = @import("builtin");...@@ -7,8 +7,8 @@ const builtin = @import("builtin");
7const fmt = std.fmt;7const fmt = std.fmt;
88
9const Endian = builtin.Endian;9const Endian = builtin.Endian;
10const readIntSliceLittle = std.mem.readIntSliceLittle;10const readIntLittle = std.mem.readIntLittle;
11const writeIntSliceLittle = std.mem.writeIntSliceLittle;11const writeIntLittle = std.mem.writeIntLittle;
1212
13// Based on Supercop's ref10 implementation.13// Based on Supercop's ref10 implementation.
14pub const X25519 = struct {14pub const X25519 = struct {
...@@ -255,16 +255,16 @@ const Fe = struct {...@@ -255,16 +255,16 @@ const Fe = struct {
255255
256 var t: [10]i64 = undefined;256 var t: [10]i64 = undefined;
257257
258 t[0] = readIntSliceLittle(u32, s[0..4]);258 t[0] = readIntLittle(u32, s[0..4]);
259 t[1] = @as(u32, readIntSliceLittle(u24, s[4..7])) << 6;259 t[1] = @as(u32, readIntLittle(u24, s[4..7])) << 6;
260 t[2] = @as(u32, readIntSliceLittle(u24, s[7..10])) << 5;260 t[2] = @as(u32, readIntLittle(u24, s[7..10])) << 5;
261 t[3] = @as(u32, readIntSliceLittle(u24, s[10..13])) << 3;261 t[3] = @as(u32, readIntLittle(u24, s[10..13])) << 3;
262 t[4] = @as(u32, readIntSliceLittle(u24, s[13..16])) << 2;262 t[4] = @as(u32, readIntLittle(u24, s[13..16])) << 2;
263 t[5] = readIntSliceLittle(u32, s[16..20]);263 t[5] = readIntLittle(u32, s[16..20]);
264 t[6] = @as(u32, readIntSliceLittle(u24, s[20..23])) << 7;264 t[6] = @as(u32, readIntLittle(u24, s[20..23])) << 7;
265 t[7] = @as(u32, readIntSliceLittle(u24, s[23..26])) << 5;265 t[7] = @as(u32, readIntLittle(u24, s[23..26])) << 5;
266 t[8] = @as(u32, readIntSliceLittle(u24, s[26..29])) << 4;266 t[8] = @as(u32, readIntLittle(u24, s[26..29])) << 4;
267 t[9] = (@as(u32, readIntSliceLittle(u24, s[29..32])) & 0x7fffff) << 2;267 t[9] = (@as(u32, readIntLittle(u24, s[29..32])) & 0x7fffff) << 2;
268268
269 carry1(h, t[0..]);269 carry1(h, t[0..]);
270 }270 }
...@@ -544,15 +544,14 @@ const Fe = struct {...@@ -544,15 +544,14 @@ const Fe = struct {
544 ut[i] = @bitCast(u32, @intCast(i32, t[i]));544 ut[i] = @bitCast(u32, @intCast(i32, t[i]));
545 }545 }
546546
547 // TODO https://github.com/ziglang/zig/issues/863547 writeIntLittle(u32, s[0..4], (ut[0] >> 0) | (ut[1] << 26));
548 writeIntSliceLittle(u32, s[0..4], (ut[0] >> 0) | (ut[1] << 26));548 writeIntLittle(u32, s[4..8], (ut[1] >> 6) | (ut[2] << 19));
549 writeIntSliceLittle(u32, s[4..8], (ut[1] >> 6) | (ut[2] << 19));549 writeIntLittle(u32, s[8..12], (ut[2] >> 13) | (ut[3] << 13));
550 writeIntSliceLittle(u32, s[8..12], (ut[2] >> 13) | (ut[3] << 13));550 writeIntLittle(u32, s[12..16], (ut[3] >> 19) | (ut[4] << 6));
551 writeIntSliceLittle(u32, s[12..16], (ut[3] >> 19) | (ut[4] << 6));551 writeIntLittle(u32, s[16..20], (ut[5] >> 0) | (ut[6] << 25));
552 writeIntSliceLittle(u32, s[16..20], (ut[5] >> 0) | (ut[6] << 25));552 writeIntLittle(u32, s[20..24], (ut[6] >> 7) | (ut[7] << 19));
553 writeIntSliceLittle(u32, s[20..24], (ut[6] >> 7) | (ut[7] << 19));553 writeIntLittle(u32, s[24..28], (ut[7] >> 13) | (ut[8] << 12));
554 writeIntSliceLittle(u32, s[24..28], (ut[7] >> 13) | (ut[8] << 12));554 writeIntLittle(u32, s[28..32], (ut[8] >> 20) | (ut[9] << 6));
555 writeIntSliceLittle(u32, s[28..], (ut[8] >> 20) | (ut[9] << 6));
556555
557 std.mem.secureZero(i64, t[0..]);556 std.mem.secureZero(i64, t[0..]);
558 }557 }
lib/std/hash/siphash.zig+3-3
...@@ -39,8 +39,8 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round...@@ -39,8 +39,8 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
39 pub fn init(key: []const u8) Self {39 pub fn init(key: []const u8) Self {
40 assert(key.len >= 16);40 assert(key.len >= 16);
4141
42 const k0 = mem.readIntSliceLittle(u64, key[0..8]);42 const k0 = mem.readIntLittle(u64, key[0..8]);
43 const k1 = mem.readIntSliceLittle(u64, key[8..16]);43 const k1 = mem.readIntLittle(u64, key[8..16]);
4444
45 var d = Self{45 var d = Self{
46 .v0 = k0 ^ 0x736f6d6570736575,46 .v0 = k0 ^ 0x736f6d6570736575,
...@@ -111,7 +111,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round...@@ -111,7 +111,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
111 fn round(self: *Self, b: []const u8) void {111 fn round(self: *Self, b: []const u8) void {
112 assert(b.len == 8);112 assert(b.len == 8);
113113
114 const m = mem.readIntSliceLittle(u64, b[0..]);114 const m = mem.readIntLittle(u64, b[0..8]);
115 self.v3 ^= m;115 self.v3 ^= m;
116116
117 // TODO this is a workaround, should be able to supply the value without a separate variable117 // TODO this is a workaround, should be able to supply the value without a separate variable
lib/std/hash/wyhash.zig+1-1
...@@ -11,7 +11,7 @@ const primes = [_]u64{...@@ -11,7 +11,7 @@ const primes = [_]u64{
1111
12fn read_bytes(comptime bytes: u8, data: []const u8) u64 {12fn read_bytes(comptime bytes: u8, data: []const u8) u64 {
13 const T = std.meta.IntType(false, 8 * bytes);13 const T = std.meta.IntType(false, 8 * bytes);
14 return mem.readIntSliceLittle(T, data[0..bytes]);14 return mem.readIntLittle(T, data[0..bytes]);
15}15}
1616
17fn read_8bytes_swapped(data: []const u8) u64 {17fn read_8bytes_swapped(data: []const u8) u64 {
lib/std/mem.zig+2-4
...@@ -824,8 +824,7 @@ pub const readIntBig = switch (builtin.endian) {...@@ -824,8 +824,7 @@ pub const readIntBig = switch (builtin.endian) {
824pub fn readIntSliceNative(comptime T: type, bytes: []const u8) T {824pub fn readIntSliceNative(comptime T: type, bytes: []const u8) T {
825 const n = @divExact(T.bit_count, 8);825 const n = @divExact(T.bit_count, 8);
826 assert(bytes.len >= n);826 assert(bytes.len >= n);
827 // TODO https://github.com/ziglang/zig/issues/863827 return readIntNative(T, bytes[0..n]);
828 return readIntNative(T, @ptrCast(*const [n]u8, bytes.ptr));
829}828}
830829
831/// Asserts that bytes.len >= T.bit_count / 8. Reads the integer starting from index 0830/// Asserts that bytes.len >= T.bit_count / 8. Reads the integer starting from index 0
...@@ -863,8 +862,7 @@ pub fn readInt(comptime T: type, bytes: *const [@divExact(T.bit_count, 8)]u8, en...@@ -863,8 +862,7 @@ pub fn readInt(comptime T: type, bytes: *const [@divExact(T.bit_count, 8)]u8, en
863pub fn readIntSlice(comptime T: type, bytes: []const u8, endian: builtin.Endian) T {862pub fn readIntSlice(comptime T: type, bytes: []const u8, endian: builtin.Endian) T {
864 const n = @divExact(T.bit_count, 8);863 const n = @divExact(T.bit_count, 8);
865 assert(bytes.len >= n);864 assert(bytes.len >= n);
866 // TODO https://github.com/ziglang/zig/issues/863865 return readInt(T, bytes[0..n], endian);
867 return readInt(T, @ptrCast(*const [n]u8, bytes.ptr), endian);
868}866}
869867
870test "comptime read/write int" {868test "comptime read/write int" {
lib/std/rand.zig+1-1
...@@ -5,7 +5,7 @@...@@ -5,7 +5,7 @@
5// ```5// ```
6// var buf: [8]u8 = undefined;6// var buf: [8]u8 = undefined;
7// try std.crypto.randomBytes(buf[0..]);7// try std.crypto.randomBytes(buf[0..]);
8// const seed = mem.readIntSliceLittle(u64, buf[0..8]);8// const seed = mem.readIntLittle(u64, buf[0..8]);
9//9//
10// var r = DefaultPrng.init(seed);10// var r = DefaultPrng.init(seed);
11//11//
lib/std/unicode.zig+10-10
...@@ -251,12 +251,12 @@ pub const Utf16LeIterator = struct {...@@ -251,12 +251,12 @@ pub const Utf16LeIterator = struct {
251 pub fn nextCodepoint(it: *Utf16LeIterator) !?u21 {251 pub fn nextCodepoint(it: *Utf16LeIterator) !?u21 {
252 assert(it.i <= it.bytes.len);252 assert(it.i <= it.bytes.len);
253 if (it.i == it.bytes.len) return null;253 if (it.i == it.bytes.len) return null;
254 const c0: u21 = mem.readIntSliceLittle(u16, it.bytes[it.i .. it.i + 2]);254 const c0: u21 = mem.readIntLittle(u16, it.bytes[it.i..][0..2]);
255 if (c0 & ~@as(u21, 0x03ff) == 0xd800) {255 if (c0 & ~@as(u21, 0x03ff) == 0xd800) {
256 // surrogate pair256 // surrogate pair
257 it.i += 2;257 it.i += 2;
258 if (it.i >= it.bytes.len) return error.DanglingSurrogateHalf;258 if (it.i >= it.bytes.len) return error.DanglingSurrogateHalf;
259 const c1: u21 = mem.readIntSliceLittle(u16, it.bytes[it.i .. it.i + 2]);259 const c1: u21 = mem.readIntLittle(u16, it.bytes[it.i..][0..2]);
260 if (c1 & ~@as(u21, 0x03ff) != 0xdc00) return error.ExpectedSecondSurrogateHalf;260 if (c1 & ~@as(u21, 0x03ff) != 0xdc00) return error.ExpectedSecondSurrogateHalf;
261 it.i += 2;261 it.i += 2;
262 return 0x10000 + (((c0 & 0x03ff) << 10) | (c1 & 0x03ff));262 return 0x10000 + (((c0 & 0x03ff) << 10) | (c1 & 0x03ff));
...@@ -630,11 +630,11 @@ test "utf8ToUtf16LeWithNull" {...@@ -630,11 +630,11 @@ test "utf8ToUtf16LeWithNull" {
630 }630 }
631}631}
632632
633/// Converts a UTF-8 string literal into a UTF-16LE string literal. 633/// Converts a UTF-8 string literal into a UTF-16LE string literal.
634pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16LeLen(utf8) :0] u16 {634pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16LeLen(utf8):0]u16 {
635 comptime {635 comptime {
636 const len: usize = calcUtf16LeLen(utf8);636 const len: usize = calcUtf16LeLen(utf8);
637 var utf16le: [len :0]u16 = [_ :0]u16{0} ** len;637 var utf16le: [len:0]u16 = [_:0]u16{0} ** len;
638 const utf16le_len = utf8ToUtf16Le(&utf16le, utf8[0..]) catch |err| @compileError(err);638 const utf16le_len = utf8ToUtf16Le(&utf16le, utf8[0..]) catch |err| @compileError(err);
639 assert(len == utf16le_len);639 assert(len == utf16le_len);
640 return &utf16le;640 return &utf16le;
...@@ -660,8 +660,8 @@ fn calcUtf16LeLen(utf8: []const u8) usize {...@@ -660,8 +660,8 @@ fn calcUtf16LeLen(utf8: []const u8) usize {
660}660}
661661
662test "utf8ToUtf16LeStringLiteral" {662test "utf8ToUtf16LeStringLiteral" {
663{663 {
664 const bytes = [_:0]u16{ 0x41 };664 const bytes = [_:0]u16{0x41};
665 const utf16 = utf8ToUtf16LeStringLiteral("A");665 const utf16 = utf8ToUtf16LeStringLiteral("A");
666 testing.expectEqualSlices(u16, &bytes, utf16);666 testing.expectEqualSlices(u16, &bytes, utf16);
667 testing.expect(utf16[1] == 0);667 testing.expect(utf16[1] == 0);
...@@ -673,19 +673,19 @@ test "utf8ToUtf16LeStringLiteral" {...@@ -673,19 +673,19 @@ test "utf8ToUtf16LeStringLiteral" {
673 testing.expect(utf16[2] == 0);673 testing.expect(utf16[2] == 0);
674 }674 }
675 {675 {
676 const bytes = [_:0]u16{ 0x02FF };676 const bytes = [_:0]u16{0x02FF};
677 const utf16 = utf8ToUtf16LeStringLiteral("\u{02FF}");677 const utf16 = utf8ToUtf16LeStringLiteral("\u{02FF}");
678 testing.expectEqualSlices(u16, &bytes, utf16);678 testing.expectEqualSlices(u16, &bytes, utf16);
679 testing.expect(utf16[1] == 0);679 testing.expect(utf16[1] == 0);
680 }680 }
681 {681 {
682 const bytes = [_:0]u16{ 0x7FF };682 const bytes = [_:0]u16{0x7FF};
683 const utf16 = utf8ToUtf16LeStringLiteral("\u{7FF}");683 const utf16 = utf8ToUtf16LeStringLiteral("\u{7FF}");
684 testing.expectEqualSlices(u16, &bytes, utf16);684 testing.expectEqualSlices(u16, &bytes, utf16);
685 testing.expect(utf16[1] == 0);685 testing.expect(utf16[1] == 0);
686 }686 }
687 {687 {
688 const bytes = [_:0]u16{ 0x801 };688 const bytes = [_:0]u16{0x801};
689 const utf16 = utf8ToUtf16LeStringLiteral("\u{801}");689 const utf16 = utf8ToUtf16LeStringLiteral("\u{801}");
690 testing.expectEqualSlices(u16, &bytes, utf16);690 testing.expectEqualSlices(u16, &bytes, utf16);
691 testing.expect(utf16[1] == 0);691 testing.expect(utf16[1] == 0);