blindsecp256k1-js/test/index.test.ts
test/index.test.tsRaw
1import * as assert from 'assert'
2import * as BigInteger from 'bigi'
3import { keccak256 } from '@ethersproject/keccak256'
4
5import { pointFromHex, newKeyPair, newRequestParameters, blind, blindSign, unblind, verify, signatureFromHex, signatureToHex, messageToBigNumber, pointToHex } from '../src/index'
6
7describe('keccak256', function () {
8 it('should hash strings and big numbers', async () => {
9 const m = BigInteger.fromBuffer(Buffer.from('test', 'utf8'))
10 const mHex = m.toString(16)
11 const hHex = keccak256('0x' + mHex)
12 assert.strictEqual('0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658', hHex)
13 const h = BigInteger.fromHex(hHex.slice(2))
14 assert.strictEqual('70622639689279718371527342103894932928233838121221666359043189029713682937432', h.toString())
15 })
16})
17
18describe('test blind', function () {
19 it('should blind', async () => {
20 const { sk, pk } = newKeyPair()
21
22 const { k, signerR } = newRequestParameters()
23
24 const msg = BigInteger.fromBuffer(
25 Buffer.from('test', 'utf8')
26 )
27 assert.strictEqual('1952805748', msg.toString())
28
29 const { mBlinded, userSecretData } = blind(msg, signerR)
30
31 const sBlind = blindSign(sk, mBlinded, k)
32
33 const sig = unblind(sBlind, userSecretData)
34
35 const verified = verify(msg, sig, pk)
36 assert(verified)
37 })
38})
39
40describe('import point from hex', function () {
41 it('should import a point', async () => {
42 // pointHex and expected values was generated from
43 // go-blindsecp256k1 (Point.Bytes())
44 const pointHex = '73a68e845e626a2d7f683dd2ceb57956f755d623c0b729af30a72c7bf4dee7a6932d08955c98cf21edf35f5df218b56c41014db06f513cecc85dc3d8671f9521'
45 const p = pointFromHex(pointHex)
46 assert.strictEqual('75493613315673782629634797792529672610524641864826422940379513836118869976691', p.x.toString())
47 assert.strictEqual('15189800969738851359449622716277258420468338317311652509571160848111971610003', p.y.toString())
48
49 assert.strictEqual(pointToHex(pointFromHex(pointHex)), pointHex)
50 })
51
52 it("should import a signature", () => {
53 const originalSignatureHex = "36cbb281c7d3f51fa6c027c39d1ec1115e72f4cf78beda1292ef16736894f327a77b4a7e7ba7b7daa54de6a111cb69b6a89bd8c15756a3d0bc620e77b134a9be2cf4d4758c2a5c59381f30bca694b585d9bbb192d349f7220b4e414503cd7eb6"
54 const { s, f } = signatureFromHex(originalSignatureHex)
55
56 assert.strictEqual(s.toString(10), "18070569205902663717816547129479407690196063715098828904678654635357913664310")
57 assert.strictEqual(f.affineX.toString(10), "86238402060026834577259540812071998074585426519937266202081591629132264536999")
58 assert.strictEqual(f.affineY.toString(10), "82544976118490904161760686015396902971720732626625394239692986344240783356972")
59
60 assert.strictEqual(signatureToHex(signatureFromHex(originalSignatureHex)), originalSignatureHex)
61 })
62
63 it("should import a signature with leading 0's on the big integers", () => {
64 const originalSignatureHex = "089a89f07bd41560454b1640fd30e51ee088d1be8355275a88e38a38f2e7a3000080fcc14af9c47c8066c6726e7d3cac9370494d5c67936b2978d6cecf5a4d00003ef00b060c47ba874c0764d662eff2d0e9daa8ba766f4aa6a2be8ec3d37500"
65 const { s, f } = signatureFromHex(originalSignatureHex)
66
67 assert.strictEqual(s.toString(10), "289596905226702587911033129111285060518391679824489553383063470810094737928")
68 assert.strictEqual(f.affineX.toString(10), "136673983650666514404918584888974483560635967304551499498329425981927620608")
69 assert.strictEqual(f.affineY.toString(10), "208182647280612121090241159906896377333338287898138605151276081386137599488")
70
71 assert.strictEqual(signatureToHex(signatureFromHex(originalSignatureHex)), originalSignatureHex)
72 })
73})