git ssb

0+

wanderer🌟 / bls-lib



Tree: c3d1416e0ad20053143d6673a868240ef276838e

Files: c3d1416e0ad20053143d6673a868240ef276838e / index.js

7879 bytesRaw
1const mod = require('./build/bls_lib.js')
2const exportedFuncs = require('./exportedFuncs.json')
3
4let init = false
5let initCb = () => {}
6
7/**
8 * takes a callback that is called once the module is setup
9 * @params {Function} cb - the callback tobe called once the module is intailized
10 */
11exports.onModuleInit = function (cb) {
12 if (init) {
13 cb()
14 } else {
15 init = true
16 initCb = cb
17 }
18}
19
20/**
21 * the FP254BNB curve
22 */
23exports.MCLBN_CURVE_FP254BNB = 0
24
25/**
26 * the FP382_1 curve
27 */
28exports.MCLBN_CURVE_FP382_1 = 1
29
30/**
31 * the FP382_2 curve
32 */
33exports.MCLBN_CURVE_FP382_2 = 2
34
35const MCLBN_FP_UNIT_SIZE = 6
36const FR_SIZE = MCLBN_FP_UNIT_SIZE * 8
37const ID_SIZE = FR_SIZE
38const G1_SIZE = FR_SIZE * 3
39const G2_SIZE = FR_SIZE * 3 * 2
40
41mod.onRuntimeInitialized = function () {
42 exportedFuncs.forEach(func => {
43 exports[func.exportName] = mod.cwrap(func.name, func.returns, func.args)
44 })
45
46 /**
47 * intailizes the libary to use a given curve
48 * @param {number} curve - the curves that can be used are MCLBN_CURVE_FP254BNB, MCLBN_CURVE_FP382_1 or MCLBN_CURVE_FP382_2
49 */
50 exports.init = function (curve = exports.MCLBN_CURVE_FP254BNB) {
51 return exports._init(curve, MCLBN_FP_UNIT_SIZE)
52 }
53
54 /**
55 * allocates a secret key
56 * @returns {number} the pointer to the key
57 */
58 exports.secretKey = function () {
59 return mod._malloc(FR_SIZE)
60 }
61
62 /**
63 * allocates a secret key
64 * @returns {number} the pointer to the key
65 */
66 exports.publicKey = function () {
67 return mod._malloc(G2_SIZE)
68 }
69
70 /**
71 * allocates a signature
72 * @returns {number} the pointer to the signture
73 */
74 exports.signature = function () {
75 return mod._malloc(G1_SIZE)
76 }
77
78 /**
79 * frees a pointer
80 */
81 exports.free = function (x) {
82 mod._free(x)
83 }
84
85 /**
86 * signs a message
87 * @param {number} sig - a pointer to the a signature
88 * @param {number} sk - a pointer to the secret key
89 * @param {TypedArray|String} msg - the message to sign
90 */
91 exports.sign = wrapInput(exports._sign)
92
93 /**
94 * verifies a signature
95 * @param {number} sig - a pointer to the a signature
96 * @param {number} pk - a pointer to the secret key
97 * @param {TypedArray|String} msg - the message that was signed
98 */
99 exports.verify = wrapInput(exports._verify, true)
100
101 /**
102 * given a pointer to a public key this returns 64 byte Int8Array containing the key
103 * @param {number} pk - a pointer to the secret key
104 * @return {TypedArray}
105 */
106 exports.publicKeySerialize = wrapOutput(exports._publicKeySerialize, 64)
107
108 /**
109 * given a pointer to a secret key this returns 32 byte Int8Array containing the key
110 * @param {number} pk - a pointer to the secret key
111 * @return {TypedArray}
112 */
113 exports.secretKeySerialize = wrapOutput(exports._secretKeySerialize, 32)
114
115 /**
116 * given a pointer to a signature this returns 32 byte Int8Array containing the signature
117 * @param {number} pk - a pointer to the secret key
118 * @return {TypedArray}
119 */
120 exports.signatureSerialize = wrapOutput(exports._signatureSerialize, 32)
121
122 /**
123 * write a secretKey to memory
124 * @param {number} sk - a pointer to a secret key
125 * @param {TypedArray} array - the secret key as a 32 byte TypedArray
126 */
127 exports.secretKeyDeserialize = wrapInput(exports._secretKeyDeserialize, true)
128
129 /**
130 * write a publicKey to memory
131 * @param {number} sk - a pointer to a secret key
132 * @param {TypedArray} array - the secret key as a 64 byte TypedArray
133 */
134 exports.publicKeyDeserialize = wrapInput(exports._publicKeyDeserialize, true)
135
136 /**
137 * write a signature to memory
138 * @param {number} sig - a pointer to a signature
139 * @param {TypedArray} array - the signature as a 32 byte TypedArray
140 */
141 exports.signatureDeserialize = wrapInput(exports._signatureDeserialize, true)
142
143 /**
144 * Recovers a secret key for a group given the groups secret keys shares and the groups ids
145 * @param {number} sk - a pointer to a secret key that will be generated
146 * @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
147 * @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
148 */
149 exports.secretKeyRecover = wrapRecover(exports._secretKeyRecover, FR_SIZE, ID_SIZE)
150
151 /**
152 * Recovers a public key for a group given the groups public keys shares and the groups ids
153 * @param {number} pk - a pointer to a secret key that will be generated
154 * @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
155 * @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
156 */
157 exports.publicKeyRecover = wrapRecover(exports._publicKeyRecover, G2_SIZE, ID_SIZE)
158
159 /**
160 * Recovers a signature for a group given the groups public keys shares and the groups ids
161 * @param {number} sig - a pointer to the signature that will be generated
162 * @param {Array<number>} sigArray - an array of pointers to signature shares. The length of the array should be the threshold number for the group
163 * @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
164 */
165 exports.signatureRecover = wrapRecover(exports._signatureRecover, G1_SIZE, ID_SIZE)
166
167 /**
168 * 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
169 * @param {number} skshare - a pointer to a secret key that will be generated
170 * @param {Array<number>} msk - an array of master secret keys. The number of keys is the threshould of the group.
171 * @param {number} id - the id of the member
172 */
173 exports.secretKeyShare = wrapKeyShare(exports._secretKeyShare, FR_SIZE)
174
175 /**
176 * 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
177 * @param {number} pkshare - a pointer to a secret key that will be generated
178 * @param {Array<number>} mpk - an array of master public keys. The number of keys is the threshould of the group.
179 * @param {number} id - the id of the member
180 */
181 exports.publicKeyShare = wrapKeyShare(exports._publicKeyShare, G2_SIZE)
182
183 initCb()
184}
185
186function wrapInput (func, returnValue = false) {
187 return function () {
188 const args = [...arguments]
189 const buf = args.pop()
190 const ioMode = 0
191 const pos = mod._malloc(buf.length)
192 if (typeof buf === 'string') {
193 mod.writeStringToMemory(buf, pos)
194 } else {
195 mod.HEAP8.set(buf, pos)
196 }
197 let r = func(...args, pos, buf.length, ioMode)
198 mod._free(pos)
199 if (returnValue) return r
200 }
201}
202
203function wrapOutput (func, size) {
204 return function (x, ioMode = 0) {
205 const pos = mod._malloc(size)
206 const n = func(pos, size, x, ioMode)
207 const a = mod.HEAP8.slice(pos, pos + n)
208 mod._free(pos)
209 return a
210 }
211}
212
213function wrapKeyShare (func, dataSize) {
214 return function (x, vec, id) {
215 const k = vec.length
216 const p = mod._malloc(dataSize * k)
217 for (let i = 0; i < k; i++) {
218 mod._memcpy(p + i * dataSize, vec[i], dataSize)
219 }
220 const r = func(x, p, k, id)
221 mod._free(p)
222 return r
223 }
224}
225
226function wrapRecover (func, dataSize, idDataSize) {
227 return function (x, vec, idVec) {
228 const n = vec.length
229 const p = mod._malloc(dataSize * n)
230 const q = mod._malloc(idDataSize * n)
231 for (let i = 0; i < n; i++) {
232 mod._memcpy(p + i * dataSize, vec[i], dataSize)
233 mod._memcpy(q + i * idDataSize, idVec[i], idDataSize)
234 }
235 const r = func(x, p, q, n)
236 mod._free(q)
237 mod._free(p)
238 return r
239 }
240}
241

Built with git-ssb-web