aboutsummaryrefslogtreecommitdiffstats
path: root/docs/demo/bls.js
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2017-10-25 16:17:55 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2017-10-25 16:17:55 +0800
commitd1ab8affd173fda11be9f1018915f9cb7429772d (patch)
tree6e60f40d220aadfb78b4ada3ed019ffdfa2d62df /docs/demo/bls.js
parent066af2461132a6f867e863e7fb3dd590fa38de04 (diff)
downloaddexon-bls-d1ab8affd173fda11be9f1018915f9cb7429772d.tar
dexon-bls-d1ab8affd173fda11be9f1018915f9cb7429772d.tar.gz
dexon-bls-d1ab8affd173fda11be9f1018915f9cb7429772d.tar.bz2
dexon-bls-d1ab8affd173fda11be9f1018915f9cb7429772d.tar.lz
dexon-bls-d1ab8affd173fda11be9f1018915f9cb7429772d.tar.xz
dexon-bls-d1ab8affd173fda11be9f1018915f9cb7429772d.tar.zst
dexon-bls-d1ab8affd173fda11be9f1018915f9cb7429772d.zip
[js] refactor bls.js
Diffstat (limited to 'docs/demo/bls.js')
-rw-r--r--docs/demo/bls.js709
1 files changed, 406 insertions, 303 deletions
diff --git a/docs/demo/bls.js b/docs/demo/bls.js
index 81c8107..75d6c98 100644
--- a/docs/demo/bls.js
+++ b/docs/demo/bls.js
@@ -1,32 +1,49 @@
-function setupWasm(fileName, nameSpace, setupFct) {
- console.log('setupWasm ' + fileName)
- let mod = {}
- fetch(fileName)
- .then(response => response.arrayBuffer())
- .then(buffer => new Uint8Array(buffer))
- .then(binary => {
- mod['wasmBinary'] = binary
- mod['onRuntimeInitialized'] = function() {
- setupFct(mod, nameSpace)
- console.log('setupWasm end')
- }
- Module(mod)
- })
- return mod
-}
+(function(return_bls) {
+ if (typeof exports === 'object') {
+ module.exports = return_bls()
+ } else {
+ window.bls = return_bls()
+ }
+})(function() {
+ const crypto = window.crypto || window.msCrypto
-const MCLBN_CURVE_FP254BNB = 0
-const MCLBN_CURVE_FP382_1 = 1
-const MCLBN_CURVE_FP382_2 = 2
+ const MCLBN_CURVE_FP254BNB = 0
+ const MCLBN_CURVE_FP382_1 = 1
+ const MCLBN_CURVE_FP382_2 = 2
-const MCLBN_FP_UNIT_SIZE = 6
+ const MCLBN_FP_UNIT_SIZE = 6
-const BLS_ID_SIZE = MCLBN_FP_UNIT_SIZE * 8
-const BLS_SECRETKEY_SIZE = BLS_ID_SIZE
-const BLS_PUBLICKEY_SIZE = BLS_ID_SIZE * 3 * 2
-const BLS_SIGNATURE_SIZE = BLS_ID_SIZE * 3
+ const MCLBN_FP_SIZE = MCLBN_FP_UNIT_SIZE * 8
+ const MCLBN_G1_SIZE = MCLBN_FP_SIZE * 3
+ const MCLBN_G2_SIZE = MCLBN_FP_SIZE * 6
+ const MCLBN_GT_SIZE = MCLBN_FP_SIZE * 12
-function define_bls_extra_functions(mod) {
+ const BLS_ID_SIZE = MCLBN_FP_UNIT_SIZE * 8
+ const BLS_SECRETKEY_SIZE = BLS_ID_SIZE
+ const BLS_PUBLICKEY_SIZE = BLS_ID_SIZE * 3 * 2
+ const BLS_SIGNATURE_SIZE = BLS_ID_SIZE * 3
+
+ let mod = {}
+ let capi = {}
+ let self = {}
+ self.mod = mod
+ self.capi = capi
+
+ const setupWasm = function(fileName, nameSpace, setupFct) {
+ console.log('setupWasm ' + fileName)
+ fetch(fileName)
+ .then(response => response.arrayBuffer())
+ .then(buffer => new Uint8Array(buffer))
+ .then(binary => {
+ mod['wasmBinary'] = binary
+ mod['onRuntimeInitialized'] = function() {
+ setupFct(mod, nameSpace)
+ console.log('setupWasm end')
+ }
+ Module(mod)
+ })
+ return mod
+ }
const ptrToStr = function(pos, n) {
let s = ''
for (let i = 0; i < n; i++) {
@@ -44,6 +61,38 @@ function define_bls_extra_functions(mod) {
mod.HEAP8[pos + i] = s.charCodeAt(i)
}
}
+ const copyToUint32Array = function(a, pos) {
+ for (let i = 0; i < a.length; i++) {
+ a[i] = mod.HEAP32[pos / 4 + i]
+ }
+ }
+ const copyFromUint32Array = function(pos, a) {
+ for (let i = 0; i < a.length; i++) {
+ mod.HEAP32[pos / 4 + i] = a[i]
+ }
+ }
+ self.toHex = function(a, start, n) {
+ let s = ''
+ for (let i = 0; i < n; i++) {
+ s += ('0' + a[start + i].toString(16)).slice(-2)
+ }
+ return s
+ }
+ // Uint8Array to hex string
+ self.toHexStr = function(a) {
+ return self.toHex(a, 0, a.length)
+ }
+ // hex string to Uint8Array
+ self.fromHexStr = function(s) {
+ if (s.length & 1) throw('fromHexStr:length must be even ' + s.length)
+ let n = s.length / 2
+ let a = new Uint8Array(n)
+ for (let i = 0; i < n; i++) {
+ a[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16)
+ }
+ return a
+ }
+
const wrap_outputString = function(func, doesReturnString = true) {
return function(x, ioMode = 0) {
let maxBufSize = 2048
@@ -70,6 +119,29 @@ function define_bls_extra_functions(mod) {
const wrap_outputArray = function(func) {
return wrap_outputString(func, false)
}
+ /*
+ argNum : n
+ func(x0, ..., x_(n-1), buf, ioMode)
+ => func(x0, ..., x_(n-1), pos, buf.length, ioMode)
+ */
+ const wrap_input = function(func, argNum, returnValue = false) {
+ return function() {
+ const args = [...arguments]
+ let buf = args[argNum]
+ let ioMode = args[argNum + 1] // may undefined
+ let stack = mod.Runtime.stackSave()
+ let pos = mod.Runtime.stackAlloc(buf.length)
+ if (typeof(buf) == "string") {
+ AsciiStrToMem(pos, buf)
+ } else {
+ Uint8ArrayToMem(pos, buf)
+ }
+ let r = func(...args.slice(0, argNum), pos, buf.length, ioMode)
+ mod.Runtime.stackRestore(stack)
+ if (returnValue) return r
+ if (r) throw('err wrap_input0 ' + buf)
+ }
+ }
const wrap_input0 = function(func, returnValue = false) {
return function(buf, ioMode = 0) {
let stack = mod.Runtime.stackSave()
@@ -115,6 +187,26 @@ function define_bls_extra_functions(mod) {
if (r) throw('err wrap_input2 ' + buf)
}
}
+ const callSetter = function(func, a, p1, p2) {
+ let pos = mod._malloc(a.length * 4)
+ func(pos, p1, p2) // p1, p2 may be undefined
+ copyToUint32Array(a, pos)
+ mod._free(pos)
+ }
+ const callGetter = function(func, a, p1, p2) {
+ let pos = mod._malloc(a.length * 4)
+ mod.HEAP32.set(a, pos / 4)
+ let s = func(pos, p1, p2)
+ mod._free(pos)
+ return s
+ }
+ const callModifier = function(func, a, p1, p2) {
+ let pos = mod._malloc(a.length * 4)
+ mod.HEAP32.set(a, pos / 4)
+ func(pos, p1, p2) // p1, p2 may be undefined
+ copyToUint32Array(a, pos)
+ mod._free(pos)
+ }
const wrap_keyShare = function(func, dataSize) {
return function(x, vec, id) {
let k = vec.length
@@ -142,257 +234,7 @@ function define_bls_extra_functions(mod) {
if (r) throw('recover ' + n)
}
}
-
- let crypto = window.crypto || window.msCrypto
-
- let copyToUint32Array = function(a, pos) {
- for (let i = 0; i < a.length; i++) {
- a[i] = mod.HEAP32[pos / 4 + i]
- }
- }
- let copyFromUint32Array = function(pos, a) {
- for (let i = 0; i < a.length; i++) {
- mod.HEAP32[pos / 4 + i] = a[i]
- }
- }
- let callSetter = function(func, a, p1, p2) {
- let pos = mod._malloc(a.length * 4)
- func(pos, p1, p2) // p1, p2 may be undefined
- copyToUint32Array(a, pos)
- mod._free(pos)
- }
- let callGetter = function(func, a, p1, p2) {
- let pos = mod._malloc(a.length * 4)
- mod.HEAP32.set(a, pos / 4)
- let s = func(pos, p1, p2)
- mod._free(pos)
- return s
- }
- let callModifier = function(func, a, p1, p2) {
- let pos = mod._malloc(a.length * 4)
- mod.HEAP32.set(a, pos / 4)
- func(pos, p1, p2) // p1, p2 may be undefined
- copyToUint32Array(a, pos)
- mod._free(pos)
- }
- ///////////////////////////////////////////////////////////////
- const FR_SIZE = MCLBN_FP_UNIT_SIZE * 8
- const G1_SIZE = FR_SIZE * 3
- const G2_SIZE = FR_SIZE * 3 * 2
- const GT_SIZE = FR_SIZE * 12
-
- mclBnFr_malloc = function() {
- return mod._malloc(FR_SIZE)
- }
- mcl_free = function(x) {
- mod._free(x)
- }
- mclBnFr_deserialize = wrap_input1(_mclBnFr_deserialize)
- mclBnFr_setLittleEndian = wrap_input1(_mclBnFr_setLittleEndian)
- mclBnFr_setStr = wrap_input1(_mclBnFr_setStr)
- mclBnFr_getStr = wrap_outputString(_mclBnFr_getStr)
- mclBnFr_setHashOf = wrap_input1(_mclBnFr_setHashOf)
-
- ///////////////////////////////////////////////////////////////
- mclBnG1_malloc = function() {
- return mod._malloc(G1_SIZE)
- }
- mclBnG1_setStr = wrap_input1(_mclBnG1_setStr)
- mclBnG1_getStr = wrap_outputString(_mclBnG1_getStr)
- mclBnG1_deserialize = wrap_input1(_mclBnG1_deserialize)
- mclBnG1_serialize = wrap_outputArray(_mclBnG1_serialize)
- mclBnG1_hashAndMapTo = wrap_input1(_mclBnG1_hashAndMapTo)
-
- ///////////////////////////////////////////////////////////////
- mclBnG2_malloc = function() {
- return mod._malloc(G2_SIZE)
- }
- mclBnG2_setStr = wrap_input1(_mclBnG2_setStr)
- mclBnG2_getStr = wrap_outputString(_mclBnG2_getStr)
- mclBnG2_deserialize = wrap_input1(_mclBnG2_deserialize)
- mclBnG2_serialize = wrap_outputArray(_mclBnG2_serialize)
- mclBnG2_hashAndMapTo = wrap_input1(_mclBnG2_hashAndMapTo)
-
- ///////////////////////////////////////////////////////////////
- mclBnGT_malloc = function() {
- return mod._malloc(GT_SIZE)
- }
- mclBnGT_deserialize = wrap_input1(_mclBnGT_deserialize)
- mclBnGT_serialize = wrap_outputArray(_mclBnGT_serialize)
- mclBnGT_setStr = wrap_input1(_mclBnGT_setStr)
- mclBnGT_getStr = wrap_outputString(_mclBnGT_getStr)
- ///////////////////////////////////////////////////////////////
- bls_free = mcl_free
- blsId_malloc = mclBnFr_malloc
- blsSecretKey_malloc = mclBnFr_malloc
- blsPublicKey_malloc = mclBnG2_malloc
- blsSignature_malloc = mclBnG1_malloc
-
- blsInit = function(curveType) {
- return _blsInit(curveType, MCLBN_FP_UNIT_SIZE)
- }
-
- blsGetCurveOrder = wrap_outputString(_blsGetCurveOrder)
- blsGetFieldOrder = wrap_outputString(_blsGetFieldOrder)
-
- blsIdSetDecStr = wrap_input1(_blsIdSetDecStr)
- blsIdSetHexStr = wrap_input1(_blsIdSetHexStr)
- blsIdGetDecStr = wrap_outputString(_blsIdGetDecStr)
- blsIdGetHexStr = wrap_outputString(_blsIdGetHexStr)
-
- blsSecretKeySetDecStr = wrap_input1(_blsSecretKeySetDecStr)
- blsSecretKeySetHexStr = wrap_input1(_blsSecretKeySetHexStr)
- blsSecretKeyGetDecStr = wrap_outputString(_blsSecretKeyGetDecStr)
- blsSecretKeyGetHexStr = wrap_outputString(_blsSecretKeyGetHexStr)
-
- blsIdSerialize = wrap_outputArray(_blsIdSerialize)
- blsSecretKeySerialize = wrap_outputArray(_blsSecretKeySerialize)
- blsPublicKeySerialize = wrap_outputArray(_blsPublicKeySerialize)
- blsSignatureSerialize = wrap_outputArray(_blsSignatureSerialize)
-
- blsIdDeserialize = wrap_input1(_blsIdDeserialize)
- blsSecretKeyDeserialize = wrap_input1(_blsSecretKeyDeserialize)
- blsPublicKeyDeserialize = wrap_input1(_blsPublicKeyDeserialize)
- blsSignatureDeserialize = wrap_input1(_blsSignatureDeserialize)
-
- blsSecretKeySetLittleEndian = wrap_input1(_blsSecretKeySetLittleEndian)
- blsHashToSecretKey = wrap_input1(_blsHashToSecretKey)
- blsSign = wrap_input2(_blsSign)
- blsVerify = wrap_input2(_blsVerify, true)
-
- blsSecretKeyShare = wrap_keyShare(_blsSecretKeyShare, BLS_SECRETKEY_SIZE)
- blsPublicKeyShare = wrap_keyShare(_blsPublicKeyShare, BLS_PUBLICKEY_SIZE)
-
- blsSecretKeyRecover = wrap_recover(_blsSecretKeyRecover, BLS_SECRETKEY_SIZE, BLS_ID_SIZE)
- blsPublicKeyRecover = wrap_recover(_blsPublicKeyRecover, BLS_PUBLICKEY_SIZE, BLS_ID_SIZE)
- blsSignatureRecover = wrap_recover(_blsSignatureRecover, BLS_SIGNATURE_SIZE, BLS_ID_SIZE)
-
- /// BlsId
- BlsId = function() {
- this.a_ = new Uint32Array(BLS_ID_SIZE / 4)
- }
- BlsId.prototype.setInt = function(x) {
- callSetter(blsIdSetInt, this.a_, x)
- }
- BlsId.prototype.setByCSPRNG = function() {
- callSetter(blsSecretKeySetByCSPRNG, this.a_) // same type of BlsSecretKey
- }
- BlsId.prototype.setStr = function(s, base = 10) {
- switch (base) {
- case 10:
- callSetter(blsIdSetDecStr, this.a_, s)
- return
- case 16:
- callSetter(blsIdSetHexStr, this.a_, s)
- return
- default:
- throw('BlsId.setStr:bad base:' + base)
- }
- }
- BlsId.prototype.deserialize = function(s) {
- callSetter(blsIdDeserialize, this.a_, s)
- }
- BlsId.prototype.getStr = function(base = 10) {
- switch (base) {
- case 10:
- return callGetter(blsIdGetDecStr, this.a_)
- case 16:
- return callGetter(blsIdGetHexStr, this.a_)
- default:
- throw('BlsId.getStr:bad base:' + base)
- }
- }
- BlsId.prototype.serialize = function() {
- return callGetter(blsIdSerialize, this.a_)
- }
- /// BlsSecretKey
- BlsSecretKey = function() {
- this.a_ = new Uint32Array(BLS_SECRETKEY_SIZE / 4)
- }
- BlsSecretKey.prototype.setInt = function(x) {
- callSetter(blsIdSetInt, this.a_, x) // same as Id
- }
- BlsSecretKey.prototype.deserialize = function(s) {
- callSetter(blsSecretKeyDeserialize, this.a_, s)
- }
- BlsSecretKey.prototype.setLittleEndian = function(s) {
- callSetter(blsSecretKeySetLittleEndian, this.a_, s)
- }
- BlsSecretKey.prototype.serialize = function() {
- return callGetter(blsSecretKeySerialize, this.a_)
- }
- BlsSecretKey.prototype.setHashOf = function(s) {
- callSetter(blsHashToSecretKey, this.a_, s)
- }
- BlsSecretKey.prototype.setByCSPRNG = function() {
- let a = new Uint8Array(BLS_SECRETKEY_SIZE)
- crypto.getRandomValues(a)
- this.setLittleEndian(a)
-// callSetter(blsSecretKeySetByCSPRNG, this.a_)
- }
- // return BlsPublicKey
- BlsSecretKey.prototype.getPublicKey = function() {
- let pub = new BlsPublicKey()
- let stack = mod.Runtime.stackSave()
- let secPos = mod.Runtime.stackAlloc(this.a_.length * 4)
- let pubPos = mod.Runtime.stackAlloc(pub.a_.length * 4)
- mod.HEAP32.set(this.a_, secPos / 4)
- blsGetPublicKey(pubPos, secPos)
- copyToUint32Array(pub.a_, pubPos)
- mod.Runtime.stackRestore(stack)
- return pub
- }
- /*
- input
- m : message (string or Uint8Array)
- return
- BlsSignature
- */
- BlsSecretKey.prototype.sign = function(m) {
- let sig = new BlsSignature()
- let stack = mod.Runtime.stackSave()
- let secPos = mod.Runtime.stackAlloc(this.a_.length * 4)
- let sigPos = mod.Runtime.stackAlloc(sig.a_.length * 4)
- mod.HEAP32.set(this.a_, secPos / 4)
- blsSign(sigPos, secPos, m)
- copyToUint32Array(sig.a_, sigPos)
- mod.Runtime.stackRestore(stack)
- return sig
- }
-
- /// BlsPublicKey
- BlsPublicKey = function() {
- this.a_ = new Uint32Array(BLS_PUBLICKEY_SIZE / 4)
- }
- BlsPublicKey.prototype.deserialize = function(s) {
- callSetter(blsPublicKeyDeserialize, this.a_, s)
- }
- BlsPublicKey.prototype.serialize = function() {
- return callGetter(blsPublicKeySerialize, this.a_)
- }
- BlsPublicKey.prototype.verify = function(sig, m) {
- let stack = mod.Runtime.stackSave()
- let pubPos = mod.Runtime.stackAlloc(this.a_.length * 4)
- let sigPos = mod.Runtime.stackAlloc(sig.a_.length * 4)
- mod.HEAP32.set(this.a_, pubPos / 4)
- mod.HEAP32.set(sig.a_, sigPos / 4)
- let r = blsVerify(sigPos, pubPos, m)
- mod.Runtime.stackRestore(stack)
- return r != 0
- }
-
- /// BlsSignature
- BlsSignature = function() {
- this.a_ = new Uint32Array(BLS_SIGNATURE_SIZE / 4)
- }
- BlsSignature.prototype.deserialize = function(s) {
- callSetter(blsSignatureDeserialize, this.a_, s)
- }
- BlsSignature.prototype.serialize = function() {
- return callGetter(blsSignatureSerialize, this.a_)
- }
-
- const share = function(func, a, size, vec, id) {
+ const callShare = function(func, a, size, vec, id) {
let stack = mod.Runtime.stackSave()
let pos = mod.Runtime.stackAlloc(a.length * 4)
let idPos = mod.Runtime.stackAlloc(id.a_.length * 4)
@@ -407,20 +249,7 @@ function define_bls_extra_functions(mod) {
copyToUint32Array(a, pos)
mod.Runtime.stackRestore(stack)
}
- /*
- set shared BlsSecretKey by msk and id
- input
- msk : master secret key(array of BlsSecretKey)
- id : BlsId
- */
- BlsSecretKey.prototype.share = function(msk, id) {
- share(_blsSecretKeyShare, this.a_, BLS_SECRETKEY_SIZE, msk, id)
- }
- BlsPublicKey.prototype.share = function(msk, id) {
- share(_blsPublicKeyShare, this.a_, BLS_PUBLICKEY_SIZE, msk, id)
- }
-
- const recover = function(func, a, size, vec, idVec) {
+ const callRecover = function(func, a, size, vec, idVec) {
let n = vec.length
if (n != idVec.length) throw('recover:bad length')
let stack = mod.Runtime.stackSave()
@@ -437,18 +266,292 @@ function define_bls_extra_functions(mod) {
copyToUint32Array(a, secPos)
mod.Runtime.stackRestore(stack)
}
- /*
- recover BlsSecretKey from (secVec, idVec)
- secVec : array of BlsSecretKey
- */
- BlsSecretKey.prototype.recover = function(secVec, idVec) {
- recover(_blsSecretKeyRecover, this.a_, BLS_SECRETKEY_SIZE, secVec, idVec)
- }
- BlsPublicKey.prototype.recover = function(secVec, idVec) {
- recover(_blsPublicKeyRecover, this.a_, BLS_PUBLICKEY_SIZE, secVec, idVec)
+ const define_extra_functions = function(mod) {
+ capi.mclBnFr_malloc = function() {
+ return mod._malloc(MCLBN_FP_SIZE)
+ }
+ capi.mcl_free = function(x) {
+ mod._free(x)
+ }
+ capi.mclBnFr_deserialize = wrap_input1(capi._mclBnFr_deserialize)
+ capi.mclBnFr_setLittleEndian = wrap_input1(capi._mclBnFr_setLittleEndian)
+ capi.mclBnFr_setStr = wrap_input1(capi._mclBnFr_setStr)
+ capi.mclBnFr_getStr = wrap_outputString(capi._mclBnFr_getStr)
+ capi.mclBnFr_setHashOf = wrap_input1(capi._mclBnFr_setHashOf)
+
+ ///////////////////////////////////////////////////////////////
+ capi.mclBnG1_malloc = function() {
+ return mod._malloc(MCLBN_G1_SIZE)
+ }
+ capi.mclBnG1_setStr = wrap_input1(capi._mclBnG1_setStr)
+ capi.mclBnG1_getStr = wrap_outputString(capi._mclBnG1_getStr)
+ capi.mclBnG1_deserialize = wrap_input1(capi._mclBnG1_deserialize)
+ capi.mclBnG1_serialize = wrap_outputArray(capi._mclBnG1_serialize)
+ capi.mclBnG1_hashAndMapTo = wrap_input1(capi._mclBnG1_hashAndMapTo)
+
+ ///////////////////////////////////////////////////////////////
+ capi.mclBnG2_malloc = function() {
+ return mod._malloc(MCLBN_G2_SIZE)
+ }
+ capi.mclBnG2_setStr = wrap_input1(capi._mclBnG2_setStr)
+ capi.mclBnG2_getStr = wrap_outputString(capi._mclBnG2_getStr)
+ capi.mclBnG2_deserialize = wrap_input1(capi._mclBnG2_deserialize)
+ capi.mclBnG2_serialize = wrap_outputArray(capi._mclBnG2_serialize)
+ capi.mclBnG2_hashAndMapTo = wrap_input1(capi._mclBnG2_hashAndMapTo)
+
+ ///////////////////////////////////////////////////////////////
+ capi.mclBnGT_malloc = function() {
+ return mod._malloc(MCLBN_GT_SIZE)
+ }
+ capi.mclBnGT_deserialize = wrap_input1(capi._mclBnGT_deserialize)
+ capi.mclBnGT_serialize = wrap_outputArray(capi._mclBnGT_serialize)
+ capi.mclBnGT_setStr = wrap_input1(capi._mclBnGT_setStr)
+ capi.mclBnGT_getStr = wrap_outputString(capi._mclBnGT_getStr)
+ ///////////////////////////////////////////////////////////////
+ capi.bls_free = capi.mcl_free
+ capi.blsId_malloc = capi.mclBnFr_malloc
+ capi.blsSecretKey_malloc = capi.mclBnFr_malloc
+ capi.blsPublicKey_malloc = capi.mclBnG2_malloc
+ capi.blsSignature_malloc = capi.mclBnG1_malloc
+
+ capi.blsInit = function(curveType = MCLBN_CURVE_FP254BNB) {
+ return capi._blsInit(curveType, MCLBN_FP_UNIT_SIZE)
+ }
+
+ capi.blsGetCurveOrder = wrap_outputString(capi._blsGetCurveOrder)
+ capi.blsGetFieldOrder = wrap_outputString(capi._blsGetFieldOrder)
+
+ capi.blsIdSetDecStr = wrap_input1(capi._blsIdSetDecStr)
+ capi.blsIdSetHexStr = wrap_input1(capi._blsIdSetHexStr)
+ capi.blsIdGetDecStr = wrap_outputString(capi._blsIdGetDecStr)
+ capi.blsIdGetHexStr = wrap_outputString(capi._blsIdGetHexStr)
+
+ capi.blsSecretKeySetDecStr = wrap_input1(capi._blsSecretKeySetDecStr)
+ capi.blsSecretKeySetHexStr = wrap_input1(capi._blsSecretKeySetHexStr)
+ capi.blsSecretKeyGetDecStr = wrap_outputString(capi._blsSecretKeyGetDecStr)
+ capi.blsSecretKeyGetHexStr = wrap_outputString(capi._blsSecretKeyGetHexStr)
+
+ capi.blsIdSerialize = wrap_outputArray(capi._blsIdSerialize)
+ capi.blsSecretKeySerialize = wrap_outputArray(capi._blsSecretKeySerialize)
+ capi.blsPublicKeySerialize = wrap_outputArray(capi._blsPublicKeySerialize)
+ capi.blsSignatureSerialize = wrap_outputArray(capi._blsSignatureSerialize)
+
+ capi.blsIdDeserialize = wrap_input1(capi._blsIdDeserialize)
+ capi.blsSecretKeyDeserialize = wrap_input1(capi._blsSecretKeyDeserialize)
+ capi.blsPublicKeyDeserialize = wrap_input1(capi._blsPublicKeyDeserialize)
+ capi.blsSignatureDeserialize = wrap_input1(capi._blsSignatureDeserialize)
+
+ capi.blsSecretKeySetLittleEndian = wrap_input1(capi._blsSecretKeySetLittleEndian)
+ capi.blsHashToSecretKey = wrap_input1(capi._blsHashToSecretKey)
+ capi.blsSign = wrap_input2(capi._blsSign)
+ capi.blsVerify = wrap_input2(capi._blsVerify, true)
+
+ capi.blsSecretKeyShare = wrap_keyShare(capi._blsSecretKeyShare, BLS_SECRETKEY_SIZE)
+ capi.blsPublicKeyShare = wrap_keyShare(capi._blsPublicKeyShare, BLS_PUBLICKEY_SIZE)
+ capi.blsSecretKeyRecover = wrap_recover(capi._blsSecretKeyRecover, BLS_SECRETKEY_SIZE, BLS_ID_SIZE)
+ capi.blsPublicKeyRecover = wrap_recover(capi._blsPublicKeyRecover, BLS_PUBLICKEY_SIZE, BLS_ID_SIZE)
+ capi.blsSignatureRecover = wrap_recover(capi._blsSignatureRecover, BLS_SIGNATURE_SIZE, BLS_ID_SIZE)
+
+ capi.sheInit = function(curveType = MCLBN_CURVE_FP254BNB) {
+ let r = capi._sheInit(curveType, MCLBN_FP_UNIT_SIZE)
+ console.log('sheInit ' + r)
+ if (r) throw('sheInit')
+ }
+ class Common {
+ constructor(size) {
+ this.a_ = new Uint32Array(size / 4)
+ }
+ fromHexStr(s) {
+ this.deserialize(self.fromHexStr(s))
+ }
+ toHexStr() {
+ return self.toHexStr(this.serialize())
+ }
+ dump(msg = '') {
+ console.log(msg + this.toHexStr())
+ }
+ }
+
+ self.Id = class extends Common {
+ constructor() {
+ super(BLS_ID_SIZE)
+ }
+ setInt(x) {
+ callSetter(capi.blsIdSetInt, this.a_, x)
+ }
+ setByCSPRNG() {
+ callSetter(capi.blsSecretKeySetByCSPRNG, this.a_) // same type of BlsSecretKey
+ }
+ setStr(s, base = 10) {
+ switch (base) {
+ case 10:
+ callSetter(capi.blsIdSetDecStr, this.a_, s)
+ return
+ case 16:
+ callSetter(capi.blsIdSetHexStr, this.a_, s)
+ return
+ default:
+ throw('BlsId.setStr:bad base:' + base)
+ }
+ }
+ deserialize(s) {
+ callSetter(capi.blsIdDeserialize, this.a_, s)
+ }
+ getStr(base = 10) {
+ switch (base) {
+ case 10:
+ return callGetter(capi.blsIdGetDecStr, this.a_)
+ case 16:
+ return callGetter(capi.blsIdGetHexStr, this.a_)
+ default:
+ throw('BlsId.getStr:bad base:' + base)
+ }
+ }
+ serialize() {
+ return callGetter(capi.blsIdSerialize, this.a_)
+ }
+ }
+ self.getIdFromHexStr = function(s) {
+ r = new self.Id()
+ r.fromHexStr(s)
+ return r
+ }
+
+ self.SecretKey = class extends Common {
+ constructor() {
+ super(BLS_SECRETKEY_SIZE)
+ }
+ setInt(x) {
+ callSetter(capi.blsIdSetInt, this.a_, x) // same as Id
+ }
+ deserialize(s) {
+ callSetter(capi.blsSecretKeyDeserialize, this.a_, s)
+ }
+ setLittleEndian(s) {
+ callSetter(capi.blsSecretKeySetLittleEndian, this.a_, s)
+ }
+ serialize() {
+ return callGetter(capi.blsSecretKeySerialize, this.a_)
+ }
+ share(msk, id) {
+ callShare(capi._blsSecretKeyShare, this.a_, BLS_SECRETKEY_SIZE, msk, id)
+ }
+ recover(secVec, idVec) {
+ callRecover(capi._blsSecretKeyRecover, this.a_, BLS_SECRETKEY_SIZE, secVec, idVec)
+ }
+ setHashOf(s) {
+ callSetter(capi.blsHashToSecretKey, this.a_, s)
+ }
+ setByCSPRNG() {
+ let a = new Uint8Array(BLS_SECRETKEY_SIZE)
+ crypto.getRandomValues(a)
+ this.setLittleEndian(a)
+ // callSetter(capi.blsSecretKeySetByCSPRNG, this.a_)
+ }
+ getPublicKey() {
+ let pub = new self.PublicKey()
+ let stack = mod.Runtime.stackSave()
+ let secPos = mod.Runtime.stackAlloc(this.a_.length * 4)
+ let pubPos = mod.Runtime.stackAlloc(pub.a_.length * 4)
+ mod.HEAP32.set(this.a_, secPos / 4)
+ capi.blsGetPublicKey(pubPos, secPos)
+ copyToUint32Array(pub.a_, pubPos)
+ mod.Runtime.stackRestore(stack)
+ return pub
+ }
+ /*
+ input
+ m : message (string or Uint8Array)
+ return
+ BlsSignature
+ */
+ sign(m) {
+ let sig = new self.Signature()
+ let stack = mod.Runtime.stackSave()
+ let secPos = mod.Runtime.stackAlloc(this.a_.length * 4)
+ let sigPos = mod.Runtime.stackAlloc(sig.a_.length * 4)
+ mod.HEAP32.set(this.a_, secPos / 4)
+ capi.blsSign(sigPos, secPos, m)
+ copyToUint32Array(sig.a_, sigPos)
+ mod.Runtime.stackRestore(stack)
+ return sig
+ }
+ }
+ self.getSecretKeyFromHexStr = function(s) {
+ r = new self.SecretKey()
+ r.fromHexStr(s)
+ return r
+ }
+
+ self.PublicKey = class extends Common {
+ constructor() {
+ super(BLS_PUBLICKEY_SIZE)
+ }
+ deserialize(s) {
+ callSetter(capi.blsPublicKeyDeserialize, this.a_, s)
+ }
+ serialize() {
+ return callGetter(capi.blsPublicKeySerialize, this.a_)
+ }
+ share(msk, id) {
+ callShare(capi._blsPublicKeyShare, this.a_, BLS_PUBLICKEY_SIZE, msk, id)
+ }
+ recover(secVec, idVec) {
+ callRecover(capi._blsPublicKeyRecover, this.a_, BLS_PUBLICKEY_SIZE, secVec, idVec)
+ }
+ verify(sig, m) {
+ let stack = mod.Runtime.stackSave()
+ let pubPos = mod.Runtime.stackAlloc(this.a_.length * 4)
+ let sigPos = mod.Runtime.stackAlloc(sig.a_.length * 4)
+ mod.HEAP32.set(this.a_, pubPos / 4)
+ mod.HEAP32.set(sig.a_, sigPos / 4)
+ let r = capi.blsVerify(sigPos, pubPos, m)
+ mod.Runtime.stackRestore(stack)
+ return r != 0
+ }
+ }
+ self.getPublicKeyFromHexStr = function(s) {
+ r = new self.PublicKey()
+ r.fromHexStr(s)
+ return r
+ }
+
+ self.Signature = class extends Common {
+ constructor() {
+ super(BLS_SIGNATURE_SIZE)
+ }
+ deserialize(s) {
+ callSetter(capi.blsSignatureDeserialize, this.a_, s)
+ }
+ serialize() {
+ return callGetter(capi.blsSignatureSerialize, this.a_)
+ }
+ recover(secVec, idVec) {
+ callRecover(capi._blsSignatureRecover, this.a_, BLS_SIGNATURE_SIZE, secVec, idVec)
+ }
+ }
+ self.getSignatureFromHexStr = function(s) {
+ r = new self.Signature()
+ r.fromHexStr(s)
+ return r
+ }
}
- BlsSignature.prototype.recover = function(secVec, idVec) {
- recover(_blsSignatureRecover, this.a_, BLS_SIGNATURE_SIZE, secVec, idVec)
+ self.init = function(curveType = MCLBN_CURVE_FP254BNB, callback = null) {
+ setupWasm('bls_c.wasm', null, function(_mod, ns) {
+ mod = _mod
+ fetch('exported-bls.json')
+ .then(response => response.json())
+ .then(json => {
+ mod.json = json
+ json.forEach(func => {
+ capi[func.exportName] = mod.cwrap(func.name, func.returns, func.args)
+ })
+ define_extra_functions(mod)
+ let r = capi.blsInit(curveType)
+ console.log('finished ' + r)
+ if (callback) callback()
+ })
+ })
}
-}
-
+ return self
+})