Skip to content

Commit

Permalink
refactor: make everything async
Browse files Browse the repository at this point in the history
  • Loading branch information
feri42 committed Nov 28, 2024
1 parent 79a545c commit a72f683
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions features/keychain/module/crypto/schnorr-z.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { hashSync } from '@exodus/crypto/hash'
import { hmacSync } from '@exodus/crypto/hmac'
import { hash } from '@exodus/crypto/hash'
import { hmac } from '@exodus/crypto/hmac'
import { randomBytes } from '@exodus/crypto/randomBytes'
import * as secp256k1 from '@noble/secp256k1'

function singleRoundHmacDRBG(nonce) {
async function singleRoundHmacDRBG(nonce) {
const seed = randomBytes(32)
let K = Buffer.alloc(32, 0)
let V = Buffer.alloc(32, 1)
K = hmacSync('sha256', K, [V, new Uint8Array([0]), seed, nonce])
V = hmacSync('sha256', K, V)
K = hmacSync('sha256', K, [V, new Uint8Array([1]), seed, nonce])
V = hmacSync('sha256', K, V)
return hmacSync('sha256', K, V)
K = await hmac('sha256', K, [V, new Uint8Array([0]), seed, nonce])
V = await hmac('sha256', K, V)
K = await hmac('sha256', K, [V, new Uint8Array([1]), seed, nonce])
V = await hmac('sha256', K, V)
return hmac('sha256', K, V)
}

/**
Expand All @@ -33,22 +33,22 @@ function singleRoundHmacDRBG(nonce) {
* @param {Buffer} privateKey
* @returns {string}
*/
export function schnorrZ({ data, privateKey }) {
export async function schnorrZ({ data, privateKey }) {
const { utils, Signature, CURVE, getPublicKey } = secp256k1
const big = (buf) => BigInt('0x' + buf.toString('hex'))

const pk = getPublicKey(privateKey, true)
const pk = await getPublicKey(privateKey, true)

// eslint-disable-next-line no-constant-condition
while (true) {
// 1. k comes from drbg until satisfies 0 < k < n
const k = singleRoundHmacDRBG(data)
const k = await singleRoundHmacDRBG(data)
const kn = big(k)
if (!(kn > BigInt(0) && kn < CURVE.n)) continue // this is rechecked below

const Q = getPublicKey(k, true) // 2. This is Q = G * k multiplication. Also checks 0 < k < n and throws

const r = utils.mod(big(hashSync('sha256', [Q, pk, data])), CURVE.n) // 3
const Q = await getPublicKey(k, true) // 2. This is Q = G * k multiplication. Also checks 0 < k < n and throws
const H = await hash('sha256', [Q, pk, data])
const r = utils.mod(big(H), CURVE.n) // 3
if (r === BigInt(0)) continue // 4

const s = utils.mod(kn - r * big(privateKey), CURVE.n) // 5
Expand Down

0 comments on commit a72f683

Please sign in to comment.