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

37 lines
1 KiB
JavaScript
Raw Normal View History

2022-10-10 12:42:34 +03:00
async function hashToCurve(secretMessage) {
2022-10-10 22:56:03 +03:00
console.log(
'### secretMessage',
nobleSecp256k1.utils.bytesToHex(secretMessage)
)
2022-10-10 12:42:34 +03:00
let point
while (!point) {
const hash = await nobleSecp256k1.utils.sha256(secretMessage)
2022-10-10 22:56:03 +03:00
const hashHex = nobleSecp256k1.utils.bytesToHex(hash)
const pointX = '02' + hashHex
console.log('### pointX', pointX)
2022-10-10 12:42:34 +03:00
try {
2022-10-10 22:56:03 +03:00
point = nobleSecp256k1.Point.fromHex(pointX)
console.log('### point', point.toHex())
2022-10-10 12:42:34 +03:00
} catch (error) {
2022-10-10 22:56:03 +03:00
secretMessage = await nobleSecp256k1.utils.sha256(secretMessage)
2022-10-10 12:42:34 +03:00
}
}
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) {
2022-10-10 22:56:03 +03:00
const rInt = BigInt(r)
const C = C_.subtract(A.multiply(rInt))
2022-10-10 12:42:34 +03:00
return C
}