arnaucube
arnaucube/blindsecp256k1-jsPublic Self-hosted repository
blindsecp256k1-js/src/index.ts
src/index.tsRaw
  1import { randomBytes } from 'crypto'
  2import * as BigInteger from 'bigi'
  3import { getCurveByName, Point } from 'ecurve'
  4import { keccak256 } from '@ethersproject/keccak256'
  5
  6const ecparams = getCurveByName('secp256k1')
  7const G = ecparams.G
  8const n = ecparams.n as BigInteger
  9
 10export { ecparams }
 11
 12export type UserSecretData = { a: BigInteger, b: BigInteger, f: Point }
 13export type UnblindedSignature = { s: BigInteger, f: Point }
 14
 15/**
 16 * Imports a Point from hex string where X and Y coordinates were encoded as 32
 17 * & 32 bytes in LittleEndian.
 18 */
 19export function pointFromHex(pointHex: string) {
 20    const xBuff = Buffer.from(pointHex.substr(0, 64), 'hex').reverse().toString('hex')
 21    const yBuff = Buffer.from(pointHex.substr(64), 'hex').reverse().toString('hex')
 22    const x = BigInteger.fromHex(xBuff)
 23    const y = BigInteger.fromHex(yBuff)
 24    const p = Point.fromAffine(ecparams, x, y)
 25    return p
 26}
 27
 28export function pointToHex(point: Point): string {
 29    const buffX = point.affineX.toBuffer(32).reverse()
 30    const buffY = point.affineY.toBuffer(32).reverse()
 31
 32    return buffX.toString("hex") + buffY.toString("hex")
 33}
 34
 35export function signatureFromHex(hexSignature: string): UnblindedSignature {
 36    if (!hexSignature || hexSignature.length != 192) throw new Error("Invalid hex signature (96 bytes expected)")
 37
 38    const s = BigInteger.fromBuffer(Buffer.from(hexSignature.substr(0, 64), "hex").reverse())
 39    const f = pointFromHex(hexSignature.substr(64))
 40    return { s, f }
 41}
 42
 43export function signatureToHex(signature: UnblindedSignature): string {
 44    if (!signature || !signature.f || !signature.s) throw new Error("The signature is empty")
 45    const { f, s } = signature
 46
 47    // hex(swapEndiannes(s) ) + hex(f)
 48    const flippedHexS = s.toBuffer(32).reverse().toString("hex")
 49    return flippedHexS + pointToHex(f)
 50}
 51
 52export function messageToBigNumber(message: string) {
 53    const msg = Buffer.from(message, 'utf8')
 54    return BigInteger.fromBuffer(msg)
 55}
 56
 57export function newBigFromString(s: string) {
 58    let a = new BigInteger(null, null, null)
 59    a.fromString(s, null)
 60    return a
 61}
 62
 63export function newKeyPair() {
 64    const sk = random(32)
 65    return { sk: sk, pk: G.multiply(sk) }
 66}
 67
 68export function newRequestParameters() {
 69    const k = random(32)
 70    return { k: k, signerR: G.multiply(k) }
 71}
 72
 73/**
 74 * Blinds the message for the signer R.
 75 * @param {BigInteger} m
 76 * @param {Point} signerR
 77 * @returns {struct} {mBlinded: BigInteger, userSecretData: {a: BigInteger, b: BigInteger, f: Point}}
 78 */
 79export function blind(m: BigInteger, signerR: Point): { mBlinded: BigInteger, userSecretData: UserSecretData } {
 80    const u: UserSecretData = { a: BigInteger.ZERO as BigInteger, b: BigInteger.ZERO as BigInteger, f: G }
 81    u.a = random(32)
 82    u.b = random(32)
 83
 84    const aR = signerR.multiply(u.a)
 85    const bG = G.multiply(u.b)
 86    u.f = aR.add(bG)
 87
 88    const rx = u.f.affineX.mod(n)
 89
 90    const ainv = u.a.modInverse(n as unknown as number)
 91    const ainvrx = ainv.multiply(rx)
 92
 93    const mHex = m.toString(16)
 94    const hHex = keccak256('0x' + mHex)
 95    const h = BigInteger.fromHex(hHex.slice(2))
 96    const mBlinded = ainvrx.multiply(h)
 97
 98    return { mBlinded: mBlinded.mod(n), userSecretData: u }
 99}
100
101export function blindSign(sk: BigInteger, mBlinded: BigInteger, k: BigInteger): BigInteger {
102    let sBlind = sk.multiply(mBlinded)
103    sBlind = sBlind.add(k)
104    return sBlind.mod(n)
105}
106
107/**
108 * Unblinds the blinded signature.
109 * @param blinded signature
110 * @param userSecretData
111 * @returns unblinded signature
112 */
113export function unblind(sBlind: BigInteger, userSecretData: UserSecretData): UnblindedSignature {
114    const s = userSecretData.a.multiply(sBlind).add(userSecretData.b)
115    return { s: s.mod(n), f: userSecretData.f }
116}
117
118export function verify(m: BigInteger, s: UnblindedSignature, q: Point) {
119    const sG = G.multiply(s.s)
120
121    const mHex = m.toString(16)
122    const hHex = keccak256('0x' + mHex)
123    const h = BigInteger.fromHex(hHex.slice(2))
124
125    const rx = s.f.affineX.mod(n)
126    const right = s.f.add(
127        q.multiply(
128            rx.multiply(h)
129        )
130    )
131
132    if ((sG.affineX.toString() == right.affineX.toString())
133        && (sG.affineY.toString() == right.affineY.toString())) {
134        return true
135    }
136    return false
137}
138
139// HELPERS
140
141function random(bytes: number) {
142    let k: BigInteger
143    do {
144        k = BigInteger.fromByteArrayUnsigned(randomBytes(bytes)) as unknown as BigInteger
145    } while (k.toString() == '0' && k.gcd(n).toString() != '1')
146    return k
147}