Skip to content

Commit fdcc161

Browse files
committed
cleanup bn254 mulScalar
1 parent 6b2a1dd commit fdcc161

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/crypto/bn254/lib.zig

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub const G2 = struct {
221221
// G2 does *not* have prime order, so we need to perform a secondary subgroup membership check.
222222
// https://eprint.iacr.org/2022/348, Sec 3.1.
223223
// [r]P == 0 <==> [x+1]P + ψ([x]P) + ψ²([x]P) = ψ³([2x]P)
224-
const xp: G2 = mulScalar(p, @bitCast(Fp.constants.x));
224+
const xp: G2 = mulScalar(p, Fp.constants.x);
225225

226226
const psi = xp.frob();
227227
const psi2 = xp.frob2();
@@ -464,16 +464,17 @@ fn dbl(p: anytype) @TypeOf(p) {
464464
///
465465
/// https://encrypt.a41.io/primitives/abstract-algebra/elliptic-curve/scalar-multiplication/double-and-add
466466
/// https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Double-and-add
467-
fn mulScalar(a: anytype, scalar: [4]u64) @TypeOf(a) {
467+
fn mulScalar(a: anytype, scalar: u256) @TypeOf(a) {
468468
// TODO: can be further optimized with GLV and wNAF
469-
const leading = @clz(@as(u256, @bitCast(scalar)));
469+
const limbs: [4]u64 = @bitCast(scalar);
470+
const leading = @clz(scalar);
470471
if (leading == 256) return .zero;
471472
var i: u8 = @intCast(256 - 1 - leading);
472473
var r = a;
473474
while (i > 0) {
474475
i -= 1;
475476
r = dbl(r);
476-
if (bit(scalar, i)) r = addMixed(r, a);
477+
if (bit(limbs, i)) r = addMixed(r, a);
477478
}
478479
return r;
479480
}
@@ -488,7 +489,7 @@ pub fn addSyscall(out: *[64]u8, input: *const [128]u8) !void {
488489
pub fn mulSyscall(out: *[64]u8, input: *const [96]u8) !void {
489490
const a: G1 = try .fromBytes(input[0..64]);
490491
// Scalar is provided in big-endian and we do *not* validate it.
491-
const b: [4]u64 = @bitCast(Fp.byteSwap(input[64..][0..32].*));
492+
const b: u256 = @bitCast(Fp.byteSwap(input[64..][0..32].*));
492493
const result = mulScalar(a, b);
493494
result.toBytes(out);
494495
}

0 commit comments

Comments
 (0)