arnaucube
arnaucube/blindsecp256k1-jsPublic Self-hosted repository
blindsecp256k1-js/test/index.test.ts
test/index.test.tsRaw
 1import * as assert from 'assert'
 2import * as BigInteger from 'bigi'
 3import { Point } from 'ecurve'
 4import { keccak256 } from '@ethersproject/keccak256'
 5
 6import { newBigFromString, ecparams, importPointFromHex, newKeyPair, newRequestParameters, blind, blindSign, unblind, verify } from '../src/index'
 7
 8describe('keccak256', function () {
 9	it('keccak256', async () => {
10		const m = BigInteger.fromBuffer(Buffer.from('test', 'utf8'))
11		const mHex = m.toString(16)
12		const hHex = keccak256('0x' + mHex)
13		assert.strictEqual('0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658', hHex)
14		const h = BigInteger.fromHex(hHex.slice(2))
15		assert.strictEqual('70622639689279718371527342103894932928233838121221666359043189029713682937432', h.toString())
16	})
17})
18
19describe('test blind', function () {
20	it('should blind', async () => {
21		const { sk, pk } = newKeyPair()
22
23		const { k, signerR } = newRequestParameters()
24
25		const msg = BigInteger.fromBuffer(
26			Buffer.from('test', 'utf8')
27		)
28		assert.strictEqual('1952805748', msg.toString())
29
30		const { mBlinded, userSecretData } = blind(msg, signerR)
31
32		const sBlind = blindSign(sk, mBlinded, k)
33
34		const sig = unblind(sBlind, userSecretData)
35
36		const verified = verify(msg, sig, pk)
37		assert(verified)
38	})
39})
40
41describe('import point from hex', function () {
42	it('', async () => {
43		// pointHex and expected values was generated from
44		// go-blindsecp256k1 (Point.Bytes())
45		const pointHex = '73a68e845e626a2d7f683dd2ceb57956f755d623c0b729af30a72c7bf4dee7a6932d08955c98cf21edf35f5df218b56c41014db06f513cecc85dc3d8671f9521'
46		const p = importPointFromHex(pointHex)
47		assert.strictEqual('75493613315673782629634797792529672610524641864826422940379513836118869976691', p.x.toString())
48		assert.strictEqual('15189800969738851359449622716277258420468338317311652509571160848111971610003', p.y.toString())
49	})
50})