| ... | ... | @@ -144,7 +144,7 @@ pub const Edwards25519 = struct { |
| 144 | 144 | |
| 145 | 145 | inline fn pcSelect(pc: [16]Edwards25519, b: u8) Edwards25519 { |
| 146 | 146 | var t = Edwards25519.identityElement; |
| 147 | | comptime var i: u8 = 0; |
| 147 | comptime var i: u8 = 1; |
| 148 | 148 | inline while (i < 16) : (i += 1) { |
| 149 | 149 | t.cMov(pc[i], ((@as(usize, b ^ i) -% 1) >> 8) & 1); |
| 150 | 150 | } |
| ... | ... | @@ -155,7 +155,6 @@ pub const Edwards25519 = struct { |
| 155 | 155 | var q = Edwards25519.identityElement; |
| 156 | 156 | var pos: usize = 252; |
| 157 | 157 | while (true) : (pos -= 4) { |
| 158 | | q = q.dbl().dbl().dbl().dbl(); |
| 159 | 158 | const bit = (s[pos >> 3] >> @truncate(u3, pos)) & 0xf; |
| 160 | 159 | if (vartime) { |
| 161 | 160 | if (bit != 0) { |
| ... | ... | @@ -165,6 +164,7 @@ pub const Edwards25519 = struct { |
| 165 | 164 | q = q.add(pcSelect(pc, bit)); |
| 166 | 165 | } |
| 167 | 166 | if (pos == 0) break; |
| 167 | q = q.dbl().dbl().dbl().dbl(); |
| 168 | 168 | } |
| 169 | 169 | try q.rejectIdentity(); |
| 170 | 170 | return q; |
| ... | ... | @@ -181,32 +181,31 @@ pub const Edwards25519 = struct { |
| 181 | 181 | return pc; |
| 182 | 182 | } |
| 183 | 183 | |
| 184 | const basePointPc = comptime pc: { |
| 185 | @setEvalBranchQuota(10000); |
| 186 | break :pc precompute(Edwards25519.basePoint); |
| 187 | }; |
| 188 | |
| 184 | 189 | /// Multiply an Edwards25519 point by a scalar without clamping it. |
| 185 | 190 | /// Return error.WeakPublicKey if the resulting point is |
| 186 | 191 | /// the identity element. |
| 187 | 192 | pub fn mul(p: Edwards25519, s: [32]u8) !Edwards25519 { |
| 188 | | var pc: [16]Edwards25519 = undefined; |
| 189 | | if (p.is_base) { |
| 190 | | @setEvalBranchQuota(10000); |
| 191 | | pc = comptime precompute(Edwards25519.basePoint); |
| 192 | | } else { |
| 193 | | pc = precompute(p); |
| 194 | | pc[4].rejectIdentity() catch |_| return error.WeakPublicKey; |
| 195 | | } |
| 193 | const pc = if (p.is_base) basePointPc else pc: { |
| 194 | const xpc = precompute(p); |
| 195 | xpc[4].rejectIdentity() catch |_| return error.WeakPublicKey; |
| 196 | break :pc xpc; |
| 197 | }; |
| 196 | 198 | return pcMul(pc, s, false); |
| 197 | 199 | } |
| 198 | 200 | |
| 199 | 201 | /// Multiply an Edwards25519 point by a *PUBLIC* scalar *IN VARIABLE TIME* |
| 200 | 202 | /// This can be used for signature verification. |
| 201 | 203 | pub fn mulPublic(p: Edwards25519, s: [32]u8) !Edwards25519 { |
| 202 | | var pc: [16]Edwards25519 = undefined; |
| 203 | | if (p.is_base) { |
| 204 | | @setEvalBranchQuota(10000); |
| 205 | | pc = comptime precompute(Edwards25519.basePoint); |
| 206 | | } else { |
| 207 | | pc = precompute(p); |
| 208 | | pc[4].rejectIdentity() catch |_| return error.WeakPublicKey; |
| 209 | | } |
| 204 | const pc = if (p.is_base) basePointPc else pc: { |
| 205 | const xpc = precompute(p); |
| 206 | xpc[4].rejectIdentity() catch |_| return error.WeakPublicKey; |
| 207 | break :pc xpc; |
| 208 | }; |
| 210 | 209 | return pcMul(pc, s, true); |
| 211 | 210 | } |
| 212 | 211 | |
| ... | ... | @@ -215,18 +214,15 @@ pub const Edwards25519 = struct { |
| 215 | 214 | pub fn mulMulti(comptime count: usize, ps: [count]Edwards25519, ss: [count][32]u8) !Edwards25519 { |
| 216 | 215 | var pcs: [count][16]Edwards25519 = undefined; |
| 217 | 216 | for (ps) |p, i| { |
| 218 | | if (p.is_base) { |
| 219 | | @setEvalBranchQuota(10000); |
| 220 | | pcs[i] = comptime precompute(Edwards25519.basePoint); |
| 221 | | } else { |
| 222 | | pcs[i] = precompute(p); |
| 223 | | pcs[i][4].rejectIdentity() catch |_| return error.WeakPublicKey; |
| 224 | | } |
| 217 | pcs[i] = if (p.is_base) basePointPc else pc: { |
| 218 | const xpc = precompute(p); |
| 219 | xpc[4].rejectIdentity() catch |_| return error.WeakPublicKey; |
| 220 | break :pc xpc; |
| 221 | }; |
| 225 | 222 | } |
| 226 | 223 | var q = Edwards25519.identityElement; |
| 227 | 224 | var pos: usize = 252; |
| 228 | 225 | while (true) : (pos -= 4) { |
| 229 | | q = q.dbl().dbl().dbl().dbl(); |
| 230 | 226 | for (ss) |s, i| { |
| 231 | 227 | const bit = (s[pos >> 3] >> @truncate(u3, pos)) & 0xf; |
| 232 | 228 | if (bit != 0) { |
| ... | ... | @@ -234,6 +230,7 @@ pub const Edwards25519 = struct { |
| 234 | 230 | } |
| 235 | 231 | } |
| 236 | 232 | if (pos == 0) break; |
| 233 | q = q.dbl().dbl().dbl().dbl(); |
| 237 | 234 | } |
| 238 | 235 | try q.rejectIdentity(); |
| 239 | 236 | return q; |