|
|
@ -1,20 +1,10 @@ |
|
|
|
function setupWasm(fileName, nameSpace, setupFct) { |
|
|
|
(function(return_she) { |
|
|
|
console.log('setupWasm ' + fileName) |
|
|
|
if (typeof exports === 'object') { |
|
|
|
let mod = {} |
|
|
|
module.exports = return_she() |
|
|
|
fetch(fileName) |
|
|
|
} else { |
|
|
|
.then(response => response.arrayBuffer()) |
|
|
|
window.she = return_she() |
|
|
|
.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() { |
|
|
|
const MCLBN_CURVE_FP254BNB = 0 |
|
|
|
const MCLBN_CURVE_FP254BNB = 0 |
|
|
|
const MCLBN_FP_UNIT_SIZE = 4 |
|
|
|
const MCLBN_FP_UNIT_SIZE = 4 |
|
|
|
const MCLBN_FP_SIZE = MCLBN_FP_UNIT_SIZE * 8 |
|
|
|
const MCLBN_FP_SIZE = MCLBN_FP_UNIT_SIZE * 8 |
|
|
@ -28,29 +18,43 @@ const SHE_CIPHERTEXT_G1_SIZE = MCLBN_G1_SIZE * 2 |
|
|
|
const SHE_CIPHERTEXT_G2_SIZE = MCLBN_G2_SIZE * 2 |
|
|
|
const SHE_CIPHERTEXT_G2_SIZE = MCLBN_G2_SIZE * 2 |
|
|
|
const SHE_CIPHERTEXT_GT_SIZE = MCLBN_GT_SIZE * 4 |
|
|
|
const SHE_CIPHERTEXT_GT_SIZE = MCLBN_GT_SIZE * 4 |
|
|
|
|
|
|
|
|
|
|
|
//SheSecretKey = function() {
|
|
|
|
let she = {} |
|
|
|
// this.a_ = new Uint8Array(
|
|
|
|
let mod = {} |
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function define_she_extra_functions(mod) { |
|
|
|
const setupWasm = function(fileName, nameSpace, setupFct) { |
|
|
|
ptrToStr = function(pos, n) { |
|
|
|
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 define_she_extra_functions = function(mod) { |
|
|
|
|
|
|
|
const ptrToStr = function(pos, n) { |
|
|
|
let s = '' |
|
|
|
let s = '' |
|
|
|
for (let i = 0; i < n; i++) { |
|
|
|
for (let i = 0; i < n; i++) { |
|
|
|
s += String.fromCharCode(mod.HEAP8[pos + i]) |
|
|
|
s += String.fromCharCode(mod.HEAP8[pos + i]) |
|
|
|
} |
|
|
|
} |
|
|
|
return s |
|
|
|
return s |
|
|
|
} |
|
|
|
} |
|
|
|
Uint8ArrayToMem = function(pos, buf) { |
|
|
|
const Uint8ArrayToMem = function(pos, buf) { |
|
|
|
for (let i = 0; i < buf.length; i++) { |
|
|
|
for (let i = 0; i < buf.length; i++) { |
|
|
|
mod.HEAP8[pos + i] = buf[i] |
|
|
|
mod.HEAP8[pos + i] = buf[i] |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
AsciiStrToMem = function(pos, s) { |
|
|
|
const AsciiStrToMem = function(pos, s) { |
|
|
|
for (let i = 0; i < s.length; i++) { |
|
|
|
for (let i = 0; i < s.length; i++) { |
|
|
|
mod.HEAP8[pos + i] = s.charCodeAt(i) |
|
|
|
mod.HEAP8[pos + i] = s.charCodeAt(i) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
wrap_outputString = function(func, doesReturnString = true) { |
|
|
|
const wrap_outputString = function(func, doesReturnString = true) { |
|
|
|
return function(x, ioMode = 0) { |
|
|
|
return function(x, ioMode = 0) { |
|
|
|
let maxBufSize = 2048 |
|
|
|
let maxBufSize = 2048 |
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
@ -73,10 +77,10 @@ function define_she_extra_functions(mod) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
wrap_outputArray = function(func) { |
|
|
|
const wrap_outputArray = function(func) { |
|
|
|
return wrap_outputString(func, false) |
|
|
|
return wrap_outputString(func, false) |
|
|
|
} |
|
|
|
} |
|
|
|
wrap_input0 = function(func, returnValue = false) { |
|
|
|
const wrap_input0 = function(func, returnValue = false) { |
|
|
|
return function(buf, ioMode = 0) { |
|
|
|
return function(buf, ioMode = 0) { |
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
|
let pos = mod.Runtime.stackAlloc(buf.length) |
|
|
|
let pos = mod.Runtime.stackAlloc(buf.length) |
|
|
@ -91,7 +95,7 @@ function define_she_extra_functions(mod) { |
|
|
|
if (r) throw('err wrap_input0 ' + buf) |
|
|
|
if (r) throw('err wrap_input0 ' + buf) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
wrap_input1 = function(func, returnValue = false) { |
|
|
|
const wrap_input1 = function(func, returnValue = false) { |
|
|
|
return function(x1, buf, ioMode = 0) { |
|
|
|
return function(x1, buf, ioMode = 0) { |
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
|
let pos = mod.Runtime.stackAlloc(buf.length) |
|
|
|
let pos = mod.Runtime.stackAlloc(buf.length) |
|
|
@ -106,7 +110,7 @@ function define_she_extra_functions(mod) { |
|
|
|
if (r) throw('err wrap_input1 ' + buf) |
|
|
|
if (r) throw('err wrap_input1 ' + buf) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
wrap_input2 = function(func, returnValue = false) { |
|
|
|
const wrap_input2 = function(func, returnValue = false) { |
|
|
|
return function(x1, x2, buf, ioMode = 0) { |
|
|
|
return function(x1, x2, buf, ioMode = 0) { |
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
|
let pos = mod.Runtime.stackAlloc(buf.length) |
|
|
|
let pos = mod.Runtime.stackAlloc(buf.length) |
|
|
@ -121,7 +125,7 @@ function define_she_extra_functions(mod) { |
|
|
|
if (r) throw('err wrap_input2 ' + buf) |
|
|
|
if (r) throw('err wrap_input2 ' + buf) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
wrap_dec = function(func) { |
|
|
|
const wrap_dec = function(func) { |
|
|
|
return function(sec, c) { |
|
|
|
return function(sec, c) { |
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
|
let pos = mod.Runtime.stackAlloc(8) |
|
|
|
let pos = mod.Runtime.stackAlloc(8) |
|
|
@ -132,7 +136,7 @@ function define_she_extra_functions(mod) { |
|
|
|
return v |
|
|
|
return v |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
let crypto = window.crypto || window.msCrypto |
|
|
|
const crypto = window.crypto || window.msCrypto |
|
|
|
|
|
|
|
|
|
|
|
let copyToUint32Array = function(a, pos) { |
|
|
|
let copyToUint32Array = function(a, pos) { |
|
|
|
for (let i = 0; i < a.length; i++) { |
|
|
|
for (let i = 0; i < a.length; i++) { |
|
|
@ -144,26 +148,63 @@ function define_she_extra_functions(mod) { |
|
|
|
mod.HEAP32[pos / 4 + i] = a[i] |
|
|
|
mod.HEAP32[pos / 4 + i] = a[i] |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
let callSetter = function(func, a, p1, p2) { |
|
|
|
she.callSetter = function(func, a, p1, p2) { |
|
|
|
let pos = mod._malloc(a.length * 4) |
|
|
|
let pos = mod._malloc(a.length * 4) |
|
|
|
func(pos, p1, p2) // p1, p2 may be undefined
|
|
|
|
func(pos, p1, p2) // p1, p2 may be undefined
|
|
|
|
copyToUint32Array(a, pos) |
|
|
|
copyToUint32Array(a, pos) |
|
|
|
mod._free(pos) |
|
|
|
mod._free(pos) |
|
|
|
} |
|
|
|
} |
|
|
|
let callGetter = function(func, a, p1, p2) { |
|
|
|
she.callGetter = function(func, a, p1, p2) { |
|
|
|
let pos = mod._malloc(a.length * 4) |
|
|
|
let pos = mod._malloc(a.length * 4) |
|
|
|
mod.HEAP32.set(a, pos / 4) |
|
|
|
mod.HEAP32.set(a, pos / 4) |
|
|
|
let s = func(pos, p1, p2) |
|
|
|
let s = func(pos, p1, p2) |
|
|
|
mod._free(pos) |
|
|
|
mod._free(pos) |
|
|
|
return s |
|
|
|
return s |
|
|
|
} |
|
|
|
} |
|
|
|
let callModifier = function(func, a, p1, p2) { |
|
|
|
she.callModifier = function(func, a, p1, p2) { |
|
|
|
let pos = mod._malloc(a.length * 4) |
|
|
|
let pos = mod._malloc(a.length * 4) |
|
|
|
mod.HEAP32.set(a, pos / 4) |
|
|
|
mod.HEAP32.set(a, pos / 4) |
|
|
|
func(pos, p1, p2) // p1, p2 may be undefined
|
|
|
|
func(pos, p1, p2) // p1, p2 may be undefined
|
|
|
|
copyToUint32Array(a, pos) |
|
|
|
copyToUint32Array(a, pos) |
|
|
|
mod._free(pos) |
|
|
|
mod._free(pos) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
she.callEnc = function(func, cstr, pub, m) { |
|
|
|
|
|
|
|
let c = new cstr() |
|
|
|
|
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
|
|
|
|
|
let cPos = mod.Runtime.stackAlloc(c.length * 4) |
|
|
|
|
|
|
|
let pubPos = mod.Runtime.stackAlloc(pub.length * 4) |
|
|
|
|
|
|
|
copyFromUint32Array(pubPos, pub); |
|
|
|
|
|
|
|
func(cPos, pubPos, m) |
|
|
|
|
|
|
|
copyToUint32Array(c.a_, cPos) |
|
|
|
|
|
|
|
mod.Runtime.stackRestore(stack) |
|
|
|
|
|
|
|
return c |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.callDec = function(func, sec, c) { |
|
|
|
|
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
|
|
|
|
|
let secPos = mod.Runtime.stackAlloc(sec.length * 4) |
|
|
|
|
|
|
|
let cPos = mod.Runtime.stackAlloc(c.length * 4) |
|
|
|
|
|
|
|
copyFromUint32Array(secPos, sec); |
|
|
|
|
|
|
|
copyFromUint32Array(cPos, c); |
|
|
|
|
|
|
|
let r = func(secPos, cPos) |
|
|
|
|
|
|
|
mod.Runtime.stackRestore(stack) |
|
|
|
|
|
|
|
return r |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.callSetByCSPRNG = function(sec) { |
|
|
|
|
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
|
|
|
|
|
let secPos = mod.Runtime.stackAlloc(sec.length * 4) |
|
|
|
|
|
|
|
sheSecretKeySetByCSPRNG(secPos) |
|
|
|
|
|
|
|
copyToUint32Array(sec, secPos) |
|
|
|
|
|
|
|
mod.Runtime.stackRestore(stack) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.callGetPublicKey = function(pub, sec) { |
|
|
|
|
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
|
|
|
|
|
let secPos = mod.Runtime.stackAlloc(sec.length * 4) |
|
|
|
|
|
|
|
let pubPos = mod.Runtime.stackAlloc(pub.length * 4) |
|
|
|
|
|
|
|
copyFromUint32Array(secPos, sec) |
|
|
|
|
|
|
|
sheGetPublicKey(pubPos, secPos) |
|
|
|
|
|
|
|
copyToUint32Array(pub, pubPos) |
|
|
|
|
|
|
|
mod.Runtime.stackRestore(stack) |
|
|
|
|
|
|
|
} |
|
|
|
///////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////
|
|
|
|
she_free = function(p) { |
|
|
|
she_free = function(p) { |
|
|
|
mod._free(p) |
|
|
|
mod._free(p) |
|
|
@ -209,3 +250,71 @@ function define_she_extra_functions(mod) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
she.init = function(callback = null) { |
|
|
|
|
|
|
|
setupWasm('mclshe.wasm', null, function(mod, ns) { |
|
|
|
|
|
|
|
define_exported_she(mod) |
|
|
|
|
|
|
|
define_she_extra_functions(mod) |
|
|
|
|
|
|
|
sheInit() |
|
|
|
|
|
|
|
console.log('initializing sheSetRangeForDLP') |
|
|
|
|
|
|
|
let r = sheSetRangeForDLP(256, 2048) |
|
|
|
|
|
|
|
console.log('finished ' + r) |
|
|
|
|
|
|
|
if (callback) callback() |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.SecretKey = function() { |
|
|
|
|
|
|
|
this.a_ = new Uint32Array(SHE_SECRETKEY_SIZE / 4) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.SecretKey.prototype.serialize = function() { |
|
|
|
|
|
|
|
return she.callGetter(sheSecretKeySerialize, this.a_) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.SecretKey.prototype.deserialize = function(s) { |
|
|
|
|
|
|
|
return she.callSetter(sheSecretKeyDeserialize, this.a_, s) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.PublicKey = function() { |
|
|
|
|
|
|
|
this.a_ = new Uint32Array(SHE_PUBLICKEY_SIZE / 4) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.PublicKey.prototype.serialize = function() { |
|
|
|
|
|
|
|
return she.callGetter(shePublicKeySerialize, this.a_) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.PublicKey.prototype.deserialize = function(s) { |
|
|
|
|
|
|
|
return she.callSetter(shePublicKeyDeserialize, this.a_, s) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.CipherTextG1 = function() { |
|
|
|
|
|
|
|
this.a_ = new Uint32Array(SHE_CIPHERTEXT_G1_SIZE / 4) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.CipherTextG1.prototype.serialize = function() { |
|
|
|
|
|
|
|
return she.callGetter(sheCipherTextG1Serialize, this.a_) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.CipherTextG1.prototype.deserialize = function(s) { |
|
|
|
|
|
|
|
return she.callSetter(sheCipherTextG1Deserialize, this.a_, s) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.SecretKey.prototype.setByCSPRNG = function() { |
|
|
|
|
|
|
|
she.callSetByCSPRNG(this.a_) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.SecretKey.prototype.getPublicKey = function() { |
|
|
|
|
|
|
|
let pub = new she.PublicKey() |
|
|
|
|
|
|
|
she.callGetPublicKey(pub.a_, this.a_) |
|
|
|
|
|
|
|
return pub |
|
|
|
|
|
|
|
/* |
|
|
|
|
|
|
|
let pub = new she.PublicKey() |
|
|
|
|
|
|
|
let stack = mod.Runtime.stackSave() |
|
|
|
|
|
|
|
let secPos = mod.Runtime.stackAlloc(this.a_.length * 4) |
|
|
|
|
|
|
|
let pubPos = mod.Runtime.stackAlloc(pub.a_.length * 4) |
|
|
|
|
|
|
|
copyFromUint32Array(secPos, this.a_) |
|
|
|
|
|
|
|
sheGetPublicKey(pubPos, secPos) |
|
|
|
|
|
|
|
copyToUint32Array(pub.a_, pubPos) |
|
|
|
|
|
|
|
mod.Runtime.stackRestore(stack) |
|
|
|
|
|
|
|
return pub |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.PublicKey.prototype.enc = function(m) { |
|
|
|
|
|
|
|
return she.callEnc(sheEnc32G1, she.CipherTextG1, this.a_, m) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
she.SecretKey.prototype.dec = function(c) { |
|
|
|
|
|
|
|
if (she.CipherTextG1.prototype.isPrototypeOf(c)) { |
|
|
|
|
|
|
|
return she.callDec(sheDecG1, this.a_, c) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
throw('she.SecretKey.dec is not supported') |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return she |
|
|
|
|
|
|
|
}) |
|
|
|