From fa2654cdb5f73c2f50d2d8c1b9d5e2f236c9f77f Mon Sep 17 00:00:00 2001 From: src48597962 <48597962@qq.com> Date: Tue, 7 Mar 2023 09:26:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20'test.js'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test.js | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/test.js b/test.js index 804deba4..ec102951 100644 --- a/test.js +++ b/test.js @@ -19,4 +19,49 @@ function generateKeyPair() { publicKey: '04' + publicKey.x.toString(16, 64) + publicKey.y.toString(16, 64) }; } -log('aaa'); \ No newline at end of file +function hexToBigInt(hex) { + return BigInt('0x' + hex.slice(2)); +} +function pointMultiply(G, d) { + let Q = null; + let k = 1; + let k2 = 0; + while (d > 0) { + if (d & 1) { + if (Q === null) { + Q = G; + } else { + Q = pointAdd(Q, G, curve); + } + k2 = k; + k = 0; + } + G = pointAdd(G, G, curve); + d >>= 1; + k += k; + } + if (Q === null) { + Q = pointMultiply(G, 0n); + } else { + Q = pointMultiply(Q, k2); + } + return Q; +} +function pointAdd(P, Q, curve) { + const m = ((P.y - Q.y) * modInverse(P.x - Q.x, curve.p)) %!c(MISSING)urve.p; + const x = (m * m - P.x - Q.x) %!c(MISSING)urve.p; + const y = (m * (P.x - x) - P.y) %!c(MISSING)urve.p; + return { x, y }; +} +function modInverse(a, m) { + a = a %!m(MISSING); + for (let x = 1n; x < m; x++) { + if ((a * x) %!m(MISSING) === 1n) { + return x; + } + } + return 1n; +} +const keyPair = generateKeyPair(); +console.log('Private key:', keyPair.privateKey); +console.log('Public key:', keyPair.publicKey); \ No newline at end of file