blindsecp256k1-js/test/index.test.ts
test/index.test.tsRaw
1import * as assert from 'assert'
2
3import {
4 stringToBigNumber,
5 messageToBigNumber,
6 decodePoint,
7 ecParams,
8 newKeyPair,
9 newRequestParameters,
10 blind,
11 blindSign,
12 unblind,
13 verify,
14 hashBigNumber,
15 Point,
16 BigNumber
17} from "../src/index"
18
19describe("keccak256", function () {
20 it("should hash strings and big numbers", async () => {
21 const m = messageToBigNumber("test")
22 assert.strictEqual('1952805748', m.toString())
23
24 const hHex = hashBigNumber(m)
25 assert.strictEqual(hHex, '9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658')
26 const h = new BigNumber(Buffer.from(hHex, "hex"))
27 assert.strictEqual(h.toString(), '70622639689279718371527342103894932928233838121221666359043189029713682937432')
28 })
29
30 it("should decode points in the secp256k1 curve", () => {
31 const tokenR = "7cfe4af054e13b4e7231d876d23205fb5f939ac8185271ca6b64c635a365faae259fb8cabdb06dde39d1ebeada3cb75cb9739621a79c61a8cf1e9a38abaf782a"
32 const point = decodePoint("04" + tokenR)
33 assert.strictEqual(point.getX().toString(16), tokenR.substr(0, 64))
34 assert.strictEqual(point.getY().toString(16), tokenR.substr(64))
35 })
36})
37
38describe("blind signatures", function () {
39 it("should blind, unblind and verify", async () => {
40 const { sk, pk } = newKeyPair()
41
42 const { k, signerR } = newRequestParameters()
43
44 const msg = messageToBigNumber("test")
45
46 const { mBlinded, userSecretData } = blind(msg, signerR)
47
48 const sBlind = blindSign(sk, mBlinded, k)
49
50 const sig = unblind(sBlind, userSecretData)
51
52 const verified = verify(msg, sig, pk)
53 assert(verified)
54 })
55})