lnbits-legend/lnbits/extensions/cashu/static/js/dhke.js

32 lines
915 B
JavaScript
Raw Normal View History

2022-10-10 12:42:34 +03:00
async function hashToCurve(secretMessage) {
let point
while (!point) {
const hash = await nobleSecp256k1.utils.sha256(secretMessage)
try {
point = nobleSecp256k1.Point.fromHex(hash)
} catch (error) {
// console.error(error)
// const x = bytesToNumber(hash) + ''
// const msg = await nobleSecp256k1.utils.sha256(x)
secretMessage = await nobleSecp256k1.utils.sha256(hash)
// secretMessage = nobleSecp256k1.utils.bytesToHex(msg)
}
}
return point
}
async function step1Bob(secretMessage) {
const Y = await hashToCurve(secretMessage)
const randomBlindingFactor = bytesToNumber(
nobleSecp256k1.utils.randomPrivateKey()
)
const P = nobleSecp256k1.Point.fromPrivateKey(randomBlindingFactor)
const B_ = Y.add(P)
2022-10-10 13:06:11 +03:00
return {B_: B_.toHex(true), randomBlindingFactor}
2022-10-10 12:42:34 +03:00
}
function step3Bob(C_, r, A) {
const C = C_.subtract(A.multiply(r))
return C
}