diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-06-24 16:58:19 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-24 16:58:19 -0700 |
| commit | 146b79af153bbd5dafda0ba12a040385c7fc58f8 (patch) | |
| tree | 67e3db8b444d65c667e314770fc983a7fc8ba293 /lib/std/crypto | |
| parent | 13853bef0df3c90633021850cc6d6abaeea03282 (diff) | |
| parent | 21ac0beb436f49fe49c6982a872f2dc48e4bea5e (diff) | |
| download | zig-146b79af153bbd5dafda0ba12a040385c7fc58f8.tar.gz zig-146b79af153bbd5dafda0ba12a040385c7fc58f8.zip | |
Merge pull request #16163 from mlugg/feat/builtins-infer-dest-ty
Infer destination type of cast builtins using result type
Diffstat (limited to 'lib/std/crypto')
46 files changed, 697 insertions, 697 deletions
diff --git a/lib/std/crypto/25519/curve25519.zig b/lib/std/crypto/25519/curve25519.zig index f5938dd218..7c3343ba8c 100644 --- a/lib/std/crypto/25519/curve25519.zig +++ b/lib/std/crypto/25519/curve25519.zig @@ -54,7 +54,7 @@ pub const Curve25519 = struct { var swap: u8 = 0; var pos: usize = bits - 1; while (true) : (pos -= 1) { - const bit = (s[pos >> 3] >> @truncate(u3, pos)) & 1; + const bit = (s[pos >> 3] >> @as(u3, @truncate(pos))) & 1; swap ^= bit; Fe.cSwap2(&x2, &x3, &z2, &z3, swap); swap = bit; diff --git a/lib/std/crypto/25519/edwards25519.zig b/lib/std/crypto/25519/edwards25519.zig index 50f34c45f3..bf0c62f9de 100644 --- a/lib/std/crypto/25519/edwards25519.zig +++ b/lib/std/crypto/25519/edwards25519.zig @@ -162,8 +162,8 @@ pub const Edwards25519 = struct { const reduced = if ((s[s.len - 1] & 0x80) == 0) s else scalar.reduce(s); var e: [2 * 32]i8 = undefined; for (reduced, 0..) |x, i| { - e[i * 2 + 0] = @as(i8, @truncate(u4, x)); - e[i * 2 + 1] = @as(i8, @truncate(u4, x >> 4)); + e[i * 2 + 0] = @as(i8, @as(u4, @truncate(x))); + e[i * 2 + 1] = @as(i8, @as(u4, @truncate(x >> 4))); } // Now, e[0..63] is between 0 and 15, e[63] is between 0 and 7 var carry: i8 = 0; @@ -190,9 +190,9 @@ pub const Edwards25519 = struct { while (true) : (pos -= 1) { const slot = e[pos]; if (slot > 0) { - q = q.add(pc[@intCast(usize, slot)]); + q = q.add(pc[@as(usize, @intCast(slot))]); } else if (slot < 0) { - q = q.sub(pc[@intCast(usize, -slot)]); + q = q.sub(pc[@as(usize, @intCast(-slot))]); } if (pos == 0) break; q = q.dbl().dbl().dbl().dbl(); @@ -206,7 +206,7 @@ pub const Edwards25519 = struct { var q = Edwards25519.identityElement; var pos: usize = 252; while (true) : (pos -= 4) { - const slot = @truncate(u4, (s[pos >> 3] >> @truncate(u3, pos))); + const slot = @as(u4, @truncate((s[pos >> 3] >> @as(u3, @truncate(pos))))); if (vartime) { if (slot != 0) { q = q.add(pc[slot]); @@ -283,15 +283,15 @@ pub const Edwards25519 = struct { while (true) : (pos -= 1) { const slot1 = e1[pos]; if (slot1 > 0) { - q = q.add(pc1[@intCast(usize, slot1)]); + q = q.add(pc1[@as(usize, @intCast(slot1))]); } else if (slot1 < 0) { - q = q.sub(pc1[@intCast(usize, -slot1)]); + q = q.sub(pc1[@as(usize, @intCast(-slot1))]); } const slot2 = e2[pos]; if (slot2 > 0) { - q = q.add(pc2[@intCast(usize, slot2)]); + q = q.add(pc2[@as(usize, @intCast(slot2))]); } else if (slot2 < 0) { - q = q.sub(pc2[@intCast(usize, -slot2)]); + q = q.sub(pc2[@as(usize, @intCast(-slot2))]); } if (pos == 0) break; q = q.dbl().dbl().dbl().dbl(); @@ -326,9 +326,9 @@ pub const Edwards25519 = struct { for (es, 0..) |e, i| { const slot = e[pos]; if (slot > 0) { - q = q.add(pcs[i][@intCast(usize, slot)]); + q = q.add(pcs[i][@as(usize, @intCast(slot))]); } else if (slot < 0) { - q = q.sub(pcs[i][@intCast(usize, -slot)]); + q = q.sub(pcs[i][@as(usize, @intCast(-slot))]); } } if (pos == 0) break; @@ -427,7 +427,7 @@ pub const Edwards25519 = struct { } const empty_block = [_]u8{0} ** H.block_length; var t = [3]u8{ 0, n * h_l, 0 }; - var xctx_len_u8 = [1]u8{@intCast(u8, xctx.len)}; + var xctx_len_u8 = [1]u8{@as(u8, @intCast(xctx.len))}; var st = H.init(.{}); st.update(empty_block[0..]); st.update(s); diff --git a/lib/std/crypto/25519/field.zig b/lib/std/crypto/25519/field.zig index eec83f3d2e..627df9d4cb 100644 --- a/lib/std/crypto/25519/field.zig +++ b/lib/std/crypto/25519/field.zig @@ -254,11 +254,11 @@ pub const Fe = struct { var rs: [5]u64 = undefined; comptime var i = 0; inline while (i < 4) : (i += 1) { - rs[i] = @truncate(u64, r[i]) & MASK51; - r[i + 1] += @intCast(u64, r[i] >> 51); + rs[i] = @as(u64, @truncate(r[i])) & MASK51; + r[i + 1] += @as(u64, @intCast(r[i] >> 51)); } - rs[4] = @truncate(u64, r[4]) & MASK51; - var carry = @intCast(u64, r[4] >> 51); + rs[4] = @as(u64, @truncate(r[4])) & MASK51; + var carry = @as(u64, @intCast(r[4] >> 51)); rs[0] += 19 * carry; carry = rs[0] >> 51; rs[0] &= MASK51; @@ -278,8 +278,8 @@ pub const Fe = struct { var r: [5]u128 = undefined; comptime var i = 0; inline while (i < 5) : (i += 1) { - ax[i] = @intCast(u128, a.limbs[i]); - bx[i] = @intCast(u128, b.limbs[i]); + ax[i] = @as(u128, @intCast(a.limbs[i])); + bx[i] = @as(u128, @intCast(b.limbs[i])); } i = 1; inline while (i < 5) : (i += 1) { @@ -299,7 +299,7 @@ pub const Fe = struct { var r: [5]u128 = undefined; comptime var i = 0; inline while (i < 5) : (i += 1) { - ax[i] = @intCast(u128, a.limbs[i]); + ax[i] = @as(u128, @intCast(a.limbs[i])); } const a0_2 = 2 * ax[0]; const a1_2 = 2 * ax[1]; @@ -334,15 +334,15 @@ pub const Fe = struct { /// Multiply a field element with a small (32-bit) integer pub inline fn mul32(a: Fe, comptime n: u32) Fe { - const sn = @intCast(u128, n); + const sn = @as(u128, @intCast(n)); var fe: Fe = undefined; var x: u128 = 0; comptime var i = 0; inline while (i < 5) : (i += 1) { x = a.limbs[i] * sn + (x >> 51); - fe.limbs[i] = @truncate(u64, x) & MASK51; + fe.limbs[i] = @as(u64, @truncate(x)) & MASK51; } - fe.limbs[0] += @intCast(u64, x >> 51) * 19; + fe.limbs[0] += @as(u64, @intCast(x >> 51)) * 19; return fe; } @@ -402,7 +402,7 @@ pub const Fe = struct { const t2 = t.sqn(30).mul(t); const t3 = t2.sqn(60).mul(t2); const t4 = t3.sqn(120).mul(t3).sqn(10).mul(u).sqn(3).mul(_11).sq(); - return @bitCast(bool, @truncate(u1, ~(t4.toBytes()[1] & 1))); + return @as(bool, @bitCast(@as(u1, @truncate(~(t4.toBytes()[1] & 1))))); } fn uncheckedSqrt(x2: Fe) Fe { diff --git a/lib/std/crypto/25519/scalar.zig b/lib/std/crypto/25519/scalar.zig index fd6d42aebe..1699c68e12 100644 --- a/lib/std/crypto/25519/scalar.zig +++ b/lib/std/crypto/25519/scalar.zig @@ -27,8 +27,8 @@ pub fn rejectNonCanonical(s: CompressedScalar) NonCanonicalError!void { while (true) : (i -= 1) { const xs = @as(u16, s[i]); const xfield_order_s = @as(u16, field_order_s[i]); - c |= @intCast(u8, ((xs -% xfield_order_s) >> 8) & n); - n &= @intCast(u8, ((xs ^ xfield_order_s) -% 1) >> 8); + c |= @as(u8, @intCast(((xs -% xfield_order_s) >> 8) & n)); + n &= @as(u8, @intCast(((xs ^ xfield_order_s) -% 1) >> 8)); if (i == 0) break; } if (c == 0) { @@ -89,7 +89,7 @@ pub fn neg(s: CompressedScalar) CompressedScalar { var i: usize = 0; while (i < 64) : (i += 1) { carry = @as(u32, fs[i]) -% sx[i] -% @as(u32, carry); - sx[i] = @truncate(u8, carry); + sx[i] = @as(u8, @truncate(carry)); carry = (carry >> 8) & 1; } return reduce64(sx); @@ -129,7 +129,7 @@ pub const Scalar = struct { while (i < 4) : (i += 1) { mem.writeIntLittle(u64, bytes[i * 7 ..][0..8], expanded.limbs[i]); } - mem.writeIntLittle(u32, bytes[i * 7 ..][0..4], @intCast(u32, expanded.limbs[i])); + mem.writeIntLittle(u32, bytes[i * 7 ..][0..4], @as(u32, @intCast(expanded.limbs[i]))); return bytes; } @@ -234,42 +234,42 @@ pub const Scalar = struct { const z80 = xy440; const carry0 = z00 >> 56; - const t10 = @truncate(u64, z00) & 0xffffffffffffff; + const t10 = @as(u64, @truncate(z00)) & 0xffffffffffffff; const c00 = carry0; const t00 = t10; const carry1 = (z10 + c00) >> 56; - const t11 = @truncate(u64, (z10 + c00)) & 0xffffffffffffff; + const t11 = @as(u64, @truncate((z10 + c00))) & 0xffffffffffffff; const c10 = carry1; const t12 = t11; const carry2 = (z20 + c10) >> 56; - const t13 = @truncate(u64, (z20 + c10)) & 0xffffffffffffff; + const t13 = @as(u64, @truncate((z20 + c10))) & 0xffffffffffffff; const c20 = carry2; const t20 = t13; const carry3 = (z30 + c20) >> 56; - const t14 = @truncate(u64, (z30 + c20)) & 0xffffffffffffff; + const t14 = @as(u64, @truncate((z30 + c20))) & 0xffffffffffffff; const c30 = carry3; const t30 = t14; const carry4 = (z40 + c30) >> 56; - const t15 = @truncate(u64, (z40 + c30)) & 0xffffffffffffff; + const t15 = @as(u64, @truncate((z40 + c30))) & 0xffffffffffffff; const c40 = carry4; const t40 = t15; const carry5 = (z50 + c40) >> 56; - const t16 = @truncate(u64, (z50 + c40)) & 0xffffffffffffff; + const t16 = @as(u64, @truncate((z50 + c40))) & 0xffffffffffffff; const c50 = carry5; const t50 = t16; const carry6 = (z60 + c50) >> 56; - const t17 = @truncate(u64, (z60 + c50)) & 0xffffffffffffff; + const t17 = @as(u64, @truncate((z60 + c50))) & 0xffffffffffffff; const c60 = carry6; const t60 = t17; const carry7 = (z70 + c60) >> 56; - const t18 = @truncate(u64, (z70 + c60)) & 0xffffffffffffff; + const t18 = @as(u64, @truncate((z70 + c60))) & 0xffffffffffffff; const c70 = carry7; const t70 = t18; const carry8 = (z80 + c70) >> 56; - const t19 = @truncate(u64, (z80 + c70)) & 0xffffffffffffff; + const t19 = @as(u64, @truncate((z80 + c70))) & 0xffffffffffffff; const c80 = carry8; const t80 = t19; - const t90 = (@truncate(u64, c80)); + const t90 = (@as(u64, @truncate(c80))); const r0 = t00; const r1 = t12; const r2 = t20; @@ -356,26 +356,26 @@ pub const Scalar = struct { const carry12 = (z32 + c21) >> 56; const c31 = carry12; const carry13 = (z42 + c31) >> 56; - const t24 = @truncate(u64, z42 + c31) & 0xffffffffffffff; + const t24 = @as(u64, @truncate(z42 + c31)) & 0xffffffffffffff; const c41 = carry13; const t41 = t24; const carry14 = (z5 + c41) >> 56; - const t25 = @truncate(u64, z5 + c41) & 0xffffffffffffff; + const t25 = @as(u64, @truncate(z5 + c41)) & 0xffffffffffffff; const c5 = carry14; const t5 = t25; const carry15 = (z6 + c5) >> 56; - const t26 = @truncate(u64, z6 + c5) & 0xffffffffffffff; + const t26 = @as(u64, @truncate(z6 + c5)) & 0xffffffffffffff; const c6 = carry15; const t6 = t26; const carry16 = (z7 + c6) >> 56; - const t27 = @truncate(u64, z7 + c6) & 0xffffffffffffff; + const t27 = @as(u64, @truncate(z7 + c6)) & 0xffffffffffffff; const c7 = carry16; const t7 = t27; const carry17 = (z8 + c7) >> 56; - const t28 = @truncate(u64, z8 + c7) & 0xffffffffffffff; + const t28 = @as(u64, @truncate(z8 + c7)) & 0xffffffffffffff; const c8 = carry17; const t8 = t28; - const t9 = @truncate(u64, c8); + const t9 = @as(u64, @truncate(c8)); const qmu4_ = t41; const qmu5_ = t5; @@ -425,22 +425,22 @@ pub const Scalar = struct { const xy31 = @as(u128, qdiv3) * @as(u128, m1); const xy40 = @as(u128, qdiv4) * @as(u128, m0); const carry18 = xy00 >> 56; - const t29 = @truncate(u64, xy00) & 0xffffffffffffff; + const t29 = @as(u64, @truncate(xy00)) & 0xffffffffffffff; const c0 = carry18; const t01 = t29; const carry19 = (xy01 + xy10 + c0) >> 56; - const t31 = @truncate(u64, xy01 + xy10 + c0) & 0xffffffffffffff; + const t31 = @as(u64, @truncate(xy01 + xy10 + c0)) & 0xffffffffffffff; const c12 = carry19; const t110 = t31; const carry20 = (xy02 + xy11 + xy20 + c12) >> 56; - const t32 = @truncate(u64, xy02 + xy11 + xy20 + c12) & 0xffffffffffffff; + const t32 = @as(u64, @truncate(xy02 + xy11 + xy20 + c12)) & 0xffffffffffffff; const c22 = carry20; const t210 = t32; const carry = (xy03 + xy12 + xy21 + xy30 + c22) >> 56; - const t33 = @truncate(u64, xy03 + xy12 + xy21 + xy30 + c22) & 0xffffffffffffff; + const t33 = @as(u64, @truncate(xy03 + xy12 + xy21 + xy30 + c22)) & 0xffffffffffffff; const c32 = carry; const t34 = t33; - const t42 = @truncate(u64, xy04 + xy13 + xy22 + xy31 + xy40 + c32) & 0xffffffffff; + const t42 = @as(u64, @truncate(xy04 + xy13 + xy22 + xy31 + xy40 + c32)) & 0xffffffffff; const qmul0 = t01; const qmul1 = t110; @@ -498,7 +498,7 @@ pub const Scalar = struct { const t = ((b << 56) + s4) -% (y41 + b3); const b4 = b; const t4 = t; - const mask = (b4 -% @intCast(u64, ((1)))); + const mask = (b4 -% @as(u64, @intCast(((1))))); const z04 = s0 ^ (mask & (s0 ^ t0)); const z14 = s1 ^ (mask & (s1 ^ t1)); const z24 = s2 ^ (mask & (s2 ^ t2)); @@ -691,26 +691,26 @@ const ScalarDouble = struct { const carry3 = (z31 + c20) >> 56; const c30 = carry3; const carry4 = (z41 + c30) >> 56; - const t103 = @as(u64, @truncate(u64, z41 + c30)) & 0xffffffffffffff; + const t103 = @as(u64, @as(u64, @truncate(z41 + c30))) & 0xffffffffffffff; const c40 = carry4; const t410 = t103; const carry5 = (z5 + c40) >> 56; - const t104 = @as(u64, @truncate(u64, z5 + c40)) & 0xffffffffffffff; + const t104 = @as(u64, @as(u64, @truncate(z5 + c40))) & 0xffffffffffffff; const c5 = carry5; const t51 = t104; const carry6 = (z6 + c5) >> 56; - const t105 = @as(u64, @truncate(u64, z6 + c5)) & 0xffffffffffffff; + const t105 = @as(u64, @as(u64, @truncate(z6 + c5))) & 0xffffffffffffff; const c6 = carry6; const t61 = t105; const carry7 = (z7 + c6) >> 56; - const t106 = @as(u64, @truncate(u64, z7 + c6)) & 0xffffffffffffff; + const t106 = @as(u64, @as(u64, @truncate(z7 + c6))) & 0xffffffffffffff; const c7 = carry7; const t71 = t106; const carry8 = (z8 + c7) >> 56; - const t107 = @as(u64, @truncate(u64, z8 + c7)) & 0xffffffffffffff; + const t107 = @as(u64, @as(u64, @truncate(z8 + c7))) & 0xffffffffffffff; const c8 = carry8; const t81 = t107; - const t91 = @as(u64, @truncate(u64, c8)); + const t91 = @as(u64, @as(u64, @truncate(c8))); const qmu4_ = t410; const qmu5_ = t51; @@ -760,22 +760,22 @@ const ScalarDouble = struct { const xy31 = @as(u128, qdiv3) * @as(u128, m1); const xy40 = @as(u128, qdiv4) * @as(u128, m0); const carry9 = xy00 >> 56; - const t108 = @truncate(u64, xy00) & 0xffffffffffffff; + const t108 = @as(u64, @truncate(xy00)) & 0xffffffffffffff; const c0 = carry9; const t010 = t108; const carry10 = (xy01 + xy10 + c0) >> 56; - const t109 = @truncate(u64, xy01 + xy10 + c0) & 0xffffffffffffff; + const t109 = @as(u64, @truncate(xy01 + xy10 + c0)) & 0xffffffffffffff; const c11 = carry10; const t110 = t109; const carry11 = (xy02 + xy11 + xy20 + c11) >> 56; - const t1010 = @truncate(u64, xy02 + xy11 + xy20 + c11) & 0xffffffffffffff; + const t1010 = @as(u64, @truncate(xy02 + xy11 + xy20 + c11)) & 0xffffffffffffff; const c21 = carry11; const t210 = t1010; const carry = (xy03 + xy12 + xy21 + xy30 + c21) >> 56; - const t1011 = @truncate(u64, xy03 + xy12 + xy21 + xy30 + c21) & 0xffffffffffffff; + const t1011 = @as(u64, @truncate(xy03 + xy12 + xy21 + xy30 + c21)) & 0xffffffffffffff; const c31 = carry; const t310 = t1011; - const t411 = @truncate(u64, xy04 + xy13 + xy22 + xy31 + xy40 + c31) & 0xffffffffff; + const t411 = @as(u64, @truncate(xy04 + xy13 + xy22 + xy31 + xy40 + c31)) & 0xffffffffff; const qmul0 = t010; const qmul1 = t110; diff --git a/lib/std/crypto/Certificate.zig b/lib/std/crypto/Certificate.zig index 51eb97ab32..a4f0ff604b 100644 --- a/lib/std/crypto/Certificate.zig +++ b/lib/std/crypto/Certificate.zig @@ -312,7 +312,7 @@ pub const Parsed = struct { while (name_i < general_names.slice.end) { const general_name = try der.Element.parse(subject_alt_name, name_i); name_i = general_name.slice.end; - switch (@enumFromInt(GeneralNameTag, @intFromEnum(general_name.identifier.tag))) { + switch (@as(GeneralNameTag, @enumFromInt(@intFromEnum(general_name.identifier.tag)))) { .dNSName => { const dns_name = subject_alt_name[general_name.slice.start..general_name.slice.end]; if (checkHostName(host_name, dns_name)) return; @@ -379,7 +379,7 @@ pub fn parse(cert: Certificate) ParseError!Parsed { const tbs_certificate = try der.Element.parse(cert_bytes, certificate.slice.start); const version_elem = try der.Element.parse(cert_bytes, tbs_certificate.slice.start); const version = try parseVersion(cert_bytes, version_elem); - const serial_number = if (@bitCast(u8, version_elem.identifier) == 0xa0) + const serial_number = if (@as(u8, @bitCast(version_elem.identifier)) == 0xa0) try der.Element.parse(cert_bytes, version_elem.slice.end) else version_elem; @@ -597,8 +597,8 @@ const Date = struct { var month: u4 = 1; while (month < date.month) : (month += 1) { const days: u64 = std.time.epoch.getDaysInMonth( - @enumFromInt(std.time.epoch.YearLeapKind, @intFromBool(is_leap)), - @enumFromInt(std.time.epoch.Month, month), + @as(std.time.epoch.YearLeapKind, @enumFromInt(@intFromBool(is_leap))), + @as(std.time.epoch.Month, @enumFromInt(month)), ); sec += days * std.time.epoch.secs_per_day; } @@ -685,7 +685,7 @@ fn parseEnum(comptime E: type, bytes: []const u8, element: der.Element) ParseEnu pub const ParseVersionError = error{ UnsupportedCertificateVersion, CertificateFieldHasInvalidLength }; pub fn parseVersion(bytes: []const u8, version_elem: der.Element) ParseVersionError!Version { - if (@bitCast(u8, version_elem.identifier) != 0xa0) + if (@as(u8, @bitCast(version_elem.identifier)) != 0xa0) return .v1; if (version_elem.slice.end - version_elem.slice.start != 3) @@ -864,7 +864,7 @@ pub const der = struct { pub fn parse(bytes: []const u8, index: u32) ParseElementError!Element { var i = index; - const identifier = @bitCast(Identifier, bytes[i]); + const identifier = @as(Identifier, @bitCast(bytes[i])); i += 1; const size_byte = bytes[i]; i += 1; @@ -878,7 +878,7 @@ pub const der = struct { }; } - const len_size = @truncate(u7, size_byte); + const len_size = @as(u7, @truncate(size_byte)); if (len_size > @sizeOf(u32)) { return error.CertificateFieldHasInvalidLength; } @@ -1042,10 +1042,10 @@ pub const rsa = struct { var hashed: [Hash.digest_length]u8 = undefined; while (idx < len) { - c[0] = @intCast(u8, (counter >> 24) & 0xFF); - c[1] = @intCast(u8, (counter >> 16) & 0xFF); - c[2] = @intCast(u8, (counter >> 8) & 0xFF); - c[3] = @intCast(u8, counter & 0xFF); + c[0] = @as(u8, @intCast((counter >> 24) & 0xFF)); + c[1] = @as(u8, @intCast((counter >> 16) & 0xFF)); + c[2] = @as(u8, @intCast((counter >> 8) & 0xFF)); + c[3] = @as(u8, @intCast(counter & 0xFF)); std.mem.copyForwards(u8, hash[seed.len..], &c); Hash.hash(&hash, &hashed, .{}); diff --git a/lib/std/crypto/Certificate/Bundle.zig b/lib/std/crypto/Certificate/Bundle.zig index 434de6e0a8..2a5555e301 100644 --- a/lib/std/crypto/Certificate/Bundle.zig +++ b/lib/std/crypto/Certificate/Bundle.zig @@ -131,7 +131,7 @@ pub fn rescanWindows(cb: *Bundle, gpa: Allocator) RescanWindowsError!void { var ctx = w.crypt32.CertEnumCertificatesInStore(store, null); while (ctx) |context| : (ctx = w.crypt32.CertEnumCertificatesInStore(store, ctx)) { - const decoded_start = @intCast(u32, cb.bytes.items.len); + const decoded_start = @as(u32, @intCast(cb.bytes.items.len)); const encoded_cert = context.pbCertEncoded[0..context.cbCertEncoded]; try cb.bytes.appendSlice(gpa, encoded_cert); try cb.parseCert(gpa, decoded_start, now_sec); @@ -213,7 +213,7 @@ pub fn addCertsFromFile(cb: *Bundle, gpa: Allocator, file: fs.File) AddCertsFrom const needed_capacity = std.math.cast(u32, decoded_size_upper_bound + size) orelse return error.CertificateAuthorityBundleTooBig; try cb.bytes.ensureUnusedCapacity(gpa, needed_capacity); - const end_reserved = @intCast(u32, cb.bytes.items.len + decoded_size_upper_bound); + const end_reserved = @as(u32, @intCast(cb.bytes.items.len + decoded_size_upper_bound)); const buffer = cb.bytes.allocatedSlice()[end_reserved..]; const end_index = try file.readAll(buffer); const encoded_bytes = buffer[0..end_index]; @@ -230,7 +230,7 @@ pub fn addCertsFromFile(cb: *Bundle, gpa: Allocator, file: fs.File) AddCertsFrom return error.MissingEndCertificateMarker; start_index = cert_end + end_marker.len; const encoded_cert = mem.trim(u8, encoded_bytes[cert_start..cert_end], " \t\r\n"); - const decoded_start = @intCast(u32, cb.bytes.items.len); + const decoded_start = @as(u32, @intCast(cb.bytes.items.len)); const dest_buf = cb.bytes.allocatedSlice()[decoded_start..]; cb.bytes.items.len += try base64.decode(dest_buf, encoded_cert); try cb.parseCert(gpa, decoded_start, now_sec); diff --git a/lib/std/crypto/Certificate/Bundle/macos.zig b/lib/std/crypto/Certificate/Bundle/macos.zig index bd7100eb46..028275a06b 100644 --- a/lib/std/crypto/Certificate/Bundle/macos.zig +++ b/lib/std/crypto/Certificate/Bundle/macos.zig @@ -21,7 +21,7 @@ pub fn rescanMac(cb: *Bundle, gpa: Allocator) RescanMacError!void { const reader = stream.reader(); const db_header = try reader.readStructBig(ApplDbHeader); - assert(mem.eql(u8, "kych", &@bitCast([4]u8, db_header.signature))); + assert(mem.eql(u8, "kych", &@as([4]u8, @bitCast(db_header.signature)))); try stream.seekTo(db_header.schema_offset); @@ -42,7 +42,7 @@ pub fn rescanMac(cb: *Bundle, gpa: Allocator) RescanMacError!void { const table_header = try reader.readStructBig(TableHeader); - if (@enumFromInt(std.os.darwin.cssm.DB_RECORDTYPE, table_header.table_id) != .X509_CERTIFICATE) { + if (@as(std.os.darwin.cssm.DB_RECORDTYPE, @enumFromInt(table_header.table_id)) != .X509_CERTIFICATE) { continue; } @@ -61,7 +61,7 @@ pub fn rescanMac(cb: *Bundle, gpa: Allocator) RescanMacError!void { try cb.bytes.ensureUnusedCapacity(gpa, cert_header.cert_size); - const cert_start = @intCast(u32, cb.bytes.items.len); + const cert_start = @as(u32, @intCast(cb.bytes.items.len)); const dest_buf = cb.bytes.allocatedSlice()[cert_start..]; cb.bytes.items.len += try reader.readAtLeast(dest_buf, cert_header.cert_size); diff --git a/lib/std/crypto/aegis.zig b/lib/std/crypto/aegis.zig index 9709a3a958..75633f7c69 100644 --- a/lib/std/crypto/aegis.zig +++ b/lib/std/crypto/aegis.zig @@ -625,7 +625,7 @@ test "Aegis MAC" { const key = [_]u8{0x00} ** Aegis128LMac.key_length; var msg: [64]u8 = undefined; for (&msg, 0..) |*m, i| { - m.* = @truncate(u8, i); + m.* = @as(u8, @truncate(i)); } const st_init = Aegis128LMac.init(&key); var st = st_init; diff --git a/lib/std/crypto/aes/soft.zig b/lib/std/crypto/aes/soft.zig index 4c2a8ff80d..0b15555ad0 100644 --- a/lib/std/crypto/aes/soft.zig +++ b/lib/std/crypto/aes/soft.zig @@ -51,13 +51,13 @@ pub const Block = struct { const s3 = block.repr[3]; var x: [4]u32 = undefined; - x = table_lookup(&table_encrypt, @truncate(u8, s0), @truncate(u8, s1 >> 8), @truncate(u8, s2 >> 16), @truncate(u8, s3 >> 24)); + x = table_lookup(&table_encrypt, @as(u8, @truncate(s0)), @as(u8, @truncate(s1 >> 8)), @as(u8, @truncate(s2 >> 16)), @as(u8, @truncate(s3 >> 24))); var t0 = x[0] ^ x[1] ^ x[2] ^ x[3]; - x = table_lookup(&table_encrypt, @truncate(u8, s1), @truncate(u8, s2 >> 8), @truncate(u8, s3 >> 16), @truncate(u8, s0 >> 24)); + x = table_lookup(&table_encrypt, @as(u8, @truncate(s1)), @as(u8, @truncate(s2 >> 8)), @as(u8, @truncate(s3 >> 16)), @as(u8, @truncate(s0 >> 24))); var t1 = x[0] ^ x[1] ^ x[2] ^ x[3]; - x = table_lookup(&table_encrypt, @truncate(u8, s2), @truncate(u8, s3 >> 8), @truncate(u8, s0 >> 16), @truncate(u8, s1 >> 24)); + x = table_lookup(&table_encrypt, @as(u8, @truncate(s2)), @as(u8, @truncate(s3 >> 8)), @as(u8, @truncate(s0 >> 16)), @as(u8, @truncate(s1 >> 24))); var t2 = x[0] ^ x[1] ^ x[2] ^ x[3]; - x = table_lookup(&table_encrypt, @truncate(u8, s3), @truncate(u8, s0 >> 8), @truncate(u8, s1 >> 16), @truncate(u8, s2 >> 24)); + x = table_lookup(&table_encrypt, @as(u8, @truncate(s3)), @as(u8, @truncate(s0 >> 8)), @as(u8, @truncate(s1 >> 16)), @as(u8, @truncate(s2 >> 24))); var t3 = x[0] ^ x[1] ^ x[2] ^ x[3]; t0 ^= round_key.repr[0]; @@ -77,31 +77,31 @@ pub const Block = struct { var x: [4]u32 = undefined; x = .{ - table_encrypt[0][@truncate(u8, s0)], - table_encrypt[1][@truncate(u8, s1 >> 8)], - table_encrypt[2][@truncate(u8, s2 >> 16)], - table_encrypt[3][@truncate(u8, s3 >> 24)], + table_encrypt[0][@as(u8, @truncate(s0))], + table_encrypt[1][@as(u8, @truncate(s1 >> 8))], + table_encrypt[2][@as(u8, @truncate(s2 >> 16))], + table_encrypt[3][@as(u8, @truncate(s3 >> 24))], }; var t0 = x[0] ^ x[1] ^ x[2] ^ x[3]; x = .{ - table_encrypt[0][@truncate(u8, s1)], - table_encrypt[1][@truncate(u8, s2 >> 8)], - table_encrypt[2][@truncate(u8, s3 >> 16)], - table_encrypt[3][@truncate(u8, s0 >> 24)], + table_encrypt[0][@as(u8, @truncate(s1))], + table_encrypt[1][@as(u8, @truncate(s2 >> 8))], + table_encrypt[2][@as(u8, @truncate(s3 >> 16))], + table_encrypt[3][@as(u8, @truncate(s0 >> 24))], }; var t1 = x[0] ^ x[1] ^ x[2] ^ x[3]; x = .{ - table_encrypt[0][@truncate(u8, s2)], - table_encrypt[1][@truncate(u8, s3 >> 8)], - table_encrypt[2][@truncate(u8, s0 >> 16)], - table_encrypt[3][@truncate(u8, s1 >> 24)], + table_encrypt[0][@as(u8, @truncate(s2))], + table_encrypt[1][@as(u8, @truncate(s3 >> 8))], + table_encrypt[2][@as(u8, @truncate(s0 >> 16))], + table_encrypt[3][@as(u8, @truncate(s1 >> 24))], }; var t2 = x[0] ^ x[1] ^ x[2] ^ x[3]; x = .{ - table_encrypt[0][@truncate(u8, s3)], - table_encrypt[1][@truncate(u8, s0 >> 8)], - table_encrypt[2][@truncate(u8, s1 >> 16)], - table_encrypt[3][@truncate(u8, s2 >> 24)], + table_encrypt[0][@as(u8, @truncate(s3))], + table_encrypt[1][@as(u8, @truncate(s0 >> 8))], + table_encrypt[2][@as(u8, @truncate(s1 >> 16))], + table_encrypt[3][@as(u8, @truncate(s2 >> 24))], }; var t3 = x[0] ^ x[1] ^ x[2] ^ x[3]; @@ -122,13 +122,13 @@ pub const Block = struct { // Last round uses s-box directly and XORs to produce output. var x: [4]u8 = undefined; - x = sbox_lookup(&sbox_encrypt, @truncate(u8, s3 >> 24), @truncate(u8, s2 >> 16), @truncate(u8, s1 >> 8), @truncate(u8, s0)); + x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s3 >> 24)), @as(u8, @truncate(s2 >> 16)), @as(u8, @truncate(s1 >> 8)), @as(u8, @truncate(s0))); var t0 = @as(u32, x[0]) << 24 | @as(u32, x[1]) << 16 | @as(u32, x[2]) << 8 | @as(u32, x[3]); - x = sbox_lookup(&sbox_encrypt, @truncate(u8, s0 >> 24), @truncate(u8, s3 >> 16), @truncate(u8, s2 >> 8), @truncate(u8, s1)); + x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s0 >> 24)), @as(u8, @truncate(s3 >> 16)), @as(u8, @truncate(s2 >> 8)), @as(u8, @truncate(s1))); var t1 = @as(u32, x[0]) << 24 | @as(u32, x[1]) << 16 | @as(u32, x[2]) << 8 | @as(u32, x[3]); - x = sbox_lookup(&sbox_encrypt, @truncate(u8, s1 >> 24), @truncate(u8, s0 >> 16), @truncate(u8, s3 >> 8), @truncate(u8, s2)); + x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s1 >> 24)), @as(u8, @truncate(s0 >> 16)), @as(u8, @truncate(s3 >> 8)), @as(u8, @truncate(s2))); var t2 = @as(u32, x[0]) << 24 | @as(u32, x[1]) << 16 | @as(u32, x[2]) << 8 | @as(u32, x[3]); - x = sbox_lookup(&sbox_encrypt, @truncate(u8, s2 >> 24), @truncate(u8, s1 >> 16), @truncate(u8, s0 >> 8), @truncate(u8, s3)); + x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s2 >> 24)), @as(u8, @truncate(s1 >> 16)), @as(u8, @truncate(s0 >> 8)), @as(u8, @truncate(s3))); var t3 = @as(u32, x[0]) << 24 | @as(u32, x[1]) << 16 | @as(u32, x[2]) << 8 | @as(u32, x[3]); t0 ^= round_key.repr[0]; @@ -147,13 +147,13 @@ pub const Block = struct { const s3 = block.repr[3]; var x: [4]u32 = undefined; - x = table_lookup(&table_decrypt, @truncate(u8, s0), @truncate(u8, s3 >> 8), @truncate(u8, s2 >> 16), @truncate(u8, s1 >> 24)); + x = table_lookup(&table_decrypt, @as(u8, @truncate(s0)), @as(u8, @truncate(s3 >> 8)), @as(u8, @truncate(s2 >> 16)), @as(u8, @truncate(s1 >> 24))); var t0 = x[0] ^ x[1] ^ x[2] ^ x[3]; - x = table_lookup(&table_decrypt, @truncate(u8, s1), @truncate(u8, s0 >> 8), @truncate(u8, s3 >> 16), @truncate(u8, s2 >> 24)); + x = table_lookup(&table_decrypt, @as(u8, @truncate(s1)), @as(u8, @truncate(s0 >> 8)), @as(u8, @truncate(s3 >> 16)), @as(u8, @truncate(s2 >> 24))); var t1 = x[0] ^ x[1] ^ x[2] ^ x[3]; - x = table_lookup(&table_decrypt, @truncate(u8, s2), @truncate(u8, s1 >> 8), @truncate(u8, s0 >> 16), @truncate(u8, s3 >> 24)); + x = table_lookup(&table_decrypt, @as(u8, @truncate(s2)), @as(u8, @truncate(s1 >> 8)), @as(u8, @truncate(s0 >> 16)), @as(u8, @truncate(s3 >> 24))); var t2 = x[0] ^ x[1] ^ x[2] ^ x[3]; - x = table_lookup(&table_decrypt, @truncate(u8, s3), @truncate(u8, s2 >> 8), @truncate(u8, s1 >> 16), @truncate(u8, s0 >> 24)); + x = table_lookup(&table_decrypt, @as(u8, @truncate(s3)), @as(u8, @truncate(s2 >> 8)), @as(u8, @truncate(s1 >> 16)), @as(u8, @truncate(s0 >> 24))); var t3 = x[0] ^ x[1] ^ x[2] ^ x[3]; t0 ^= round_key.repr[0]; @@ -173,31 +173,31 @@ pub const Block = struct { var x: [4]u32 = undefined; x = .{ - table_decrypt[0][@truncate(u8, s0)], - table_decrypt[1][@truncate(u8, s3 >> 8)], - table_decrypt[2][@truncate(u8, s2 >> 16)], - table_decrypt[3][@truncate(u8, s1 >> 24)], + table_decrypt[0][@as(u8, @truncate(s0))], + table_decrypt[1][@as(u8, @truncate(s3 >> 8))], + table_decrypt[2][@as(u8, @truncate(s2 >> 16))], + table_decrypt[3][@as(u8, @truncate(s1 >> 24))], }; var t0 = x[0] ^ x[1] ^ x[2] ^ x[3]; x = .{ - table_decrypt[0][@truncate(u8, s1)], - table_decrypt[1][@truncate(u8, s0 >> 8)], - table_decrypt[2][@truncate(u8, s3 >> 16)], - table_decrypt[3][@truncate(u8, s2 >> 24)], + table_decrypt[0][@as(u8, @truncate(s1))], + table_decrypt[1][@as(u8, @truncate(s0 >> 8))], + table_decrypt[2][@as(u8, @truncate(s3 >> 16))], + table_decrypt[3][@as(u8, @truncate(s2 >> 24))], }; var t1 = x[0] ^ x[1] ^ x[2] ^ x[3]; x = .{ - table_decrypt[0][@truncate(u8, s2)], - table_decrypt[1][@truncate(u8, s1 >> 8)], - table_decrypt[2][@truncate(u8, s0 >> 16)], - table_decrypt[3][@truncate(u8, s3 >> 24)], + table_decrypt[0][@as(u8, @truncate(s2))], + table_decrypt[1][@as(u8, @truncate(s1 >> 8))], + table_decrypt[2][@as(u8, @truncate(s0 >> 16))], + table_decrypt[3][@as(u8, @truncate(s3 >> 24))], }; var t2 = x[0] ^ x[1] ^ x[2] ^ x[3]; x = .{ - table_decrypt[0][@truncate(u8, s3)], - table_decrypt[1][@truncate(u8, s2 >> 8)], - table_decrypt[2][@truncate(u8, s1 >> 16)], - table_decrypt[3][@truncate(u8, s0 >> 24)], + table_decrypt[0][@as(u8, @truncate(s3))], + table_decrypt[1][@as(u8, @truncate(s2 >> 8))], + table_decrypt[2][@as(u8, @truncate(s1 >> 16))], + table_decrypt[3][@as(u8, @truncate(s0 >> 24))], }; var t3 = x[0] ^ x[1] ^ x[2] ^ x[3]; @@ -218,13 +218,13 @@ pub const Block = struct { // Last round uses s-box directly and XORs to produce output. var x: [4]u8 = undefined; - x = sbox_lookup(&sbox_decrypt, @truncate(u8, s1 >> 24), @truncate(u8, s2 >> 16), @truncate(u8, s3 >> 8), @truncate(u8, s0)); + x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s1 >> 24)), @as(u8, @truncate(s2 >> 16)), @as(u8, @truncate(s3 >> 8)), @as(u8, @truncate(s0))); var t0 = @as(u32, x[0]) << 24 | @as(u32, x[1]) << 16 | @as(u32, x[2]) << 8 | @as(u32, x[3]); - x = sbox_lookup(&sbox_decrypt, @truncate(u8, s2 >> 24), @truncate(u8, s3 >> 16), @truncate(u8, s0 >> 8), @truncate(u8, s1)); + x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s2 >> 24)), @as(u8, @truncate(s3 >> 16)), @as(u8, @truncate(s0 >> 8)), @as(u8, @truncate(s1))); var t1 = @as(u32, x[0]) << 24 | @as(u32, x[1]) << 16 | @as(u32, x[2]) << 8 | @as(u32, x[3]); - x = sbox_lookup(&sbox_decrypt, @truncate(u8, s3 >> 24), @truncate(u8, s0 >> 16), @truncate(u8, s1 >> 8), @truncate(u8, s2)); + x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s3 >> 24)), @as(u8, @truncate(s0 >> 16)), @as(u8, @truncate(s1 >> 8)), @as(u8, @truncate(s2))); var t2 = @as(u32, x[0]) << 24 | @as(u32, x[1]) << 16 | @as(u32, x[2]) << 8 | @as(u32, x[3]); - x = sbox_lookup(&sbox_decrypt, @truncate(u8, s0 >> 24), @truncate(u8, s1 >> 16), @truncate(u8, s2 >> 8), @truncate(u8, s3)); + x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s0 >> 24)), @as(u8, @truncate(s1 >> 16)), @as(u8, @truncate(s2 >> 8)), @as(u8, @truncate(s3))); var t3 = @as(u32, x[0]) << 24 | @as(u32, x[1]) << 16 | @as(u32, x[2]) << 8 | @as(u32, x[3]); t0 ^= round_key.repr[0]; @@ -348,7 +348,7 @@ fn KeySchedule(comptime Aes: type) type { const subw = struct { // Apply sbox_encrypt to each byte in w. fn func(w: u32) u32 { - const x = sbox_lookup(&sbox_key_schedule, @truncate(u8, w), @truncate(u8, w >> 8), @truncate(u8, w >> 16), @truncate(u8, w >> 24)); + const x = sbox_lookup(&sbox_key_schedule, @as(u8, @truncate(w)), @as(u8, @truncate(w >> 8)), @as(u8, @truncate(w >> 16)), @as(u8, @truncate(w >> 24))); return @as(u32, x[3]) << 24 | @as(u32, x[2]) << 16 | @as(u32, x[1]) << 8 | @as(u32, x[0]); } }.func; @@ -386,7 +386,7 @@ fn KeySchedule(comptime Aes: type) type { inline while (j < 4) : (j += 1) { var rk = round_keys[(ei + j) / 4].repr[(ei + j) % 4]; if (i > 0 and i + 4 < total_words) { - const x = sbox_lookup(&sbox_key_schedule, @truncate(u8, rk >> 24), @truncate(u8, rk >> 16), @truncate(u8, rk >> 8), @truncate(u8, rk)); + const x = sbox_lookup(&sbox_key_schedule, @as(u8, @truncate(rk >> 24)), @as(u8, @truncate(rk >> 16)), @as(u8, @truncate(rk >> 8)), @as(u8, @truncate(rk))); const y = table_lookup(&table_decrypt, x[3], x[2], x[1], x[0]); rk = y[0] ^ y[1] ^ y[2] ^ y[3]; } @@ -664,7 +664,7 @@ fn mul(a: u8, b: u8) u8 { } } - return @truncate(u8, s); + return @as(u8, @truncate(s)); } const cache_line_bytes = 64; diff --git a/lib/std/crypto/aes_ocb.zig b/lib/std/crypto/aes_ocb.zig index 6d5ce3779a..a05e8a7248 100644 --- a/lib/std/crypto/aes_ocb.zig +++ b/lib/std/crypto/aes_ocb.zig @@ -86,18 +86,18 @@ fn AesOcb(comptime Aes: anytype) type { fn getOffset(aes_enc_ctx: EncryptCtx, npub: [nonce_length]u8) Block { var nx = [_]u8{0} ** 16; - nx[0] = @intCast(u8, @truncate(u7, tag_length * 8) << 1); + nx[0] = @as(u8, @intCast(@as(u7, @truncate(tag_length * 8)) << 1)); nx[16 - nonce_length - 1] = 1; nx[nx.len - nonce_length ..].* = npub; - const bottom = @truncate(u6, nx[15]); + const bottom = @as(u6, @truncate(nx[15])); nx[15] &= 0xc0; var ktop_: Block = undefined; aes_enc_ctx.encrypt(&ktop_, &nx); const ktop = mem.readIntBig(u128, &ktop_); - var stretch = (@as(u192, ktop) << 64) | @as(u192, @truncate(u64, ktop >> 64) ^ @truncate(u64, ktop >> 56)); + var stretch = (@as(u192, ktop) << 64) | @as(u192, @as(u64, @truncate(ktop >> 64)) ^ @as(u64, @truncate(ktop >> 56))); var offset: Block = undefined; - mem.writeIntBig(u128, &offset, @truncate(u128, stretch >> (64 - @as(u7, bottom)))); + mem.writeIntBig(u128, &offset, @as(u128, @truncate(stretch >> (64 - @as(u7, bottom))))); return offset; } diff --git a/lib/std/crypto/argon2.zig b/lib/std/crypto/argon2.zig index 40df3290c0..898bc24e6f 100644 --- a/lib/std/crypto/argon2.zig +++ b/lib/std/crypto/argon2.zig @@ -95,7 +95,7 @@ pub const Params = struct { pub fn fromLimits(ops_limit: u32, mem_limit: usize) Self { const m = mem_limit / 1024; std.debug.assert(m <= max_int); - return .{ .t = ops_limit, .m = @intCast(u32, m), .p = 1 }; + return .{ .t = ops_limit, .m = @as(u32, @intCast(m)), .p = 1 }; } }; @@ -111,26 +111,26 @@ fn initHash( var tmp: [4]u8 = undefined; var b2 = Blake2b512.init(.{}); mem.writeIntLittle(u32, parameters[0..4], params.p); - mem.writeIntLittle(u32, parameters[4..8], @intCast(u32, dk_len)); + mem.writeIntLittle(u32, parameters[4..8], @as(u32, @intCast(dk_len))); mem.writeIntLittle(u32, parameters[8..12], params.m); mem.writeIntLittle(u32, parameters[12..16], params.t); mem.writeIntLittle(u32, parameters[16..20], version); mem.writeIntLittle(u32, parameters[20..24], @intFromEnum(mode)); b2.update(¶meters); - mem.writeIntLittle(u32, &tmp, @intCast(u32, password.len)); + mem.writeIntLittle(u32, &tmp, @as(u32, @intCast(password.len))); b2.update(&tmp); b2.update(password); - mem.writeIntLittle(u32, &tmp, @intCast(u32, salt.len)); + mem.writeIntLittle(u32, &tmp, @as(u32, @intCast(salt.len))); b2.update(&tmp); b2.update(salt); const secret = params.secret orelse ""; std.debug.assert(secret.len <= max_int); - mem.writeIntLittle(u32, &tmp, @intCast(u32, secret.len)); + mem.writeIntLittle(u32, &tmp, @as(u32, @intCast(secret.len))); b2.update(&tmp); b2.update(secret); const ad = params.ad orelse ""; std.debug.assert(ad.len <= max_int); - mem.writeIntLittle(u32, &tmp, @intCast(u32, ad.len)); + mem.writeIntLittle(u32, &tmp, @as(u32, @intCast(ad.len))); b2.update(&tmp); b2.update(ad); b2.final(h0[0..Blake2b512.digest_length]); @@ -140,7 +140,7 @@ fn initHash( fn blake2bLong(out: []u8, in: []const u8) void { const H = Blake2b512; var outlen_bytes: [4]u8 = undefined; - mem.writeIntLittle(u32, &outlen_bytes, @intCast(u32, out.len)); + mem.writeIntLittle(u32, &outlen_bytes, @as(u32, @intCast(out.len))); var out_buf: [H.digest_length]u8 = undefined; @@ -391,7 +391,7 @@ fn Rp(a: usize, b: usize, c: usize, d: usize) QuarterRound { } fn fBlaMka(x: u64, y: u64) u64 { - const xy = @as(u64, @truncate(u32, x)) * @as(u64, @truncate(u32, y)); + const xy = @as(u64, @as(u32, @truncate(x))) * @as(u64, @as(u32, @truncate(y))); return x +% y +% 2 *% xy; } @@ -448,7 +448,7 @@ fn indexAlpha( lane: u24, index: u32, ) u32 { - var ref_lane = @intCast(u32, rand >> 32) % threads; + var ref_lane = @as(u32, @intCast(rand >> 32)) % threads; if (n == 0 and slice == 0) { ref_lane = lane; } @@ -467,10 +467,10 @@ fn indexAlpha( if (index == 0 or lane == ref_lane) { m -= 1; } - var p = @as(u64, @truncate(u32, rand)); + var p = @as(u64, @as(u32, @truncate(rand))); p = (p * p) >> 32; p = (p * m) >> 32; - return ref_lane * lanes + @intCast(u32, ((s + m - (p + 1)) % lanes)); + return ref_lane * lanes + @as(u32, @intCast(((s + m - (p + 1)) % lanes))); } /// Derives a key from the password, salt, and argon2 parameters. diff --git a/lib/std/crypto/ascon.zig b/lib/std/crypto/ascon.zig index ae4bb57d29..8aa0b109f2 100644 --- a/lib/std/crypto/ascon.zig +++ b/lib/std/crypto/ascon.zig @@ -95,8 +95,8 @@ pub fn State(comptime endian: builtin.Endian) type { /// XOR a byte into the state at a given offset. pub fn addByte(self: *Self, byte: u8, offset: usize) void { const z = switch (endian) { - .Big => 64 - 8 - 8 * @truncate(u6, offset % 8), - .Little => 8 * @truncate(u6, offset % 8), + .Big => 64 - 8 - 8 * @as(u6, @truncate(offset % 8)), + .Little => 8 * @as(u6, @truncate(offset % 8)), }; self.st[offset / 8] ^= @as(u64, byte) << z; } diff --git a/lib/std/crypto/bcrypt.zig b/lib/std/crypto/bcrypt.zig index 7bd140d584..87d2eef79a 100644 --- a/lib/std/crypto/bcrypt.zig +++ b/lib/std/crypto/bcrypt.zig @@ -376,10 +376,10 @@ pub const State = struct { const Halves = struct { l: u32, r: u32 }; fn halfRound(state: *const State, i: u32, j: u32, n: usize) u32 { - var r = state.sboxes[0][@truncate(u8, j >> 24)]; - r +%= state.sboxes[1][@truncate(u8, j >> 16)]; - r ^= state.sboxes[2][@truncate(u8, j >> 8)]; - r +%= state.sboxes[3][@truncate(u8, j)]; + var r = state.sboxes[0][@as(u8, @truncate(j >> 24))]; + r +%= state.sboxes[1][@as(u8, @truncate(j >> 16))]; + r ^= state.sboxes[2][@as(u8, @truncate(j >> 8))]; + r +%= state.sboxes[3][@as(u8, @truncate(j))]; return i ^ r ^ state.subkeys[n]; } diff --git a/lib/std/crypto/benchmark.zig b/lib/std/crypto/benchmark.zig index f47c334ee9..17f11382ca 100644 --- a/lib/std/crypto/benchmark.zig +++ b/lib/std/crypto/benchmark.zig @@ -54,8 +54,8 @@ pub fn benchmarkHash(comptime Hash: anytype, comptime bytes: comptime_int) !u64 const end = timer.read(); - const elapsed_s = @floatFromInt(f64, end - start) / time.ns_per_s; - const throughput = @intFromFloat(u64, bytes / elapsed_s); + const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; + const throughput = @as(u64, @intFromFloat(bytes / elapsed_s)); return throughput; } @@ -95,8 +95,8 @@ pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 { } const end = timer.read(); - const elapsed_s = @floatFromInt(f64, end - start) / time.ns_per_s; - const throughput = @intFromFloat(u64, bytes / elapsed_s); + const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; + const throughput = @as(u64, @intFromFloat(bytes / elapsed_s)); return throughput; } @@ -125,8 +125,8 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c } const end = timer.read(); - const elapsed_s = @floatFromInt(f64, end - start) / time.ns_per_s; - const throughput = @intFromFloat(u64, exchange_count / elapsed_s); + const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; + const throughput = @as(u64, @intFromFloat(exchange_count / elapsed_s)); return throughput; } @@ -148,8 +148,8 @@ pub fn benchmarkSignature(comptime Signature: anytype, comptime signatures_count } const end = timer.read(); - const elapsed_s = @floatFromInt(f64, end - start) / time.ns_per_s; - const throughput = @intFromFloat(u64, signatures_count / elapsed_s); + const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; + const throughput = @as(u64, @intFromFloat(signatures_count / elapsed_s)); return throughput; } @@ -172,8 +172,8 @@ pub fn benchmarkSignatureVerification(comptime Signature: anytype, comptime sign } const end = timer.read(); - const elapsed_s = @floatFromInt(f64, end - start) / time.ns_per_s; - const throughput = @intFromFloat(u64, signatures_count / elapsed_s); + const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; + const throughput = @as(u64, @intFromFloat(signatures_count / elapsed_s)); return throughput; } @@ -201,8 +201,8 @@ pub fn benchmarkBatchSignatureVerification(comptime Signature: anytype, comptime } const end = timer.read(); - const elapsed_s = @floatFromInt(f64, end - start) / time.ns_per_s; - const throughput = batch.len * @intFromFloat(u64, signatures_count / elapsed_s); + const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; + const throughput = batch.len * @as(u64, @intFromFloat(signatures_count / elapsed_s)); return throughput; } @@ -227,8 +227,8 @@ pub fn benchmarkKem(comptime Kem: anytype, comptime kems_count: comptime_int) !u } const end = timer.read(); - const elapsed_s = @floatFromInt(f64, end - start) / time.ns_per_s; - const throughput = @intFromFloat(u64, kems_count / elapsed_s); + const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; + const throughput = @as(u64, @intFromFloat(kems_count / elapsed_s)); return throughput; } @@ -249,8 +249,8 @@ pub fn benchmarkKemDecaps(comptime Kem: anytype, comptime kems_count: comptime_i } const end = timer.read(); - const elapsed_s = @floatFromInt(f64, end - start) / time.ns_per_s; - const throughput = @intFromFloat(u64, kems_count / elapsed_s); + const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; + const throughput = @as(u64, @intFromFloat(kems_count / elapsed_s)); return throughput; } @@ -267,8 +267,8 @@ pub fn benchmarkKemKeyGen(comptime Kem: anytype, comptime kems_count: comptime_i } const end = timer.read(); - const elapsed_s = @floatFromInt(f64, end - start) / time.ns_per_s; - const throughput = @intFromFloat(u64, kems_count / elapsed_s); + const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; + const throughput = @as(u64, @intFromFloat(kems_count / elapsed_s)); return throughput; } @@ -309,8 +309,8 @@ pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64 mem.doNotOptimizeAway(&in); const end = timer.read(); - const elapsed_s = @floatFromInt(f64, end - start) / time.ns_per_s; - const throughput = @intFromFloat(u64, 2 * bytes / elapsed_s); + const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; + const throughput = @as(u64, @intFromFloat(2 * bytes / elapsed_s)); return throughput; } @@ -338,8 +338,8 @@ pub fn benchmarkAes(comptime Aes: anytype, comptime count: comptime_int) !u64 { mem.doNotOptimizeAway(&in); const end = timer.read(); - const elapsed_s = @floatFromInt(f64, end - start) / time.ns_per_s; - const throughput = @intFromFloat(u64, count / elapsed_s); + const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; + const throughput = @as(u64, @intFromFloat(count / elapsed_s)); return throughput; } @@ -367,8 +367,8 @@ pub fn benchmarkAes8(comptime Aes: anytype, comptime count: comptime_int) !u64 { mem.doNotOptimizeAway(&in); const end = timer.read(); - const elapsed_s = @floatFromInt(f64, end - start) / time.ns_per_s; - const throughput = @intFromFloat(u64, 8 * count / elapsed_s); + const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; + const throughput = @as(u64, @intFromFloat(8 * count / elapsed_s)); return throughput; } @@ -406,7 +406,7 @@ fn benchmarkPwhash( const password = "testpass" ** 2; const opts = .{ .allocator = allocator, - .params = @ptrCast(*const ty.Params, @alignCast(std.meta.alignment(ty.Params), params)).*, + .params = @as(*const ty.Params, @ptrCast(@alignCast(params))).*, .encoding = .phc, }; var buf: [256]u8 = undefined; @@ -422,7 +422,7 @@ fn benchmarkPwhash( } const end = timer.read(); - const elapsed_s = @floatFromInt(f64, end - start) / time.ns_per_s; + const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; const throughput = elapsed_s / count; return throughput; diff --git a/lib/std/crypto/blake2.zig b/lib/std/crypto/blake2.zig index 316ea5e6b7..ba07226d08 100644 --- a/lib/std/crypto/blake2.zig +++ b/lib/std/crypto/blake2.zig @@ -80,7 +80,7 @@ pub fn Blake2s(comptime out_bits: usize) type { const key_len = if (options.key) |key| key.len else 0; // default parameters - d.h[0] ^= 0x01010000 ^ @truncate(u32, key_len << 8) ^ @intCast(u32, options.expected_out_bits >> 3); + d.h[0] ^= 0x01010000 ^ @as(u32, @truncate(key_len << 8)) ^ @as(u32, @intCast(options.expected_out_bits >> 3)); d.t = 0; d.buf_len = 0; @@ -127,7 +127,7 @@ pub fn Blake2s(comptime out_bits: usize) type { // Copy any remainder for next pass. const b_slice = b[off..]; @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); - d.buf_len += @intCast(u8, b_slice.len); + d.buf_len += @as(u8, @intCast(b_slice.len)); } pub fn final(d: *Self, out: *[digest_length]u8) void { @@ -135,7 +135,7 @@ pub fn Blake2s(comptime out_bits: usize) type { d.t += d.buf_len; d.round(d.buf[0..], true); for (&d.h) |*x| x.* = mem.nativeToLittle(u32, x.*); - out.* = @ptrCast(*[digest_length]u8, &d.h).*; + out.* = @as(*[digest_length]u8, @ptrCast(&d.h)).*; } fn round(d: *Self, b: *const [64]u8, last: bool) void { @@ -152,8 +152,8 @@ pub fn Blake2s(comptime out_bits: usize) type { v[k + 8] = iv[k]; } - v[12] ^= @truncate(u32, d.t); - v[13] ^= @intCast(u32, d.t >> 32); + v[12] ^= @as(u32, @truncate(d.t)); + v[13] ^= @as(u32, @intCast(d.t >> 32)); if (last) v[14] = ~v[14]; const rounds = comptime [_]RoundParam{ @@ -563,7 +563,7 @@ pub fn Blake2b(comptime out_bits: usize) type { // Copy any remainder for next pass. const b_slice = b[off..]; @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); - d.buf_len += @intCast(u8, b_slice.len); + d.buf_len += @as(u8, @intCast(b_slice.len)); } pub fn final(d: *Self, out: *[digest_length]u8) void { @@ -571,7 +571,7 @@ pub fn Blake2b(comptime out_bits: usize) type { d.t += d.buf_len; d.round(d.buf[0..], true); for (&d.h) |*x| x.* = mem.nativeToLittle(u64, x.*); - out.* = @ptrCast(*[digest_length]u8, &d.h).*; + out.* = @as(*[digest_length]u8, @ptrCast(&d.h)).*; } fn round(d: *Self, b: *const [128]u8, last: bool) void { @@ -588,8 +588,8 @@ pub fn Blake2b(comptime out_bits: usize) type { v[k + 8] = iv[k]; } - v[12] ^= @truncate(u64, d.t); - v[13] ^= @intCast(u64, d.t >> 64); + v[12] ^= @as(u64, @truncate(d.t)); + v[13] ^= @as(u64, @intCast(d.t >> 64)); if (last) v[14] = ~v[14]; const rounds = comptime [_]RoundParam{ diff --git a/lib/std/crypto/blake3.zig b/lib/std/crypto/blake3.zig index 7ad1511e79..fc1859b99d 100644 --- a/lib/std/crypto/blake3.zig +++ b/lib/std/crypto/blake3.zig @@ -89,7 +89,7 @@ const CompressVectorized = struct { counter: u64, flags: u8, ) [16]u32 { - const md = Lane{ @truncate(u32, counter), @truncate(u32, counter >> 32), block_len, @as(u32, flags) }; + const md = Lane{ @as(u32, @truncate(counter)), @as(u32, @truncate(counter >> 32)), block_len, @as(u32, flags) }; var rows = Rows{ chaining_value[0..4].*, chaining_value[4..8].*, IV[0..4].*, md }; var m = Rows{ block_words[0..4].*, block_words[4..8].*, block_words[8..12].*, block_words[12..16].* }; @@ -134,7 +134,7 @@ const CompressVectorized = struct { rows[2] ^= @Vector(4, u32){ chaining_value[0], chaining_value[1], chaining_value[2], chaining_value[3] }; rows[3] ^= @Vector(4, u32){ chaining_value[4], chaining_value[5], chaining_value[6], chaining_value[7] }; - return @bitCast([16]u32, rows); + return @as([16]u32, @bitCast(rows)); } }; @@ -184,8 +184,8 @@ const CompressGeneric = struct { IV[1], IV[2], IV[3], - @truncate(u32, counter), - @truncate(u32, counter >> 32), + @as(u32, @truncate(counter)), + @as(u32, @truncate(counter >> 32)), block_len, flags, }; @@ -206,7 +206,7 @@ else CompressGeneric.compress; fn first8Words(words: [16]u32) [8]u32 { - return @ptrCast(*const [8]u32, &words).*; + return @as(*const [8]u32, @ptrCast(&words)).*; } fn wordsFromLittleEndianBytes(comptime count: usize, bytes: [count * 4]u8) [count]u32 { @@ -285,7 +285,7 @@ const ChunkState = struct { const want = BLOCK_LEN - self.block_len; const take = @min(want, input.len); @memcpy(self.block[self.block_len..][0..take], input[0..take]); - self.block_len += @truncate(u8, take); + self.block_len += @as(u8, @truncate(take)); return input[take..]; } @@ -658,7 +658,7 @@ fn testBlake3(hasher: *Blake3, input_len: usize, expected_hex: [262]u8) !void { // Setup input pattern var input_pattern: [251]u8 = undefined; - for (&input_pattern, 0..) |*e, i| e.* = @truncate(u8, i); + for (&input_pattern, 0..) |*e, i| e.* = @as(u8, @truncate(i)); // Write repeating input pattern to hasher var input_counter = input_len; diff --git a/lib/std/crypto/chacha20.zig b/lib/std/crypto/chacha20.zig index 776387cbd9..6688fb97fa 100644 --- a/lib/std/crypto/chacha20.zig +++ b/lib/std/crypto/chacha20.zig @@ -587,8 +587,8 @@ fn ChaChaWith64BitNonce(comptime rounds_nb: usize) type { const k = keyToWords(key); var c: [4]u32 = undefined; - c[0] = @truncate(u32, counter); - c[1] = @truncate(u32, counter >> 32); + c[0] = @as(u32, @truncate(counter)); + c[1] = @as(u32, @truncate(counter >> 32)); c[2] = mem.readIntLittle(u32, nonce[0..4]); c[3] = mem.readIntLittle(u32, nonce[4..8]); ChaChaImpl(rounds_nb).chacha20Xor(out, in, k, c, true); @@ -600,8 +600,8 @@ fn ChaChaWith64BitNonce(comptime rounds_nb: usize) type { const k = keyToWords(key); var c: [4]u32 = undefined; - c[0] = @truncate(u32, counter); - c[1] = @truncate(u32, counter >> 32); + c[0] = @as(u32, @truncate(counter)); + c[1] = @as(u32, @truncate(counter >> 32)); c[2] = mem.readIntLittle(u32, nonce[0..4]); c[3] = mem.readIntLittle(u32, nonce[4..8]); ChaChaImpl(rounds_nb).chacha20Stream(out, k, c, true); diff --git a/lib/std/crypto/ecdsa.zig b/lib/std/crypto/ecdsa.zig index e552af2e26..1a5335b07e 100644 --- a/lib/std/crypto/ecdsa.zig +++ b/lib/std/crypto/ecdsa.zig @@ -122,9 +122,9 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { pub fn toDer(self: Signature, buf: *[der_encoded_max_length]u8) []u8 { var fb = io.fixedBufferStream(buf); const w = fb.writer(); - const r_len = @intCast(u8, self.r.len + (self.r[0] >> 7)); - const s_len = @intCast(u8, self.s.len + (self.s[0] >> 7)); - const seq_len = @intCast(u8, 2 + r_len + 2 + s_len); + const r_len = @as(u8, @intCast(self.r.len + (self.r[0] >> 7))); + const s_len = @as(u8, @intCast(self.s.len + (self.s[0] >> 7))); + const seq_len = @as(u8, @intCast(2 + r_len + 2 + s_len)); w.writeAll(&[_]u8{ 0x30, seq_len }) catch unreachable; w.writeAll(&[_]u8{ 0x02, r_len }) catch unreachable; if (self.r[0] >> 7 != 0) { diff --git a/lib/std/crypto/ff.zig b/lib/std/crypto/ff.zig index 7b298c71c2..0a99058b21 100644 --- a/lib/std/crypto/ff.zig +++ b/lib/std/crypto/ff.zig @@ -100,7 +100,7 @@ pub fn Uint(comptime max_bits: comptime_int) type { var x = x_; var out = Self.zero; for (0..out.limbs.capacity()) |i| { - const t = if (@bitSizeOf(T) > t_bits) @truncate(TLimb, x) else x; + const t = if (@bitSizeOf(T) > t_bits) @as(TLimb, @truncate(x)) else x; out.limbs.set(i, t); x = math.shr(T, x, t_bits); } @@ -143,9 +143,9 @@ pub fn Uint(comptime max_bits: comptime_int) type { var remaining_bits = t_bits; var limb = self.limbs.get(i); while (remaining_bits >= 8) { - bytes[out_i] |= math.shl(u8, @truncate(u8, limb), shift); + bytes[out_i] |= math.shl(u8, @as(u8, @truncate(limb)), shift); const consumed = 8 - shift; - limb >>= @truncate(u4, consumed); + limb >>= @as(u4, @truncate(consumed)); remaining_bits -= consumed; shift = 0; switch (endian) { @@ -169,7 +169,7 @@ pub fn Uint(comptime max_bits: comptime_int) type { }, } } - bytes[out_i] |= @truncate(u8, limb); + bytes[out_i] |= @as(u8, @truncate(limb)); shift = remaining_bits; } } @@ -190,7 +190,7 @@ pub fn Uint(comptime max_bits: comptime_int) type { shift += 8; if (shift >= t_bits) { shift -= t_bits; - out.limbs.set(out_i, @truncate(TLimb, out.limbs.get(out_i))); + out.limbs.set(out_i, @as(TLimb, @truncate(out.limbs.get(out_i)))); const overflow = math.shr(Limb, bi, 8 - shift); out_i += 1; if (out_i >= out.limbs.len) { @@ -242,7 +242,7 @@ pub fn Uint(comptime max_bits: comptime_int) type { /// Returns `true` if the integer is odd. pub fn isOdd(x: Self) bool { - return @bitCast(bool, @truncate(u1, x.limbs.get(0))); + return @as(bool, @bitCast(@as(u1, @truncate(x.limbs.get(0))))); } /// Adds `y` to `x`, and returns `true` if the operation overflowed. @@ -273,8 +273,8 @@ pub fn Uint(comptime max_bits: comptime_int) type { var carry: u1 = 0; for (0..x.limbs_count()) |i| { const res = x_limbs[i] + y_limbs[i] + carry; - x_limbs[i] = ct.select(on, @truncate(TLimb, res), x_limbs[i]); - carry = @truncate(u1, res >> t_bits); + x_limbs[i] = ct.select(on, @as(TLimb, @truncate(res)), x_limbs[i]); + carry = @as(u1, @truncate(res >> t_bits)); } return carry; } @@ -288,8 +288,8 @@ pub fn Uint(comptime max_bits: comptime_int) type { var borrow: u1 = 0; for (0..x.limbs_count()) |i| { const res = x_limbs[i] -% y_limbs[i] -% borrow; - x_limbs[i] = ct.select(on, @truncate(TLimb, res), x_limbs[i]); - borrow = @truncate(u1, res >> t_bits); + x_limbs[i] = ct.select(on, @as(TLimb, @truncate(res)), x_limbs[i]); + borrow = @as(u1, @truncate(res >> t_bits)); } return borrow; } @@ -432,7 +432,7 @@ pub fn Modulus(comptime max_bits: comptime_int) type { inline for (0..comptime math.log2_int(usize, t_bits)) |_| { y = y *% (2 -% lo *% y); } - const m0inv = (@as(Limb, 1) << t_bits) - (@truncate(TLimb, y)); + const m0inv = (@as(Limb, 1) << t_bits) - (@as(TLimb, @truncate(y))); const zero = Fe{ .v = FeUint.zero }; @@ -508,18 +508,18 @@ pub fn Modulus(comptime max_bits: comptime_int) type { var need_sub = false; var i: usize = t_bits - 1; while (true) : (i -= 1) { - var carry = @truncate(u1, math.shr(Limb, y, i)); + var carry = @as(u1, @truncate(math.shr(Limb, y, i))); var borrow: u1 = 0; for (0..self.limbs_count()) |j| { const l = ct.select(need_sub, d_limbs[j], x_limbs[j]); var res = (l << 1) + carry; - x_limbs[j] = @truncate(TLimb, res); - carry = @truncate(u1, res >> t_bits); + x_limbs[j] = @as(TLimb, @truncate(res)); + carry = @as(u1, @truncate(res >> t_bits)); res = x_limbs[j] -% m_limbs[j] -% borrow; - d_limbs[j] = @truncate(TLimb, res); + d_limbs[j] = @as(TLimb, @truncate(res)); - borrow = @truncate(u1, res >> t_bits); + borrow = @as(u1, @truncate(res >> t_bits)); } need_sub = ct.eql(carry, borrow); if (i == 0) break; @@ -531,7 +531,7 @@ pub fn Modulus(comptime max_bits: comptime_int) type { pub fn add(self: Self, x: Fe, y: Fe) Fe { var out = x; const overflow = out.v.addWithOverflow(y.v); - const underflow = @bitCast(u1, ct.limbsCmpLt(out.v, self.v)); + const underflow = @as(u1, @bitCast(ct.limbsCmpLt(out.v, self.v))); const need_sub = ct.eql(overflow, underflow); _ = out.v.conditionalSubWithOverflow(need_sub, self.v); return out; @@ -540,7 +540,7 @@ pub fn Modulus(comptime max_bits: comptime_int) type { /// Subtracts two field elements (mod m). pub fn sub(self: Self, x: Fe, y: Fe) Fe { var out = x; - const underflow = @bitCast(bool, out.v.subWithOverflow(y.v)); + const underflow = @as(bool, @bitCast(out.v.subWithOverflow(y.v))); _ = out.v.conditionalAddWithOverflow(underflow, self.v); return out; } @@ -601,7 +601,7 @@ pub fn Modulus(comptime max_bits: comptime_int) type { var wide = ct.mulWide(a_limbs[i], b_limbs[0]); var z_lo = @addWithOverflow(d_limbs[0], wide.lo); - const f = @truncate(TLimb, z_lo[0] *% self.m0inv); + const f = @as(TLimb, @truncate(z_lo[0] *% self.m0inv)); var z_hi = wide.hi +% z_lo[1]; wide = ct.mulWide(f, m_limbs[0]); z_lo = @addWithOverflow(z_lo[0], wide.lo); @@ -620,13 +620,13 @@ pub fn Modulus(comptime max_bits: comptime_int) type { z_lo = @addWithOverflow(z_lo[0], carry); z_hi +%= z_lo[1]; if (j > 0) { - d_limbs[j - 1] = @truncate(TLimb, z_lo[0]); + d_limbs[j - 1] = @as(TLimb, @truncate(z_lo[0])); } carry = (z_hi << 1) | (z_lo[0] >> t_bits); } const z = overflow + carry; - d_limbs[self.limbs_count() - 1] = @truncate(TLimb, z); - overflow = @truncate(u1, z >> t_bits); + d_limbs[self.limbs_count() - 1] = @as(TLimb, @truncate(z)); + overflow = @as(u1, @truncate(z >> t_bits)); } return overflow; } @@ -735,7 +735,7 @@ pub fn Modulus(comptime max_bits: comptime_int) type { t0 = pc[k - 1]; } else { for (pc, 0..) |t, i| { - t0.v.cmov(ct.eql(k, @truncate(u8, i + 1)), t.v); + t0.v.cmov(ct.eql(k, @as(u8, @truncate(i + 1))), t.v); } } const t1 = self.montgomeryMul(out, t0); @@ -771,7 +771,7 @@ const ct_protected = struct { fn eql(x: anytype, y: @TypeOf(x)) bool { const c1 = @subWithOverflow(x, y)[1]; const c2 = @subWithOverflow(y, x)[1]; - return @bitCast(bool, 1 - (c1 | c2)); + return @as(bool, @bitCast(1 - (c1 | c2))); } // Compares two big integers in constant time, returning true if x < y. @@ -782,28 +782,28 @@ const ct_protected = struct { var c: u1 = 0; for (0..x.limbs_count()) |i| { - c = @truncate(u1, (x_limbs[i] -% y_limbs[i] -% c) >> t_bits); + c = @as(u1, @truncate((x_limbs[i] -% y_limbs[i] -% c) >> t_bits)); } - return @bitCast(bool, c); + return @as(bool, @bitCast(c)); } // Compares two big integers in constant time, returning true if x >= y. fn limbsCmpGeq(x: anytype, y: @TypeOf(x)) bool { - return @bitCast(bool, 1 - @intFromBool(ct.limbsCmpLt(x, y))); + return @as(bool, @bitCast(1 - @intFromBool(ct.limbsCmpLt(x, y)))); } // Multiplies two limbs and returns the result as a wide limb. fn mulWide(x: Limb, y: Limb) WideLimb { const half_bits = @typeInfo(Limb).Int.bits / 2; const Half = meta.Int(.unsigned, half_bits); - const x0 = @truncate(Half, x); - const x1 = @truncate(Half, x >> half_bits); - const y0 = @truncate(Half, y); - const y1 = @truncate(Half, y >> half_bits); + const x0 = @as(Half, @truncate(x)); + const x1 = @as(Half, @truncate(x >> half_bits)); + const y0 = @as(Half, @truncate(y)); + const y1 = @as(Half, @truncate(y >> half_bits)); const w0 = math.mulWide(Half, x0, y0); const t = math.mulWide(Half, x1, y0) + (w0 >> half_bits); - var w1: Limb = @truncate(Half, t); - const w2 = @truncate(Half, t >> half_bits); + var w1: Limb = @as(Half, @truncate(t)); + const w2 = @as(Half, @truncate(t >> half_bits)); w1 += math.mulWide(Half, x0, y1); const hi = math.mulWide(Half, x1, y1) + w2 + (w1 >> half_bits); const lo = x *% y; @@ -847,8 +847,8 @@ const ct_unprotected = struct { fn mulWide(x: Limb, y: Limb) WideLimb { const wide = math.mulWide(Limb, x, y); return .{ - .hi = @truncate(Limb, wide >> @typeInfo(Limb).Int.bits), - .lo = @truncate(Limb, wide), + .hi = @as(Limb, @truncate(wide >> @typeInfo(Limb).Int.bits)), + .lo = @as(Limb, @truncate(wide)), }; } }; diff --git a/lib/std/crypto/ghash_polyval.zig b/lib/std/crypto/ghash_polyval.zig index 2fbff25f72..11379cc8e3 100644 --- a/lib/std/crypto/ghash_polyval.zig +++ b/lib/std/crypto/ghash_polyval.zig @@ -96,28 +96,28 @@ fn Hash(comptime endian: std.builtin.Endian, comptime shift_key: bool) type { const product = asm ( \\ vpclmulqdq $0x11, %[x], %[y], %[out] : [out] "=x" (-> @Vector(2, u64)), - : [x] "x" (@bitCast(@Vector(2, u64), x)), - [y] "x" (@bitCast(@Vector(2, u64), y)), + : [x] "x" (@as(@Vector(2, u64), @bitCast(x))), + [y] "x" (@as(@Vector(2, u64), @bitCast(y))), ); - return @bitCast(u128, product); + return @as(u128, @bitCast(product)); }, .lo => { const product = asm ( \\ vpclmulqdq $0x00, %[x], %[y], %[out] : [out] "=x" (-> @Vector(2, u64)), - : [x] "x" (@bitCast(@Vector(2, u64), x)), - [y] "x" (@bitCast(@Vector(2, u64), y)), + : [x] "x" (@as(@Vector(2, u64), @bitCast(x))), + [y] "x" (@as(@Vector(2, u64), @bitCast(y))), ); - return @bitCast(u128, product); + return @as(u128, @bitCast(product)); }, .hi_lo => { const product = asm ( \\ vpclmulqdq $0x10, %[x], %[y], %[out] : [out] "=x" (-> @Vector(2, u64)), - : [x] "x" (@bitCast(@Vector(2, u64), x)), - [y] "x" (@bitCast(@Vector(2, u64), y)), + : [x] "x" (@as(@Vector(2, u64), @bitCast(x))), + [y] "x" (@as(@Vector(2, u64), @bitCast(y))), ); - return @bitCast(u128, product); + return @as(u128, @bitCast(product)); }, } } @@ -129,28 +129,28 @@ fn Hash(comptime endian: std.builtin.Endian, comptime shift_key: bool) type { const product = asm ( \\ pmull2 %[out].1q, %[x].2d, %[y].2d : [out] "=w" (-> @Vector(2, u64)), - : [x] "w" (@bitCast(@Vector(2, u64), x)), - [y] "w" (@bitCast(@Vector(2, u64), y)), + : [x] "w" (@as(@Vector(2, u64), @bitCast(x))), + [y] "w" (@as(@Vector(2, u64), @bitCast(y))), ); - return @bitCast(u128, product); + return @as(u128, @bitCast(product)); }, .lo => { const product = asm ( \\ pmull %[out].1q, %[x].1d, %[y].1d : [out] "=w" (-> @Vector(2, u64)), - : [x] "w" (@bitCast(@Vector(2, u64), x)), - [y] "w" (@bitCast(@Vector(2, u64), y)), + : [x] "w" (@as(@Vector(2, u64), @bitCast(x))), + [y] "w" (@as(@Vector(2, u64), @bitCast(y))), ); - return @bitCast(u128, product); + return @as(u128, @bitCast(product)); }, .hi_lo => { const product = asm ( \\ pmull %[out].1q, %[x].1d, %[y].1d : [out] "=w" (-> @Vector(2, u64)), - : [x] "w" (@bitCast(@Vector(2, u64), x >> 64)), - [y] "w" (@bitCast(@Vector(2, u64), y)), + : [x] "w" (@as(@Vector(2, u64), @bitCast(x >> 64))), + [y] "w" (@as(@Vector(2, u64), @bitCast(y))), ); - return @bitCast(u128, product); + return @as(u128, @bitCast(product)); }, } } @@ -167,8 +167,8 @@ fn Hash(comptime endian: std.builtin.Endian, comptime shift_key: bool) type { // Software carryless multiplication of two 64-bit integers using native 128-bit registers. fn clmulSoft128(x_: u128, y_: u128, comptime half: Selector) u128 { - const x = @truncate(u64, if (half == .hi or half == .hi_lo) x_ >> 64 else x_); - const y = @truncate(u64, if (half == .hi) y_ >> 64 else y_); + const x = @as(u64, @truncate(if (half == .hi or half == .hi_lo) x_ >> 64 else x_)); + const y = @as(u64, @truncate(if (half == .hi) y_ >> 64 else y_)); const x0 = x & 0x1111111111111110; const x1 = x & 0x2222222222222220; @@ -216,12 +216,12 @@ fn Hash(comptime endian: std.builtin.Endian, comptime shift_key: bool) type { // Software carryless multiplication of two 128-bit integers using 64-bit registers. fn clmulSoft128_64(x_: u128, y_: u128, comptime half: Selector) u128 { - const a = @truncate(u64, if (half == .hi or half == .hi_lo) x_ >> 64 else x_); - const b = @truncate(u64, if (half == .hi) y_ >> 64 else y_); - const a0 = @truncate(u32, a); - const a1 = @truncate(u32, a >> 32); - const b0 = @truncate(u32, b); - const b1 = @truncate(u32, b >> 32); + const a = @as(u64, @truncate(if (half == .hi or half == .hi_lo) x_ >> 64 else x_)); + const b = @as(u64, @truncate(if (half == .hi) y_ >> 64 else y_)); + const a0 = @as(u32, @truncate(a)); + const a1 = @as(u32, @truncate(a >> 32)); + const b0 = @as(u32, @truncate(b)); + const b1 = @as(u32, @truncate(b >> 32)); const lo = clmulSoft32(a0, b0); const hi = clmulSoft32(a1, b1); const mid = clmulSoft32(a0 ^ a1, b0 ^ b1) ^ lo ^ hi; @@ -256,8 +256,8 @@ fn Hash(comptime endian: std.builtin.Endian, comptime shift_key: bool) type { // Multiply two 128-bit integers in GF(2^128). inline fn clmul128(x: u128, y: u128) I256 { if (mul_algorithm == .karatsuba) { - const x_hi = @truncate(u64, x >> 64); - const y_hi = @truncate(u64, y >> 64); + const x_hi = @as(u64, @truncate(x >> 64)); + const y_hi = @as(u64, @truncate(y >> 64)); const r_lo = clmul(x, y, .lo); const r_hi = clmul(x, y, .hi); const r_mid = clmul(x ^ x_hi, y ^ y_hi, .lo) ^ r_lo ^ r_hi; @@ -407,7 +407,7 @@ fn Hash(comptime endian: std.builtin.Endian, comptime shift_key: bool) type { st.pad(); mem.writeInt(u128, out[0..16], st.acc, endian); - utils.secureZero(u8, @ptrCast([*]u8, st)[0..@sizeOf(Self)]); + utils.secureZero(u8, @as([*]u8, @ptrCast(st))[0..@sizeOf(Self)]); } /// Compute the GHASH of a message. @@ -442,7 +442,7 @@ test "ghash2" { var key: [16]u8 = undefined; var i: usize = 0; while (i < key.len) : (i += 1) { - key[i] = @intCast(u8, i * 15 + 1); + key[i] = @as(u8, @intCast(i * 15 + 1)); } const tvs = [_]struct { len: usize, hash: [:0]const u8 }{ .{ .len = 5263, .hash = "b9395f37c131cd403a327ccf82ec016a" }, @@ -461,7 +461,7 @@ test "ghash2" { var m: [tv.len]u8 = undefined; i = 0; while (i < m.len) : (i += 1) { - m[i] = @truncate(u8, i % 254 + 1); + m[i] = @as(u8, @truncate(i % 254 + 1)); } var st = Ghash.init(&key); st.update(&m); diff --git a/lib/std/crypto/isap.zig b/lib/std/crypto/isap.zig index 5b0da739de..1d17e32be8 100644 --- a/lib/std/crypto/isap.zig +++ b/lib/std/crypto/isap.zig @@ -67,7 +67,7 @@ pub const IsapA128A = struct { var i: usize = 0; while (i < y.len * 8 - 1) : (i += 1) { const cur_byte_pos = i / 8; - const cur_bit_pos = @truncate(u3, 7 - (i % 8)); + const cur_bit_pos = @as(u3, @truncate(7 - (i % 8))); const cur_bit = ((y[cur_byte_pos] >> cur_bit_pos) & 1) << 7; isap.st.addByte(cur_bit, 0); isap.st.permuteR(1); diff --git a/lib/std/crypto/keccak_p.zig b/lib/std/crypto/keccak_p.zig index ddc9b1b847..d8130bc87a 100644 --- a/lib/std/crypto/keccak_p.zig +++ b/lib/std/crypto/keccak_p.zig @@ -33,7 +33,7 @@ pub fn KeccakF(comptime f: u11) type { 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008, }; var rc: [max_rounds]T = undefined; - for (&rc, RC64[0..max_rounds]) |*t, c| t.* = @truncate(T, c); + for (&rc, RC64[0..max_rounds]) |*t, c| t.* = @as(T, @truncate(c)); break :rc rc; }; @@ -75,7 +75,7 @@ pub fn KeccakF(comptime f: u11) type { /// XOR a byte into the state at a given offset. pub fn addByte(self: *Self, byte: u8, offset: usize) void { - const z = @sizeOf(T) * @truncate(math.Log2Int(T), offset % @sizeOf(T)); + const z = @sizeOf(T) * @as(math.Log2Int(T), @truncate(offset % @sizeOf(T))); self.st[offset / @sizeOf(T)] ^= @as(T, byte) << z; } diff --git a/lib/std/crypto/kyber_d00.zig b/lib/std/crypto/kyber_d00.zig index 3cb0f02c0d..390ff8e7f2 100644 --- a/lib/std/crypto/kyber_d00.zig +++ b/lib/std/crypto/kyber_d00.zig @@ -579,7 +579,7 @@ test "invNTTReductions bounds" { if (j < 0) { break; } - xs[@intCast(usize, j)] = 1; + xs[@as(usize, @intCast(j))] = 1; } } } @@ -615,7 +615,7 @@ fn invertMod(a: anytype, p: @TypeOf(a)) @TypeOf(a) { // Reduce mod q for testing. fn modQ32(x: i32) i16 { - var y = @intCast(i16, @rem(x, @as(i32, Q))); + var y = @as(i16, @intCast(@rem(x, @as(i32, Q)))); if (y < 0) { y += Q; } @@ -638,7 +638,7 @@ fn montReduce(x: i32) i16 { // Note that x q' might be as big as 2³² and could overflow the int32 // multiplication in the last line. However for any int32s a and b, // we have int32(int64(a)*int64(b)) = int32(a*b) and so the result is ok. - const m = @truncate(i16, @truncate(i32, x *% qInv)); + const m = @as(i16, @truncate(@as(i32, @truncate(x *% qInv)))); // Note that x - m q is divisible by R; indeed modulo R we have // @@ -652,7 +652,7 @@ fn montReduce(x: i32) i16 { // and as both 2¹⁵ q ≤ m q, x < 2¹⁵ q, we have // 2¹⁶ q ≤ x - m q < 2¹⁶ and so q ≤ (x - m q) / R < q as desired. const yR = x - @as(i32, m) * @as(i32, Q); - return @bitCast(i16, @truncate(u16, @bitCast(u32, yR) >> 16)); + return @as(i16, @bitCast(@as(u16, @truncate(@as(u32, @bitCast(yR)) >> 16)))); } test "Test montReduce" { @@ -676,7 +676,7 @@ fn feToMont(x: i16) i16 { test "Test feToMont" { var x: i32 = -(1 << 15); while (x < 1 << 15) : (x += 1) { - const y = feToMont(@intCast(i16, x)); + const y = feToMont(@as(i16, @intCast(x))); try testing.expectEqual(modQ32(@as(i32, y)), modQ32(x * r_mod_q)); } } @@ -703,14 +703,14 @@ fn feBarrettReduce(x: i16) i16 { // To actually compute this, note that // // ⌊x 20156/2²⁶⌋ = (20159 x) >> 26. - return x -% @intCast(i16, (@as(i32, x) * 20159) >> 26) *% Q; + return x -% @as(i16, @intCast((@as(i32, x) * 20159) >> 26)) *% Q; } test "Test Barrett reduction" { var x: i32 = -(1 << 15); while (x < 1 << 15) : (x += 1) { - var y1 = feBarrettReduce(@intCast(i16, x)); - const y2 = @mod(@intCast(i16, x), Q); + var y1 = feBarrettReduce(@as(i16, @intCast(x))); + const y2 = @mod(@as(i16, @intCast(x)), Q); if (x < 0 and @rem(-x, Q) == 0) { y1 -= Q; } @@ -729,9 +729,9 @@ fn csubq(x: i16) i16 { test "Test csubq" { var x: i32 = -29439; while (x < 1 << 15) : (x += 1) { - const y1 = csubq(@intCast(i16, x)); - var y2 = @intCast(i16, x); - if (@intCast(i16, x) >= Q) { + const y1 = csubq(@as(i16, @intCast(x))); + var y2 = @as(i16, @intCast(x)); + if (@as(i16, @intCast(x)) >= Q) { y2 -= Q; } try testing.expectEqual(y1, y2); @@ -762,7 +762,7 @@ fn computeZetas() [128]i16 { @setEvalBranchQuota(10000); var ret: [128]i16 = undefined; for (&ret, 0..) |*r, i| { - const t = @intCast(i16, mpow(@as(i32, zeta), @bitReverse(@intCast(u7, i)), Q)); + const t = @as(i16, @intCast(mpow(@as(i32, zeta), @bitReverse(@as(u7, @intCast(i))), Q))); r.* = csubq(feBarrettReduce(feToMont(t))); } return ret; @@ -945,7 +945,7 @@ const Poly = struct { if (i < 0) { break; } - p.cs[@intCast(usize, i)] = feBarrettReduce(p.cs[@intCast(usize, i)]); + p.cs[@as(usize, @intCast(i))] = feBarrettReduce(p.cs[@as(usize, @intCast(i))]); } } @@ -1020,8 +1020,8 @@ const Poly = struct { // = ⌊(2ᵈ/q)x+½⌋ mod⁺ 2ᵈ // = ⌊((x << d) + q/2) / q⌋ mod⁺ 2ᵈ // = DIV((x << d) + q/2, q) & ((1<<d) - 1) - const t = @intCast(u32, p.cs[in_off + i]) << d; - in[i] = @intCast(u16, @divFloor(t + q_over_2, Q) & two_d_min_1); + const t = @as(u32, @intCast(p.cs[in_off + i])) << d; + in[i] = @as(u16, @intCast(@divFloor(t + q_over_2, Q) & two_d_min_1)); } // Now we pack the d-bit integers from `in' into out as bytes. @@ -1032,7 +1032,7 @@ const Poly = struct { comptime var todo: usize = 8; inline while (todo > 0) { const out_shift = comptime 8 - todo; - out[out_off + j] |= @truncate(u8, (in[i] >> in_shift) << out_shift); + out[out_off + j] |= @as(u8, @truncate((in[i] >> in_shift) << out_shift)); const done = comptime @min(@min(d, todo), d - in_shift); todo -= done; @@ -1094,7 +1094,7 @@ const Poly = struct { // = ⌊(qx + 2ᵈ⁻¹)/2ᵈ⌋ // = (qx + (1<<(d-1))) >> d const qx = @as(u32, out) * @as(u32, Q); - ret.cs[out_off + i] = @intCast(i16, (qx + (1 << (d - 1))) >> d); + ret.cs[out_off + i] = @as(i16, @intCast((qx + (1 << (d - 1))) >> d)); } in_off += in_batch_size; @@ -1209,8 +1209,8 @@ const Poly = struct { // Extract each a and b separately and set coefficient in polynomial. inline for (0..batch_count) |j| { const mask2 = comptime (1 << eta) - 1; - const a = @intCast(i16, (d >> (comptime (2 * j * eta))) & mask2); - const b = @intCast(i16, (d >> (comptime ((2 * j + 1) * eta))) & mask2); + const a = @as(i16, @intCast((d >> (comptime (2 * j * eta))) & mask2)); + const b = @as(i16, @intCast((d >> (comptime ((2 * j + 1) * eta))) & mask2)); ret.cs[batch_count * i + j] = a - b; } } @@ -1246,7 +1246,7 @@ const Poly = struct { inline for (ts) |t| { if (t < Q) { - ret.cs[i] = @intCast(i16, t); + ret.cs[i] = @as(i16, @intCast(t)); i += 1; if (i == N) { @@ -1266,11 +1266,11 @@ const Poly = struct { fn toBytes(p: Poly) [bytes_length]u8 { var ret: [bytes_length]u8 = undefined; for (0..comptime N / 2) |i| { - const t0 = @intCast(u16, p.cs[2 * i]); - const t1 = @intCast(u16, p.cs[2 * i + 1]); - ret[3 * i] = @truncate(u8, t0); - ret[3 * i + 1] = @truncate(u8, (t0 >> 8) | (t1 << 4)); - ret[3 * i + 2] = @truncate(u8, t1 >> 4); + const t0 = @as(u16, @intCast(p.cs[2 * i])); + const t1 = @as(u16, @intCast(p.cs[2 * i + 1])); + ret[3 * i] = @as(u8, @truncate(t0)); + ret[3 * i + 1] = @as(u8, @truncate((t0 >> 8) | (t1 << 4))); + ret[3 * i + 2] = @as(u8, @truncate(t1 >> 4)); } return ret; } @@ -1356,7 +1356,7 @@ fn Vec(comptime K: u8) type { fn noise(comptime eta: u8, nonce: u8, seed: *const [32]u8) Self { var ret: Self = undefined; for (0..K) |i| { - ret.ps[i] = Poly.noise(eta, nonce + @intCast(u8, i), seed); + ret.ps[i] = Poly.noise(eta, nonce + @as(u8, @intCast(i)), seed); } return ret; } @@ -1534,7 +1534,7 @@ test "Compression" { test "noise" { var seed: [32]u8 = undefined; for (&seed, 0..) |*s, i| { - s.* = @intCast(u8, i); + s.* = @as(u8, @intCast(i)); } try testing.expectEqual(Poly.noise(3, 37, &seed).cs, .{ 0, 0, 1, -1, 0, 2, 0, -1, -1, 3, 0, 1, -2, -2, 0, 1, -2, @@ -1580,7 +1580,7 @@ test "noise" { test "uniform sampling" { var seed: [32]u8 = undefined; for (&seed, 0..) |*s, i| { - s.* = @intCast(u8, i); + s.* = @as(u8, @intCast(i)); } try testing.expectEqual(Poly.uniform(seed, 1, 0).cs, .{ 797, 993, 161, 6, 2608, 2385, 2096, 2661, 1676, 247, 2440, @@ -1623,17 +1623,17 @@ test "Test inner PKE" { var seed: [32]u8 = undefined; var pt: [32]u8 = undefined; for (&seed, &pt, 0..) |*s, *p, i| { - s.* = @intCast(u8, i); - p.* = @intCast(u8, i + 32); + s.* = @as(u8, @intCast(i)); + p.* = @as(u8, @intCast(i + 32)); } inline for (modes) |mode| { for (0..100) |i| { var pk: mode.InnerPk = undefined; var sk: mode.InnerSk = undefined; - seed[0] = @intCast(u8, i); + seed[0] = @as(u8, @intCast(i)); mode.innerKeyFromSeed(seed, &pk, &sk); for (0..10) |j| { - seed[1] = @intCast(u8, j); + seed[1] = @as(u8, @intCast(j)); try testing.expectEqual(sk.decrypt(&pk.encrypt(&pt, &seed)), pt); } } @@ -1643,18 +1643,18 @@ test "Test inner PKE" { test "Test happy flow" { var seed: [64]u8 = undefined; for (&seed, 0..) |*s, i| { - s.* = @intCast(u8, i); + s.* = @as(u8, @intCast(i)); } inline for (modes) |mode| { for (0..100) |i| { - seed[0] = @intCast(u8, i); + seed[0] = @as(u8, @intCast(i)); const kp = try mode.KeyPair.create(seed); const sk = try mode.SecretKey.fromBytes(&kp.secret_key.toBytes()); try testing.expectEqual(sk, kp.secret_key); const pk = try mode.PublicKey.fromBytes(&kp.public_key.toBytes()); try testing.expectEqual(pk, kp.public_key); for (0..10) |j| { - seed[1] = @intCast(u8, j); + seed[1] = @as(u8, @intCast(j)); const e = pk.encaps(seed[0..32].*); try testing.expectEqual(e.shared_secret, try sk.decaps(&e.ciphertext)); } @@ -1675,7 +1675,7 @@ test "NIST KAT test" { const mode = modeHash[0]; var seed: [48]u8 = undefined; for (&seed, 0..) |*s, i| { - s.* = @intCast(u8, i); + s.* = @as(u8, @intCast(i)); } var f = sha2.Sha256.init(.{}); const fw = f.writer(); diff --git a/lib/std/crypto/md5.zig b/lib/std/crypto/md5.zig index bd4a78c032..b480cbcd8e 100644 --- a/lib/std/crypto/md5.zig +++ b/lib/std/crypto/md5.zig @@ -80,7 +80,7 @@ pub const Md5 = struct { // Copy any remainder for next pass. const b_slice = b[off..]; @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); - d.buf_len += @intCast(u8, b_slice.len); + d.buf_len += @as(u8, @intCast(b_slice.len)); // Md5 uses the bottom 64-bits for length padding d.total_len +%= b.len; @@ -103,9 +103,9 @@ pub const Md5 = struct { // Append message length. var i: usize = 1; var len = d.total_len >> 5; - d.buf[56] = @intCast(u8, d.total_len & 0x1f) << 3; + d.buf[56] = @as(u8, @intCast(d.total_len & 0x1f)) << 3; while (i < 8) : (i += 1) { - d.buf[56 + i] = @intCast(u8, len & 0xff); + d.buf[56 + i] = @as(u8, @intCast(len & 0xff)); len >>= 8; } diff --git a/lib/std/crypto/pbkdf2.zig b/lib/std/crypto/pbkdf2.zig index 115fd38b3d..2e0318369b 100644 --- a/lib/std/crypto/pbkdf2.zig +++ b/lib/std/crypto/pbkdf2.zig @@ -74,7 +74,7 @@ pub fn pbkdf2(dk: []u8, password: []const u8, salt: []const u8, rounds: u32, com // block // - const blocks_count = @intCast(u32, std.math.divCeil(usize, dk_len, h_len) catch unreachable); + const blocks_count = @as(u32, @intCast(std.math.divCeil(usize, dk_len, h_len) catch unreachable)); var r = dk_len % h_len; if (r == 0) { r = h_len; diff --git a/lib/std/crypto/pcurves/common.zig b/lib/std/crypto/pcurves/common.zig index 5d41bc190a..edc437517c 100644 --- a/lib/std/crypto/pcurves/common.zig +++ b/lib/std/crypto/pcurves/common.zig @@ -120,7 +120,7 @@ pub fn Field(comptime params: FieldParams) type { /// Return true if the element is odd. pub fn isOdd(fe: Fe) bool { const s = fe.toBytes(.Little); - return @truncate(u1, s[0]) != 0; + return @as(u1, @truncate(s[0])) != 0; } /// Conditonally replace a field element with `a` if `c` is positive. @@ -179,7 +179,7 @@ pub fn Field(comptime params: FieldParams) type { var x: T = n; var t = a; while (true) { - if (@truncate(u1, x) != 0) fe = fe.mul(t); + if (@as(u1, @truncate(x)) != 0) fe = fe.mul(t); x >>= 1; if (x == 0) break; t = t.sq(); @@ -233,7 +233,7 @@ pub fn Field(comptime params: FieldParams) type { } var v_opp: Limbs = undefined; fiat.opp(&v_opp, v); - fiat.selectznz(&v, @truncate(u1, f[f.len - 1] >> (@bitSizeOf(Word) - 1)), v, v_opp); + fiat.selectznz(&v, @as(u1, @truncate(f[f.len - 1] >> (@bitSizeOf(Word) - 1))), v, v_opp); const precomp = blk: { var precomp: Limbs = undefined; diff --git a/lib/std/crypto/pcurves/p256.zig b/lib/std/crypto/pcurves/p256.zig index a797fbce3e..668c0115b2 100644 --- a/lib/std/crypto/pcurves/p256.zig +++ b/lib/std/crypto/pcurves/p256.zig @@ -318,7 +318,7 @@ pub const P256 = struct { var t = P256.identityElement; comptime var i: u8 = 1; inline while (i < pc.len) : (i += 1) { - t.cMov(pc[i], @truncate(u1, (@as(usize, b ^ i) -% 1) >> 8)); + t.cMov(pc[i], @as(u1, @truncate((@as(usize, b ^ i) -% 1) >> 8))); } return t; } @@ -326,8 +326,8 @@ pub const P256 = struct { fn slide(s: [32]u8) [2 * 32 + 1]i8 { var e: [2 * 32 + 1]i8 = undefined; for (s, 0..) |x, i| { - e[i * 2 + 0] = @as(i8, @truncate(u4, x)); - e[i * 2 + 1] = @as(i8, @truncate(u4, x >> 4)); + e[i * 2 + 0] = @as(i8, @as(u4, @truncate(x))); + e[i * 2 + 1] = @as(i8, @as(u4, @truncate(x >> 4))); } // Now, e[0..63] is between 0 and 15, e[63] is between 0 and 7 var carry: i8 = 0; @@ -351,9 +351,9 @@ pub const P256 = struct { while (true) : (pos -= 1) { const slot = e[pos]; if (slot > 0) { - q = q.add(pc[@intCast(usize, slot)]); + q = q.add(pc[@as(usize, @intCast(slot))]); } else if (slot < 0) { - q = q.sub(pc[@intCast(usize, -slot)]); + q = q.sub(pc[@as(usize, @intCast(-slot))]); } if (pos == 0) break; q = q.dbl().dbl().dbl().dbl(); @@ -366,7 +366,7 @@ pub const P256 = struct { var q = P256.identityElement; var pos: usize = 252; while (true) : (pos -= 4) { - const slot = @truncate(u4, (s[pos >> 3] >> @truncate(u3, pos))); + const slot = @as(u4, @truncate((s[pos >> 3] >> @as(u3, @truncate(pos))))); if (vartime) { if (slot != 0) { q = q.add(pc[slot]); @@ -445,15 +445,15 @@ pub const P256 = struct { while (true) : (pos -= 1) { const slot1 = e1[pos]; if (slot1 > 0) { - q = q.add(pc1[@intCast(usize, slot1)]); + q = q.add(pc1[@as(usize, @intCast(slot1))]); } else if (slot1 < 0) { - q = q.sub(pc1[@intCast(usize, -slot1)]); + q = q.sub(pc1[@as(usize, @intCast(-slot1))]); } const slot2 = e2[pos]; if (slot2 > 0) { - q = q.add(pc2[@intCast(usize, slot2)]); + q = q.add(pc2[@as(usize, @intCast(slot2))]); } else if (slot2 < 0) { - q = q.sub(pc2[@intCast(usize, -slot2)]); + q = q.sub(pc2[@as(usize, @intCast(-slot2))]); } if (pos == 0) break; q = q.dbl().dbl().dbl().dbl(); diff --git a/lib/std/crypto/pcurves/p256/p256_64.zig b/lib/std/crypto/pcurves/p256/p256_64.zig index e8ba37e845..e8dbaead33 100644 --- a/lib/std/crypto/pcurves/p256/p256_64.zig +++ b/lib/std/crypto/pcurves/p256/p256_64.zig @@ -119,8 +119,8 @@ inline fn mulxU64(out1: *u64, out2: *u64, arg1: u64, arg2: u64) void { @setRuntimeSafety(mode == .Debug); const x = @as(u128, arg1) * @as(u128, arg2); - out1.* = @truncate(u64, x); - out2.* = @truncate(u64, x >> 64); + out1.* = @as(u64, @truncate(x)); + out2.* = @as(u64, @truncate(x >> 64)); } /// The function cmovznzU64 is a single-word conditional move. @@ -1355,62 +1355,62 @@ pub fn toBytes(out1: *[32]u8, arg1: [4]u64) void { const x2 = (arg1[2]); const x3 = (arg1[1]); const x4 = (arg1[0]); - const x5 = @truncate(u8, (x4 & @as(u64, 0xff))); + const x5 = @as(u8, @truncate((x4 & @as(u64, 0xff)))); const x6 = (x4 >> 8); - const x7 = @truncate(u8, (x6 & @as(u64, 0xff))); + const x7 = @as(u8, @truncate((x6 & @as(u64, 0xff)))); const x8 = (x6 >> 8); - const x9 = @truncate(u8, (x8 & @as(u64, 0xff))); + const x9 = @as(u8, @truncate((x8 & @as(u64, 0xff)))); const x10 = (x8 >> 8); - const x11 = @truncate(u8, (x10 & @as(u64, 0xff))); + const x11 = @as(u8, @truncate((x10 & @as(u64, 0xff)))); const x12 = (x10 >> 8); - const x13 = @truncate(u8, (x12 & @as(u64, 0xff))); + const x13 = @as(u8, @truncate((x12 & @as(u64, 0xff)))); const x14 = (x12 >> 8); - const x15 = @truncate(u8, (x14 & @as(u64, 0xff))); + const x15 = @as(u8, @truncate((x14 & @as(u64, 0xff)))); const x16 = (x14 >> 8); - const x17 = @truncate(u8, (x16 & @as(u64, 0xff))); - const x18 = @truncate(u8, (x16 >> 8)); - const x19 = @truncate(u8, (x3 & @as(u64, 0xff))); + const x17 = @as(u8, @truncate((x16 & @as(u64, 0xff)))); + const x18 = @as(u8, @truncate((x16 >> 8))); + const x19 = @as(u8, @truncate((x3 & @as(u64, 0xff)))); const x20 = (x3 >> 8); - const x21 = @truncate(u8, (x20 & @as(u64, 0xff))); + const x21 = @as(u8, @truncate((x20 & @as(u64, 0xff)))); const x22 = (x20 >> 8); - const x23 = @truncate(u8, (x22 & @as(u64, 0xff))); + const x23 = @as(u8, @truncate((x22 & @as(u64, 0xff)))); const x24 = (x22 >> 8); - const x25 = @truncate(u8, (x24 & @as(u64, 0xff))); + const x25 = @as(u8, @truncate((x24 & @as(u64, 0xff)))); const x26 = (x24 >> 8); - const x27 = @truncate(u8, (x26 & @as(u64, 0xff))); + const x27 = @as(u8, @truncate((x26 & @as(u64, 0xff)))); const x28 = (x26 >> 8); - const x29 = @truncate(u8, (x28 & @as(u64, 0xff))); + const x29 = @as(u8, @truncate((x28 & @as(u64, 0xff)))); const x30 = (x28 >> 8); - const x31 = @truncate(u8, (x30 & @as(u64, 0xff))); - const x32 = @truncate(u8, (x30 >> 8)); - const x33 = @truncate(u8, (x2 & @as(u64, 0xff))); + const x31 = @as(u8, @truncate((x30 & @as(u64, 0xff)))); + const x32 = @as(u8, @truncate((x30 >> 8))); + const x33 = @as(u8, @truncate((x2 & @as(u64, 0xff)))); const x34 = (x2 >> 8); - const x35 = @truncate(u8, (x34 & @as(u64, 0xff))); + const x35 = @as(u8, @truncate((x34 & @as(u64, 0xff)))); const x36 = (x34 >> 8); - const x37 = @truncate(u8, (x36 & @as(u64, 0xff))); + const x37 = @as(u8, @truncate((x36 & @as(u64, 0xff)))); const x38 = (x36 >> 8); - const x39 = @truncate(u8, (x38 & @as(u64, 0xff))); + const x39 = @as(u8, @truncate((x38 & @as(u64, 0xff)))); const x40 = (x38 >> 8); - const x41 = @truncate(u8, (x40 & @as(u64, 0xff))); + const x41 = @as(u8, @truncate((x40 & @as(u64, 0xff)))); const x42 = (x40 >> 8); - const x43 = @truncate(u8, (x42 & @as(u64, 0xff))); + const x43 = @as(u8, @truncate((x42 & @as(u64, 0xff)))); const x44 = (x42 >> 8); - const x45 = @truncate(u8, (x44 & @as(u64, 0xff))); - const x46 = @truncate(u8, (x44 >> 8)); - const x47 = @truncate(u8, (x1 & @as(u64, 0xff))); + const x45 = @as(u8, @truncate((x44 & @as(u64, 0xff)))); + const x46 = @as(u8, @truncate((x44 >> 8))); + const x47 = @as(u8, @truncate((x1 & @as(u64, 0xff)))); const x48 = (x1 >> 8); - const x49 = @truncate(u8, (x48 & @as(u64, 0xff))); + const x49 = @as(u8, @truncate((x48 & @as(u64, 0xff)))); const x50 = (x48 >> 8); - const x51 = @truncate(u8, (x50 & @as(u64, 0xff))); + const x51 = @as(u8, @truncate((x50 & @as(u64, 0xff)))); const x52 = (x50 >> 8); - const x53 = @truncate(u8, (x52 & @as(u64, 0xff))); + const x53 = @as(u8, @truncate((x52 & @as(u64, 0xff)))); const x54 = (x52 >> 8); - const x55 = @truncate(u8, (x54 & @as(u64, 0xff))); + const x55 = @as(u8, @truncate((x54 & @as(u64, 0xff)))); const x56 = (x54 >> 8); - const x57 = @truncate(u8, (x56 & @as(u64, 0xff))); + const x57 = @as(u8, @truncate((x56 & @as(u64, 0xff)))); const x58 = (x56 >> 8); - const x59 = @truncate(u8, (x58 & @as(u64, 0xff))); - const x60 = @truncate(u8, (x58 >> 8)); + const x59 = @as(u8, @truncate((x58 & @as(u64, 0xff)))); + const x60 = @as(u8, @truncate((x58 >> 8))); out1[0] = x5; out1[1] = x7; out1[2] = x9; @@ -1593,7 +1593,7 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ var x1: u64 = undefined; var x2: u1 = undefined; addcarryxU64(&x1, &x2, 0x0, (~arg1), @as(u64, 0x1)); - const x3 = (@truncate(u1, (x1 >> 63)) & @truncate(u1, ((arg3[0]) & @as(u64, 0x1)))); + const x3 = (@as(u1, @truncate((x1 >> 63))) & @as(u1, @truncate(((arg3[0]) & @as(u64, 0x1))))); var x4: u64 = undefined; var x5: u1 = undefined; addcarryxU64(&x4, &x5, 0x0, (~arg1), @as(u64, 0x1)); @@ -1707,7 +1707,7 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ cmovznzU64(&x72, x3, (arg5[2]), x66); var x73: u64 = undefined; cmovznzU64(&x73, x3, (arg5[3]), x68); - const x74 = @truncate(u1, (x22 & @as(u64, 0x1))); + const x74 = @as(u1, @truncate((x22 & @as(u64, 0x1)))); var x75: u64 = undefined; cmovznzU64(&x75, x74, @as(u64, 0x0), x7); var x76: u64 = undefined; diff --git a/lib/std/crypto/pcurves/p256/p256_scalar_64.zig b/lib/std/crypto/pcurves/p256/p256_scalar_64.zig index ea102360cf..152c2b8787 100644 --- a/lib/std/crypto/pcurves/p256/p256_scalar_64.zig +++ b/lib/std/crypto/pcurves/p256/p256_scalar_64.zig @@ -119,8 +119,8 @@ inline fn mulxU64(out1: *u64, out2: *u64, arg1: u64, arg2: u64) void { @setRuntimeSafety(mode == .Debug); const x = @as(u128, arg1) * @as(u128, arg2); - out1.* = @truncate(u64, x); - out2.* = @truncate(u64, x >> 64); + out1.* = @as(u64, @truncate(x)); + out2.* = @as(u64, @truncate(x >> 64)); } /// The function cmovznzU64 is a single-word conditional move. @@ -1559,62 +1559,62 @@ pub fn toBytes(out1: *[32]u8, arg1: [4]u64) void { const x2 = (arg1[2]); const x3 = (arg1[1]); const x4 = (arg1[0]); - const x5 = @truncate(u8, (x4 & @as(u64, 0xff))); + const x5 = @as(u8, @truncate((x4 & @as(u64, 0xff)))); const x6 = (x4 >> 8); - const x7 = @truncate(u8, (x6 & @as(u64, 0xff))); + const x7 = @as(u8, @truncate((x6 & @as(u64, 0xff)))); const x8 = (x6 >> 8); - const x9 = @truncate(u8, (x8 & @as(u64, 0xff))); + const x9 = @as(u8, @truncate((x8 & @as(u64, 0xff)))); const x10 = (x8 >> 8); - const x11 = @truncate(u8, (x10 & @as(u64, 0xff))); + const x11 = @as(u8, @truncate((x10 & @as(u64, 0xff)))); const x12 = (x10 >> 8); - const x13 = @truncate(u8, (x12 & @as(u64, 0xff))); + const x13 = @as(u8, @truncate((x12 & @as(u64, 0xff)))); const x14 = (x12 >> 8); - const x15 = @truncate(u8, (x14 & @as(u64, 0xff))); + const x15 = @as(u8, @truncate((x14 & @as(u64, 0xff)))); const x16 = (x14 >> 8); - const x17 = @truncate(u8, (x16 & @as(u64, 0xff))); - const x18 = @truncate(u8, (x16 >> 8)); - const x19 = @truncate(u8, (x3 & @as(u64, 0xff))); + const x17 = @as(u8, @truncate((x16 & @as(u64, 0xff)))); + const x18 = @as(u8, @truncate((x16 >> 8))); + const x19 = @as(u8, @truncate((x3 & @as(u64, 0xff)))); const x20 = (x3 >> 8); - const x21 = @truncate(u8, (x20 & @as(u64, 0xff))); + const x21 = @as(u8, @truncate((x20 & @as(u64, 0xff)))); const x22 = (x20 >> 8); - const x23 = @truncate(u8, (x22 & @as(u64, 0xff))); + const x23 = @as(u8, @truncate((x22 & @as(u64, 0xff)))); const x24 = (x22 >> 8); - const x25 = @truncate(u8, (x24 & @as(u64, 0xff))); + const x25 = @as(u8, @truncate((x24 & @as(u64, 0xff)))); const x26 = (x24 >> 8); - const x27 = @truncate(u8, (x26 & @as(u64, 0xff))); + const x27 = @as(u8, @truncate((x26 & @as(u64, 0xff)))); const x28 = (x26 >> 8); - const x29 = @truncate(u8, (x28 & @as(u64, 0xff))); + const x29 = @as(u8, @truncate((x28 & @as(u64, 0xff)))); const x30 = (x28 >> 8); - const x31 = @truncate(u8, (x30 & @as(u64, 0xff))); - const x32 = @truncate(u8, (x30 >> 8)); - const x33 = @truncate(u8, (x2 & @as(u64, 0xff))); + const x31 = @as(u8, @truncate((x30 & @as(u64, 0xff)))); + const x32 = @as(u8, @truncate((x30 >> 8))); + const x33 = @as(u8, @truncate((x2 & @as(u64, 0xff)))); const x34 = (x2 >> 8); - const x35 = @truncate(u8, (x34 & @as(u64, 0xff))); + const x35 = @as(u8, @truncate((x34 & @as(u64, 0xff)))); const x36 = (x34 >> 8); - const x37 = @truncate(u8, (x36 & @as(u64, 0xff))); + const x37 = @as(u8, @truncate((x36 & @as(u64, 0xff)))); const x38 = (x36 >> 8); - const x39 = @truncate(u8, (x38 & @as(u64, 0xff))); + const x39 = @as(u8, @truncate((x38 & @as(u64, 0xff)))); const x40 = (x38 >> 8); - const x41 = @truncate(u8, (x40 & @as(u64, 0xff))); + const x41 = @as(u8, @truncate((x40 & @as(u64, 0xff)))); const x42 = (x40 >> 8); - const x43 = @truncate(u8, (x42 & @as(u64, 0xff))); + const x43 = @as(u8, @truncate((x42 & @as(u64, 0xff)))); const x44 = (x42 >> 8); - const x45 = @truncate(u8, (x44 & @as(u64, 0xff))); - const x46 = @truncate(u8, (x44 >> 8)); - const x47 = @truncate(u8, (x1 & @as(u64, 0xff))); + const x45 = @as(u8, @truncate((x44 & @as(u64, 0xff)))); + const x46 = @as(u8, @truncate((x44 >> 8))); + const x47 = @as(u8, @truncate((x1 & @as(u64, 0xff)))); const x48 = (x1 >> 8); - const x49 = @truncate(u8, (x48 & @as(u64, 0xff))); + const x49 = @as(u8, @truncate((x48 & @as(u64, 0xff)))); const x50 = (x48 >> 8); - const x51 = @truncate(u8, (x50 & @as(u64, 0xff))); + const x51 = @as(u8, @truncate((x50 & @as(u64, 0xff)))); const x52 = (x50 >> 8); - const x53 = @truncate(u8, (x52 & @as(u64, 0xff))); + const x53 = @as(u8, @truncate((x52 & @as(u64, 0xff)))); const x54 = (x52 >> 8); - const x55 = @truncate(u8, (x54 & @as(u64, 0xff))); + const x55 = @as(u8, @truncate((x54 & @as(u64, 0xff)))); const x56 = (x54 >> 8); - const x57 = @truncate(u8, (x56 & @as(u64, 0xff))); + const x57 = @as(u8, @truncate((x56 & @as(u64, 0xff)))); const x58 = (x56 >> 8); - const x59 = @truncate(u8, (x58 & @as(u64, 0xff))); - const x60 = @truncate(u8, (x58 >> 8)); + const x59 = @as(u8, @truncate((x58 & @as(u64, 0xff)))); + const x60 = @as(u8, @truncate((x58 >> 8))); out1[0] = x5; out1[1] = x7; out1[2] = x9; @@ -1797,7 +1797,7 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ var x1: u64 = undefined; var x2: u1 = undefined; addcarryxU64(&x1, &x2, 0x0, (~arg1), @as(u64, 0x1)); - const x3 = @truncate(u1, (x1 >> 63)) & @truncate(u1, ((arg3[0]) & @as(u64, 0x1))); + const x3 = @as(u1, @truncate((x1 >> 63))) & @as(u1, @truncate(((arg3[0]) & @as(u64, 0x1)))); var x4: u64 = undefined; var x5: u1 = undefined; addcarryxU64(&x4, &x5, 0x0, (~arg1), @as(u64, 0x1)); @@ -1911,7 +1911,7 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ cmovznzU64(&x72, x3, (arg5[2]), x66); var x73: u64 = undefined; cmovznzU64(&x73, x3, (arg5[3]), x68); - const x74 = @truncate(u1, (x22 & @as(u64, 0x1))); + const x74 = @as(u1, @truncate((x22 & @as(u64, 0x1)))); var x75: u64 = undefined; cmovznzU64(&x75, x74, @as(u64, 0x0), x7); var x76: u64 = undefined; diff --git a/lib/std/crypto/pcurves/p384.zig b/lib/std/crypto/pcurves/p384.zig index 3d96592f50..d5afd6eb4d 100644 --- a/lib/std/crypto/pcurves/p384.zig +++ b/lib/std/crypto/pcurves/p384.zig @@ -318,7 +318,7 @@ pub const P384 = struct { var t = P384.identityElement; comptime var i: u8 = 1; inline while (i < pc.len) : (i += 1) { - t.cMov(pc[i], @truncate(u1, (@as(usize, b ^ i) -% 1) >> 8)); + t.cMov(pc[i], @as(u1, @truncate((@as(usize, b ^ i) -% 1) >> 8))); } return t; } @@ -326,8 +326,8 @@ pub const P384 = struct { fn slide(s: [48]u8) [2 * 48 + 1]i8 { var e: [2 * 48 + 1]i8 = undefined; for (s, 0..) |x, i| { - e[i * 2 + 0] = @as(i8, @truncate(u4, x)); - e[i * 2 + 1] = @as(i8, @truncate(u4, x >> 4)); + e[i * 2 + 0] = @as(i8, @as(u4, @truncate(x))); + e[i * 2 + 1] = @as(i8, @as(u4, @truncate(x >> 4))); } // Now, e[0..63] is between 0 and 15, e[63] is between 0 and 7 var carry: i8 = 0; @@ -351,9 +351,9 @@ pub const P384 = struct { while (true) : (pos -= 1) { const slot = e[pos]; if (slot > 0) { - q = q.add(pc[@intCast(usize, slot)]); + q = q.add(pc[@as(usize, @intCast(slot))]); } else if (slot < 0) { - q = q.sub(pc[@intCast(usize, -slot)]); + q = q.sub(pc[@as(usize, @intCast(-slot))]); } if (pos == 0) break; q = q.dbl().dbl().dbl().dbl(); @@ -366,7 +366,7 @@ pub const P384 = struct { var q = P384.identityElement; var pos: usize = 380; while (true) : (pos -= 4) { - const slot = @truncate(u4, (s[pos >> 3] >> @truncate(u3, pos))); + const slot = @as(u4, @truncate((s[pos >> 3] >> @as(u3, @truncate(pos))))); if (vartime) { if (slot != 0) { q = q.add(pc[slot]); @@ -445,15 +445,15 @@ pub const P384 = struct { while (true) : (pos -= 1) { const slot1 = e1[pos]; if (slot1 > 0) { - q = q.add(pc1[@intCast(usize, slot1)]); + q = q.add(pc1[@as(usize, @intCast(slot1))]); } else if (slot1 < 0) { - q = q.sub(pc1[@intCast(usize, -slot1)]); + q = q.sub(pc1[@as(usize, @intCast(-slot1))]); } const slot2 = e2[pos]; if (slot2 > 0) { - q = q.add(pc2[@intCast(usize, slot2)]); + q = q.add(pc2[@as(usize, @intCast(slot2))]); } else if (slot2 < 0) { - q = q.sub(pc2[@intCast(usize, -slot2)]); + q = q.sub(pc2[@as(usize, @intCast(-slot2))]); } if (pos == 0) break; q = q.dbl().dbl().dbl().dbl(); diff --git a/lib/std/crypto/pcurves/p384/p384_64.zig b/lib/std/crypto/pcurves/p384/p384_64.zig index 45c12835b3..f25a7d65b5 100644 --- a/lib/std/crypto/pcurves/p384/p384_64.zig +++ b/lib/std/crypto/pcurves/p384/p384_64.zig @@ -88,8 +88,8 @@ inline fn mulxU64(out1: *u64, out2: *u64, arg1: u64, arg2: u64) void { @setRuntimeSafety(mode == .Debug); const x = @as(u128, arg1) * @as(u128, arg2); - out1.* = @truncate(u64, x); - out2.* = @truncate(u64, x >> 64); + out1.* = @as(u64, @truncate(x)); + out2.* = @as(u64, @truncate(x >> 64)); } /// The function cmovznzU64 is a single-word conditional move. @@ -2928,90 +2928,90 @@ pub fn toBytes(out1: *[48]u8, arg1: [6]u64) void { const x4 = (arg1[2]); const x5 = (arg1[1]); const x6 = (arg1[0]); - const x7 = @truncate(u8, (x6 & 0xff)); + const x7 = @as(u8, @truncate((x6 & 0xff))); const x8 = (x6 >> 8); - const x9 = @truncate(u8, (x8 & 0xff)); + const x9 = @as(u8, @truncate((x8 & 0xff))); const x10 = (x8 >> 8); - const x11 = @truncate(u8, (x10 & 0xff)); + const x11 = @as(u8, @truncate((x10 & 0xff))); const x12 = (x10 >> 8); - const x13 = @truncate(u8, (x12 & 0xff)); + const x13 = @as(u8, @truncate((x12 & 0xff))); const x14 = (x12 >> 8); - const x15 = @truncate(u8, (x14 & 0xff)); + const x15 = @as(u8, @truncate((x14 & 0xff))); const x16 = (x14 >> 8); - const x17 = @truncate(u8, (x16 & 0xff)); + const x17 = @as(u8, @truncate((x16 & 0xff))); const x18 = (x16 >> 8); - const x19 = @truncate(u8, (x18 & 0xff)); - const x20 = @truncate(u8, (x18 >> 8)); - const x21 = @truncate(u8, (x5 & 0xff)); + const x19 = @as(u8, @truncate((x18 & 0xff))); + const x20 = @as(u8, @truncate((x18 >> 8))); + const x21 = @as(u8, @truncate((x5 & 0xff))); const x22 = (x5 >> 8); - const x23 = @truncate(u8, (x22 & 0xff)); + const x23 = @as(u8, @truncate((x22 & 0xff))); const x24 = (x22 >> 8); - const x25 = @truncate(u8, (x24 & 0xff)); + const x25 = @as(u8, @truncate((x24 & 0xff))); const x26 = (x24 >> 8); - const x27 = @truncate(u8, (x26 & 0xff)); + const x27 = @as(u8, @truncate((x26 & 0xff))); const x28 = (x26 >> 8); - const x29 = @truncate(u8, (x28 & 0xff)); + const x29 = @as(u8, @truncate((x28 & 0xff))); const x30 = (x28 >> 8); - const x31 = @truncate(u8, (x30 & 0xff)); + const x31 = @as(u8, @truncate((x30 & 0xff))); const x32 = (x30 >> 8); - const x33 = @truncate(u8, (x32 & 0xff)); - const x34 = @truncate(u8, (x32 >> 8)); - const x35 = @truncate(u8, (x4 & 0xff)); + const x33 = @as(u8, @truncate((x32 & 0xff))); + const x34 = @as(u8, @truncate((x32 >> 8))); + const x35 = @as(u8, @truncate((x4 & 0xff))); const x36 = (x4 >> 8); - const x37 = @truncate(u8, (x36 & 0xff)); + const x37 = @as(u8, @truncate((x36 & 0xff))); const x38 = (x36 >> 8); - const x39 = @truncate(u8, (x38 & 0xff)); + const x39 = @as(u8, @truncate((x38 & 0xff))); const x40 = (x38 >> 8); - const x41 = @truncate(u8, (x40 & 0xff)); + const x41 = @as(u8, @truncate((x40 & 0xff))); const x42 = (x40 >> 8); - const x43 = @truncate(u8, (x42 & 0xff)); + const x43 = @as(u8, @truncate((x42 & 0xff))); const x44 = (x42 >> 8); - const x45 = @truncate(u8, (x44 & 0xff)); + const x45 = @as(u8, @truncate((x44 & 0xff))); const x46 = (x44 >> 8); - const x47 = @truncate(u8, (x46 & 0xff)); - const x48 = @truncate(u8, (x46 >> 8)); - const x49 = @truncate(u8, (x3 & 0xff)); + const x47 = @as(u8, @truncate((x46 & 0xff))); + const x48 = @as(u8, @truncate((x46 >> 8))); + const x49 = @as(u8, @truncate((x3 & 0xff))); const x50 = (x3 >> 8); - const x51 = @truncate(u8, (x50 & 0xff)); + const x51 = @as(u8, @truncate((x50 & 0xff))); const x52 = (x50 >> 8); - const x53 = @truncate(u8, (x52 & 0xff)); + const x53 = @as(u8, @truncate((x52 & 0xff))); const x54 = (x52 >> 8); - const x55 = @truncate(u8, (x54 & 0xff)); + const x55 = @as(u8, @truncate((x54 & 0xff))); const x56 = (x54 >> 8); - const x57 = @truncate(u8, (x56 & 0xff)); + const x57 = @as(u8, @truncate((x56 & 0xff))); const x58 = (x56 >> 8); - const x59 = @truncate(u8, (x58 & 0xff)); + const x59 = @as(u8, @truncate((x58 & 0xff))); const x60 = (x58 >> 8); - const x61 = @truncate(u8, (x60 & 0xff)); - const x62 = @truncate(u8, (x60 >> 8)); - const x63 = @truncate(u8, (x2 & 0xff)); + const x61 = @as(u8, @truncate((x60 & 0xff))); + const x62 = @as(u8, @truncate((x60 >> 8))); + const x63 = @as(u8, @truncate((x2 & 0xff))); const x64 = (x2 >> 8); - const x65 = @truncate(u8, (x64 & 0xff)); + const x65 = @as(u8, @truncate((x64 & 0xff))); const x66 = (x64 >> 8); - const x67 = @truncate(u8, (x66 & 0xff)); + const x67 = @as(u8, @truncate((x66 & 0xff))); const x68 = (x66 >> 8); - const x69 = @truncate(u8, (x68 & 0xff)); + const x69 = @as(u8, @truncate((x68 & 0xff))); const x70 = (x68 >> 8); - const x71 = @truncate(u8, (x70 & 0xff)); + const x71 = @as(u8, @truncate((x70 & 0xff))); const x72 = (x70 >> 8); - const x73 = @truncate(u8, (x72 & 0xff)); + const x73 = @as(u8, @truncate((x72 & 0xff))); const x74 = (x72 >> 8); - const x75 = @truncate(u8, (x74 & 0xff)); - const x76 = @truncate(u8, (x74 >> 8)); - const x77 = @truncate(u8, (x1 & 0xff)); + const x75 = @as(u8, @truncate((x74 & 0xff))); + const x76 = @as(u8, @truncate((x74 >> 8))); + const x77 = @as(u8, @truncate((x1 & 0xff))); const x78 = (x1 >> 8); - const x79 = @truncate(u8, (x78 & 0xff)); + const x79 = @as(u8, @truncate((x78 & 0xff))); const x80 = (x78 >> 8); - const x81 = @truncate(u8, (x80 & 0xff)); + const x81 = @as(u8, @truncate((x80 & 0xff))); const x82 = (x80 >> 8); - const x83 = @truncate(u8, (x82 & 0xff)); + const x83 = @as(u8, @truncate((x82 & 0xff))); const x84 = (x82 >> 8); - const x85 = @truncate(u8, (x84 & 0xff)); + const x85 = @as(u8, @truncate((x84 & 0xff))); const x86 = (x84 >> 8); - const x87 = @truncate(u8, (x86 & 0xff)); + const x87 = @as(u8, @truncate((x86 & 0xff))); const x88 = (x86 >> 8); - const x89 = @truncate(u8, (x88 & 0xff)); - const x90 = @truncate(u8, (x88 >> 8)); + const x89 = @as(u8, @truncate((x88 & 0xff))); + const x90 = @as(u8, @truncate((x88 >> 8))); out1[0] = x7; out1[1] = x9; out1[2] = x11; @@ -3246,7 +3246,7 @@ pub fn divstep(out1: *u64, out2: *[7]u64, out3: *[7]u64, out4: *[6]u64, out5: *[ var x1: u64 = undefined; var x2: u1 = undefined; addcarryxU64(&x1, &x2, 0x0, (~arg1), 0x1); - const x3 = (@truncate(u1, (x1 >> 63)) & @truncate(u1, ((arg3[0]) & 0x1))); + const x3 = (@as(u1, @truncate((x1 >> 63))) & @as(u1, @truncate(((arg3[0]) & 0x1)))); var x4: u64 = undefined; var x5: u1 = undefined; addcarryxU64(&x4, &x5, 0x0, (~arg1), 0x1); @@ -3408,7 +3408,7 @@ pub fn divstep(out1: *u64, out2: *[7]u64, out3: *[7]u64, out4: *[6]u64, out5: *[ cmovznzU64(&x102, x3, (arg5[4]), x94); var x103: u64 = undefined; cmovznzU64(&x103, x3, (arg5[5]), x96); - const x104 = @truncate(u1, (x28 & 0x1)); + const x104 = @as(u1, @truncate((x28 & 0x1))); var x105: u64 = undefined; cmovznzU64(&x105, x104, 0x0, x7); var x106: u64 = undefined; diff --git a/lib/std/crypto/pcurves/p384/p384_scalar_64.zig b/lib/std/crypto/pcurves/p384/p384_scalar_64.zig index 0ce7727148..fc787ba7b9 100644 --- a/lib/std/crypto/pcurves/p384/p384_scalar_64.zig +++ b/lib/std/crypto/pcurves/p384/p384_scalar_64.zig @@ -88,8 +88,8 @@ inline fn mulxU64(out1: *u64, out2: *u64, arg1: u64, arg2: u64) void { @setRuntimeSafety(mode == .Debug); const x = @as(u128, arg1) * @as(u128, arg2); - out1.* = @truncate(u64, x); - out2.* = @truncate(u64, x >> 64); + out1.* = @as(u64, @truncate(x)); + out2.* = @as(u64, @truncate(x >> 64)); } /// The function cmovznzU64 is a single-word conditional move. @@ -2982,90 +2982,90 @@ pub fn toBytes(out1: *[48]u8, arg1: [6]u64) void { const x4 = (arg1[2]); const x5 = (arg1[1]); const x6 = (arg1[0]); - const x7 = @truncate(u8, (x6 & 0xff)); + const x7 = @as(u8, @truncate((x6 & 0xff))); const x8 = (x6 >> 8); - const x9 = @truncate(u8, (x8 & 0xff)); + const x9 = @as(u8, @truncate((x8 & 0xff))); const x10 = (x8 >> 8); - const x11 = @truncate(u8, (x10 & 0xff)); + const x11 = @as(u8, @truncate((x10 & 0xff))); const x12 = (x10 >> 8); - const x13 = @truncate(u8, (x12 & 0xff)); + const x13 = @as(u8, @truncate((x12 & 0xff))); const x14 = (x12 >> 8); - const x15 = @truncate(u8, (x14 & 0xff)); + const x15 = @as(u8, @truncate((x14 & 0xff))); const x16 = (x14 >> 8); - const x17 = @truncate(u8, (x16 & 0xff)); + const x17 = @as(u8, @truncate((x16 & 0xff))); const x18 = (x16 >> 8); - const x19 = @truncate(u8, (x18 & 0xff)); - const x20 = @truncate(u8, (x18 >> 8)); - const x21 = @truncate(u8, (x5 & 0xff)); + const x19 = @as(u8, @truncate((x18 & 0xff))); + const x20 = @as(u8, @truncate((x18 >> 8))); + const x21 = @as(u8, @truncate((x5 & 0xff))); const x22 = (x5 >> 8); - const x23 = @truncate(u8, (x22 & 0xff)); + const x23 = @as(u8, @truncate((x22 & 0xff))); const x24 = (x22 >> 8); - const x25 = @truncate(u8, (x24 & 0xff)); + const x25 = @as(u8, @truncate((x24 & 0xff))); const x26 = (x24 >> 8); - const x27 = @truncate(u8, (x26 & 0xff)); + const x27 = @as(u8, @truncate((x26 & 0xff))); const x28 = (x26 >> 8); - const x29 = @truncate(u8, (x28 & 0xff)); + const x29 = @as(u8, @truncate((x28 & 0xff))); const x30 = (x28 >> 8); - const x31 = @truncate(u8, (x30 & 0xff)); + const x31 = @as(u8, @truncate((x30 & 0xff))); const x32 = (x30 >> 8); - const x33 = @truncate(u8, (x32 & 0xff)); - const x34 = @truncate(u8, (x32 >> 8)); - const x35 = @truncate(u8, (x4 & 0xff)); + const x33 = @as(u8, @truncate((x32 & 0xff))); + const x34 = @as(u8, @truncate((x32 >> 8))); + const x35 = @as(u8, @truncate((x4 & 0xff))); const x36 = (x4 >> 8); - const x37 = @truncate(u8, (x36 & 0xff)); + const x37 = @as(u8, @truncate((x36 & 0xff))); const x38 = (x36 >> 8); - const x39 = @truncate(u8, (x38 & 0xff)); + const x39 = @as(u8, @truncate((x38 & 0xff))); const x40 = (x38 >> 8); - const x41 = @truncate(u8, (x40 & 0xff)); + const x41 = @as(u8, @truncate((x40 & 0xff))); const x42 = (x40 >> 8); - const x43 = @truncate(u8, (x42 & 0xff)); + const x43 = @as(u8, @truncate((x42 & 0xff))); const x44 = (x42 >> 8); - const x45 = @truncate(u8, (x44 & 0xff)); + const x45 = @as(u8, @truncate((x44 & 0xff))); const x46 = (x44 >> 8); - const x47 = @truncate(u8, (x46 & 0xff)); - const x48 = @truncate(u8, (x46 >> 8)); - const x49 = @truncate(u8, (x3 & 0xff)); + const x47 = @as(u8, @truncate((x46 & 0xff))); + const x48 = @as(u8, @truncate((x46 >> 8))); + const x49 = @as(u8, @truncate((x3 & 0xff))); const x50 = (x3 >> 8); - const x51 = @truncate(u8, (x50 & 0xff)); + const x51 = @as(u8, @truncate((x50 & 0xff))); const x52 = (x50 >> 8); - const x53 = @truncate(u8, (x52 & 0xff)); + const x53 = @as(u8, @truncate((x52 & 0xff))); const x54 = (x52 >> 8); - const x55 = @truncate(u8, (x54 & 0xff)); + const x55 = @as(u8, @truncate((x54 & 0xff))); const x56 = (x54 >> 8); - const x57 = @truncate(u8, (x56 & 0xff)); + const x57 = @as(u8, @truncate((x56 & 0xff))); const x58 = (x56 >> 8); - const x59 = @truncate(u8, (x58 & 0xff)); + const x59 = @as(u8, @truncate((x58 & 0xff))); const x60 = (x58 >> 8); - const x61 = @truncate(u8, (x60 & 0xff)); - const x62 = @truncate(u8, (x60 >> 8)); - const x63 = @truncate(u8, (x2 & 0xff)); + const x61 = @as(u8, @truncate((x60 & 0xff))); + const x62 = @as(u8, @truncate((x60 >> 8))); + const x63 = @as(u8, @truncate((x2 & 0xff))); const x64 = (x2 >> 8); - const x65 = @truncate(u8, (x64 & 0xff)); + const x65 = @as(u8, @truncate((x64 & 0xff))); const x66 = (x64 >> 8); - const x67 = @truncate(u8, (x66 & 0xff)); + const x67 = @as(u8, @truncate((x66 & 0xff))); const x68 = (x66 >> 8); - const x69 = @truncate(u8, (x68 & 0xff)); + const x69 = @as(u8, @truncate((x68 & 0xff))); const x70 = (x68 >> 8); - const x71 = @truncate(u8, (x70 & 0xff)); + const x71 = @as(u8, @truncate((x70 & 0xff))); const x72 = (x70 >> 8); - const x73 = @truncate(u8, (x72 & 0xff)); + const x73 = @as(u8, @truncate((x72 & 0xff))); const x74 = (x72 >> 8); - const x75 = @truncate(u8, (x74 & 0xff)); - const x76 = @truncate(u8, (x74 >> 8)); - const x77 = @truncate(u8, (x1 & 0xff)); + const x75 = @as(u8, @truncate((x74 & 0xff))); + const x76 = @as(u8, @truncate((x74 >> 8))); + const x77 = @as(u8, @truncate((x1 & 0xff))); const x78 = (x1 >> 8); - const x79 = @truncate(u8, (x78 & 0xff)); + const x79 = @as(u8, @truncate((x78 & 0xff))); const x80 = (x78 >> 8); - const x81 = @truncate(u8, (x80 & 0xff)); + const x81 = @as(u8, @truncate((x80 & 0xff))); const x82 = (x80 >> 8); - const x83 = @truncate(u8, (x82 & 0xff)); + const x83 = @as(u8, @truncate((x82 & 0xff))); const x84 = (x82 >> 8); - const x85 = @truncate(u8, (x84 & 0xff)); + const x85 = @as(u8, @truncate((x84 & 0xff))); const x86 = (x84 >> 8); - const x87 = @truncate(u8, (x86 & 0xff)); + const x87 = @as(u8, @truncate((x86 & 0xff))); const x88 = (x86 >> 8); - const x89 = @truncate(u8, (x88 & 0xff)); - const x90 = @truncate(u8, (x88 >> 8)); + const x89 = @as(u8, @truncate((x88 & 0xff))); + const x90 = @as(u8, @truncate((x88 >> 8))); out1[0] = x7; out1[1] = x9; out1[2] = x11; @@ -3300,7 +3300,7 @@ pub fn divstep(out1: *u64, out2: *[7]u64, out3: *[7]u64, out4: *[6]u64, out5: *[ var x1: u64 = undefined; var x2: u1 = undefined; addcarryxU64(&x1, &x2, 0x0, (~arg1), 0x1); - const x3 = (@truncate(u1, (x1 >> 63)) & @truncate(u1, ((arg3[0]) & 0x1))); + const x3 = (@as(u1, @truncate((x1 >> 63))) & @as(u1, @truncate(((arg3[0]) & 0x1)))); var x4: u64 = undefined; var x5: u1 = undefined; addcarryxU64(&x4, &x5, 0x0, (~arg1), 0x1); @@ -3462,7 +3462,7 @@ pub fn divstep(out1: *u64, out2: *[7]u64, out3: *[7]u64, out4: *[6]u64, out5: *[ cmovznzU64(&x102, x3, (arg5[4]), x94); var x103: u64 = undefined; cmovznzU64(&x103, x3, (arg5[5]), x96); - const x104 = @truncate(u1, (x28 & 0x1)); + const x104 = @as(u1, @truncate((x28 & 0x1))); var x105: u64 = undefined; cmovznzU64(&x105, x104, 0x0, x7); var x106: u64 = undefined; diff --git a/lib/std/crypto/pcurves/secp256k1.zig b/lib/std/crypto/pcurves/secp256k1.zig index f0b086f974..cd7f1faf75 100644 --- a/lib/std/crypto/pcurves/secp256k1.zig +++ b/lib/std/crypto/pcurves/secp256k1.zig @@ -67,8 +67,8 @@ pub const Secp256k1 = struct { const t1 = math.mulWide(u256, k, 21949224512762693861512883645436906316123769664773102907882521278123970637873); const t2 = math.mulWide(u256, k, 103246583619904461035481197785446227098457807945486720222659797044629401272177); - const c1 = @truncate(u128, t1 >> 384) + @truncate(u1, t1 >> 383); - const c2 = @truncate(u128, t2 >> 384) + @truncate(u1, t2 >> 383); + const c1 = @as(u128, @truncate(t1 >> 384)) + @as(u1, @truncate(t1 >> 383)); + const c2 = @as(u128, @truncate(t2 >> 384)) + @as(u1, @truncate(t2 >> 383)); var buf: [32]u8 = undefined; @@ -346,7 +346,7 @@ pub const Secp256k1 = struct { var t = Secp256k1.identityElement; comptime var i: u8 = 1; inline while (i < pc.len) : (i += 1) { - t.cMov(pc[i], @truncate(u1, (@as(usize, b ^ i) -% 1) >> 8)); + t.cMov(pc[i], @as(u1, @truncate((@as(usize, b ^ i) -% 1) >> 8))); } return t; } @@ -354,8 +354,8 @@ pub const Secp256k1 = struct { fn slide(s: [32]u8) [2 * 32 + 1]i8 { var e: [2 * 32 + 1]i8 = undefined; for (s, 0..) |x, i| { - e[i * 2 + 0] = @as(i8, @truncate(u4, x)); - e[i * 2 + 1] = @as(i8, @truncate(u4, x >> 4)); + e[i * 2 + 0] = @as(i8, @as(u4, @truncate(x))); + e[i * 2 + 1] = @as(i8, @as(u4, @truncate(x >> 4))); } // Now, e[0..63] is between 0 and 15, e[63] is between 0 and 7 var carry: i8 = 0; @@ -379,9 +379,9 @@ pub const Secp256k1 = struct { while (true) : (pos -= 1) { const slot = e[pos]; if (slot > 0) { - q = q.add(pc[@intCast(usize, slot)]); + q = q.add(pc[@as(usize, @intCast(slot))]); } else if (slot < 0) { - q = q.sub(pc[@intCast(usize, -slot)]); + q = q.sub(pc[@as(usize, @intCast(-slot))]); } if (pos == 0) break; q = q.dbl().dbl().dbl().dbl(); @@ -394,7 +394,7 @@ pub const Secp256k1 = struct { var q = Secp256k1.identityElement; var pos: usize = 252; while (true) : (pos -= 4) { - const slot = @truncate(u4, (s[pos >> 3] >> @truncate(u3, pos))); + const slot = @as(u4, @truncate((s[pos >> 3] >> @as(u3, @truncate(pos))))); if (vartime) { if (slot != 0) { q = q.add(pc[slot]); @@ -482,15 +482,15 @@ pub const Secp256k1 = struct { while (true) : (pos -= 1) { const slot1 = e1[pos]; if (slot1 > 0) { - q = q.add(pc1[@intCast(usize, slot1)]); + q = q.add(pc1[@as(usize, @intCast(slot1))]); } else if (slot1 < 0) { - q = q.sub(pc1[@intCast(usize, -slot1)]); + q = q.sub(pc1[@as(usize, @intCast(-slot1))]); } const slot2 = e2[pos]; if (slot2 > 0) { - q = q.add(pc2[@intCast(usize, slot2)]); + q = q.add(pc2[@as(usize, @intCast(slot2))]); } else if (slot2 < 0) { - q = q.sub(pc2[@intCast(usize, -slot2)]); + q = q.sub(pc2[@as(usize, @intCast(-slot2))]); } if (pos == 0) break; q = q.dbl().dbl().dbl().dbl(); @@ -523,15 +523,15 @@ pub const Secp256k1 = struct { while (true) : (pos -= 1) { const slot1 = e1[pos]; if (slot1 > 0) { - q = q.add(pc1[@intCast(usize, slot1)]); + q = q.add(pc1[@as(usize, @intCast(slot1))]); } else if (slot1 < 0) { - q = q.sub(pc1[@intCast(usize, -slot1)]); + q = q.sub(pc1[@as(usize, @intCast(-slot1))]); } const slot2 = e2[pos]; if (slot2 > 0) { - q = q.add(pc2[@intCast(usize, slot2)]); + q = q.add(pc2[@as(usize, @intCast(slot2))]); } else if (slot2 < 0) { - q = q.sub(pc2[@intCast(usize, -slot2)]); + q = q.sub(pc2[@as(usize, @intCast(-slot2))]); } if (pos == 0) break; q = q.dbl().dbl().dbl().dbl(); diff --git a/lib/std/crypto/pcurves/secp256k1/secp256k1_64.zig b/lib/std/crypto/pcurves/secp256k1/secp256k1_64.zig index 5643ea88d5..ae3e97c619 100644 --- a/lib/std/crypto/pcurves/secp256k1/secp256k1_64.zig +++ b/lib/std/crypto/pcurves/secp256k1/secp256k1_64.zig @@ -88,8 +88,8 @@ inline fn mulxU64(out1: *u64, out2: *u64, arg1: u64, arg2: u64) void { @setRuntimeSafety(mode == .Debug); const x = @as(u128, arg1) * @as(u128, arg2); - out1.* = @truncate(u64, x); - out2.* = @truncate(u64, x >> 64); + out1.* = @as(u64, @truncate(x)); + out2.* = @as(u64, @truncate(x >> 64)); } /// The function cmovznzU64 is a single-word conditional move. @@ -1488,62 +1488,62 @@ pub fn toBytes(out1: *[32]u8, arg1: [4]u64) void { const x2 = (arg1[2]); const x3 = (arg1[1]); const x4 = (arg1[0]); - const x5 = @truncate(u8, (x4 & 0xff)); + const x5 = @as(u8, @truncate((x4 & 0xff))); const x6 = (x4 >> 8); - const x7 = @truncate(u8, (x6 & 0xff)); + const x7 = @as(u8, @truncate((x6 & 0xff))); const x8 = (x6 >> 8); - const x9 = @truncate(u8, (x8 & 0xff)); + const x9 = @as(u8, @truncate((x8 & 0xff))); const x10 = (x8 >> 8); - const x11 = @truncate(u8, (x10 & 0xff)); + const x11 = @as(u8, @truncate((x10 & 0xff))); const x12 = (x10 >> 8); - const x13 = @truncate(u8, (x12 & 0xff)); + const x13 = @as(u8, @truncate((x12 & 0xff))); const x14 = (x12 >> 8); - const x15 = @truncate(u8, (x14 & 0xff)); + const x15 = @as(u8, @truncate((x14 & 0xff))); const x16 = (x14 >> 8); - const x17 = @truncate(u8, (x16 & 0xff)); - const x18 = @truncate(u8, (x16 >> 8)); - const x19 = @truncate(u8, (x3 & 0xff)); + const x17 = @as(u8, @truncate((x16 & 0xff))); + const x18 = @as(u8, @truncate((x16 >> 8))); + const x19 = @as(u8, @truncate((x3 & 0xff))); const x20 = (x3 >> 8); - const x21 = @truncate(u8, (x20 & 0xff)); + const x21 = @as(u8, @truncate((x20 & 0xff))); const x22 = (x20 >> 8); - const x23 = @truncate(u8, (x22 & 0xff)); + const x23 = @as(u8, @truncate((x22 & 0xff))); const x24 = (x22 >> 8); - const x25 = @truncate(u8, (x24 & 0xff)); + const x25 = @as(u8, @truncate((x24 & 0xff))); const x26 = (x24 >> 8); - const x27 = @truncate(u8, (x26 & 0xff)); + const x27 = @as(u8, @truncate((x26 & 0xff))); const x28 = (x26 >> 8); - const x29 = @truncate(u8, (x28 & 0xff)); + const x29 = @as(u8, @truncate((x28 & 0xff))); const x30 = (x28 >> 8); - const x31 = @truncate(u8, (x30 & 0xff)); - const x32 = @truncate(u8, (x30 >> 8)); - const x33 = @truncate(u8, (x2 & 0xff)); + const x31 = @as(u8, @truncate((x30 & 0xff))); + const x32 = @as(u8, @truncate((x30 >> 8))); + const x33 = @as(u8, @truncate((x2 & 0xff))); const x34 = (x2 >> 8); - const x35 = @truncate(u8, (x34 & 0xff)); + const x35 = @as(u8, @truncate((x34 & 0xff))); const x36 = (x34 >> 8); - const x37 = @truncate(u8, (x36 & 0xff)); + const x37 = @as(u8, @truncate((x36 & 0xff))); const x38 = (x36 >> 8); - const x39 = @truncate(u8, (x38 & 0xff)); + const x39 = @as(u8, @truncate((x38 & 0xff))); const x40 = (x38 >> 8); - const x41 = @truncate(u8, (x40 & 0xff)); + const x41 = @as(u8, @truncate((x40 & 0xff))); const x42 = (x40 >> 8); - const x43 = @truncate(u8, (x42 & 0xff)); + const x43 = @as(u8, @truncate((x42 & 0xff))); const x44 = (x42 >> 8); - const x45 = @truncate(u8, (x44 & 0xff)); - const x46 = @truncate(u8, (x44 >> 8)); - const x47 = @truncate(u8, (x1 & 0xff)); + const x45 = @as(u8, @truncate((x44 & 0xff))); + const x46 = @as(u8, @truncate((x44 >> 8))); + const x47 = @as(u8, @truncate((x1 & 0xff))); const x48 = (x1 >> 8); - const x49 = @truncate(u8, (x48 & 0xff)); + const x49 = @as(u8, @truncate((x48 & 0xff))); const x50 = (x48 >> 8); - const x51 = @truncate(u8, (x50 & 0xff)); + const x51 = @as(u8, @truncate((x50 & 0xff))); const x52 = (x50 >> 8); - const x53 = @truncate(u8, (x52 & 0xff)); + const x53 = @as(u8, @truncate((x52 & 0xff))); const x54 = (x52 >> 8); - const x55 = @truncate(u8, (x54 & 0xff)); + const x55 = @as(u8, @truncate((x54 & 0xff))); const x56 = (x54 >> 8); - const x57 = @truncate(u8, (x56 & 0xff)); + const x57 = @as(u8, @truncate((x56 & 0xff))); const x58 = (x56 >> 8); - const x59 = @truncate(u8, (x58 & 0xff)); - const x60 = @truncate(u8, (x58 >> 8)); + const x59 = @as(u8, @truncate((x58 & 0xff))); + const x60 = @as(u8, @truncate((x58 >> 8))); out1[0] = x5; out1[1] = x7; out1[2] = x9; @@ -1726,7 +1726,7 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ var x1: u64 = undefined; var x2: u1 = undefined; addcarryxU64(&x1, &x2, 0x0, (~arg1), 0x1); - const x3 = (@truncate(u1, (x1 >> 63)) & @truncate(u1, ((arg3[0]) & 0x1))); + const x3 = (@as(u1, @truncate((x1 >> 63))) & @as(u1, @truncate(((arg3[0]) & 0x1)))); var x4: u64 = undefined; var x5: u1 = undefined; addcarryxU64(&x4, &x5, 0x0, (~arg1), 0x1); @@ -1840,7 +1840,7 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ cmovznzU64(&x72, x3, (arg5[2]), x66); var x73: u64 = undefined; cmovznzU64(&x73, x3, (arg5[3]), x68); - const x74 = @truncate(u1, (x22 & 0x1)); + const x74 = @as(u1, @truncate((x22 & 0x1))); var x75: u64 = undefined; cmovznzU64(&x75, x74, 0x0, x7); var x76: u64 = undefined; diff --git a/lib/std/crypto/pcurves/secp256k1/secp256k1_scalar_64.zig b/lib/std/crypto/pcurves/secp256k1/secp256k1_scalar_64.zig index aca1bd3063..12c833bb33 100644 --- a/lib/std/crypto/pcurves/secp256k1/secp256k1_scalar_64.zig +++ b/lib/std/crypto/pcurves/secp256k1/secp256k1_scalar_64.zig @@ -88,8 +88,8 @@ inline fn mulxU64(out1: *u64, out2: *u64, arg1: u64, arg2: u64) void { @setRuntimeSafety(mode == .Debug); const x = @as(u128, arg1) * @as(u128, arg2); - out1.* = @truncate(u64, x); - out2.* = @truncate(u64, x >> 64); + out1.* = @as(u64, @truncate(x)); + out2.* = @as(u64, @truncate(x >> 64)); } /// The function cmovznzU64 is a single-word conditional move. @@ -1548,62 +1548,62 @@ pub fn toBytes(out1: *[32]u8, arg1: [4]u64) void { const x2 = (arg1[2]); const x3 = (arg1[1]); const x4 = (arg1[0]); - const x5 = @truncate(u8, (x4 & 0xff)); + const x5 = @as(u8, @truncate((x4 & 0xff))); const x6 = (x4 >> 8); - const x7 = @truncate(u8, (x6 & 0xff)); + const x7 = @as(u8, @truncate((x6 & 0xff))); const x8 = (x6 >> 8); - const x9 = @truncate(u8, (x8 & 0xff)); + const x9 = @as(u8, @truncate((x8 & 0xff))); const x10 = (x8 >> 8); - const x11 = @truncate(u8, (x10 & 0xff)); + const x11 = @as(u8, @truncate((x10 & 0xff))); const x12 = (x10 >> 8); - const x13 = @truncate(u8, (x12 & 0xff)); + const x13 = @as(u8, @truncate((x12 & 0xff))); const x14 = (x12 >> 8); - const x15 = @truncate(u8, (x14 & 0xff)); + const x15 = @as(u8, @truncate((x14 & 0xff))); const x16 = (x14 >> 8); - const x17 = @truncate(u8, (x16 & 0xff)); - const x18 = @truncate(u8, (x16 >> 8)); - const x19 = @truncate(u8, (x3 & 0xff)); + const x17 = @as(u8, @truncate((x16 & 0xff))); + const x18 = @as(u8, @truncate((x16 >> 8))); + const x19 = @as(u8, @truncate((x3 & 0xff))); const x20 = (x3 >> 8); - const x21 = @truncate(u8, (x20 & 0xff)); + const x21 = @as(u8, @truncate((x20 & 0xff))); const x22 = (x20 >> 8); - const x23 = @truncate(u8, (x22 & 0xff)); + const x23 = @as(u8, @truncate((x22 & 0xff))); const x24 = (x22 >> 8); - const x25 = @truncate(u8, (x24 & 0xff)); + const x25 = @as(u8, @truncate((x24 & 0xff))); const x26 = (x24 >> 8); - const x27 = @truncate(u8, (x26 & 0xff)); + const x27 = @as(u8, @truncate((x26 & 0xff))); const x28 = (x26 >> 8); - const x29 = @truncate(u8, (x28 & 0xff)); + const x29 = @as(u8, @truncate((x28 & 0xff))); const x30 = (x28 >> 8); - const x31 = @truncate(u8, (x30 & 0xff)); - const x32 = @truncate(u8, (x30 >> 8)); - const x33 = @truncate(u8, (x2 & 0xff)); + const x31 = @as(u8, @truncate((x30 & 0xff))); + const x32 = @as(u8, @truncate((x30 >> 8))); + const x33 = @as(u8, @truncate((x2 & 0xff))); const x34 = (x2 >> 8); - const x35 = @truncate(u8, (x34 & 0xff)); + const x35 = @as(u8, @truncate((x34 & 0xff))); const x36 = (x34 >> 8); - const x37 = @truncate(u8, (x36 & 0xff)); + const x37 = @as(u8, @truncate((x36 & 0xff))); const x38 = (x36 >> 8); - const x39 = @truncate(u8, (x38 & 0xff)); + const x39 = @as(u8, @truncate((x38 & 0xff))); const x40 = (x38 >> 8); - const x41 = @truncate(u8, (x40 & 0xff)); + const x41 = @as(u8, @truncate((x40 & 0xff))); const x42 = (x40 >> 8); - const x43 = @truncate(u8, (x42 & 0xff)); + const x43 = @as(u8, @truncate((x42 & 0xff))); const x44 = (x42 >> 8); - const x45 = @truncate(u8, (x44 & 0xff)); - const x46 = @truncate(u8, (x44 >> 8)); - const x47 = @truncate(u8, (x1 & 0xff)); + const x45 = @as(u8, @truncate((x44 & 0xff))); + const x46 = @as(u8, @truncate((x44 >> 8))); + const x47 = @as(u8, @truncate((x1 & 0xff))); const x48 = (x1 >> 8); - const x49 = @truncate(u8, (x48 & 0xff)); + const x49 = @as(u8, @truncate((x48 & 0xff))); const x50 = (x48 >> 8); - const x51 = @truncate(u8, (x50 & 0xff)); + const x51 = @as(u8, @truncate((x50 & 0xff))); const x52 = (x50 >> 8); - const x53 = @truncate(u8, (x52 & 0xff)); + const x53 = @as(u8, @truncate((x52 & 0xff))); const x54 = (x52 >> 8); - const x55 = @truncate(u8, (x54 & 0xff)); + const x55 = @as(u8, @truncate((x54 & 0xff))); const x56 = (x54 >> 8); - const x57 = @truncate(u8, (x56 & 0xff)); + const x57 = @as(u8, @truncate((x56 & 0xff))); const x58 = (x56 >> 8); - const x59 = @truncate(u8, (x58 & 0xff)); - const x60 = @truncate(u8, (x58 >> 8)); + const x59 = @as(u8, @truncate((x58 & 0xff))); + const x60 = @as(u8, @truncate((x58 >> 8))); out1[0] = x5; out1[1] = x7; out1[2] = x9; @@ -1786,7 +1786,7 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ var x1: u64 = undefined; var x2: u1 = undefined; addcarryxU64(&x1, &x2, 0x0, (~arg1), 0x1); - const x3 = (@truncate(u1, (x1 >> 63)) & @truncate(u1, ((arg3[0]) & 0x1))); + const x3 = (@as(u1, @truncate((x1 >> 63))) & @as(u1, @truncate(((arg3[0]) & 0x1)))); var x4: u64 = undefined; var x5: u1 = undefined; addcarryxU64(&x4, &x5, 0x0, (~arg1), 0x1); @@ -1900,7 +1900,7 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ cmovznzU64(&x72, x3, (arg5[2]), x66); var x73: u64 = undefined; cmovznzU64(&x73, x3, (arg5[3]), x68); - const x74 = @truncate(u1, (x22 & 0x1)); + const x74 = @as(u1, @truncate((x22 & 0x1))); var x75: u64 = undefined; cmovznzU64(&x75, x74, 0x0, x7); var x76: u64 = undefined; diff --git a/lib/std/crypto/phc_encoding.zig b/lib/std/crypto/phc_encoding.zig index 1eeee39a5a..fecd7f1239 100644 --- a/lib/std/crypto/phc_encoding.zig +++ b/lib/std/crypto/phc_encoding.zig @@ -193,7 +193,7 @@ pub fn serialize(params: anytype, str: []u8) Error![]const u8 { pub fn calcSize(params: anytype) usize { var buf = io.countingWriter(io.null_writer); serializeTo(params, buf.writer()) catch unreachable; - return @intCast(usize, buf.bytes_written); + return @as(usize, @intCast(buf.bytes_written)); } fn serializeTo(params: anytype, out: anytype) !void { diff --git a/lib/std/crypto/poly1305.zig b/lib/std/crypto/poly1305.zig index 51e1c2ab24..5bcb75169d 100644 --- a/lib/std/crypto/poly1305.zig +++ b/lib/std/crypto/poly1305.zig @@ -76,12 +76,12 @@ pub const Poly1305 = struct { const m1 = h1r0 +% h0r1; const m2 = h2r0 +% h1r1; - const t0 = @truncate(u64, m0); - v = @addWithOverflow(@truncate(u64, m1), @truncate(u64, m0 >> 64)); + const t0 = @as(u64, @truncate(m0)); + v = @addWithOverflow(@as(u64, @truncate(m1)), @as(u64, @truncate(m0 >> 64))); const t1 = v[0]; - v = add(@truncate(u64, m2), @truncate(u64, m1 >> 64), v[1]); + v = add(@as(u64, @truncate(m2)), @as(u64, @truncate(m1 >> 64)), v[1]); const t2 = v[0]; - v = add(@truncate(u64, m3), @truncate(u64, m2 >> 64), v[1]); + v = add(@as(u64, @truncate(m3)), @as(u64, @truncate(m2 >> 64)), v[1]); const t3 = v[0]; // Partial reduction @@ -98,9 +98,9 @@ pub const Poly1305 = struct { h1 = v[0]; h2 +%= v[1]; const cc = (cclo | (@as(u128, cchi) << 64)) >> 2; - v = @addWithOverflow(h0, @truncate(u64, cc)); + v = @addWithOverflow(h0, @as(u64, @truncate(cc))); h0 = v[0]; - v = add(h1, @truncate(u64, cc >> 64), v[1]); + v = add(h1, @as(u64, @truncate(cc >> 64)), v[1]); h1 = v[0]; h2 +%= v[1]; } @@ -185,7 +185,7 @@ pub const Poly1305 = struct { mem.writeIntLittle(u64, out[0..8], st.h[0]); mem.writeIntLittle(u64, out[8..16], st.h[1]); - utils.secureZero(u8, @ptrCast([*]u8, st)[0..@sizeOf(Poly1305)]); + utils.secureZero(u8, @as([*]u8, @ptrCast(st))[0..@sizeOf(Poly1305)]); } pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [key_length]u8) void { diff --git a/lib/std/crypto/salsa20.zig b/lib/std/crypto/salsa20.zig index c8a639ad0b..231f941099 100644 --- a/lib/std/crypto/salsa20.zig +++ b/lib/std/crypto/salsa20.zig @@ -337,8 +337,8 @@ pub fn Salsa(comptime rounds: comptime_int) type { var d: [4]u32 = undefined; d[0] = mem.readIntLittle(u32, nonce[0..4]); d[1] = mem.readIntLittle(u32, nonce[4..8]); - d[2] = @truncate(u32, counter); - d[3] = @truncate(u32, counter >> 32); + d[2] = @as(u32, @truncate(counter)); + d[3] = @as(u32, @truncate(counter >> 32)); SalsaImpl(rounds).salsaXor(out, in, keyToWords(key), d); } }; diff --git a/lib/std/crypto/scrypt.zig b/lib/std/crypto/scrypt.zig index 97dd9b95d0..8745a3b34e 100644 --- a/lib/std/crypto/scrypt.zig +++ b/lib/std/crypto/scrypt.zig @@ -73,11 +73,11 @@ fn salsaXor(tmp: *align(16) [16]u32, in: []align(16) const u32, out: []align(16) } fn blockMix(tmp: *align(16) [16]u32, in: []align(16) const u32, out: []align(16) u32, r: u30) void { - blockCopy(tmp, @alignCast(16, in[(2 * r - 1) * 16 ..]), 1); + blockCopy(tmp, @alignCast(in[(2 * r - 1) * 16 ..]), 1); var i: usize = 0; while (i < 2 * r) : (i += 2) { - salsaXor(tmp, @alignCast(16, in[i * 16 ..]), @alignCast(16, out[i * 8 ..])); - salsaXor(tmp, @alignCast(16, in[i * 16 + 16 ..]), @alignCast(16, out[i * 8 + r * 16 ..])); + salsaXor(tmp, @alignCast(in[i * 16 ..]), @alignCast(out[i * 8 ..])); + salsaXor(tmp, @alignCast(in[i * 16 + 16 ..]), @alignCast(out[i * 8 + r * 16 ..])); } } @@ -87,8 +87,8 @@ fn integerify(b: []align(16) const u32, r: u30) u64 { } fn smix(b: []align(16) u8, r: u30, n: usize, v: []align(16) u32, xy: []align(16) u32) void { - var x = @alignCast(16, xy[0 .. 32 * r]); - var y = @alignCast(16, xy[32 * r ..]); + var x: []align(16) u32 = @alignCast(xy[0 .. 32 * r]); + var y: []align(16) u32 = @alignCast(xy[32 * r ..]); for (x, 0..) |*v1, j| { v1.* = mem.readIntSliceLittle(u32, b[4 * j ..]); @@ -97,21 +97,21 @@ fn smix(b: []align(16) u8, r: u30, n: usize, v: []align(16) u32, xy: []align(16) var tmp: [16]u32 align(16) = undefined; var i: usize = 0; while (i < n) : (i += 2) { - blockCopy(@alignCast(16, v[i * (32 * r) ..]), x, 2 * r); + blockCopy(@alignCast(v[i * (32 * r) ..]), x, 2 * r); blockMix(&tmp, x, y, r); - blockCopy(@alignCast(16, v[(i + 1) * (32 * r) ..]), y, 2 * r); + blockCopy(@alignCast(v[(i + 1) * (32 * r) ..]), y, 2 * r); blockMix(&tmp, y, x, r); } i = 0; while (i < n) : (i += 2) { - var j = @intCast(usize, integerify(x, r) & (n - 1)); - blockXor(x, @alignCast(16, v[j * (32 * r) ..]), 2 * r); + var j = @as(usize, @intCast(integerify(x, r) & (n - 1))); + blockXor(x, @alignCast(v[j * (32 * r) ..]), 2 * r); blockMix(&tmp, x, y, r); - j = @intCast(usize, integerify(y, r) & (n - 1)); - blockXor(y, @alignCast(16, v[j * (32 * r) ..]), 2 * r); + j = @as(usize, @intCast(integerify(y, r) & (n - 1))); + blockXor(y, @alignCast(v[j * (32 * r) ..]), 2 * r); blockMix(&tmp, y, x, r); } @@ -147,12 +147,12 @@ pub const Params = struct { const r: u30 = 8; if (ops < mem_limit / 32) { const max_n = ops / (r * 4); - return Self{ .r = r, .p = 1, .ln = @intCast(u6, math.log2(max_n)) }; + return Self{ .r = r, .p = 1, .ln = @as(u6, @intCast(math.log2(max_n))) }; } else { - const max_n = mem_limit / (@intCast(usize, r) * 128); - const ln = @intCast(u6, math.log2(max_n)); + const max_n = mem_limit / (@as(usize, @intCast(r)) * 128); + const ln = @as(u6, @intCast(math.log2(max_n))); const max_rp = @min(0x3fffffff, (ops / 4) / (@as(u64, 1) << ln)); - return Self{ .r = r, .p = @intCast(u30, max_rp / @as(u64, r)), .ln = ln }; + return Self{ .r = r, .p = @as(u30, @intCast(max_rp / @as(u64, r))), .ln = ln }; } } }; @@ -185,7 +185,7 @@ pub fn kdf( const n64 = @as(u64, 1) << params.ln; if (n64 > max_size) return KdfError.WeakParameters; - const n = @intCast(usize, n64); + const n = @as(usize, @intCast(n64)); if (@as(u64, params.r) * @as(u64, params.p) >= 1 << 30 or params.r > max_int / 128 / @as(u64, params.p) or params.r > max_int / 256 or @@ -201,7 +201,7 @@ pub fn kdf( try pwhash.pbkdf2(dk, password, salt, 1, HmacSha256); var i: u32 = 0; while (i < params.p) : (i += 1) { - smix(@alignCast(16, dk[i * 128 * params.r ..]), params.r, n, v, xy); + smix(@alignCast(dk[i * 128 * params.r ..]), params.r, n, v, xy); } try pwhash.pbkdf2(derived_key, password, dk, 1, HmacSha256); } @@ -309,7 +309,7 @@ const crypt_format = struct { pub fn calcSize(params: anytype) usize { var buf = io.countingWriter(io.null_writer); serializeTo(params, buf.writer()) catch unreachable; - return @intCast(usize, buf.bytes_written); + return @as(usize, @intCast(buf.bytes_written)); } fn serializeTo(params: anytype, out: anytype) !void { @@ -343,7 +343,7 @@ const crypt_format = struct { fn intEncode(dst: []u8, src: anytype) void { var n = src; for (dst) |*x| { - x.* = map64[@truncate(u6, n)]; + x.* = map64[@as(u6, @truncate(n))]; n = math.shr(@TypeOf(src), n, 6); } } @@ -352,7 +352,7 @@ const crypt_format = struct { var v: T = 0; for (src, 0..) |x, i| { const vi = mem.indexOfScalar(u8, &map64, x) orelse return EncodingError.InvalidEncoding; - v |= @intCast(T, vi) << @intCast(math.Log2Int(T), i * 6); + v |= @as(T, @intCast(vi)) << @as(math.Log2Int(T), @intCast(i * 6)); } return v; } @@ -366,10 +366,10 @@ const crypt_format = struct { const leftover = src[i * 4 ..]; var v: u24 = 0; for (leftover, 0..) |_, j| { - v |= @as(u24, try intDecode(u6, leftover[j..][0..1])) << @intCast(u5, j * 6); + v |= @as(u24, try intDecode(u6, leftover[j..][0..1])) << @as(u5, @intCast(j * 6)); } for (dst[i * 3 ..], 0..) |*x, j| { - x.* = @truncate(u8, v >> @intCast(u5, j * 8)); + x.* = @as(u8, @truncate(v >> @as(u5, @intCast(j * 8)))); } } @@ -382,7 +382,7 @@ const crypt_format = struct { const leftover = src[i * 3 ..]; var v: u24 = 0; for (leftover, 0..) |x, j| { - v |= @as(u24, x) << @intCast(u5, j * 8); + v |= @as(u24, x) << @as(u5, @intCast(j * 8)); } intEncode(dst[i * 4 ..], v); } diff --git a/lib/std/crypto/sha1.zig b/lib/std/crypto/sha1.zig index 1f5f3eaae2..82e23e0647 100644 --- a/lib/std/crypto/sha1.zig +++ b/lib/std/crypto/sha1.zig @@ -75,7 +75,7 @@ pub const Sha1 = struct { // Copy any remainder for next pass. @memcpy(d.buf[d.buf_len..][0 .. b.len - off], b[off..]); - d.buf_len += @intCast(u8, b[off..].len); + d.buf_len += @as(u8, @intCast(b[off..].len)); d.total_len += b.len; } @@ -97,9 +97,9 @@ pub const Sha1 = struct { // Append message length. var i: usize = 1; var len = d.total_len >> 5; - d.buf[63] = @intCast(u8, d.total_len & 0x1f) << 3; + d.buf[63] = @as(u8, @intCast(d.total_len & 0x1f)) << 3; while (i < 8) : (i += 1) { - d.buf[63 - i] = @intCast(u8, len & 0xff); + d.buf[63 - i] = @as(u8, @intCast(len & 0xff)); len >>= 8; } diff --git a/lib/std/crypto/sha2.zig b/lib/std/crypto/sha2.zig index bd5a7cc5d4..ce543d3906 100644 --- a/lib/std/crypto/sha2.zig +++ b/lib/std/crypto/sha2.zig @@ -132,7 +132,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { // Copy any remainder for next pass. const b_slice = b[off..]; @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); - d.buf_len += @intCast(u8, b[off..].len); + d.buf_len += @as(u8, @intCast(b[off..].len)); d.total_len += b.len; } @@ -159,9 +159,9 @@ fn Sha2x32(comptime params: Sha2Params32) type { // Append message length. var i: usize = 1; var len = d.total_len >> 5; - d.buf[63] = @intCast(u8, d.total_len & 0x1f) << 3; + d.buf[63] = @as(u8, @intCast(d.total_len & 0x1f)) << 3; while (i < 8) : (i += 1) { - d.buf[63 - i] = @intCast(u8, len & 0xff); + d.buf[63 - i] = @as(u8, @intCast(len & 0xff)); len >>= 8; } @@ -194,7 +194,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { fn round(d: *Self, b: *const [64]u8) void { var s: [64]u32 align(16) = undefined; - for (@ptrCast(*align(1) const [16]u32, b), 0..) |*elem, i| { + for (@as(*align(1) const [16]u32, @ptrCast(b)), 0..) |*elem, i| { s[i] = mem.readIntBig(u32, mem.asBytes(elem)); } @@ -203,7 +203,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { .aarch64 => if (builtin.zig_backend != .stage2_c and comptime std.Target.aarch64.featureSetHas(builtin.cpu.features, .sha2)) { var x: v4u32 = d.s[0..4].*; var y: v4u32 = d.s[4..8].*; - const s_v = @ptrCast(*[16]v4u32, &s); + const s_v = @as(*[16]v4u32, @ptrCast(&s)); comptime var k: u8 = 0; inline while (k < 16) : (k += 1) { @@ -241,7 +241,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { .x86_64 => if (builtin.zig_backend != .stage2_c and comptime std.Target.x86.featureSetHas(builtin.cpu.features, .sha)) { var x: v4u32 = [_]u32{ d.s[5], d.s[4], d.s[1], d.s[0] }; var y: v4u32 = [_]u32{ d.s[7], d.s[6], d.s[3], d.s[2] }; - const s_v = @ptrCast(*[16]v4u32, &s); + const s_v = @as(*[16]v4u32, @ptrCast(&s)); comptime var k: u8 = 0; inline while (k < 16) : (k += 1) { @@ -273,7 +273,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { : [x] "=x" (-> v4u32), : [_] "0" (x), [y] "x" (y), - [_] "{xmm0}" (@bitCast(v4u32, @bitCast(u128, w) >> 64)), + [_] "{xmm0}" (@as(v4u32, @bitCast(@as(u128, @bitCast(w)) >> 64))), ); } @@ -624,7 +624,7 @@ fn Sha2x64(comptime params: Sha2Params64) type { // Copy any remainder for next pass. const b_slice = b[off..]; @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); - d.buf_len += @intCast(u8, b[off..].len); + d.buf_len += @as(u8, @intCast(b[off..].len)); d.total_len += b.len; } @@ -651,9 +651,9 @@ fn Sha2x64(comptime params: Sha2Params64) type { // Append message length. var i: usize = 1; var len = d.total_len >> 5; - d.buf[127] = @intCast(u8, d.total_len & 0x1f) << 3; + d.buf[127] = @as(u8, @intCast(d.total_len & 0x1f)) << 3; while (i < 16) : (i += 1) { - d.buf[127 - i] = @intCast(u8, len & 0xff); + d.buf[127 - i] = @as(u8, @intCast(len & 0xff)); len >>= 8; } diff --git a/lib/std/crypto/siphash.zig b/lib/std/crypto/siphash.zig index 70f4f2fd53..4399587397 100644 --- a/lib/std/crypto/siphash.zig +++ b/lib/std/crypto/siphash.zig @@ -83,13 +83,13 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round @call(.always_inline, round, .{ self, blob }); } - self.msg_len +%= @truncate(u8, b.len); + self.msg_len +%= @as(u8, @truncate(b.len)); } fn final(self: *Self, b: []const u8) T { std.debug.assert(b.len < 8); - self.msg_len +%= @truncate(u8, b.len); + self.msg_len +%= @as(u8, @truncate(b.len)); var buf = [_]u8{0} ** 8; @memcpy(buf[0..b.len], b); @@ -202,7 +202,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) const b_slice = b[off + aligned_len ..]; @memcpy(self.buf[self.buf_len..][0..b_slice.len], b_slice); - self.buf_len += @intCast(u8, b_slice.len); + self.buf_len += @as(u8, @intCast(b_slice.len)); } pub fn peek(self: Self) [mac_length]u8 { @@ -329,7 +329,7 @@ test "siphash64-2-4 sanity" { var buffer: [64]u8 = undefined; for (vectors, 0..) |vector, i| { - buffer[i] = @intCast(u8, i); + buffer[i] = @as(u8, @intCast(i)); var out: [siphash.mac_length]u8 = undefined; siphash.create(&out, buffer[0..i], test_key); @@ -409,7 +409,7 @@ test "siphash128-2-4 sanity" { var buffer: [64]u8 = undefined; for (vectors, 0..) |vector, i| { - buffer[i] = @intCast(u8, i); + buffer[i] = @as(u8, @intCast(i)); var out: [siphash.mac_length]u8 = undefined; siphash.create(&out, buffer[0..i], test_key[0..]); @@ -420,7 +420,7 @@ test "siphash128-2-4 sanity" { test "iterative non-divisible update" { var buf: [1024]u8 = undefined; for (&buf, 0..) |*e, i| { - e.* = @truncate(u8, i); + e.* = @as(u8, @truncate(i)); } const key = "0x128dad08f12307"; diff --git a/lib/std/crypto/tlcsprng.zig b/lib/std/crypto/tlcsprng.zig index 54a30cfaba..344da9745d 100644 --- a/lib/std/crypto/tlcsprng.zig +++ b/lib/std/crypto/tlcsprng.zig @@ -102,7 +102,7 @@ fn tlsCsprngFill(_: *anyopaque, buffer: []u8) void { wipe_mem = mem.asBytes(&S.buf); } } - const ctx = @ptrCast(*Context, wipe_mem.ptr); + const ctx = @as(*Context, @ptrCast(wipe_mem.ptr)); switch (ctx.init_state) { .uninitialized => { @@ -158,7 +158,7 @@ fn childAtForkHandler() callconv(.C) void { } fn fillWithCsprng(buffer: []u8) void { - const ctx = @ptrCast(*Context, wipe_mem.ptr); + const ctx = @as(*Context, @ptrCast(wipe_mem.ptr)); return ctx.rng.fill(buffer); } @@ -174,7 +174,7 @@ fn initAndFill(buffer: []u8) void { // the `std.options.cryptoRandomSeed` function is provided. std.options.cryptoRandomSeed(&seed); - const ctx = @ptrCast(*Context, wipe_mem.ptr); + const ctx = @as(*Context, @ptrCast(wipe_mem.ptr)); ctx.rng = Rng.init(seed); std.crypto.utils.secureZero(u8, &seed); diff --git a/lib/std/crypto/tls.zig b/lib/std/crypto/tls.zig index 4c03c48973..eb5a6b4c1a 100644 --- a/lib/std/crypto/tls.zig +++ b/lib/std/crypto/tls.zig @@ -371,12 +371,12 @@ pub fn hkdfExpandLabel( const tls13 = "tls13 "; var buf: [2 + 1 + tls13.len + max_label_len + 1 + max_context_len]u8 = undefined; mem.writeIntBig(u16, buf[0..2], len); - buf[2] = @intCast(u8, tls13.len + label.len); + buf[2] = @as(u8, @intCast(tls13.len + label.len)); buf[3..][0..tls13.len].* = tls13.*; var i: usize = 3 + tls13.len; @memcpy(buf[i..][0..label.len], label); i += label.len; - buf[i] = @intCast(u8, context.len); + buf[i] = @as(u8, @intCast(context.len)); i += 1; @memcpy(buf[i..][0..context.len], context); i += context.len; @@ -411,24 +411,24 @@ pub inline fn enum_array(comptime E: type, comptime tags: []const E) [2 + @sizeO assert(@sizeOf(E) == 2); var result: [tags.len * 2]u8 = undefined; for (tags, 0..) |elem, i| { - result[i * 2] = @truncate(u8, @intFromEnum(elem) >> 8); - result[i * 2 + 1] = @truncate(u8, @intFromEnum(elem)); + result[i * 2] = @as(u8, @truncate(@intFromEnum(elem) >> 8)); + result[i * 2 + 1] = @as(u8, @truncate(@intFromEnum(elem))); } return array(2, result); } pub inline fn int2(x: u16) [2]u8 { return .{ - @truncate(u8, x >> 8), - @truncate(u8, x), + @as(u8, @truncate(x >> 8)), + @as(u8, @truncate(x)), }; } pub inline fn int3(x: u24) [3]u8 { return .{ - @truncate(u8, x >> 16), - @truncate(u8, x >> 8), - @truncate(u8, x), + @as(u8, @truncate(x >> 16)), + @as(u8, @truncate(x >> 8)), + @as(u8, @truncate(x)), }; } @@ -513,7 +513,7 @@ pub const Decoder = struct { .Enum => |info| { const int = d.decode(info.tag_type); if (info.is_exhaustive) @compileError("exhaustive enum cannot be used"); - return @enumFromInt(T, int); + return @as(T, @enumFromInt(int)); }, else => @compileError("unsupported type: " ++ @typeName(T)), } diff --git a/lib/std/crypto/tls/Client.zig b/lib/std/crypto/tls/Client.zig index 94ecf0d3ef..6d5bb86fed 100644 --- a/lib/std/crypto/tls/Client.zig +++ b/lib/std/crypto/tls/Client.zig @@ -140,7 +140,7 @@ pub fn InitError(comptime Stream: type) type { /// /// `host` is only borrowed during this function call. pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) InitError(@TypeOf(stream))!Client { - const host_len = @intCast(u16, host.len); + const host_len = @as(u16, @intCast(host.len)); var random_buffer: [128]u8 = undefined; crypto.random.bytes(&random_buffer); @@ -194,7 +194,7 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In int2(host_len); const extensions_header = - int2(@intCast(u16, extensions_payload.len + host_len)) ++ + int2(@as(u16, @intCast(extensions_payload.len + host_len))) ++ extensions_payload; const legacy_compression_methods = 0x0100; @@ -209,13 +209,13 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In const out_handshake = [_]u8{@intFromEnum(tls.HandshakeType.client_hello)} ++ - int3(@intCast(u24, client_hello.len + host_len)) ++ + int3(@as(u24, @intCast(client_hello.len + host_len))) ++ client_hello; const plaintext_header = [_]u8{ @intFromEnum(tls.ContentType.handshake), 0x03, 0x01, // legacy_record_version - } ++ int2(@intCast(u16, out_handshake.len + host_len)) ++ out_handshake; + } ++ int2(@as(u16, @intCast(out_handshake.len + host_len))) ++ out_handshake; { var iovecs = [_]std.os.iovec_const{ @@ -457,7 +457,7 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In const auth_tag = record_decoder.array(P.AEAD.tag_length).*; const V = @Vector(P.AEAD.nonce_length, u8); const pad = [1]u8{0} ** (P.AEAD.nonce_length - 8); - const operand: V = pad ++ @bitCast([8]u8, big(read_seq)); + const operand: V = pad ++ @as([8]u8, @bitCast(big(read_seq))); read_seq += 1; const nonce = @as(V, p.server_handshake_iv) ^ operand; P.AEAD.decrypt(cleartext, ciphertext, auth_tag, record_header, nonce, p.server_handshake_key) catch @@ -466,7 +466,7 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In }, }; - const inner_ct = @enumFromInt(tls.ContentType, cleartext[cleartext.len - 1]); + const inner_ct = @as(tls.ContentType, @enumFromInt(cleartext[cleartext.len - 1])); if (inner_ct != .handshake) return error.TlsUnexpectedMessage; var ctd = tls.Decoder.fromTheirSlice(cleartext[0 .. cleartext.len - 1]); @@ -520,7 +520,7 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In const subject_cert: Certificate = .{ .buffer = certd.buf, - .index = @intCast(u32, certd.idx), + .index = @as(u32, @intCast(certd.idx)), }; const subject = try subject_cert.parse(); if (cert_index == 0) { @@ -534,7 +534,7 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In if (pub_key.len > main_cert_pub_key_buf.len) return error.CertificatePublicKeyInvalid; @memcpy(main_cert_pub_key_buf[0..pub_key.len], pub_key); - main_cert_pub_key_len = @intCast(@TypeOf(main_cert_pub_key_len), pub_key.len); + main_cert_pub_key_len = @as(@TypeOf(main_cert_pub_key_len), @intCast(pub_key.len)); } else { try prev_cert.verify(subject, now_sec); } @@ -679,7 +679,7 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In .write_seq = 0, .partial_cleartext_idx = 0, .partial_ciphertext_idx = 0, - .partial_ciphertext_end = @intCast(u15, leftover.len), + .partial_ciphertext_end = @as(u15, @intCast(leftover.len)), .received_close_notify = false, .application_cipher = app_cipher, .partially_read_buffer = undefined, @@ -797,11 +797,11 @@ fn prepareCiphertextRecord( const overhead_len = tls.record_header_len + P.AEAD.tag_length + 1; const close_notify_alert_reserved = tls.close_notify_alert.len + overhead_len; while (true) { - const encrypted_content_len = @intCast(u16, @min( + const encrypted_content_len = @as(u16, @intCast(@min( @min(bytes.len - bytes_i, max_ciphertext_len - 1), ciphertext_buf.len - close_notify_alert_reserved - overhead_len - ciphertext_end, - )); + ))); if (encrypted_content_len == 0) return .{ .iovec_end = iovec_end, .ciphertext_end = ciphertext_end, @@ -826,7 +826,7 @@ fn prepareCiphertextRecord( const auth_tag = ciphertext_buf[ciphertext_end..][0..P.AEAD.tag_length]; ciphertext_end += auth_tag.len; const pad = [1]u8{0} ** (P.AEAD.nonce_length - 8); - const operand: V = pad ++ @bitCast([8]u8, big(c.write_seq)); + const operand: V = pad ++ @as([8]u8, @bitCast(big(c.write_seq))); c.write_seq += 1; // TODO send key_update on overflow const nonce = @as(V, p.client_iv) ^ operand; P.AEAD.encrypt(ciphertext, auth_tag, cleartext, ad, nonce, p.client_key); @@ -920,7 +920,7 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) // Give away the buffered cleartext we have, if any. const partial_cleartext = c.partially_read_buffer[c.partial_cleartext_idx..c.partial_ciphertext_idx]; if (partial_cleartext.len > 0) { - const amt = @intCast(u15, vp.put(partial_cleartext)); + const amt = @as(u15, @intCast(vp.put(partial_cleartext))); c.partial_cleartext_idx += amt; if (c.partial_cleartext_idx == c.partial_ciphertext_idx and @@ -1037,7 +1037,7 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) in = 0; continue; } - const ct = @enumFromInt(tls.ContentType, frag[in]); + const ct = @as(tls.ContentType, @enumFromInt(frag[in])); in += 1; const legacy_version = mem.readIntBig(u16, frag[in..][0..2]); in += 2; @@ -1070,8 +1070,8 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) switch (ct) { .alert => { if (in + 2 > frag.len) return error.TlsDecodeError; - const level = @enumFromInt(tls.AlertLevel, frag[in]); - const desc = @enumFromInt(tls.AlertDescription, frag[in + 1]); + const level = @as(tls.AlertLevel, @enumFromInt(frag[in])); + const desc = @as(tls.AlertDescription, @enumFromInt(frag[in + 1])); _ = level; try desc.toError(); @@ -1089,7 +1089,7 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) in += ciphertext_len; const auth_tag = frag[in..][0..P.AEAD.tag_length].*; const pad = [1]u8{0} ** (P.AEAD.nonce_length - 8); - const operand: V = pad ++ @bitCast([8]u8, big(c.read_seq)); + const operand: V = pad ++ @as([8]u8, @bitCast(big(c.read_seq))); const nonce: [P.AEAD.nonce_length]u8 = @as(V, p.server_iv) ^ operand; const out_buf = vp.peek(); const cleartext_buf = if (ciphertext.len <= out_buf.len) @@ -1105,11 +1105,11 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) c.read_seq = try std.math.add(u64, c.read_seq, 1); - const inner_ct = @enumFromInt(tls.ContentType, cleartext[cleartext.len - 1]); + const inner_ct = @as(tls.ContentType, @enumFromInt(cleartext[cleartext.len - 1])); switch (inner_ct) { .alert => { - const level = @enumFromInt(tls.AlertLevel, cleartext[0]); - const desc = @enumFromInt(tls.AlertDescription, cleartext[1]); + const level = @as(tls.AlertLevel, @enumFromInt(cleartext[0])); + const desc = @as(tls.AlertDescription, @enumFromInt(cleartext[1])); if (desc == .close_notify) { c.received_close_notify = true; c.partial_ciphertext_end = c.partial_ciphertext_idx; @@ -1124,7 +1124,7 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) .handshake => { var ct_i: usize = 0; while (true) { - const handshake_type = @enumFromInt(tls.HandshakeType, cleartext[ct_i]); + const handshake_type = @as(tls.HandshakeType, @enumFromInt(cleartext[ct_i])); ct_i += 1; const handshake_len = mem.readIntBig(u24, cleartext[ct_i..][0..3]); ct_i += 3; @@ -1148,7 +1148,7 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) } c.read_seq = 0; - switch (@enumFromInt(tls.KeyUpdateRequest, handshake[0])) { + switch (@as(tls.KeyUpdateRequest, @enumFromInt(handshake[0]))) { .update_requested => { switch (c.application_cipher) { inline else => |*p| { @@ -1186,13 +1186,13 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) c.partially_read_buffer[c.partial_ciphertext_idx..][0..msg.len], msg, ); - c.partial_ciphertext_idx = @intCast(@TypeOf(c.partial_ciphertext_idx), c.partial_ciphertext_idx + msg.len); + c.partial_ciphertext_idx = @as(@TypeOf(c.partial_ciphertext_idx), @intCast(c.partial_ciphertext_idx + msg.len)); } else { const amt = vp.put(msg); if (amt < msg.len) { const rest = msg[amt..]; c.partial_cleartext_idx = 0; - c.partial_ciphertext_idx = @intCast(@TypeOf(c.partial_ciphertext_idx), rest.len); + c.partial_ciphertext_idx = @as(@TypeOf(c.partial_ciphertext_idx), @intCast(rest.len)); @memcpy(c.partially_read_buffer[0..rest.len], rest); } } @@ -1220,12 +1220,12 @@ fn finishRead(c: *Client, frag: []const u8, in: usize, out: usize) usize { const saved_buf = frag[in..]; if (c.partial_ciphertext_idx > c.partial_cleartext_idx) { // There is cleartext at the beginning already which we need to preserve. - c.partial_ciphertext_end = @intCast(@TypeOf(c.partial_ciphertext_end), c.partial_ciphertext_idx + saved_buf.len); + c.partial_ciphertext_end = @as(@TypeOf(c.partial_ciphertext_end), @intCast(c.partial_ciphertext_idx + saved_buf.len)); @memcpy(c.partially_read_buffer[c.partial_ciphertext_idx..][0..saved_buf.len], saved_buf); } else { c.partial_cleartext_idx = 0; c.partial_ciphertext_idx = 0; - c.partial_ciphertext_end = @intCast(@TypeOf(c.partial_ciphertext_end), saved_buf.len); + c.partial_ciphertext_end = @as(@TypeOf(c.partial_ciphertext_end), @intCast(saved_buf.len)); @memcpy(c.partially_read_buffer[0..saved_buf.len], saved_buf); } return out; @@ -1235,14 +1235,14 @@ fn finishRead(c: *Client, frag: []const u8, in: usize, out: usize) usize { fn finishRead2(c: *Client, first: []const u8, frag1: []const u8, out: usize) usize { if (c.partial_ciphertext_idx > c.partial_cleartext_idx) { // There is cleartext at the beginning already which we need to preserve. - c.partial_ciphertext_end = @intCast(@TypeOf(c.partial_ciphertext_end), c.partial_ciphertext_idx + first.len + frag1.len); + c.partial_ciphertext_end = @as(@TypeOf(c.partial_ciphertext_end), @intCast(c.partial_ciphertext_idx + first.len + frag1.len)); // TODO: eliminate this call to copyForwards std.mem.copyForwards(u8, c.partially_read_buffer[c.partial_ciphertext_idx..][0..first.len], first); @memcpy(c.partially_read_buffer[c.partial_ciphertext_idx + first.len ..][0..frag1.len], frag1); } else { c.partial_cleartext_idx = 0; c.partial_ciphertext_idx = 0; - c.partial_ciphertext_end = @intCast(@TypeOf(c.partial_ciphertext_end), first.len + frag1.len); + c.partial_ciphertext_end = @as(@TypeOf(c.partial_ciphertext_end), @intCast(first.len + frag1.len)); // TODO: eliminate this call to copyForwards std.mem.copyForwards(u8, c.partially_read_buffer[0..first.len], first); @memcpy(c.partially_read_buffer[first.len..][0..frag1.len], frag1); diff --git a/lib/std/crypto/utils.zig b/lib/std/crypto/utils.zig index 14a235e418..ab1b6eab6a 100644 --- a/lib/std/crypto/utils.zig +++ b/lib/std/crypto/utils.zig @@ -24,7 +24,7 @@ pub fn timingSafeEql(comptime T: type, a: T, b: T) bool { const s = @typeInfo(C).Int.bits; const Cu = std.meta.Int(.unsigned, s); const Cext = std.meta.Int(.unsigned, s + 1); - return @bitCast(bool, @truncate(u1, (@as(Cext, @bitCast(Cu, acc)) -% 1) >> s)); + return @as(bool, @bitCast(@as(u1, @truncate((@as(Cext, @as(Cu, @bitCast(acc))) -% 1) >> s)))); }, .Vector => |info| { const C = info.child; @@ -35,7 +35,7 @@ pub fn timingSafeEql(comptime T: type, a: T, b: T) bool { const s = @typeInfo(C).Int.bits; const Cu = std.meta.Int(.unsigned, s); const Cext = std.meta.Int(.unsigned, s + 1); - return @bitCast(bool, @truncate(u1, (@as(Cext, @bitCast(Cu, acc)) -% 1) >> s)); + return @as(bool, @bitCast(@as(u1, @truncate((@as(Cext, @as(Cu, @bitCast(acc))) -% 1) >> s)))); }, else => { @compileError("Only arrays and vectors can be compared"); @@ -60,14 +60,14 @@ pub fn timingSafeCompare(comptime T: type, a: []const T, b: []const T, endian: E i -= 1; const x1 = a[i]; const x2 = b[i]; - gt |= @truncate(T, (@as(Cext, x2) -% @as(Cext, x1)) >> bits) & eq; - eq &= @truncate(T, (@as(Cext, (x2 ^ x1)) -% 1) >> bits); + gt |= @as(T, @truncate((@as(Cext, x2) -% @as(Cext, x1)) >> bits)) & eq; + eq &= @as(T, @truncate((@as(Cext, (x2 ^ x1)) -% 1) >> bits)); } } else { for (a, 0..) |x1, i| { const x2 = b[i]; - gt |= @truncate(T, (@as(Cext, x2) -% @as(Cext, x1)) >> bits) & eq; - eq &= @truncate(T, (@as(Cext, (x2 ^ x1)) -% 1) >> bits); + gt |= @as(T, @truncate((@as(Cext, x2) -% @as(Cext, x1)) >> bits)) & eq; + eq &= @as(T, @truncate((@as(Cext, (x2 ^ x1)) -% 1) >> bits)); } } if (gt != 0) { @@ -102,7 +102,7 @@ pub fn timingSafeAdd(comptime T: type, a: []const T, b: []const T, result: []T, carry = ov1[1] | ov2[1]; } } - return @bitCast(bool, carry); + return @as(bool, @bitCast(carry)); } /// Subtract two integers serialized as arrays of the same size, in constant time. @@ -129,7 +129,7 @@ pub fn timingSafeSub(comptime T: type, a: []const T, b: []const T, result: []T, borrow = ov1[1] | ov2[1]; } } - return @bitCast(bool, borrow); + return @as(bool, @bitCast(borrow)); } /// Sets a slice to zeroes. |
