Files: 91da326dc469caca5e31c3a2d7ef54bfa855c95f / index.js
8374 bytesRaw
1 | const mod = require('./build/bls_lib.js') |
2 | const exportedFuncs = require('./exportedFuncs.json') |
3 | |
4 | exports.mod = mod |
5 | |
6 | let init = false |
7 | let 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 | */ |
13 | exports.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 | */ |
25 | exports.MCLBN_CURVE_FP254BNB = 0 |
26 | |
27 | /** |
28 | * the FP382_1 curve |
29 | */ |
30 | exports.MCLBN_CURVE_FP382_1 = 1 |
31 | |
32 | /** |
33 | * the FP382_2 curve |
34 | */ |
35 | exports.MCLBN_CURVE_FP382_2 = 2 |
36 | |
37 | const MCLBN_FP_UNIT_SIZE = 6 |
38 | const FR_SIZE = MCLBN_FP_UNIT_SIZE * 8 |
39 | const ID_SIZE = FR_SIZE |
40 | const G1_SIZE = FR_SIZE * 3 |
41 | const G2_SIZE = FR_SIZE * 3 * 2 |
42 | |
43 | mod.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 | * creates an ID to use in with threshold groups |
89 | * @param {number} sk - a pointer to the secret key, secret key stuct is used to hold the id |
90 | * @param {number} n - a int repsenting the ID. n cannot be zero. |
91 | */ |
92 | exports.idSetInt = function (sk, n) { |
93 | if (n === 0) { |
94 | throw new Error('id cannot be zero') |
95 | } |
96 | exports._idSetInt(sk, n) |
97 | } |
98 | |
99 | /** |
100 | * signs a message |
101 | * @param {number} sig - a pointer to the a signature |
102 | * @param {number} sk - a pointer to the secret key |
103 | * @param {TypedArray|String} msg - the message to sign |
104 | */ |
105 | exports.sign = wrapInput(exports._sign) |
106 | |
107 | /** |
108 | * verifies a signature |
109 | * @param {number} sig - a pointer to the a signature |
110 | * @param {number} pk - a pointer to the secret key |
111 | * @param {TypedArray|String} msg - the message that was signed |
112 | */ |
113 | exports.verify = wrapInput(exports._verify, true) |
114 | |
115 | /** |
116 | * given a pointer to a public key this returns 64 byte Int8Array containing the key |
117 | * @param {number} pk - a pointer to the secret key |
118 | * @return {TypedArray} |
119 | */ |
120 | exports.publicKeySerialize = wrapOutput(exports._publicKeySerialize, 64) |
121 | |
122 | /** |
123 | * given a pointer to a secret key this returns 32 byte Int8Array containing the key |
124 | * @param {number} pk - a pointer to the secret key |
125 | * @return {TypedArray} |
126 | */ |
127 | exports.secretKeySerialize = wrapOutput(exports._secretKeySerialize, 32) |
128 | |
129 | /** |
130 | * given a pointer to a signature this returns 32 byte Int8Array containing the signature |
131 | * @param {number} pk - a pointer to the secret key |
132 | * @return {TypedArray} |
133 | */ |
134 | exports.signatureSerialize = wrapOutput(exports._signatureSerialize, 32) |
135 | |
136 | /** |
137 | * generates a secret key given a seed phrase. |
138 | * @param {number} sk - a pointer to a secret key |
139 | * @param {String|TypedArray} seed - the seed phrase |
140 | */ |
141 | exports.hashToSecretKey = wrapInput(exports._hashToSecretKey, true) |
142 | |
143 | /** |
144 | * write a secretKey to memory |
145 | * @param {number} sk - a pointer to a secret key |
146 | * @param {TypedArray} array - the secret key as a 32 byte TypedArray |
147 | */ |
148 | exports.secretKeyDeserialize = wrapInput(exports._secretKeyDeserialize, true) |
149 | |
150 | /** |
151 | * write a publicKey to memory |
152 | * @param {number} sk - a pointer to a secret key |
153 | * @param {TypedArray} array - the secret key as a 64 byte TypedArray |
154 | */ |
155 | exports.publicKeyDeserialize = wrapInput(exports._publicKeyDeserialize, true) |
156 | |
157 | /** |
158 | * write a signature to memory |
159 | * @param {number} sig - a pointer to a signature |
160 | * @param {TypedArray} array - the signature as a 32 byte TypedArray |
161 | */ |
162 | exports.signatureDeserialize = wrapInput(exports._signatureDeserialize) |
163 | |
164 | /** |
165 | * Recovers a secret key for a group given the groups secret keys shares and the groups ids |
166 | * @param {number} sk - a pointer to a secret key that will be generated |
167 | * @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 |
168 | * @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 |
169 | */ |
170 | exports.secretKeyRecover = wrapRecover(exports._secretKeyRecover, FR_SIZE, ID_SIZE) |
171 | |
172 | /** |
173 | * Recovers a public key for a group given the groups public keys shares and the groups ids |
174 | * @param {number} pk - a pointer to a secret key that will be generated |
175 | * @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 |
176 | * @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 |
177 | */ |
178 | exports.publicKeyRecover = wrapRecover(exports._publicKeyRecover, G2_SIZE, ID_SIZE) |
179 | |
180 | /** |
181 | * Recovers a signature for a group given the groups public keys shares and the groups ids |
182 | * @param {number} sig - a pointer to the signature that will be generated |
183 | * @param {Array<number>} sigArray - an array of pointers to signature shares. The length of the array should be the threshold number for the group |
184 | * @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 |
185 | */ |
186 | exports.signatureRecover = wrapRecover(exports._signatureRecover, G1_SIZE, ID_SIZE) |
187 | |
188 | /** |
189 | * 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 |
190 | * @param {number} skshare - a pointer to a secret key that will be generated |
191 | * @param {Array<number>} msk - an array of master secret keys. The number of keys is the threshould of the group. |
192 | * @param {number} id - the id of the member |
193 | */ |
194 | exports.secretKeyShare = wrapKeyShare(exports._secretKeyShare, FR_SIZE) |
195 | |
196 | /** |
197 | * 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 |
198 | * @param {number} pkshare - a pointer to a secret key that will be generated |
199 | * @param {Array<number>} mpk - an array of master public keys. The number of keys is the threshould of the group. |
200 | * @param {number} id - the id of the member |
201 | */ |
202 | exports.publicKeyShare = wrapKeyShare(exports._publicKeyShare, G2_SIZE) |
203 | |
204 | initCb() |
205 | } |
206 | |
207 | function wrapInput (func) { |
208 | return function () { |
209 | const args = [...arguments] |
210 | let buf = args.pop() |
211 | if (typeof buf === 'string') { |
212 | buf = Buffer.from(buf) |
213 | } |
214 | const pos = mod._malloc(buf.length) |
215 | |
216 | mod.HEAP8.set(buf, pos) |
217 | let r = func(...args, pos, buf.length) |
218 | mod._free(pos) |
219 | return r |
220 | } |
221 | } |
222 | |
223 | function wrapOutput (func, size) { |
224 | return function (x) { |
225 | const pos = mod._malloc(size) |
226 | const n = func(pos, size, x) |
227 | const a = mod.HEAP8.slice(pos, pos + n) |
228 | mod._free(pos) |
229 | return a |
230 | } |
231 | } |
232 | |
233 | function wrapKeyShare (func, dataSize) { |
234 | return function (x, vec, id) { |
235 | const k = vec.length |
236 | const p = mod._malloc(dataSize * k) |
237 | for (let i = 0; i < k; i++) { |
238 | mod._memcpy(p + i * dataSize, vec[i], dataSize) |
239 | } |
240 | const r = func(x, p, k, id) |
241 | mod._free(p) |
242 | return r |
243 | } |
244 | } |
245 | |
246 | function wrapRecover (func, dataSize, idDataSize) { |
247 | return function (x, vec, idVec) { |
248 | const n = vec.length |
249 | const p = mod._malloc(dataSize * n) |
250 | const q = mod._malloc(idDataSize * n) |
251 | for (let i = 0; i < n; i++) { |
252 | mod._memcpy(p + i * dataSize, vec[i], dataSize) |
253 | mod._memcpy(q + i * idDataSize, idVec[i], idDataSize) |
254 | } |
255 | const r = func(x, p, q, n) |
256 | mod._free(q) |
257 | mod._free(p) |
258 | return r |
259 | } |
260 | } |
261 |
Built with git-ssb-web