git ssb

0+

wanderer🌟 / bls-lib



Tree: fc378c64787f6a082a4aec751ccf9fdffe72e076

Files: fc378c64787f6a082a4aec751ccf9fdffe72e076 / index.js

9464 bytesRaw
1const mod = require('./build/bls_lib.js')
2const exportedFuncs = require('./exportedFuncs.json')
3
4exports.mod = mod
5
6let init = false
7let initCb
8
9/**
10 * takes a callback that is called once the module is setup
11 * @params {Function} cb - the callback tobe called once the module is intailized
12 */
13exports.onModuleInit = function (cb) {
14 if (init) {
15 cb()
16 } else {
17 init = true
18 initCb = cb
19 }
20}
21
22/**
23 * the FP254BNB curve
24 */
25exports.MCLBN_CURVE_FP254BNB = 0
26
27/**
28 * the FP382_1 curve
29 */
30exports.MCLBN_CURVE_FP382_1 = 1
31
32/**
33 * the FP382_2 curve
34 */
35exports.MCLBN_CURVE_FP382_2 = 2
36
37const MCLBN_FP_UNIT_SIZE = 6
38const FR_SIZE = MCLBN_FP_UNIT_SIZE * 8
39const ID_SIZE = FR_SIZE
40const G1_SIZE = FR_SIZE * 3
41const G2_SIZE = FR_SIZE * 3 * 2
42
43mod.onRuntimeInitialized = function () {
44 exportedFuncs.forEach(func => {
45 exports[func.exportName] = mod.cwrap(func.name, func.returns, func.args)
46 })
47
48 /**
49 * intailizes the libary to use a given curve
50 * @param {number} curve - the curves that can be used are MCLBN_CURVE_FP254BNB, MCLBN_CURVE_FP382_1 or MCLBN_CURVE_FP382_2
51 */
52 exports.init = function (curve = exports.MCLBN_CURVE_FP254BNB) {
53 return exports._init(curve, MCLBN_FP_UNIT_SIZE)
54 }
55
56 /**
57 * allocates a secret key
58 * @returns {number} the pointer to the key
59 */
60 exports.secretKey = function () {
61 return mod._malloc(FR_SIZE)
62 }
63
64 /**
65 * allocates a secret key
66 * @returns {number} the pointer to the key
67 */
68 exports.publicKey = function () {
69 return mod._malloc(G2_SIZE)
70 }
71
72 /**
73 * allocates a signature
74 * @returns {number} the pointer to the signture
75 */
76 exports.signature = function () {
77 return mod._malloc(G1_SIZE)
78 }
79
80 /**
81 * frees a pointer
82 */
83 exports.free = function (x) {
84 mod._free(x)
85 }
86
87 /**
88 * frees an array of pointers
89 */
90 exports.freeArray = function (a) {
91 a.forEach(el => mod._free(el))
92 }
93
94 /**
95 * creates an ID to use in with threshold groups
96 * @param {number} sk - a pointer to the secret key, secret key stuct is used to hold the id
97 * @param {number} n - a int repsenting the ID. n cannot be zero.
98 */
99 exports.idSetInt = function (sk, n) {
100 if (n === 0) {
101 throw new Error('id cannot be zero')
102 }
103 exports._idSetInt(sk, n)
104 }
105
106 /**
107 * signs a message
108 * @param {number} sig - a pointer to the a signature
109 * @param {number} sk - a pointer to the secret key
110 * @param {TypedArray|String} msg - the message to sign
111 */
112 exports.sign = wrapInput(exports._sign)
113
114 /**
115 * verifies a signature
116 * @param {number} sig - a pointer to the a signature
117 * @param {number} pk - a pointer to the secret key
118 * @param {TypedArray|String} msg - the message that was signed
119 */
120 exports.verify = wrapInput(exports._verify, true)
121
122 /**
123 * given a pointer to a public key this returns 64 byte Int8Array containing the key
124 * @param {number} pk - a pointer to the secret key
125 * @return {TypedArray}
126 */
127 exports.publicKeyExport = wrapOutput(exports._publicKeySerialize, 64)
128
129 /**
130 * given a pointer to a secret key this returns 32 byte Int8Array containing the key
131 * @param {number} pk - a pointer to the secret key
132 * @return {TypedArray}
133 */
134 exports.secretKeyExport = wrapOutput(exports._secretKeySerialize, 32)
135
136 /**
137 * given a pointer to a signature this returns 32 byte Int8Array containing the signature
138 * @param {number} pk - a pointer to the secret key
139 * @return {TypedArray}
140 */
141 exports.signatureExport = wrapOutput(exports._signatureSerialize, 32)
142
143 /**
144 * generates a secret key given a seed phrase.
145 * @param {number} sk - a pointer to a secret key
146 * @param {String|TypedArray} seed - the seed phrase
147 */
148 exports.hashToSecretKey = wrapInput(exports._hashToSecretKey, true)
149
150 /**
151 * write a secretKey to memory
152 * @param {number} sk - a pointer to a secret key
153 * @param {TypedArray} array - the secret key as a 32 byte TypedArray
154 */
155 exports.secretKeyDeserialize = wrapInput(exports._secretKeyDeserialize, true)
156
157 /**
158 * write a secretKey to memory and returns a pointer to it
159 * @param {number} sk - a pointer to a secret key
160 * @param {TypedArray} array - the secret key as a 32 byte TypedArray
161 * @return {Number}
162 */
163 exports.secretKeyImport = function (buf) {
164 const sk = exports.secretKey()
165 exports.secretKeyDeserialize(sk, buf)
166 return sk
167 }
168
169 /**
170 * write a publicKey to memory
171 * @param {number} sk - a pointer to a public key
172 * @param {TypedArray} array - the secret key as a 64 byte TypedArray
173 */
174 exports.publicKeyDeserialize = wrapInput(exports._publicKeyDeserialize, true)
175
176 /**
177 * write a publicKey to memory and returns a pointer to it
178 * @param {TypedArray} array - the secret key as a 64 byte TypedArray
179 * @return {Number}
180 */
181 exports.publicKeyImport = function (buf) {
182 const pk = exports.publicKey()
183 exports.publicKeyDeserialize(pk, buf)
184 return pk
185 }
186
187 /**
188 * write a signature to memory
189 * @param {number} sig - a pointer to a signature
190 * @param {TypedArray} array - the signature as a 32 byte TypedArray
191 */
192 exports.signatureDeserialize = wrapInput(exports._signatureDeserialize)
193
194 /**
195 * write a signature to memory and returns a pointer to it
196 * @param {TypedArray} array - the signature as a 32 byte TypedArray
197 * @return {Number}
198 */
199 exports.signatureImport = function (buf) {
200 const sig = exports.signature()
201 exports.signatureDeserialize(sig, buf)
202 return sig
203 }
204
205 /**
206 * Recovers a secret key for a group given the groups secret keys shares and the groups ids
207 * @param {number} sk - a pointer to a secret key that will be generated
208 * @param {Array<number>} sksArray - an array of pointers to the groups secret key shares. The length of the array should be the threshold number for the group
209 * @param {Array<numbers>} idArrah - an array of pointers to ids in the groups. The length of the array should be the threshold number for the group
210 */
211 exports.secretKeyRecover = wrapRecover(exports._secretKeyRecover, FR_SIZE, ID_SIZE)
212
213 /**
214 * Recovers a public key for a group given the groups public keys shares and the groups ids
215 * @param {number} pk - a pointer to a secret key that will be generated
216 * @param {Array<number>} pksArray - an array of pointers to the groups public key shares. The length of the array should be the threshold number for the group
217 * @param {Array<numbers>} idArrah - an array of pointers to ids in the groups. The length of the array should be the threshold number for the group
218 */
219 exports.publicKeyRecover = wrapRecover(exports._publicKeyRecover, G2_SIZE, ID_SIZE)
220
221 /**
222 * Recovers a signature for a group given the groups public keys shares and the groups ids
223 * @param {number} sig - a pointer to the signature that will be generated
224 * @param {Array<number>} sigArray - an array of pointers to signature shares. The length of the array should be the threshold number for the group
225 * @param {Array<numbers>} idArrah - an array of pointers to ids in the groups. The length of the array should be the threshold number for the group
226 */
227 exports.signatureRecover = wrapRecover(exports._signatureRecover, G1_SIZE, ID_SIZE)
228
229 /**
230 * Creates a secket key share for a group member given the groups members id (which is a the secret key) and array of master secret keys
231 * @param {number} skshare - a pointer to a secret key that will be generated
232 * @param {Array<number>} msk - an array of master secret keys. The number of keys is the threshould of the group.
233 * @param {number} id - the id of the member
234 */
235 exports.secretKeyShare = wrapKeyShare(exports._secretKeyShare, FR_SIZE)
236
237 /**
238 * Creates a public key share for a group member given the groups members id (which is a the secret key) and array of master public keys
239 * @param {number} pkshare - a pointer to a secret key that will be generated
240 * @param {Array<number>} mpk - an array of master public keys. The number of keys is the threshould of the group.
241 * @param {number} id - the id of the member
242 */
243 exports.publicKeyShare = wrapKeyShare(exports._publicKeyShare, G2_SIZE)
244
245 initCb()
246}
247
248function wrapInput (func) {
249 return function () {
250 const args = [...arguments]
251 let buf = args.pop()
252 if (typeof buf === 'string') {
253 buf = Buffer.from(buf)
254 }
255 const pos = mod._malloc(buf.length)
256
257 mod.HEAP8.set(buf, pos)
258 let r = func(...args, pos, buf.length)
259 mod._free(pos)
260 return r
261 }
262}
263
264function wrapOutput (func, size) {
265 return function (x) {
266 const pos = mod._malloc(size)
267 const n = func(pos, size, x)
268 const a = mod.HEAP8.slice(pos, pos + n)
269 mod._free(pos)
270 return a
271 }
272}
273
274function wrapKeyShare (func, dataSize) {
275 return function (x, vec, id) {
276 const k = vec.length
277 const p = mod._malloc(dataSize * k)
278 for (let i = 0; i < k; i++) {
279 mod._memcpy(p + i * dataSize, vec[i], dataSize)
280 }
281 const r = func(x, p, k, id)
282 mod._free(p)
283 return r
284 }
285}
286
287function wrapRecover (func, dataSize, idDataSize) {
288 return function (x, vec, idVec) {
289 const n = vec.length
290 const p = mod._malloc(dataSize * n)
291 const q = mod._malloc(idDataSize * n)
292 for (let i = 0; i < n; i++) {
293 mod._memcpy(p + i * dataSize, vec[i], dataSize)
294 mod._memcpy(q + i * idDataSize, idVec[i], idDataSize)
295 }
296 const r = func(x, p, q, n)
297 mod._free(q)
298 mod._free(p)
299 return r
300 }
301}
302

Built with git-ssb-web