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

40 lines
1.2 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
}
2022-10-28 19:34:04 +02:00
async function step1Alice(secretMessage) {
2022-11-03 15:15:09 +02:00
// todo: document & validate `secretMessage` format
secretMessage = uint8ToBase64.encode(secretMessage)
2022-11-04 17:17:57 +01:00
secretMessage = new TextEncoder().encode(secretMessage)
2022-10-10 12:42:34 +03:00
const Y = await hashToCurve(secretMessage)
2022-11-02 23:43:37 +01:00
const rpk = nobleSecp256k1.utils.randomPrivateKey()
const r = bytesToNumber(rpk)
2022-10-28 19:34:04 +02:00
const P = nobleSecp256k1.Point.fromPrivateKey(r)
2022-10-10 12:42:34 +03:00
const B_ = Y.add(P)
2022-11-02 23:43:37 +01:00
return {B_: B_.toHex(true), r: nobleSecp256k1.utils.bytesToHex(rpk)}
2022-10-10 12:42:34 +03:00
}
2022-10-28 19:34:04 +02:00
function step3Alice(C_, r, A) {
2022-11-02 23:43:37 +01:00
// const rInt = BigInt(r)
const rInt = bytesToNumber(r)
2022-10-10 22:56:03 +03:00
const C = C_.subtract(A.multiply(rInt))
2022-10-10 12:42:34 +03:00
return C
}