Created
February 13, 2024 10:50
-
-
Save msinkec/025c7ff90f8945dfb5ce4f93af1af1a3 to your computer and use it in GitHub Desktop.
Create Sig and PubKey with unknown PrivKey
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
import { bsv } from 'scrypt-ts' | |
async function main() { | |
const m = bsv.PrivateKey.fromRandom().bn | |
const hBuff = bsv.crypto.Hash.sha256(m.toBuffer()) | |
const h = bsv.crypto.BN.fromBuffer(hBuff) | |
const G = bsv.crypto.Point.getG() | |
const n = bsv.crypto.Point.getN() | |
const s = bsv.PrivateKey.fromRandom().bn | |
let r: bsv.crypto.BN = new bsv.crypto.BN(0) | |
let R: bsv.crypto.Point = G | |
let isValidR = false | |
while (!isValidR) { | |
r = bsv.PrivateKey.fromRandom().bn | |
try { | |
R = bsv.crypto.Point.fromX(false, r) | |
isValidR = true | |
} catch (e) { | |
isValidR = false | |
} | |
} | |
const sR = R.mul(s) | |
const hG = G.mul(h) | |
const sR_hG = sR.add(hG.neg()) | |
const inv_r = r.invm(n) | |
const Y = sR_hG.mul(inv_r) | |
const pubKey = new bsv.PublicKey(Y) | |
const sig = new bsv.crypto.Signature(r, s) | |
const valid = bsv.crypto.ECDSA.verify( | |
hBuff, | |
sig, | |
pubKey | |
) | |
console.log(pubKey.toString()) | |
console.log(sig.toString()) | |
console.log(valid) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment