Created
January 25, 2023 02:10
-
-
Save paulmillr/178042240169f0f531f8cc95e532f9db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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