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-10-10 12:42:34 +03:00
|
|
|
const Y = await hashToCurve(secretMessage)
|
2022-10-28 19:34:04 +02:00
|
|
|
const r = bytesToNumber(nobleSecp256k1.utils.randomPrivateKey())
|
|
|
|
const P = nobleSecp256k1.Point.fromPrivateKey(r)
|
2022-10-10 12:42:34 +03:00
|
|
|
const B_ = Y.add(P)
|
2022-10-28 19:34:04 +02:00
|
|
|
return {B_: B_.toHex(true), r}
|
2022-10-10 12:42:34 +03:00
|
|
|
}
|
|
|
|
|
2022-10-28 19:34:04 +02:00
|
|
|
function step3Alice(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
|
|
|
|
}
|