authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2026-10-04 16:56:00+02:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2026-10-04 16:56:00+02:00
log6fcb9e9532637e362c9495204798c21a5ff1c75f
tree3825920a388b45c427432d5b35ba9046450b4712
parente38b151e614eecf572f791b5712d2e6edd33dd84
parentd7fe3b57fcb207532d1e37d6185269112f02910c

Merge pull request 'crypto.ff: hide constant-time selection masks from optimizer' (#37025) from jedisct1/zig:ff-fixes6 into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37025

1 files changed, 13 insertions(+), 1 deletions(-)

lib/std/crypto/ff.zig+13-1
...@@ -1083,12 +1083,24 @@ pub fn Modulus(comptime max_bits: comptime_int) type {...@@ -1083,12 +1083,24 @@ pub fn Modulus(comptime max_bits: comptime_int) type {
1083 };1083 };
1084}1084}
10851085
1086fn valueBarrier(x: Limb) Limb {
1087 if (@inComptime()) return x;
1088 switch (builtin.zig_backend) {
1089 .stage2_c, .stage2_wasm => return x,
1090 else => {},
1091 }
1092 return asm (""
1093 : [ret] "=r" (-> Limb),
1094 : [x] "0" (x),
1095 );
1096}
1097
1086const ct = if (std.options.side_channels_mitigations == .none) ct_unprotected else ct_protected;1098const ct = if (std.options.side_channels_mitigations == .none) ct_unprotected else ct_protected;
10871099
1088const ct_protected = struct {1100const ct_protected = struct {
1089 // Returns x if on is true, otherwise y.1101 // Returns x if on is true, otherwise y.
1090 fn select(on: bool, x: Limb, y: Limb) Limb {1102 fn select(on: bool, x: Limb, y: Limb) Limb {
1091 const mask = @as(Limb, 0) -% @intFromBool(on);1103 const mask = valueBarrier(@as(Limb, 0) -% @intFromBool(on));
1092 return y ^ (mask & (y ^ x));1104 return y ^ (mask & (y ^ x));
1093 }1105 }
10941106