Skip to content

Instantly share code, notes, and snippets.

@paulmillr
Created January 25, 2023 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulmillr/178042240169f0f531f8cc95e532f9db to your computer and use it in GitHub Desktop.
Save paulmillr/178042240169f0f531f8cc95e532f9db to your computer and use it in GitHub Desktop.
multiplyAndAddUnsafe(Q: ProjectivePoint, a: bigint, b: bigint): ProjectivePoint | undefined {
// R=R*2+P //bits from a,b = 1,0
// R=R*2 //bits from a,b = 0,0
// R=R*2+Q //bits from a,b = 0,1
// R=R*2+P+Q //bits from a,b = 1,1
const G: ProjectivePoint = this;
const spl = (a: bigint) => a.toString(2).padStart(max, '0').split('').map(i => Number(i));
const max = Math.max(ut.bitLen(a), ut.bitLen(b));
const aBits = spl(a);
const bBits = spl(b);
const choices = [ProjectivePoint.ZERO, G, Q, G.add(Q)]; // 0, G, Q, G+Q
const select = (bitNumber: number) => choices[aBits[bitNumber] + (bBits[bitNumber] * 2)];
let R = select(0);
for (let i = 1; i < max; i++) {
R = R.double()
R = R.add(select(i));
}
return R.is0() ? undefined : R;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment