Files: 410c677aaa8a9f8c0a2a117b9d72c7df060bd4bb / tests / index.js
5552 bytesRaw
1 | const tape = require('tape') |
2 | const shuffle = require('array-shuffle') |
3 | const bls = require('../index.js') |
4 | |
5 | bls.onModuleInit(() => { |
6 | tape('basic', t => { |
7 | t.plan(2) |
8 | bls.onModuleInit(() => { |
9 | t.pass(true) |
10 | bls.init() |
11 | const sig = bls.signature() |
12 | |
13 | const secString = '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08' |
14 | const secArray = Buffer.from(secString, 'hex') |
15 | const sec = bls.secretKeyImport(secArray) |
16 | |
17 | const pubString = '7ca19ff032c22a00b3d79d8961495af4c6c93c9c2b62bd7279570fcc2ca8d120fc75fd16f55ded79f6392a0769496817cded4760ed658d62627b9e6852b1100d' |
18 | |
19 | const pub = bls.publicKeyImport(Buffer.from(pubString, 'hex')) |
20 | |
21 | const msg = 'test' |
22 | bls.sign(sig, sec, msg) |
23 | |
24 | const v = bls.verify(sig, pub, msg) |
25 | t.equals(v, 1) |
26 | }) |
27 | }) |
28 | |
29 | tape('verify', t => { |
30 | bls.init() |
31 | const sec = bls.secretKey() |
32 | const pub = bls.publicKey() |
33 | const sig = bls.signature() |
34 | |
35 | bls.secretKeySetByCSPRNG(sec) |
36 | const msg = Buffer.from('hello world') |
37 | bls.sign(sig, sec, msg) |
38 | |
39 | bls.getPublicKey(pub, sec) |
40 | |
41 | const v = bls.verify(sig, pub, msg) |
42 | t.equals(v, 1) |
43 | |
44 | bls.free(sec) |
45 | bls.free(sig) |
46 | bls.free(pub) |
47 | |
48 | t.end() |
49 | }) |
50 | |
51 | tape('import/export', t => { |
52 | bls.init() |
53 | let sig = bls.signature() |
54 | let pub = bls.publicKey() |
55 | |
56 | const secString = 'a3d34923c039a45a50bfb6bc0943c77589a23cd27d7118c1bede0c61d6bab108' |
57 | let secArray = Buffer.from(secString, 'hex') |
58 | const sec = bls.secretKeyImport(secArray) |
59 | |
60 | secArray = bls.secretKeyExport(sec) |
61 | t.equals(Buffer.from(secArray).toString('hex'), secString) |
62 | |
63 | bls.getPublicKey(pub, sec) |
64 | const pubArray = bls.publicKeyExport(pub) |
65 | |
66 | secArray = bls.secretKeyExport(sec) |
67 | |
68 | const msg = 'hello world' |
69 | bls.sign(sig, sec, msg) |
70 | |
71 | const sigArray = bls.signatureExport(sig) |
72 | |
73 | bls.free(sec) |
74 | bls.free(sig) |
75 | bls.free(pub) |
76 | |
77 | // recover |
78 | sig = bls.signatureImport(sigArray) |
79 | pub = bls.publicKeyImport(pubArray) |
80 | |
81 | const v = bls.verify(sig, pub, msg) |
82 | t.equals(v, 1) |
83 | |
84 | bls.free(sec) |
85 | bls.free(sig) |
86 | bls.free(pub) |
87 | |
88 | t.end() |
89 | }) |
90 | |
91 | tape('shares', t => { |
92 | bls.init() |
93 | const numOfPlayers = 5 |
94 | const threshold = 3 |
95 | |
96 | const masterSecretKey = [] |
97 | const masterPublicKey = [] |
98 | |
99 | const ids = [] |
100 | const secretKeys = [] |
101 | const publicKeys = [] |
102 | const sigs = [] |
103 | const msg = 'hello world' |
104 | |
105 | // set up master key share |
106 | for (let i = 0; i < threshold; i++) { |
107 | const sk = bls.secretKey() |
108 | bls.secretKeySetByCSPRNG(sk) |
109 | masterSecretKey.push(sk) |
110 | |
111 | const pk = bls.publicKey() |
112 | bls.getPublicKey(pk, sk) |
113 | |
114 | masterPublicKey.push(pk) |
115 | } |
116 | |
117 | const masterSig = bls.signature() |
118 | bls.sign(masterSig, masterSecretKey[0], msg) |
119 | |
120 | // key sharing |
121 | for (let i = 0; i < numOfPlayers; i++) { |
122 | const id = bls.secretKey() |
123 | bls.secretKeySetByCSPRNG(id) |
124 | ids.push(id) |
125 | |
126 | const sk = bls.secretKey() |
127 | bls.secretKeyShare(sk, masterSecretKey, id) |
128 | secretKeys.push(sk) |
129 | |
130 | const pk = bls.publicKey() |
131 | bls.publicKeyShare(pk, masterPublicKey, id) |
132 | publicKeys.push(pk) |
133 | |
134 | const pk2 = bls.publicKey() |
135 | bls.getPublicKey(pk2, sk) |
136 | |
137 | const pubArray1 = bls.publicKeyExport(pk) |
138 | const pubArray2 = bls.publicKeyExport(pk2) |
139 | bls.free(pk2) |
140 | t.equals(Buffer.from(pubArray2).toString('hex'), Buffer.from(pubArray1).toString('hex'), 'public keys should be equals') |
141 | |
142 | const sig = bls.signature() |
143 | bls.sign(sig, sk, msg) |
144 | |
145 | sigs.push(sig) |
146 | const r = bls.verify(sig, pk, msg) |
147 | |
148 | t.equals(r, 1, 'should verify') |
149 | } |
150 | |
151 | // recover |
152 | const subIds = [] |
153 | const subSecretKeys = [] |
154 | const subPubs = [] |
155 | const subSigs = [] |
156 | |
157 | let indexes = new Array(numOfPlayers).fill(0).map((el, i) => i) |
158 | indexes = shuffle(indexes) |
159 | for (let i = 0; i < threshold; i++) { |
160 | const index = indexes[i] |
161 | subIds.push(ids[index]) |
162 | subSecretKeys.push(secretKeys[index]) |
163 | subPubs.push(publicKeys[index]) |
164 | subSigs.push(sigs[index]) |
165 | } |
166 | |
167 | const sk = bls.secretKey() |
168 | const pk = bls.publicKey() |
169 | const sig = bls.signature() |
170 | |
171 | bls.secretKeyRecover(sk, subSecretKeys, subIds) |
172 | bls.publicKeyRecover(pk, subPubs, subIds) |
173 | bls.signatureRecover(sig, subSigs, subIds) |
174 | |
175 | const secArray = bls.secretKeyExport(sk) |
176 | const masterSk = bls.secretKeyExport(masterSecretKey[0]) |
177 | |
178 | t.equals(Buffer.from(secArray).toString('hex'), Buffer.from(masterSk).toString('hex'), 'should recover master SK') |
179 | |
180 | const publicKey = bls.publicKeyExport(pk) |
181 | const masterPk = bls.publicKeyExport(masterPublicKey[0]) |
182 | |
183 | t.equals(Buffer.from(publicKey).toString('hex'), Buffer.from(masterPk).toString('hex'), 'should recover master PK') |
184 | |
185 | const signature = bls.signatureExport(sig) |
186 | const sMasterSig = bls.signatureExport(masterSig) |
187 | t.equals(Buffer.from(signature).toString('hex'), Buffer.from(sMasterSig).toString('hex'), 'signature should be the same as master') |
188 | |
189 | bls.free(sig) |
190 | bls.free(pk) |
191 | bls.free(sk) |
192 | |
193 | bls.freeArray(ids) |
194 | bls.freeArray(secretKeys) |
195 | bls.freeArray(publicKeys) |
196 | bls.freeArray(sigs) |
197 | |
198 | t.end() |
199 | }) |
200 | |
201 | tape('int ids', t => { |
202 | bls.init() |
203 | t.plan(2) |
204 | const sec = bls.secretKey() |
205 | bls.idSetInt(sec, 7) |
206 | |
207 | const secKey = bls.secretKeyExport(sec) |
208 | const expected = new Uint8Array(32) |
209 | expected[0] = 7 |
210 | t.deepEqual(secKey, expected) |
211 | |
212 | try { |
213 | bls.idSetInt(sec, 0) |
214 | } catch (e) { |
215 | t.pass('shouldnt accept 0 as an id') |
216 | } |
217 | t.end() |
218 | }) |
219 | }) |
220 |
Built with git-ssb-web