parent
e2336179e9
commit
b42b8ddeec
@ -0,0 +1,40 @@ |
||||
function define_exported_she(mod) { |
||||
_sheInit = mod.cwrap('sheInit', 'number', ['number', 'number', ]) |
||||
_sheSecretKeySerialize = mod.cwrap('sheSecretKeySerialize', 'number', ['number', 'number', 'number', ]) |
||||
_shePublicKeySerialize = mod.cwrap('shePublicKeySerialize', 'number', ['number', 'number', 'number', ]) |
||||
_sheCipherTextG1Serialize = mod.cwrap('sheCipherTextG1Serialize', 'number', ['number', 'number', 'number', ]) |
||||
_sheCipherTextG2Serialize = mod.cwrap('sheCipherTextG2Serialize', 'number', ['number', 'number', 'number', ]) |
||||
_sheCipherTextGTSerialize = mod.cwrap('sheCipherTextGTSerialize', 'number', ['number', 'number', 'number', ]) |
||||
_sheSecretKeyDeserialize = mod.cwrap('sheSecretKeyDeserialize', 'number', ['number', 'number', 'number', ]) |
||||
_shePublicKeyDeserialize = mod.cwrap('shePublicKeyDeserialize', 'number', ['number', 'number', 'number', ]) |
||||
_sheCipherTextG1Deserialize = mod.cwrap('sheCipherTextG1Deserialize', 'number', ['number', 'number', 'number', ]) |
||||
_sheCipherTextG2Deserialize = mod.cwrap('sheCipherTextG2Deserialize', 'number', ['number', 'number', 'number', ]) |
||||
_sheCipherTextGTDeserialize = mod.cwrap('sheCipherTextGTDeserialize', 'number', ['number', 'number', 'number', ]) |
||||
sheSecretKeySetByCSPRNG = mod.cwrap('sheSecretKeySetByCSPRNG', 'number', ['number', ]) |
||||
sheGetPublicKey = mod.cwrap('sheGetPublicKey', 'null', ['number', 'number', ]) |
||||
sheSetRangeForDLP = mod.cwrap('sheSetRangeForDLP', 'number', ['number', 'number', ]) |
||||
sheSetRangeForG1DLP = mod.cwrap('sheSetRangeForG1DLP', 'number', ['number', 'number', ]) |
||||
sheSetRangeForG2DLP = mod.cwrap('sheSetRangeForG2DLP', 'number', ['number', 'number', ]) |
||||
sheSetRangeForGTDLP = mod.cwrap('sheSetRangeForGTDLP', 'number', ['number', 'number', ]) |
||||
sheEncG1 = mod.cwrap('sheEncG1', 'number', ['number', 'number', 'number', ]) |
||||
sheEncG2 = mod.cwrap('sheEncG2', 'number', ['number', 'number', 'number', ]) |
||||
sheEncGT = mod.cwrap('sheEncGT', 'number', ['number', 'number', 'number', ]) |
||||
sheEnc32G1 = mod.cwrap('sheEnc32G1', 'number', ['number', 'number', 'number', ]) |
||||
sheEnc32G2 = mod.cwrap('sheEnc32G2', 'number', ['number', 'number', 'number', ]) |
||||
sheEnc32GT = mod.cwrap('sheEnc32GT', 'number', ['number', 'number', 'number', ]) |
||||
_sheDecG1 = mod.cwrap('sheDecG1', 'number', ['number', 'number', 'number', ]) |
||||
_sheDecGT = mod.cwrap('sheDecGT', 'number', ['number', 'number', 'number', ]) |
||||
sheAddG1 = mod.cwrap('sheAddG1', 'number', ['number', 'number', 'number', ]) |
||||
sheAddG2 = mod.cwrap('sheAddG2', 'number', ['number', 'number', 'number', ]) |
||||
sheAddGT = mod.cwrap('sheAddGT', 'number', ['number', 'number', 'number', ]) |
||||
sheSubG1 = mod.cwrap('sheSubG1', 'number', ['number', 'number', 'number', ]) |
||||
sheSubG2 = mod.cwrap('sheSubG2', 'number', ['number', 'number', 'number', ]) |
||||
sheSubGT = mod.cwrap('sheSubGT', 'number', ['number', 'number', 'number', ]) |
||||
sheMulG1 = mod.cwrap('sheMulG1', 'number', ['number', 'number', 'number', ]) |
||||
sheMulG2 = mod.cwrap('sheMulG2', 'number', ['number', 'number', 'number', ]) |
||||
sheMulGT = mod.cwrap('sheMulGT', 'number', ['number', 'number', 'number', ]) |
||||
sheMul = mod.cwrap('sheMul', 'number', ['number', 'number', 'number', ]) |
||||
sheReRandG1 = mod.cwrap('sheReRandG1', 'number', ['number', 'number', ]) |
||||
sheReRandG2 = mod.cwrap('sheReRandG2', 'number', ['number', 'number', ]) |
||||
sheReRandGT = mod.cwrap('sheReRandGT', 'number', ['number', 'number', ]) |
||||
} |
File diff suppressed because one or more lines are too long
Binary file not shown.
@ -0,0 +1,114 @@ |
||||
function getValue(name) { return document.getElementsByName(name)[0].value } |
||||
function setValue(name, val) { document.getElementsByName(name)[0].value = val } |
||||
function getText(name) { return document.getElementsByName(name)[0].innerText } |
||||
function setText(name, val) { document.getElementsByName(name)[0].innerText = val } |
||||
|
||||
let moduleInited = false |
||||
|
||||
let module = setupWasm('mclshe.wasm', null, function(mod, ns) { |
||||
define_exported_she(mod) |
||||
define_she_extra_functions(mod) |
||||
moduleInited = true |
||||
sheInit() |
||||
console.log('initializing sheSetRangeForDLP') |
||||
let r = sheSetRangeForDLP(256, 2048) |
||||
console.log('finished') |
||||
setText('status', r ? 'err:' + r : 'ok') |
||||
}) |
||||
|
||||
function putSecretKey(x, msg = "") { |
||||
console.log(msg + ' sk=' + Uint8ArrayToHexString(sheSecretKeySerialize(x))) |
||||
} |
||||
function putPublicKey(x, msg = "") { |
||||
console.log(msg + ' pk=' + Uint8ArrayToHexString(shePublicKeySerialize(x))) |
||||
} |
||||
function putCipherTextG1(x, msg = "") { |
||||
console.log(msg + ' ctG1=' + Uint8ArrayToHexString(sheCipherTextG1Serialize(x))) |
||||
} |
||||
function putCipherTextG2(x, msg = "") { |
||||
console.log(msg + ' ctG2=' + Uint8ArrayToHexString(sheCipherTextG2Serialize(x))) |
||||
} |
||||
function putCipherTextGT(x, msg = "") { |
||||
console.log(msg + ' ctGT=' + Uint8ArrayToHexString(sheCipherTextGTSerialize(x))) |
||||
} |
||||
|
||||
function bench(label, count, func) { |
||||
let start = Date.now() |
||||
for (let i = 0; i < count; i++) { |
||||
func() |
||||
} |
||||
let end = Date.now() |
||||
let t = (end - start) / count |
||||
setText(label, t) |
||||
} |
||||
|
||||
function benchPairing() { |
||||
// bench('time_pairing', 50, () => mclBn_pairing(e, P, Q))
|
||||
} |
||||
|
||||
function onClickBenchmark() { |
||||
} |
||||
|
||||
function onClickTestSHE() { |
||||
try { |
||||
let sec = sheSecretKey_malloc() |
||||
let pub = shePublicKey_malloc() |
||||
let c11 = sheCipherTextG1_malloc() |
||||
let c12 = sheCipherTextG1_malloc() |
||||
let c21 = sheCipherTextG2_malloc() |
||||
let c22 = sheCipherTextG2_malloc() |
||||
let ct = sheCipherTextGT_malloc() |
||||
|
||||
sheSecretKeySetByCSPRNG(sec) |
||||
setText('secretKey', Uint8ArrayToHexString(sheSecretKeySerialize(sec))) |
||||
sheGetPublicKey(pub, sec) |
||||
setText('publicKey', Uint8ArrayToHexString(shePublicKeySerialize(pub))) |
||||
putPublicKey(pub) |
||||
|
||||
let m1 = getValue('msg1') |
||||
let m2 = getValue('msg2') |
||||
let m3 = getValue('msg3') |
||||
let m4 = getValue('msg4') |
||||
sheEnc32G1(c11, pub, m1) |
||||
sheEnc32G1(c12, pub, m2) |
||||
sheEnc32G2(c21, pub, m3) |
||||
sheEnc32G2(c22, pub, m4) |
||||
setText('encG11', Uint8ArrayToHexString(sheCipherTextG1Serialize(c11))) |
||||
setText('encG12', Uint8ArrayToHexString(sheCipherTextG1Serialize(c12))) |
||||
setText('encG21', Uint8ArrayToHexString(sheCipherTextG2Serialize(c21))) |
||||
setText('encG22', Uint8ArrayToHexString(sheCipherTextG2Serialize(c22))) |
||||
sheAddG1(c11, c11, c12) |
||||
sheAddG2(c21, c21, c22) |
||||
sheMul(ct, c11, c21) |
||||
setText('encGT', Uint8ArrayToHexString(sheCipherTextGTSerialize(ct))) |
||||
let d = sheDecGT(sec, ct) |
||||
setText('decMsg', d) |
||||
|
||||
she_free(ct) |
||||
she_free(c22) |
||||
she_free(c21) |
||||
she_free(c12) |
||||
she_free(c11) |
||||
she_free(pub) |
||||
she_free(sec) |
||||
} catch (e) { |
||||
console.log('exception ' + e) |
||||
} |
||||
} |
||||
|
||||
function Uint8ArrayToHexString(a) { |
||||
let s = '' |
||||
for (let i = 0; i < a.length; i++) { |
||||
s += ('0' + a[i].toString(16)).slice(-2) |
||||
} |
||||
return s |
||||
} |
||||
|
||||
function HexStringToUint8Array(s) { |
||||
let a = new Uint8Array(s.length / 2) |
||||
for (let i = 0; i < s.length / 2; i++) { |
||||
a[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16) |
||||
} |
||||
return a |
||||
} |
||||
|
@ -0,0 +1,33 @@ |
||||
<!DOCTYPE html> |
||||
<html lang="ja"> |
||||
<head> |
||||
<meta charset="UTF-8"> |
||||
<title>SHE with 1multi sample</title> |
||||
<script type='text/javascript' src="./exported-she.js"></script> |
||||
<script type='text/javascript' src="./mclshe.js"></script> |
||||
<script type='text/javascript' src="./she.js"></script> |
||||
<script type='text/javascript' src="./she-demo.js"></script> |
||||
</script> |
||||
</head> |
||||
<body> |
||||
<p> |
||||
library status <span name="status">initializing...</span> |
||||
</p> |
||||
<button type="text" id="testSHE" onclick="onClickTestSHE()">test SHE</button> |
||||
<div> |
||||
secretKey : <span name="secretKey"></span><br> |
||||
publicKey : <span name="publicKey"></span><br> |
||||
msg1 : <input type="text" name="msg1" value="1"><br> |
||||
msg2 : <input type="text" name="msg2" value="2"><br> |
||||
msg3 : <input type="text" name="msg3" value="3"><br> |
||||
msg4 : <input type="text" name="msg4" value="4"><br> |
||||
encG1(msg1) : <span name="encG11"></span><br> |
||||
encG1(msg2) : <span name="encG12"></span><br> |
||||
encG2(msg3) : <span name="encG21"></span><br> |
||||
encG2(msg4) : <span name="encG22"></span><br> |
||||
mul((c11 + c12) * (c21 + c22)) : <span name="encGT"></span><br> |
||||
dec : <span name="decMsg"></span><br> |
||||
</div> |
||||
<hr> |
||||
</body> |
||||
</html> |
@ -0,0 +1,186 @@ |
||||
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 |
||||
} |
||||
|
||||
const MCLBN_CURVE_FP254BNB = 0 |
||||
const MCLBN_FP_UNIT_SIZE = 4 |
||||
|
||||
function define_she_extra_functions(mod) { |
||||
wrap_outputString = function(func, doesReturnString = true) { |
||||
return function(x, ioMode = 0) { |
||||
let maxBufSize = 2048 |
||||
let stack = mod.Runtime.stackSave() |
||||
let pos = mod.Runtime.stackAlloc(maxBufSize) |
||||
let n = func(pos, maxBufSize, x, ioMode) |
||||
if (n < 0) { |
||||
throw('err gen_str:' + x) |
||||
} |
||||
if (doesReturnString) { |
||||
let s = '' |
||||
for (let i = 0; i < n; i++) { |
||||
s += String.fromCharCode(mod.HEAP8[pos + i]) |
||||
} |
||||
mod.Runtime.stackRestore(stack) |
||||
return s |
||||
} else { |
||||
let a = new Uint8Array(n) |
||||
for (let i = 0; i < n; i++) { |
||||
a[i] = mod.HEAP8[pos + i] |
||||
} |
||||
mod.Runtime.stackRestore(stack) |
||||
return a |
||||
} |
||||
} |
||||
} |
||||
wrap_outputArray = function(func) { |
||||
return wrap_outputString(func, false) |
||||
} |
||||
wrap_input0 = function(func, returnValue = false) { |
||||
return function(buf, ioMode = 0) { |
||||
let stack = mod.Runtime.stackSave() |
||||
let pos = mod.Runtime.stackAlloc(buf.length) |
||||
if (typeof(buf) == "string") { |
||||
for (let i = 0; i < buf.length; i++) { |
||||
mod.HEAP8[pos + i] = buf.charCodeAt(i) |
||||
} |
||||
} else { |
||||
for (let i = 0; i < buf.length; i++) { |
||||
mod.HEAP8[pos + i] = buf[i] |
||||
} |
||||
} |
||||
let r = func(pos, buf.length, ioMode) |
||||
mod.Runtime.stackRestore(stack) |
||||
if (returnValue) return r |
||||
if (r) throw('err wrap_input0 ' + buf) |
||||
} |
||||
} |
||||
wrap_input1 = function(func, returnValue = false) { |
||||
return function(x1, buf, ioMode = 0) { |
||||
let stack = mod.Runtime.stackSave() |
||||
let pos = mod.Runtime.stackAlloc(buf.length) |
||||
if (typeof(buf) == "string") { |
||||
for (let i = 0; i < buf.length; i++) { |
||||
mod.HEAP8[pos + i] = buf.charCodeAt(i) |
||||
} |
||||
} else { |
||||
for (let i = 0; i < buf.length; i++) { |
||||
mod.HEAP8[pos + i] = buf[i] |
||||
} |
||||
} |
||||
let r = func(x1, pos, buf.length, ioMode) |
||||
mod.Runtime.stackRestore(stack) |
||||
if (returnValue) return r |
||||
if (r) throw('err wrap_input1 ' + buf) |
||||
} |
||||
} |
||||
wrap_input2 = function(func, returnValue = false) { |
||||
return function(x1, x2, buf, ioMode = 0) { |
||||
let stack = mod.Runtime.stackSave() |
||||
let pos = mod.Runtime.stackAlloc(buf.length) |
||||
if (typeof(buf) == "string") { |
||||
for (let i = 0; i < buf.length; i++) { |
||||
mod.HEAP8[pos + i] = buf.charCodeAt(i) |
||||
} |
||||
} else { |
||||
for (let i = 0; i < buf.length; i++) { |
||||
mod.HEAP8[pos + i] = buf[i] |
||||
} |
||||
} |
||||
let r = func(x1, x2, pos, buf.length, ioMode) |
||||
mod.Runtime.stackRestore(stack) |
||||
if (returnValue) return r |
||||
if (r) throw('err wrap_input2 ' + buf) |
||||
} |
||||
} |
||||
wrap_keyShare = function(func, dataSize) { |
||||
return function(x, vec, id) { |
||||
let k = vec.length |
||||
let p = mod._malloc(dataSize * k) |
||||
for (let i = 0; i < k; i++) { |
||||
mod._memcpy(p + i * dataSize, vec[i], dataSize) |
||||
} |
||||
let r = func(x, p, k, id) |
||||
mod._free(p) |
||||
if (r) throw('keyShare ' + k) |
||||
} |
||||
} |
||||
wrap_dec = function(func) { |
||||
return function(sec, c) { |
||||
let stack = mod.Runtime.stackSave() |
||||
let pos = mod.Runtime.stackAlloc(8) |
||||
let r = func(pos, sec, c) |
||||
mod.Runtime.stackRestore(stack) |
||||
if (r != 0) throw('sheDec') |
||||
let v = mod.HEAP32[pos / 4] |
||||
return v |
||||
} |
||||
} |
||||
///////////////////////////////////////////////////////////////
|
||||
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 |
||||
|
||||
const SECRETKEY_SIZE = FR_SIZE * 2 |
||||
const PUBLICKEY_SIZE = G1_SIZE + G2_SIZE |
||||
const CIPHERTEXT_G1_SIZE = G1_SIZE * 2 |
||||
const CIPHERTEXT_G2_SIZE = G2_SIZE * 2 |
||||
const CIPHERTEXT_GT_SIZE = GT_SIZE * 4 |
||||
|
||||
she_free = function(p) { |
||||
mod._free(p) |
||||
} |
||||
///////////////////////////////////////////////////////////////
|
||||
sheSecretKey_malloc = function() { |
||||
return mod._malloc(SECRETKEY_SIZE) |
||||
} |
||||
sheSecretKeySerialize = wrap_outputArray(_sheSecretKeySerialize) |
||||
sheSecretKeyDeserialize = wrap_input1(_sheSecretKeyDeserialize) |
||||
///////////////////////////////////////////////////////////////
|
||||
shePublicKey_malloc = function() { |
||||
return mod._malloc(PUBLICKEY_SIZE) |
||||
} |
||||
shePublicKeySerialize = wrap_outputArray(_shePublicKeySerialize) |
||||
shePublicKeyDeserialize = wrap_input1(_shePublicKeyDeserialize) |
||||
///////////////////////////////////////////////////////////////
|
||||
sheCipherTextG1_malloc = function() { |
||||
return mod._malloc(CIPHERTEXT_G1_SIZE) |
||||
} |
||||
sheCipherTextG1Serialize = wrap_outputArray(_sheCipherTextG1Serialize) |
||||
sheCipherTextG1Deserialize = wrap_input1(_sheCipherTextG1Deserialize) |
||||
sheDecG1 = wrap_dec(_sheDecG1) |
||||
///////////////////////////////////////////////////////////////
|
||||
sheCipherTextG2_malloc = function() { |
||||
return mod._malloc(CIPHERTEXT_G2_SIZE) |
||||
} |
||||
sheCipherTextG2Serialize = wrap_outputArray(_sheCipherTextG2Serialize) |
||||
sheCipherTextG2Deserialize = wrap_input1(_sheCipherTextG2Deserialize) |
||||
///////////////////////////////////////////////////////////////
|
||||
sheCipherTextGT_malloc = function() { |
||||
return mod._malloc(CIPHERTEXT_GT_SIZE) |
||||
} |
||||
sheCipherTextGTSerialize = wrap_outputArray(_sheCipherTextGTSerialize) |
||||
sheCipherTextGTDeserialize = wrap_input1(_sheCipherTextGTDeserialize) |
||||
sheDecGT = wrap_dec(_sheDecGT) |
||||
|
||||
sheInit = function(curveType = MCLBN_CURVE_FP254BNB) { |
||||
let r = _sheInit(curveType, MCLBN_FP_UNIT_SIZE) |
||||
console.log('sheInit ' + r) |
||||
if (r) throw('sheInit') |
||||
// r = sheSetRangeForGTDLP(128, 1024)
|
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue