diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-10-10 14:56:32 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-10-10 14:56:32 +0800 |
commit | 8f1d217f654ee9e8e4cf769e0140e3c511a9fdaf (patch) | |
tree | 62eca2834317bcbb889534fbaf7460ae662dfce6 /docs/demo/bls.js | |
parent | b0328ca6ad728dceb37f3244d1301899fba2ac0b (diff) | |
download | dexon-bls-8f1d217f654ee9e8e4cf769e0140e3c511a9fdaf.tar dexon-bls-8f1d217f654ee9e8e4cf769e0140e3c511a9fdaf.tar.gz dexon-bls-8f1d217f654ee9e8e4cf769e0140e3c511a9fdaf.tar.bz2 dexon-bls-8f1d217f654ee9e8e4cf769e0140e3c511a9fdaf.tar.lz dexon-bls-8f1d217f654ee9e8e4cf769e0140e3c511a9fdaf.tar.xz dexon-bls-8f1d217f654ee9e8e4cf769e0140e3c511a9fdaf.tar.zst dexon-bls-8f1d217f654ee9e8e4cf769e0140e3c511a9fdaf.zip |
[js] add BlsSecretKey
Diffstat (limited to 'docs/demo/bls.js')
-rw-r--r-- | docs/demo/bls.js | 101 |
1 files changed, 61 insertions, 40 deletions
diff --git a/docs/demo/bls.js b/docs/demo/bls.js index 67c8b49..9b2433c 100644 --- a/docs/demo/bls.js +++ b/docs/demo/bls.js @@ -22,9 +22,15 @@ const MCLBN_CURVE_FP382_2 = 2 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 BlsId = function() { - this.v_ = new Uint32Array(BLS_ID_SIZE / 4) + this.a_ = new Uint32Array(BLS_ID_SIZE / 4) +} +BlsSecretKey = function() { + this.a_ = new Uint32Array(BLS_SECRETKEY_SIZE / 4) } function define_bls_extra_functions(mod) { @@ -33,12 +39,17 @@ function define_bls_extra_functions(mod) { for (let i = 0; i < n; i++) { s += String.fromCharCode(mod.HEAP8[pos + i]) } - // let s = mod.Pointer_stringify(pos, n) return s } - Uint8ArrayToMem = function(a, buf) { - mod.HEAP8.set(a, buf) - // mod.writeArrayToMemory(a, buf) + Uint8ArrayToMem = function(pos, buf) { + for (let i = 0; i < buf.length; i++) { + mod.HEAP8[pos + i] = buf[i] + } + } + AsciiStrToMem = function(pos, s) { + for (let i = 0; i < s.length; i++) { + mod.HEAP8[pos + i] = s.charCodeAt(i) + } } wrap_outputString = function(func, doesReturnString = true) { return function(x, ioMode = 0) { @@ -71,7 +82,7 @@ function define_bls_extra_functions(mod) { let stack = mod.Runtime.stackSave() let pos = mod.Runtime.stackAlloc(buf.length) if (typeof(buf) == "string") { - mod.writeAsciiToMemory(buf, pos) + AsciiStrToMem(pos, buf) } else { Uint8ArrayToMem(pos, buf) } @@ -86,7 +97,7 @@ function define_bls_extra_functions(mod) { let stack = mod.Runtime.stackSave() let pos = mod.Runtime.stackAlloc(buf.length) if (typeof(buf) == "string") { - mod.writeAsciiToMemory(buf, pos) + AsciiStrToMem(pos, buf) } else { Uint8ArrayToMem(pos, buf) } @@ -101,7 +112,7 @@ function define_bls_extra_functions(mod) { let stack = mod.Runtime.stackSave() let pos = mod.Runtime.stackAlloc(buf.length) if (typeof(buf) == "string") { - mod.writeAsciiToMemory(buf, pos) + AsciiStrToMem(pos, buf) } else { Uint8ArrayToMem(pos, buf) } @@ -144,10 +155,6 @@ function define_bls_extra_functions(mod) { const G2_SIZE = FR_SIZE * 3 * 2 const GT_SIZE = FR_SIZE * 12 - const SECRETKEY_SIZE = FR_SIZE - const PUBLICKEY_SIZE = G2_SIZE - const SIGNATURE_SIZE = G1_SIZE - mclBnFr_malloc = function() { return mod._malloc(FR_SIZE) } @@ -207,6 +214,11 @@ function define_bls_extra_functions(mod) { 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) @@ -217,72 +229,81 @@ function define_bls_extra_functions(mod) { 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, SECRETKEY_SIZE) - blsPublicKeyShare = wrap_keyShare(_blsPublicKeyShare, PUBLICKEY_SIZE) + blsSecretKeyShare = wrap_keyShare(_blsSecretKeyShare, BLS_SECRETKEY_SIZE) + blsPublicKeyShare = wrap_keyShare(_blsPublicKeyShare, BLS_PUBLICKEY_SIZE) - blsSecretKeyRecover = wrap_recover(_blsSecretKeyRecover, SECRETKEY_SIZE, BLS_ID_SIZE) - blsPublicKeyRecover = wrap_recover(_blsPublicKeyRecover, PUBLICKEY_SIZE, BLS_ID_SIZE) - blsSignatureRecover = wrap_recover(_blsSignatureRecover, SIGNATURE_SIZE, BLS_ID_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) - var mallocAndCopyFromUint32Array = function(a) { - let pos = mod._malloc(a.length * 4) - mod.HEAP32.set(a, pos / 4) -// for (let i = 0; i < a.length; i++) { -// mod.HEAP32[pos / 4 + i] = a[i] -// } - return pos - } - var copyToUint32ArrayAndFree = function(a, pos) { + var copyToUint32Array = function(a, pos) { for (let i = 0; i < a.length; i++) { a[i] = mod.HEAP32[pos / 4 + i] } - mod._free(pos) } - var callSetter1 = function(func, v, p1) { - let pos = blsId_malloc() + var callSetter1 = function(func, a, p1) { + let pos = mod._malloc(a.length * 4) + mod.HEAP32.set(a, pos / 4) func(pos, p1) - copyToUint32ArrayAndFree(v, pos) + copyToUint32Array(a, pos) + mod._free(pos) } - var callGetter0 = function(func, v) { - let pos = mallocAndCopyFromUint32Array(v) + var callGetter0 = function(func, a) { + let pos = mod._malloc(a.length * 4) + mod.HEAP32.set(a, pos / 4) let s = func(pos) - bls_free(pos) + mod._free(pos) return s } + /// BlsId BlsId.prototype.setInt = function(x) { - callSetter1(blsIdSetInt, this.v_, x) + callSetter1(blsIdSetInt, this.a_, x) } BlsId.prototype.setStr = function(s, base = 10) { switch (base) { case 10: - callSetter1(blsIdSetDecStr, this.v_, s) + callSetter1(blsIdSetDecStr, this.a_, s) return case 16: - callSetter1(blsIdSetHexStr, this.v_, s) + callSetter1(blsIdSetHexStr, this.a_, s) return default: throw('BlsId.setStr:bad base:' + base) } } BlsId.prototype.deserialize = function(s) { - callSetter1(blsIdDeserialize, this.v_, s) + callSetter1(blsIdDeserialize, this.a_, s) } BlsId.prototype.getStr = function(base = 10) { switch (base) { case 10: - return callGetter0(blsIdGetDecStr, this.v_) + return callGetter0(blsIdGetDecStr, this.a_) case 16: - return callGetter0(blsIdGetHexStr, this.v_) + return callGetter0(blsIdGetHexStr, this.a_) default: throw('BlsId.getStr:bad base:' + base) } } BlsId.prototype.serialize = function() { - return callGetter0(blsIdSerialize, this.v_) + return callGetter0(blsIdSerialize, this.a_) + } + /// BlsSecretKey + BlsSecretKey.prototype.setInt = function(x) { + callSetter1(blsIdSetInt, this.a_, x) // same as Id + } + BlsSecretKey.prototype.deserialize = function(s) { + callSetter1(blsSecretKeyDeserialize, this.a_, s) + } + BlsSecretKey.prototype.setLittleEndian = function(s) { + callSetter1(blsSecretKeySetLittleEndian, this.a_, s) + } + BlsSecretKey.prototype.serialize = function() { + return callGetter0(blsSecretKeySerialize, this.a_) } } |