| ... | ... | @@ -1083,12 +1083,24 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 1083 | 1083 | }; |
| 1084 | 1084 | } |
| 1085 | 1085 | |
| 1086 | fn 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 | |
| 1086 | 1098 | const ct = if (std.options.side_channels_mitigations == .none) ct_unprotected else ct_protected; |
| 1087 | 1099 | |
| 1088 | 1100 | const ct_protected = struct { |
| 1089 | 1101 | // Returns x if on is true, otherwise y. |
| 1090 | 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 | 1104 | return y ^ (mask & (y ^ x)); |
| 1093 | 1105 | } |
| 1094 | 1106 | |