git ssb

0+

wanderer🌟 / bls-lib



Tree: fd809fb0134868c1f4d481a16255753af43fe5f6

Files: fd809fb0134868c1f4d481a16255753af43fe5f6 / tests / index.js

5848 bytesRaw
1const tape = require('tape')
2const shuffle = require('array-shuffle')
3const bls = require('../index.js')
4
5bls.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, true)
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, true)
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, true)
83
84 bls.free(sec)
85 bls.free(sig)
86 bls.free(pub)
87
88 t.end()
89 })
90
91 tape('bad import', t => {
92 bls.init()
93 t.plan(1)
94 try {
95 bls.secretKeyImport(new Uint8Array([1, 2, 3]))
96 } catch (e) {
97 t.pass(true)
98 }
99 t.end()
100 })
101
102 tape('shares', t => {
103 bls.init()
104 const numOfPlayers = 5
105 const threshold = 3
106
107 const masterSecretKey = []
108 const masterPublicKey = []
109
110 const ids = []
111 const secretKeys = []
112 const publicKeys = []
113 const sigs = []
114 const msg = 'hello world'
115
116 // set up master key share
117 for (let i = 0; i < threshold; i++) {
118 const sk = bls.secretKey()
119 bls.secretKeySetByCSPRNG(sk)
120 masterSecretKey.push(sk)
121
122 const pk = bls.publicKey()
123 bls.getPublicKey(pk, sk)
124
125 masterPublicKey.push(pk)
126 }
127
128 const masterSig = bls.signature()
129 bls.sign(masterSig, masterSecretKey[0], msg)
130
131 // key sharing
132 for (let i = 0; i < numOfPlayers; i++) {
133 const id = bls.secretKey()
134 bls.secretKeySetByCSPRNG(id)
135 ids.push(id)
136
137 const sk = bls.secretKey()
138 bls.secretKeyShare(sk, masterSecretKey, id)
139 secretKeys.push(sk)
140
141 const pk = bls.publicKey()
142 bls.publicKeyShare(pk, masterPublicKey, id)
143 publicKeys.push(pk)
144
145 const pk2 = bls.publicKey()
146 bls.getPublicKey(pk2, sk)
147
148 const pubArray1 = bls.publicKeyExport(pk)
149 const pubArray2 = bls.publicKeyExport(pk2)
150 bls.free(pk2)
151 t.equals(Buffer.from(pubArray2).toString('hex'), Buffer.from(pubArray1).toString('hex'), 'public keys should be equals')
152
153 const sig = bls.signature()
154 bls.sign(sig, sk, msg)
155
156 sigs.push(sig)
157 const r = bls.verify(sig, pk, msg)
158
159 t.equals(r, true, 'should verify')
160 }
161
162 // recover
163 const subIds = []
164 const subSecretKeys = []
165 const subPubs = []
166 const subSigs = []
167
168 let indexes = new Array(numOfPlayers).fill(0).map((el, i) => i)
169 indexes = shuffle(indexes)
170 for (let i = 0; i < threshold; i++) {
171 const index = indexes[i]
172 subIds.push(ids[index])
173 subSecretKeys.push(secretKeys[index])
174 subPubs.push(publicKeys[index])
175 subSigs.push(sigs[index])
176 }
177
178 const sk = bls.secretKey()
179 const pk = bls.publicKey()
180 const sig = bls.signature()
181
182 bls.secretKeyRecover(sk, subSecretKeys, subIds)
183 bls.publicKeyRecover(pk, subPubs, subIds)
184 bls.signatureRecover(sig, subSigs, subIds)
185
186 const secArray = bls.secretKeyExport(sk)
187 const masterSk = bls.secretKeyExport(masterSecretKey[0])
188
189 t.equals(Buffer.from(secArray).toString('hex'), Buffer.from(masterSk).toString('hex'), 'should recover master SK')
190
191 const publicKey = bls.publicKeyExport(pk)
192 const masterPk = bls.publicKeyExport(masterPublicKey[0])
193
194 t.equals(Buffer.from(publicKey).toString('hex'), Buffer.from(masterPk).toString('hex'), 'should recover master PK')
195
196 const signature = bls.signatureExport(sig)
197 const sMasterSig = bls.signatureExport(masterSig)
198 t.equals(Buffer.from(signature).toString('hex'), Buffer.from(sMasterSig).toString('hex'), 'signature should be the same as master')
199
200 bls.free(sig)
201 bls.free(pk)
202 bls.free(sk)
203
204 bls.freeArray(ids)
205 bls.freeArray(secretKeys)
206 bls.freeArray(publicKeys)
207 bls.freeArray(sigs)
208
209 t.end()
210 })
211
212 tape('int ids', t => {
213 bls.init()
214 t.plan(3)
215 const sec = bls.idImport(7)
216 const sec2 = bls.idImport(Buffer.from([7]))
217
218 const secKey = bls.secretKeyExport(sec)
219 const secKey2 = bls.secretKeyExport(sec2)
220 const expected = new Uint8Array(32)
221 expected[0] = 7
222 t.deepEqual(secKey, expected)
223 t.notEqual(secKey2, expected)
224
225 try {
226 bls.idSetInt(sec, 0)
227 } catch (e) {
228 t.pass('shouldnt accept 0 as an id')
229 }
230 t.end()
231 })
232})
233

Built with git-ssb-web