From bb9c784b04f0c8ccb0c1991de7e677bbce32a074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8r=E2=88=82=C2=A1?= Date: Fri, 5 Feb 2021 22:51:33 +0100 Subject: [PATCH] Padding the hex strings being hashed to 32 bytes --- src/index.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1bc1d58..a9beaf8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -93,8 +93,8 @@ export function blind(m: BigInteger, signerR: Point): { mBlinded: BigInteger, us const ainvrx = ainv.multiply(rx) const mHex = m.toString(16) - const hHex = keccak256('0x' + mHex) - const h = BigInteger.fromHex(hHex.slice(2)) + const hHex = keccak256('0x' + zeroPad(mHex, 32)).substr(2) + const h = BigInteger.fromHex(hHex) const mBlinded = ainvrx.multiply(h) return { mBlinded: mBlinded.mod(n), userSecretData: u } @@ -121,8 +121,8 @@ export function verify(m: BigInteger, s: UnblindedSignature, q: Point) { const sG = G.multiply(s.s) const mHex = m.toString(16) - const hHex = keccak256('0x' + mHex) - const h = BigInteger.fromHex(hHex.slice(2)) + const hHex = keccak256('0x' + zeroPad(mHex, 32)).substr(2) + const h = BigInteger.fromHex(hHex) const rx = s.f.affineX.mod(n) const right = s.f.add( @@ -147,3 +147,11 @@ function random(bytes: number) { } while (k.toString() == '0' && k.gcd(n).toString() != '1') return k } + +function zeroPad(hexString: string, byteLength: number) { + if (hexString.length > (byteLength * 2)) throw new Error("Out of bounds") + while (hexString.length < (byteLength * 2)) { + hexString = "0" + hexString + } + return hexString +}