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