From 1c8f9e6ffe341f5184486e505c3e85c5c00582c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8r=E2=88=82=C2=A1?= Date: Thu, 4 Feb 2021 14:03:32 +0100 Subject: [PATCH] Using up to date libraries for EC and BigNumber --- package-lock.json | 208 +++++++++++++++++++++++++++++++-------------- package.json | 9 +- src/index.ts | 97 +++++++++++---------- test/index.test.ts | 31 ++++--- 4 files changed, 222 insertions(+), 123 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0cbc15c..1a36183 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,14 +10,13 @@ "license": "GPL-3.0", "dependencies": { "@ethersproject/keccak256": "5.0.7", - "bigi": "^1.4.2", - "bignumber.js": "7.0.2", - "ecurve": "1.0.0" + "bn.js": "^5.1.3", + "elliptic": "^6.5.4" }, "devDependencies": { - "@types/bigi": "^1.4.2", + "@types/bn.js": "^5.1.0", "@types/chai": "^4.2.14", - "@types/ecurve": "^1.0.0", + "@types/elliptic": "^6.4.12", "@types/mocha": "^8.2.0", "@types/node": "^14.14.25", "chai": "^4.2.0", @@ -147,11 +146,14 @@ "resolved": "https://registry.npmjs.org/@ethersproject/logger/-/logger-5.0.8.tgz", "integrity": "sha512-SkJCTaVTnaZ3/ieLF5pVftxGEFX56pTH+f2Slrpv7cU0TNpUZNib84QQdukd++sWUp/S7j5t5NW+WegbXd4U/A==" }, - "node_modules/@types/bigi": { - "version": "1.4.2", - "resolved": "https://registry.npmjs.org/@types/bigi/-/bigi-1.4.2.tgz", - "integrity": "sha512-St8Vm0x1ApYlU9yNaFx3jBis5JVU6oR/5Xtgvn8+N8Ts8f3ze6kOvAAg0aNkbGMGhhG6PrP0nMOgDI9NMFETkA==", - "dev": true + "node_modules/@types/bn.js": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@types/bn.js/-/bn.js-5.1.0.tgz", + "integrity": "sha512-QSSVYj7pYFN49kW77o2s9xTCwZ8F2xLbjLLSEVh8D2F4JUhZtPAGOFLTD+ffqksBx/u4cE/KImFjyhqCjn/LIA==", + "dev": true, + "dependencies": { + "@types/node": "*" + } }, "node_modules/@types/chai": { "version": "4.2.14", @@ -159,14 +161,13 @@ "integrity": "sha512-G+ITQPXkwTrslfG5L/BksmbLUA0M1iybEsmCWPqzSxsRRhJZimBKJkoMi8fr/CPygPTj4zO5pJH7I2/cm9M7SQ==", "dev": true }, - "node_modules/@types/ecurve": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/@types/ecurve/-/ecurve-1.0.0.tgz", - "integrity": "sha1-DR/OAi2LqyvEurKMXXgbZCEAGMQ=", + "node_modules/@types/elliptic": { + "version": "6.4.12", + "resolved": "https://registry.npmjs.org/@types/elliptic/-/elliptic-6.4.12.tgz", + "integrity": "sha512-gP1KsqoouLJGH6IJa28x7PXb3cRqh83X8HCLezd2dF+XcAIMKYv53KV+9Zn6QA561E120uOqZBQ+Jy/cl+fviw==", "dev": true, "dependencies": { - "@types/bigi": "*", - "@types/node": "*" + "@types/bn.js": "*" } }, "node_modules/@types/mocha": { @@ -263,19 +264,6 @@ "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", "dev": true }, - "node_modules/bigi": { - "version": "1.4.2", - "resolved": "https://registry.npmjs.org/bigi/-/bigi-1.4.2.tgz", - "integrity": "sha1-nGZalfiLiwj8Bc/XMfVhhZ1yWCU=" - }, - "node_modules/bignumber.js": { - "version": "7.0.2", - "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-7.0.2.tgz", - "integrity": "sha512-TosM7Yg1Ux0ZCNwwS/tW95r3q9xIZstgsUGKWaez0Cgq8Oy3qia9RGvyG/fbxlQAvigjza1d057QNQLGvYXCeg==", - "engines": { - "node": "*" - } - }, "node_modules/binary-extensions": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", @@ -285,6 +273,11 @@ "node": ">=8" } }, + "node_modules/bn.js": { + "version": "5.1.3", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-5.1.3.tgz", + "integrity": "sha512-GkTiFpjFtUzU9CbMeJ5iazkCzGL3jrhzerzZIuqLABjbwRaFt33I9tUdSNryIptM+RxDet6OKm2WnLXzW51KsQ==" + }, "node_modules/brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -307,6 +300,11 @@ "node": ">=8" } }, + "node_modules/brorand": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/brorand/-/brorand-1.1.0.tgz", + "integrity": "sha1-EsJe/kCkXjwyPrhnWgoM5XsiNx8=" + }, "node_modules/browser-stdout": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz", @@ -531,14 +529,25 @@ "node": ">=0.3.1" } }, - "node_modules/ecurve": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/ecurve/-/ecurve-1.0.0.tgz", - "integrity": "sha1-Ms/Vzl9CHpNRIGoz1OPP0280ZaQ=", + "node_modules/elliptic": { + "version": "6.5.4", + "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.5.4.tgz", + "integrity": "sha512-iLhC6ULemrljPZb+QutR5TQGB+pdW6KGD5RSegS+8sorOZT+rdQFbsQFJgvN3eRqNALqJer4oQ16YvJHlU8hzQ==", "dependencies": { - "bigi": "^1.1.0" + "bn.js": "^4.11.9", + "brorand": "^1.1.0", + "hash.js": "^1.0.0", + "hmac-drbg": "^1.0.1", + "inherits": "^2.0.4", + "minimalistic-assert": "^1.0.1", + "minimalistic-crypto-utils": "^1.0.1" } }, + "node_modules/elliptic/node_modules/bn.js": { + "version": "4.11.9", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.9.tgz", + "integrity": "sha512-E6QoYqCKZfgatHTdHzs1RRKP7ip4vvm+EyRUeE2RF0NblwVvb0p6jSVeNTOFxPn26QXN2o6SMfNxKp6kU8zQaw==" + }, "node_modules/emoji-regex": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", @@ -711,6 +720,15 @@ "node": ">=8" } }, + "node_modules/hash.js": { + "version": "1.1.7", + "resolved": "https://registry.npmjs.org/hash.js/-/hash.js-1.1.7.tgz", + "integrity": "sha512-taOaskGt4z4SOANNseOviYDvjEJinIkRgmp7LbKP2YTTmVxWBl87s/uzK9r+44BclBSp2X7K1hqeNfz9JbBeXA==", + "dependencies": { + "inherits": "^2.0.3", + "minimalistic-assert": "^1.0.1" + } + }, "node_modules/he": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", @@ -720,6 +738,16 @@ "he": "bin/he" } }, + "node_modules/hmac-drbg": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/hmac-drbg/-/hmac-drbg-1.0.1.tgz", + "integrity": "sha1-0nRXAQJabHdabFRXk+1QL8DGSaE=", + "dependencies": { + "hash.js": "^1.0.3", + "minimalistic-assert": "^1.0.0", + "minimalistic-crypto-utils": "^1.0.1" + } + }, "node_modules/inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", @@ -733,8 +761,7 @@ "node_modules/inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "node_modules/is-binary-path": { "version": "2.1.0", @@ -868,6 +895,16 @@ "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", "dev": true }, + "node_modules/minimalistic-assert": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz", + "integrity": "sha512-UtJcAD4yEaGtjPezWuO9wC4nwUnVH/8/Im3yEHQP4b67cXlD/Qr9hdITCU1xDbSEXg2XKNaP8jsReV7vQd00/A==" + }, + "node_modules/minimalistic-crypto-utils": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz", + "integrity": "sha1-9sAMHAsIIkblxNmd+4x8CDsrWCo=" + }, "node_modules/minimatch": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", @@ -1823,11 +1860,14 @@ "resolved": "https://registry.npmjs.org/@ethersproject/logger/-/logger-5.0.8.tgz", "integrity": "sha512-SkJCTaVTnaZ3/ieLF5pVftxGEFX56pTH+f2Slrpv7cU0TNpUZNib84QQdukd++sWUp/S7j5t5NW+WegbXd4U/A==" }, - "@types/bigi": { - "version": "1.4.2", - "resolved": "https://registry.npmjs.org/@types/bigi/-/bigi-1.4.2.tgz", - "integrity": "sha512-St8Vm0x1ApYlU9yNaFx3jBis5JVU6oR/5Xtgvn8+N8Ts8f3ze6kOvAAg0aNkbGMGhhG6PrP0nMOgDI9NMFETkA==", - "dev": true + "@types/bn.js": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@types/bn.js/-/bn.js-5.1.0.tgz", + "integrity": "sha512-QSSVYj7pYFN49kW77o2s9xTCwZ8F2xLbjLLSEVh8D2F4JUhZtPAGOFLTD+ffqksBx/u4cE/KImFjyhqCjn/LIA==", + "dev": true, + "requires": { + "@types/node": "*" + } }, "@types/chai": { "version": "4.2.14", @@ -1835,14 +1875,13 @@ "integrity": "sha512-G+ITQPXkwTrslfG5L/BksmbLUA0M1iybEsmCWPqzSxsRRhJZimBKJkoMi8fr/CPygPTj4zO5pJH7I2/cm9M7SQ==", "dev": true }, - "@types/ecurve": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/@types/ecurve/-/ecurve-1.0.0.tgz", - "integrity": "sha1-DR/OAi2LqyvEurKMXXgbZCEAGMQ=", + "@types/elliptic": { + "version": "6.4.12", + "resolved": "https://registry.npmjs.org/@types/elliptic/-/elliptic-6.4.12.tgz", + "integrity": "sha512-gP1KsqoouLJGH6IJa28x7PXb3cRqh83X8HCLezd2dF+XcAIMKYv53KV+9Zn6QA561E120uOqZBQ+Jy/cl+fviw==", "dev": true, "requires": { - "@types/bigi": "*", - "@types/node": "*" + "@types/bn.js": "*" } }, "@types/mocha": { @@ -1921,22 +1960,17 @@ "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", "dev": true }, - "bigi": { - "version": "1.4.2", - "resolved": "https://registry.npmjs.org/bigi/-/bigi-1.4.2.tgz", - "integrity": "sha1-nGZalfiLiwj8Bc/XMfVhhZ1yWCU=" - }, - "bignumber.js": { - "version": "7.0.2", - "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-7.0.2.tgz", - "integrity": "sha512-TosM7Yg1Ux0ZCNwwS/tW95r3q9xIZstgsUGKWaez0Cgq8Oy3qia9RGvyG/fbxlQAvigjza1d057QNQLGvYXCeg==" - }, "binary-extensions": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", "dev": true }, + "bn.js": { + "version": "5.1.3", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-5.1.3.tgz", + "integrity": "sha512-GkTiFpjFtUzU9CbMeJ5iazkCzGL3jrhzerzZIuqLABjbwRaFt33I9tUdSNryIptM+RxDet6OKm2WnLXzW51KsQ==" + }, "brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -1956,6 +1990,11 @@ "fill-range": "^7.0.1" } }, + "brorand": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/brorand/-/brorand-1.1.0.tgz", + "integrity": "sha1-EsJe/kCkXjwyPrhnWgoM5XsiNx8=" + }, "browser-stdout": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz", @@ -2128,12 +2167,25 @@ "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", "dev": true }, - "ecurve": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/ecurve/-/ecurve-1.0.0.tgz", - "integrity": "sha1-Ms/Vzl9CHpNRIGoz1OPP0280ZaQ=", + "elliptic": { + "version": "6.5.4", + "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.5.4.tgz", + "integrity": "sha512-iLhC6ULemrljPZb+QutR5TQGB+pdW6KGD5RSegS+8sorOZT+rdQFbsQFJgvN3eRqNALqJer4oQ16YvJHlU8hzQ==", "requires": { - "bigi": "^1.1.0" + "bn.js": "^4.11.9", + "brorand": "^1.1.0", + "hash.js": "^1.0.0", + "hmac-drbg": "^1.0.1", + "inherits": "^2.0.4", + "minimalistic-assert": "^1.0.1", + "minimalistic-crypto-utils": "^1.0.1" + }, + "dependencies": { + "bn.js": { + "version": "4.11.9", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.9.tgz", + "integrity": "sha512-E6QoYqCKZfgatHTdHzs1RRKP7ip4vvm+EyRUeE2RF0NblwVvb0p6jSVeNTOFxPn26QXN2o6SMfNxKp6kU8zQaw==" + } } }, "emoji-regex": { @@ -2254,12 +2306,31 @@ "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", "dev": true }, + "hash.js": { + "version": "1.1.7", + "resolved": "https://registry.npmjs.org/hash.js/-/hash.js-1.1.7.tgz", + "integrity": "sha512-taOaskGt4z4SOANNseOviYDvjEJinIkRgmp7LbKP2YTTmVxWBl87s/uzK9r+44BclBSp2X7K1hqeNfz9JbBeXA==", + "requires": { + "inherits": "^2.0.3", + "minimalistic-assert": "^1.0.1" + } + }, "he": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==", "dev": true }, + "hmac-drbg": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/hmac-drbg/-/hmac-drbg-1.0.1.tgz", + "integrity": "sha1-0nRXAQJabHdabFRXk+1QL8DGSaE=", + "requires": { + "hash.js": "^1.0.3", + "minimalistic-assert": "^1.0.0", + "minimalistic-crypto-utils": "^1.0.1" + } + }, "inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", @@ -2273,8 +2344,7 @@ "inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "is-binary-path": { "version": "2.1.0", @@ -2378,6 +2448,16 @@ "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", "dev": true }, + "minimalistic-assert": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz", + "integrity": "sha512-UtJcAD4yEaGtjPezWuO9wC4nwUnVH/8/Im3yEHQP4b67cXlD/Qr9hdITCU1xDbSEXg2XKNaP8jsReV7vQd00/A==" + }, + "minimalistic-crypto-utils": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz", + "integrity": "sha1-9sAMHAsIIkblxNmd+4x8CDsrWCo=" + }, "minimatch": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", diff --git a/package.json b/package.json index b83be78..178bec6 100644 --- a/package.json +++ b/package.json @@ -16,14 +16,13 @@ "license": "GPL-3.0", "dependencies": { "@ethersproject/keccak256": "5.0.7", - "bigi": "^1.4.2", - "bignumber.js": "7.0.2", - "ecurve": "1.0.0" + "bn.js": "^5.1.3", + "elliptic": "^6.5.4" }, "devDependencies": { - "@types/bigi": "^1.4.2", + "@types/bn.js": "^5.1.0", "@types/chai": "^4.2.14", - "@types/ecurve": "^1.0.0", + "@types/elliptic": "^6.4.12", "@types/mocha": "^8.2.0", "@types/node": "^14.14.25", "chai": "^4.2.0", diff --git a/src/index.ts b/src/index.ts index 3bbc5ef..235a2f4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,71 +1,82 @@ import { randomBytes } from 'crypto' -import * as BigInteger from 'bigi' -import { getCurveByName, Point } from 'ecurve' +import * as BigNumber from 'bn.js' +import { ec, curve } from 'elliptic' import { keccak256 } from "@ethersproject/keccak256" -const ecparams = getCurveByName('secp256k1') -const G = ecparams.G -const n = ecparams.n as BigInteger +export type Point = curve.base.BasePoint +export { BigNumber } -export { ecparams } +const secp256k1 = new ec("secp256k1") +const G: Point = secp256k1.g +const n = secp256k1.n // as BigNumber -export type UserSecretData = { a: BigInteger, b: BigInteger, f: Point } -export type UnblindedSignature = { s: BigInteger, f: Point } +export const ecParams = { G, n } -export function newBigFromString(s: string) { - let a = new BigInteger(null, null, null) - a.fromString(s, null) - return a +export type UserSecretData = { a: BigNumber, b: BigNumber, f: Point } +export type UnblindedSignature = { s: BigNumber, f: Point } + +export function hashBigNumber(m: BigNumber) { + const mHex = m.toString(16) + + if (mHex.length % 2 == 0) + return keccak256('0x' + mHex).slice(2) // Trim 0x + else + return keccak256('0x0' + mHex).slice(2) // Trim 0x +} + +export function bigNumberFromString(s: string) { + return new BigNumber(s) } function random(bytes: number) { - let k: BigInteger + let k: BigNumber do { - k = BigInteger.fromByteArrayUnsigned(randomBytes(bytes)) as unknown as BigInteger + k = new BigNumber(randomBytes(bytes)) } while (k.toString() == "0" && k.gcd(n).toString() != "1") return k } export function newKeyPair() { const sk = random(32) - return { sk: sk, pk: G.multiply(sk) } + return { sk: sk, pk: G.mul(sk) } } export function newRequestParameters() { const k = random(32) - return { k: k, signerR: G.multiply(k) } + return { k: k, signerR: G.mul(k) } } /** * Blinds the message for the signer R. - * @param {BigInteger} m + * @param {BigNumber} m * @param {Point} signerR - * @returns {struct} {mBlinded: BigInteger, userSecretData: {a: BigInteger, b: BigInteger, f: Point}} + * @returns {struct} {mBlinded: BigNumber, userSecretData: {a: BigNumber, b: BigNumber, f: Point}} */ -export function blind(m: BigInteger, signerR: Point): { mBlinded: BigInteger, userSecretData: UserSecretData } { - const u: UserSecretData = { a: BigInteger.ZERO as BigInteger, b: BigInteger.ZERO as BigInteger, f: G } +export function blind(m: BigNumber, signerR: Point): { mBlinded: BigNumber, userSecretData: UserSecretData } { + const u: UserSecretData = { a: new BigNumber(0), b: new BigNumber(0), f: G } u.a = random(32) u.b = random(32) - const aR = signerR.multiply(u.a) - const bG = G.multiply(u.b) + const aR = signerR.mul(u.a) + const bG = G.mul(u.b) u.f = aR.add(bG) - const rx = u.f.affineX.mod(n) + const rx = u.f.getX().mod(n) - const ainv = u.a.modInverse(n as unknown as number) - const ainvrx = ainv.multiply(rx) + const ainv = u.a.invm(n) + const ainvrx = ainv.mul(rx) - const mHex = m.toString(16) - const hHex = keccak256('0x' + mHex) - const h = BigInteger.fromHex(hHex.slice(2)) - const mBlinded = ainvrx.multiply(h) + // const mHex = m.toString(16) + const hHex = hashBigNumber(m) + + const h = new BigNumber(Buffer.from(hHex, "hex")) + const mBlinded = ainvrx.mul(h) return { mBlinded: mBlinded.mod(n), userSecretData: u } } -export function blindSign(sk: BigInteger, mBlinded: BigInteger, k: BigInteger): BigInteger { - let sBlind = sk.multiply(mBlinded) +export function blindSign(sk: BigNumber, mBlinded: BigNumber, k: BigNumber): BigNumber { + let sBlind = sk.mul(mBlinded) sBlind = sBlind.add(k) return sBlind.mod(n) } @@ -76,27 +87,27 @@ export function blindSign(sk: BigInteger, mBlinded: BigInteger, k: BigInteger): * @param userSecretData * @returns unblinded signature */ -export function unblind(sBlind: BigInteger, userSecretData: UserSecretData): UnblindedSignature { - const s = userSecretData.a.multiply(sBlind).add(userSecretData.b) +export function unblind(sBlind: BigNumber, userSecretData: UserSecretData): UnblindedSignature { + const s = userSecretData.a.mul(sBlind).add(userSecretData.b) return { s: s.mod(n), f: userSecretData.f } } -export function verify(m: BigInteger, s: UnblindedSignature, q: Point) { - const sG = G.multiply(s.s) +export function verify(m: BigNumber, s: UnblindedSignature, q: Point) { + const sG = G.mul(s.s) - const mHex = m.toString(16) - const hHex = keccak256('0x' + mHex) - const h = BigInteger.fromHex(hHex.slice(2)) + const hHex = hashBigNumber(m) + + const h = new BigNumber(Buffer.from(hHex, "hex")) - const rx = s.f.affineX.mod(n) + const rx = s.f.getX().mod(n) const right = s.f.add( - q.multiply( - rx.multiply(h) + q.mul( + rx.mul(h) ) ) - if ((sG.affineX.toString() == right.affineX.toString()) - && (sG.affineY.toString() == right.affineY.toString())) { + if ((sG.getX().toString() == right.getX().toString()) + && (sG.getY().toString() == right.getY().toString())) { return true } return false diff --git a/test/index.test.ts b/test/index.test.ts index 32ab7f2..fd7c29b 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,18 +1,27 @@ import * as assert from 'assert' -import * as BigInteger from 'bigi' -// import { Point } from 'ecurve' -import { keccak256 } from "@ethersproject/keccak256" -import { newBigFromString, ecparams, newKeyPair, newRequestParameters, blind, blindSign, unblind, verify } from "../src/index" +import { + bigNumberFromString, + ecParams, + newKeyPair, + newRequestParameters, + blind, + blindSign, + unblind, + verify, + hashBigNumber, + Point, + BigNumber +} from "../src/index" describe("keccak256", function () { it("keccak256", async () => { - const m = BigInteger.fromBuffer(Buffer.from("test", 'utf8')) - const mHex = m.toString(16) - const hHex = keccak256('0x' + mHex) - assert.strictEqual('0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658', hHex) - const h = BigInteger.fromHex(hHex.slice(2)) - assert.strictEqual('70622639689279718371527342103894932928233838121221666359043189029713682937432', h.toString()) + const msg = Buffer.from("test", 'utf8') + const m = new BigNumber(msg) + const hHex = hashBigNumber(m) + assert.strictEqual(hHex, '9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658') + const h = new BigNumber(Buffer.from(hHex, "hex")) + assert.strictEqual(h.toString(), '70622639689279718371527342103894932928233838121221666359043189029713682937432') }) }) @@ -22,7 +31,7 @@ describe("test blind", function () { const { k, signerR } = newRequestParameters() - const msg = BigInteger.fromBuffer( + const msg = new BigNumber( Buffer.from("test", 'utf8') ) assert.strictEqual('1952805748', msg.toString())