authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-10-28 08:24:12-07:00
committergravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-10-28 15:21:10-07:00
logf9fe548e41a41e3edcff4d30f495246d0fee145b
tree30a80aa37a47e5cd4f58b62df331506b12ac6f43
parent4c1f71e866088a1a2e943331256115ed7e3daf98

std.crypto: Add `isComptime` guard around intrinsics

Comptime code can't execute assembly code, so we need some way to force comptime code to use the generic path. This should be replaced with whatever is implemented for #868, when that day comes. I am seeing that the result for the hash is incorrect in stage1 and crashes stage2, so presumably this never worked correctly. I will follow up on that soon.

1 files changed, 8 insertions(+), 2 deletions(-)

lib/std/crypto/sha2.zig+8-2
......@@ -71,6 +71,12 @@ const Sha256Params = Sha2Params32{
7171
7272const v4u32 = @Vector(4, u32);
7373
74// TODO: Remove once https://github.com/ziglang/zig/issues/868 is resolved.
75fn isComptime() bool {
76 var a: u8 = 0;
77 return @typeInfo(@TypeOf(.{a})).Struct.fields[0].is_comptime;
78}
79
7480/// SHA-224
7581pub const Sha224 = Sha2x32(Sha224Params);
7682
......@@ -187,7 +193,7 @@ fn Sha2x32(comptime params: Sha2Params32) type {
187193 }
188194
189195 switch (builtin.cpu.arch) {
190 .aarch64 => if (comptime builtin.cpu.features.isEnabled(@enumToInt(std.Target.aarch64.Feature.sha2))) {
196 .aarch64 => if (!isComptime() and comptime builtin.cpu.features.isEnabled(@enumToInt(std.Target.aarch64.Feature.sha2))) {
191197 var x: v4u32 = d.s[0..4].*;
192198 var y: v4u32 = d.s[4..8].*;
193199 const s_v = @ptrCast(*[16]v4u32, &s);
......@@ -224,7 +230,7 @@ fn Sha2x32(comptime params: Sha2Params32) type {
224230 d.s[4..8].* = y +% @as(v4u32, d.s[4..8].*);
225231 return;
226232 },
227 .x86_64 => if (comptime builtin.cpu.features.isEnabled(@enumToInt(std.Target.x86.Feature.sha))) {
233 .x86_64 => if (!isComptime() and comptime builtin.cpu.features.isEnabled(@enumToInt(std.Target.x86.Feature.sha))) {
228234 var x: v4u32 = [_]u32{ d.s[5], d.s[4], d.s[1], d.s[0] };
229235 var y: v4u32 = [_]u32{ d.s[7], d.s[6], d.s[3], d.s[2] };
230236 const s_v = @ptrCast(*[16]v4u32, &s);