parent
60df863498
commit
d331a6488e
@ -1,147 +0,0 @@ |
||||
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 } |
||||
|
||||
she.init() |
||||
.then(() => { |
||||
setText('status', 'ok') |
||||
}) |
||||
|
||||
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 = new she.SecretKey() |
||||
sec.setByCSPRNG() |
||||
setText('secretKey', sec.toHexStr()) |
||||
let pub = sec.getPublicKey() |
||||
setText('publicKey', pub.toHexStr()) |
||||
|
||||
let m1 = getValue('msg1') |
||||
let m2 = getValue('msg2') |
||||
let m3 = getValue('msg3') |
||||
let m4 = getValue('msg4') |
||||
let c11 = pub.encG1(m1) |
||||
console.log('dec c11=' + sec.dec(c11)) |
||||
let c12 = pub.encG1(m2) |
||||
console.log('dec c12=' + sec.dec(c12)) |
||||
let c21 = pub.encG2(m3) |
||||
console.log('dec c21=' + sec.dec(c21)) |
||||
let c22 = pub.encG2(m4) |
||||
console.log('dec c22=' + sec.dec(c22)) |
||||
setText('encG11', c11.toHexStr()) |
||||
setText('encG12', c12.toHexStr()) |
||||
setText('encG21', c21.toHexStr()) |
||||
setText('encG22', c22.toHexStr()) |
||||
c11 = she.add(c11, c12) |
||||
c21 = she.add(c21, c22) |
||||
let ct = she.mul(c11, c21) |
||||
setText('encGT', ct.toHexStr()) |
||||
let d = sec.dec(ct) |
||||
setText('decMsg', d) |
||||
} 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 |
||||
} |
||||
|
||||
function reRandTest(sec, pub, c) { |
||||
console.log('before ' + sec.dec(c)) |
||||
c.dump() |
||||
pub.reRand(c) |
||||
console.log('after ' + sec.dec(c)) |
||||
c.dump() |
||||
} |
||||
function onClickTestSHEclass() { |
||||
try { |
||||
let sec = new she.SecretKey() |
||||
sec.setByCSPRNG() |
||||
sec.dump() |
||||
setText('sec2', sec.toHexStr()) |
||||
let pub = sec.getPublicKey() |
||||
setText('pub2', pub.toHexStr()) |
||||
let m1 = 15 |
||||
let m2 = 17 |
||||
setText('m2', m1) |
||||
let c11 = pub.encG1(m1) |
||||
console.log('dec c11=' + sec.dec(c11)) |
||||
let c21 = pub.encG2(m1) |
||||
let ct1 = pub.encGT(m1) |
||||
setText('c2', c11.toHexStr()) |
||||
let d = sec.dec(c11) |
||||
setText('d2', d) |
||||
console.log('dec c21=' + sec.dec(c21)) |
||||
console.log('dec ct1=' + sec.dec(ct1)) |
||||
let c12 = pub.encG1(m2) |
||||
let c22 = pub.encG2(m2) |
||||
let ct2 = pub.encGT(m2) |
||||
c11 = she.add(c11, c12) |
||||
c21 = she.add(c21, c22) |
||||
ct1 = she.add(ct1, ct2) |
||||
console.log('expect ' + (m1 + m2)) |
||||
console.log('dec c11=' + sec.dec(c11)) |
||||
console.log('dec c21=' + sec.dec(c21)) |
||||
console.log('dec ct1=' + sec.dec(ct1)) |
||||
|
||||
c12 = she.sub(c12, c11) |
||||
c22 = she.sub(c22, c21) |
||||
ct2 = she.sub(ct2, ct1) |
||||
console.log('expect ' + (-m1)) |
||||
console.log('dec c11=' + sec.dec(c12)) |
||||
console.log('dec c21=' + sec.dec(c22)) |
||||
console.log('dec ct1=' + sec.dec(ct2)) |
||||
c12 = she.mulInt(c12, -3) |
||||
c22 = she.mulInt(c22, -3) |
||||
ct2 = she.mulInt(ct2, -3) |
||||
let expect = m1 * 3 |
||||
console.log('dec c11=' + (sec.dec(c12) == expect)) |
||||
console.log('dec c21=' + (sec.dec(c22) == expect)) |
||||
console.log('dec ct1=' + (sec.dec(ct2) == expect)) |
||||
c12 = pub.encG1(4) |
||||
c22 = pub.encG2(5) |
||||
ct2 = she.mul(c12, c22) |
||||
console.log('dec ct2=' + sec.dec(ct2)) |
||||
ct2 = pub.convertToCipherTextGT(c12) |
||||
console.log('convertToCipherTextGT(G1)=' + (sec.dec(ct2) == 4)) |
||||
ct2 = pub.convertToCipherTextGT(c22) |
||||
console.log('convertToCipherTextGT(G2)=' + (sec.dec(ct2) == 5)) |
||||
|
||||
reRandTest(sec, pub, c12) |
||||
reRandTest(sec, pub, c22) |
||||
reRandTest(sec, pub, ct2) |
||||
console.log('ok') |
||||
} catch (e) { |
||||
console.log('err ' + e) |
||||
} |
||||
} |
@ -1,105 +0,0 @@ |
||||
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 sec = null |
||||
let pub = null |
||||
|
||||
she.init() |
||||
.then(() => { |
||||
setText('status', 'ok') |
||||
sec = new she.SecretKey() |
||||
sec.setByCSPRNG() |
||||
sec.dump('sec=') |
||||
pub = sec.getPublicKey() |
||||
pub.dump('pub=') |
||||
}) |
||||
|
||||
function append() { |
||||
let v = getValue('append') |
||||
let vs = v.split(',') |
||||
let x = parseInt(vs[0]) |
||||
let y = parseInt(vs[1]) |
||||
console.log('x = ' + x + ', y = ' + y) |
||||
let c1 = pub.encG1(x) |
||||
let c2 = pub.encG2(y) |
||||
$('#client_table').append( |
||||
$('<tr>').append( |
||||
$('<td>').text(x) |
||||
).append( |
||||
$('<td>').text(y) |
||||
).append( |
||||
$('<td class="encG1x">').text(c1.toHexStr()) |
||||
).append( |
||||
$('<td class="encG2y">').text(c2.toHexStr()) |
||||
) |
||||
) |
||||
} |
||||
|
||||
function send() { |
||||
let ct1 = [] |
||||
$('.encG1x').each(function() { |
||||
ct1.push($(this).text()) |
||||
}) |
||||
let ct2 = [] |
||||
$('.encG2y').each(function() { |
||||
ct2.push($(this).text()) |
||||
}) |
||||
let obj = $('#server_table') |
||||
obj.html('') |
||||
{ |
||||
let header = [ |
||||
'EncG1(x)', 'EncG2(y)', 'EncGT(x * y)' |
||||
] |
||||
let t = $('<tr>').attr('id', 'header') |
||||
for (let i = 0; i < header.length; i++) { |
||||
t.append( |
||||
$('<th>').append(header[i]) |
||||
) |
||||
} |
||||
obj.append(t) |
||||
} |
||||
for (let i = 0; i < ct1.length; i++) { |
||||
let t = $('<tr>') |
||||
t.append( |
||||
$('<td class="encG1xS">').append(ct1[i]) |
||||
).append( |
||||
$('<td class="encG2yS">').append(ct2[i]) |
||||
).append( |
||||
$('<td class="encGTxyS">').append('') |
||||
) |
||||
obj.append(t) |
||||
} |
||||
} |
||||
|
||||
function mul() { |
||||
$('.encG1xS').each(function() { |
||||
let o = $(this) |
||||
let c1 = she.getCipherTextG1FromHexStr(o.text()) |
||||
let c2 = she.getCipherTextG2FromHexStr(o.next().text()) |
||||
let ct = she.mul(c1, c2) |
||||
o.next().next().text(ct.toHexStr()) |
||||
}) |
||||
} |
||||
|
||||
function sum() { |
||||
let csum = pub.encGT(0) |
||||
$('.encGTxyS').each(function() { |
||||
let s = $(this).text() |
||||
let ct = she.getCipherTextGTFromHexStr(s) |
||||
csum = she.add(csum, ct) |
||||
}) |
||||
setText('encSumS', csum.toHexStr()) |
||||
} |
||||
|
||||
function recv() { |
||||
setText('encSumC', getText('encSumS')) |
||||
} |
||||
|
||||
function dec() { |
||||
let s = getText('encSumC') |
||||
let ct = she.getCipherTextGTFromHexStr(s) |
||||
let v = sec.dec(ct) |
||||
setText('ret', v) |
||||
} |
@ -1,41 +0,0 @@ |
||||
<!DOCTYPE html> |
||||
<html lang="ja"> |
||||
<head> |
||||
<meta charset="UTF-8"> |
||||
<title>SHE with 1multi sample</title> |
||||
<link rel="stylesheet" type="text/css"> |
||||
<script src="./she_c.js"></script> |
||||
<script src="./she.js"></script> |
||||
<script 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> |
||||
enc((msg1 + msg2) * (msg3 + msg4)) : <span name="encGT"></span><br> |
||||
dec : <span name="decMsg"></span><br> |
||||
</div> |
||||
<hr> |
||||
<button type="text" id="testSHE" onclick="onClickTestSHEclass()">test SHE class</button> |
||||
<div> |
||||
sec2 : <span name="sec2"></span><br> |
||||
pub2 : <span name="pub2"></span><br> |
||||
m2 : <span name="m2"></span><br> |
||||
c2 : <span name="c2"></span><br> |
||||
d2 : <span name="d2"></span><br> |
||||
</div> |
||||
</body> |
||||
</html> |
@ -1,534 +0,0 @@ |
||||
(function(generator) { |
||||
"use strict" |
||||
if (typeof exports === 'object') { |
||||
exports.mod = require('./she_c.js') |
||||
generator(exports, true) |
||||
} else { |
||||
const exports = {} |
||||
exports.mod = {} |
||||
window.she = generator(exports, false) |
||||
} |
||||
})(function(exports, isNodeJs) { |
||||
|
||||
const MCLBN_CURVE_FP254BNB = 0 |
||||
const MCLBN_CURVE_FP382_1 = 1 |
||||
const MCLBN_CURVE_FP382_2 = 2 |
||||
|
||||
const MCLBN_FP_UNIT_SIZE = 4 |
||||
|
||||
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 |
||||
|
||||
const SHE_SECRETKEY_SIZE = MCLBN_FP_SIZE * 2 |
||||
const SHE_PUBLICKEY_SIZE = MCLBN_G1_SIZE + MCLBN_G2_SIZE |
||||
const SHE_CIPHERTEXT_G1_SIZE = MCLBN_G1_SIZE * 2 |
||||
const SHE_CIPHERTEXT_G2_SIZE = MCLBN_G2_SIZE * 2 |
||||
const SHE_CIPHERTEXT_GT_SIZE = MCLBN_GT_SIZE * 4 |
||||
|
||||
const mod = exports.mod |
||||
|
||||
/* |
||||
init she |
||||
@param range [in] table size of DLP ; require 8 * table size |
||||
@param tryNum [in] how many search ; O(tryNum) time |
||||
can decrypt (range * tryNum) range value |
||||
*/ |
||||
exports.init = (range = 1024, tryNum = 1024) => { |
||||
if (!isNodeJs) { |
||||
fetch('she_c.wasm') |
||||
.then(response => response.arrayBuffer()) |
||||
.then(buffer => new Uint8Array(buffer)) |
||||
.then(binary => { Module(mod) }) |
||||
} |
||||
return new Promise((resolve) => { |
||||
mod.onRuntimeInitialized = () => { |
||||
define_extra_functions(mod) |
||||
let r = mod._sheInit(MCLBN_CURVE_FP254BNB, MCLBN_FP_UNIT_SIZE) |
||||
if (r) throw('sheInit err ' + r) |
||||
console.log(`initializing sheSetRangeForDLP(range=${range}, tryNum=${tryNum})`) |
||||
r = mod._sheSetRangeForDLP(range, tryNum) |
||||
if (r) throw('_sheSetRangeForDLP err ' + r) |
||||
console.log('finished') |
||||
resolve() |
||||
} |
||||
}) |
||||
} |
||||
|
||||
const ptrToStr = function(pos, n) { |
||||
let s = '' |
||||
for (let i = 0; i < n; i++) { |
||||
s += String.fromCharCode(mod.HEAP8[pos + i]) |
||||
} |
||||
return s |
||||
} |
||||
const Uint8ArrayToMem = function(pos, buf) { |
||||
for (let i = 0; i < buf.length; i++) { |
||||
mod.HEAP8[pos + i] = buf[i] |
||||
} |
||||
} |
||||
const AsciiStrToMem = function(pos, s) { |
||||
for (let i = 0; i < s.length; i++) { |
||||
mod.HEAP8[pos + i] = s.charCodeAt(i) |
||||
} |
||||
} |
||||
const copyToUint32Array = function(a, pos) { |
||||
a.set(mod.HEAP32.subarray(pos / 4, pos / 4 + a.length)) |
||||
// 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] |
||||
} |
||||
} |
||||
exports.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
|
||||
exports.toHexStr = function(a) { |
||||
return exports.toHex(a, 0, a.length) |
||||
} |
||||
// hex string to Uint8Array
|
||||
exports.fromHexStr = function(s) { |
||||
if (s.length & 1) throw('fromHexStr:length must be even ' + s.length) |
||||
const n = s.length / 2 |
||||
const 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) { |
||||
const maxBufSize = 2048 |
||||
const stack = mod.Runtime.stackSave() |
||||
const pos = mod.Runtime.stackAlloc(maxBufSize) |
||||
const n = func(pos, maxBufSize, x, ioMode) |
||||
if (n < 0) { |
||||
throw('err gen_str:' + x) |
||||
} |
||||
if (doesReturnString) { |
||||
const s = ptrToStr(pos, n) |
||||
mod.Runtime.stackRestore(stack) |
||||
return s |
||||
} else { |
||||
const a = new Uint8Array(n) |
||||
for (let i = 0; i < n; i++) { |
||||
a[i] = mod.HEAP8[pos + i] |
||||
} |
||||
mod.Runtime.stackRestore(stack) |
||||
return a |
||||
} |
||||
} |
||||
} |
||||
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_input ' + 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 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 callEnc = function(func, cstr, pub, m) { |
||||
let c = new cstr() |
||||
let stack = mod.Runtime.stackSave() |
||||
let cPos = mod.Runtime.stackAlloc(c.a_.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 |
||||
} |
||||
// return func(x, y)
|
||||
const callAddSub = function(func, cstr, x, y) { |
||||
let z = new cstr() |
||||
let stack = mod.Runtime.stackSave() |
||||
let xPos = mod.Runtime.stackAlloc(x.length * 4) |
||||
let yPos = mod.Runtime.stackAlloc(y.length * 4) |
||||
let zPos = mod.Runtime.stackAlloc(z.a_.length * 4) |
||||
copyFromUint32Array(xPos, x); |
||||
copyFromUint32Array(yPos, y); |
||||
func(zPos, xPos, yPos) |
||||
copyToUint32Array(z.a_, zPos) |
||||
mod.Runtime.stackRestore(stack) |
||||
return z |
||||
} |
||||
// return func(x, y)
|
||||
const callMulInt = function(func, cstr, x, y) { |
||||
let z = new cstr() |
||||
let stack = mod.Runtime.stackSave() |
||||
let xPos = mod.Runtime.stackAlloc(x.length * 4) |
||||
let zPos = mod.Runtime.stackAlloc(z.a_.length * 4) |
||||
copyFromUint32Array(xPos, x); |
||||
func(zPos, xPos, y) |
||||
copyToUint32Array(z.a_, zPos) |
||||
mod.Runtime.stackRestore(stack) |
||||
return z |
||||
} |
||||
// return func(c)
|
||||
const 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 |
||||
} |
||||
const callIsZero = 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 |
||||
} |
||||
// reRand(c)
|
||||
const callReRand = function(func, c, pub) { |
||||
let stack = mod.Runtime.stackSave() |
||||
let cPos = mod.Runtime.stackAlloc(c.length * 4) |
||||
let pubPos = mod.Runtime.stackAlloc(pub.length * 4) |
||||
copyFromUint32Array(cPos, c); |
||||
copyFromUint32Array(pubPos, pub); |
||||
let r = func(cPos, pubPos) |
||||
copyToUint32Array(c, cPos) |
||||
mod.Runtime.stackRestore(stack) |
||||
if (r) throw('callReRand err') |
||||
} |
||||
// convert
|
||||
const callConvert = function(func, pub, c) { |
||||
let ct = new exports.CipherTextGT() |
||||
let stack = mod.Runtime.stackSave() |
||||
let ctPos = mod.Runtime.stackAlloc(ct.a_.length * 4) |
||||
let pubPos = mod.Runtime.stackAlloc(pub.length * 4) |
||||
let cPos = mod.Runtime.stackAlloc(c.length * 4) |
||||
copyFromUint32Array(pubPos, pub); |
||||
copyFromUint32Array(cPos, c); |
||||
let r = func(ctPos, pubPos, cPos) |
||||
copyToUint32Array(ct.a_, ctPos) |
||||
mod.Runtime.stackRestore(stack) |
||||
if (r) throw('callConvert err') |
||||
return ct |
||||
} |
||||
const define_extra_functions = function(mod) { |
||||
mod.sheSecretKeySerialize = wrap_outputArray(mod._sheSecretKeySerialize) |
||||
mod.sheSecretKeyDeserialize = wrap_input(mod._sheSecretKeyDeserialize, 1) |
||||
mod.shePublicKeySerialize = wrap_outputArray(mod._shePublicKeySerialize) |
||||
mod.shePublicKeyDeserialize = wrap_input(mod._shePublicKeyDeserialize, 1) |
||||
mod.sheCipherTextG1Serialize = wrap_outputArray(mod._sheCipherTextG1Serialize) |
||||
mod.sheCipherTextG1Deserialize = wrap_input(mod._sheCipherTextG1Deserialize, 1) |
||||
mod.sheDecG1 = wrap_dec(mod._sheDecG1) |
||||
mod.sheCipherTextG2Serialize = wrap_outputArray(mod._sheCipherTextG2Serialize) |
||||
mod.sheCipherTextG2Deserialize = wrap_input(mod._sheCipherTextG2Deserialize, 1) |
||||
mod.sheDecG2 = wrap_dec(mod._sheDecG2) |
||||
mod.sheCipherTextGTSerialize = wrap_outputArray(mod._sheCipherTextGTSerialize) |
||||
mod.sheCipherTextGTDeserialize = wrap_input(mod._sheCipherTextGTDeserialize, 1) |
||||
mod.sheDecGT = wrap_dec(mod._sheDecGT) |
||||
|
||||
class Common { |
||||
constructor(size) { |
||||
this.a_ = new Uint32Array(size / 4) |
||||
} |
||||
fromHexStr(s) { |
||||
this.deserialize(exports.fromHexStr(s)) |
||||
} |
||||
toHexStr() { |
||||
return exports.toHexStr(this.serialize()) |
||||
} |
||||
dump(msg = '') { |
||||
console.log(msg + this.toHexStr()) |
||||
} |
||||
} |
||||
exports.SecretKey = class extends Common { |
||||
constructor() { |
||||
super(SHE_SECRETKEY_SIZE) |
||||
} |
||||
serialize() { |
||||
return callGetter(mod.sheSecretKeySerialize, this.a_) |
||||
} |
||||
deserialize(s) { |
||||
callSetter(mod.sheSecretKeyDeserialize, this.a_, s) |
||||
} |
||||
setByCSPRNG() { |
||||
let stack = mod.Runtime.stackSave() |
||||
let secPos = mod.Runtime.stackAlloc(this.a_.length * 4) |
||||
mod._sheSecretKeySetByCSPRNG(secPos) |
||||
copyToUint32Array(this.a_, secPos) |
||||
mod.Runtime.stackRestore(stack) |
||||
} |
||||
getPublicKey() { |
||||
let pub = new exports.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_) |
||||
mod._sheGetPublicKey(pubPos, secPos) |
||||
copyToUint32Array(pub.a_, pubPos) |
||||
mod.Runtime.stackRestore(stack) |
||||
return pub |
||||
} |
||||
dec(c) { |
||||
let dec = null |
||||
if (exports.CipherTextG1.prototype.isPrototypeOf(c)) { |
||||
dec = mod.sheDecG1 |
||||
} else if (exports.CipherTextG2.prototype.isPrototypeOf(c)) { |
||||
dec = mod.sheDecG2 |
||||
} else if (exports.CipherTextGT.prototype.isPrototypeOf(c)) { |
||||
dec = mod.sheDecGT |
||||
} else { |
||||
throw('exports.SecretKey.dec:not supported') |
||||
} |
||||
return callDec(dec, this.a_, c.a_) |
||||
} |
||||
isZero(c) { |
||||
let isZero = null |
||||
if (exports.CipherTextG1.prototype.isPrototypeOf(c)) { |
||||
isZero = mod._sheIsZeroG1 |
||||
} else if (exports.CipherTextG2.prototype.isPrototypeOf(c)) { |
||||
isZero = mod._sheIsZeroG2 |
||||
} else if (exports.CipherTextGT.prototype.isPrototypeOf(c)) { |
||||
isZero = mod._sheIsZeroGT |
||||
} else { |
||||
throw('exports.SecretKey.isZero:not supported') |
||||
} |
||||
return callIsZero(isZero, this.a_, c.a_) |
||||
} |
||||
} |
||||
|
||||
exports.getSecretKeyFromHexStr = function(s) { |
||||
const r = new exports.SecretKey() |
||||
r.fromHexStr(s) |
||||
return r |
||||
} |
||||
exports.PublicKey = class extends Common { |
||||
constructor() { |
||||
super(SHE_PUBLICKEY_SIZE) |
||||
} |
||||
serialize() { |
||||
return callGetter(mod.shePublicKeySerialize, this.a_) |
||||
} |
||||
deserialize(s) { |
||||
callSetter(mod.shePublicKeyDeserialize, this.a_, s) |
||||
} |
||||
encG1(m) { |
||||
return callEnc(mod._sheEnc32G1, exports.CipherTextG1, this.a_, m) |
||||
} |
||||
encG2(m) { |
||||
return callEnc(mod._sheEnc32G2, exports.CipherTextG2, this.a_, m) |
||||
} |
||||
encGT(m) { |
||||
return callEnc(mod._sheEnc32GT, exports.CipherTextGT, this.a_, m) |
||||
} |
||||
reRand(c) { |
||||
let reRand = null |
||||
if (exports.CipherTextG1.prototype.isPrototypeOf(c)) { |
||||
reRand = mod._sheReRandG1 |
||||
} else if (exports.CipherTextG2.prototype.isPrototypeOf(c)) { |
||||
reRand = mod._sheReRandG2 |
||||
} else if (exports.CipherTextGT.prototype.isPrototypeOf(c)) { |
||||
reRand = mod._sheReRandGT |
||||
} else { |
||||
throw('exports.PublicKey.reRand:not supported') |
||||
} |
||||
return callReRand(reRand, c.a_, this.a_) |
||||
} |
||||
// convert to CipherTextGT
|
||||
convert(c) { |
||||
let convert = null |
||||
if (exports.CipherTextG1.prototype.isPrototypeOf(c)) { |
||||
convert = mod._sheConvertG1 |
||||
} else if (exports.CipherTextG2.prototype.isPrototypeOf(c)) { |
||||
convert = mod._sheConvertG2 |
||||
} else { |
||||
throw('exports.PublicKey.convert:not supported') |
||||
} |
||||
return callConvert(convert, this.a_, c.a_) |
||||
} |
||||
} |
||||
|
||||
exports.getPublicKeyFromHexStr = function(s) { |
||||
const r = new exports.PublicKey() |
||||
r.fromHexStr(s) |
||||
return r |
||||
} |
||||
exports.CipherTextG1 = class extends Common { |
||||
constructor() { |
||||
super(SHE_CIPHERTEXT_G1_SIZE) |
||||
} |
||||
serialize() { |
||||
return callGetter(mod.sheCipherTextG1Serialize, this.a_) |
||||
} |
||||
deserialize(s) { |
||||
callSetter(mod.sheCipherTextG1Deserialize, this.a_, s) |
||||
} |
||||
} |
||||
|
||||
exports.getCipherTextG1FromHexStr = function(s) { |
||||
const r = new exports.CipherTextG1() |
||||
r.fromHexStr(s) |
||||
return r |
||||
} |
||||
exports.CipherTextG2 = class extends Common { |
||||
constructor() { |
||||
super(SHE_CIPHERTEXT_G2_SIZE) |
||||
} |
||||
serialize() { |
||||
return callGetter(mod.sheCipherTextG2Serialize, this.a_) |
||||
} |
||||
deserialize(s) { |
||||
callSetter(mod.sheCipherTextG2Deserialize, this.a_, s) |
||||
} |
||||
} |
||||
|
||||
exports.getCipherTextG2FromHexStr = function(s) { |
||||
const r = new exports.CipherTextG2() |
||||
r.fromHexStr(s) |
||||
return r |
||||
} |
||||
|
||||
exports.CipherTextGT = class extends Common { |
||||
constructor() { |
||||
super(SHE_CIPHERTEXT_GT_SIZE) |
||||
} |
||||
serialize() { |
||||
return callGetter(mod.sheCipherTextGTSerialize, this.a_) |
||||
} |
||||
deserialize(s) { |
||||
callSetter(mod.sheCipherTextGTDeserialize, this.a_, s) |
||||
} |
||||
} |
||||
|
||||
exports.getCipherTextGTFromHexStr = function(s) { |
||||
const r = new exports.CipherTextGT() |
||||
r.fromHexStr(s) |
||||
return r |
||||
} |
||||
// return x + y
|
||||
exports.add = function(x, y) { |
||||
if (x.a_.length != y.a_.length) throw('exports.add:bad type') |
||||
let add = null |
||||
let cstr = null |
||||
if (exports.CipherTextG1.prototype.isPrototypeOf(x)) { |
||||
add = mod._sheAddG1 |
||||
cstr = exports.CipherTextG1 |
||||
} else if (exports.CipherTextG2.prototype.isPrototypeOf(x)) { |
||||
add = mod._sheAddG2 |
||||
cstr = exports.CipherTextG2 |
||||
} else if (exports.CipherTextGT.prototype.isPrototypeOf(x)) { |
||||
add = mod._sheAddGT |
||||
cstr = exports.CipherTextGT |
||||
} else { |
||||
throw('exports.add:not supported') |
||||
} |
||||
return callAddSub(add, cstr, x.a_, y.a_) |
||||
} |
||||
// return x - y
|
||||
exports.sub = function(x, y) { |
||||
if (x.a_.length != y.a_.length) throw('exports.sub:bad type') |
||||
let sub = null |
||||
let cstr = null |
||||
if (exports.CipherTextG1.prototype.isPrototypeOf(x)) { |
||||
sub = mod._sheSubG1 |
||||
cstr = exports.CipherTextG1 |
||||
} else if (exports.CipherTextG2.prototype.isPrototypeOf(x)) { |
||||
sub = mod._sheSubG2 |
||||
cstr = exports.CipherTextG2 |
||||
} else if (exports.CipherTextGT.prototype.isPrototypeOf(x)) { |
||||
sub = mod._sheSubGT |
||||
cstr = exports.CipherTextGT |
||||
} else { |
||||
throw('exports.sub:not supported') |
||||
} |
||||
return callAddSub(sub, cstr, x.a_, y.a_) |
||||
} |
||||
// return x * (int)y
|
||||
exports.mulInt = function(x, y) { |
||||
let mulInt = null |
||||
let cstr = null |
||||
if (exports.CipherTextG1.prototype.isPrototypeOf(x)) { |
||||
mulInt = mod._sheMul32G1 |
||||
cstr = exports.CipherTextG1 |
||||
} else if (exports.CipherTextG2.prototype.isPrototypeOf(x)) { |
||||
mulInt = mod._sheMul32G2 |
||||
cstr = exports.CipherTextG2 |
||||
} else if (exports.CipherTextGT.prototype.isPrototypeOf(x)) { |
||||
mulInt = mod._sheMul32GT |
||||
cstr = exports.CipherTextGT |
||||
} else { |
||||
throw('exports.mulInt:not supported') |
||||
} |
||||
return callMulInt(mulInt, cstr, x.a_, y) |
||||
} |
||||
// return (G1)x * (G2)y
|
||||
exports.mul = function(x, y) { |
||||
if (!exports.CipherTextG1.prototype.isPrototypeOf(x) |
||||
|| !exports.CipherTextG2.prototype.isPrototypeOf(y)) throw('exports.mul:bad type') |
||||
let z = new exports.CipherTextGT() |
||||
let stack = mod.Runtime.stackSave() |
||||
let xPos = mod.Runtime.stackAlloc(x.a_.length * 4) |
||||
let yPos = mod.Runtime.stackAlloc(y.a_.length * 4) |
||||
let zPos = mod.Runtime.stackAlloc(z.a_.length * 4) |
||||
copyFromUint32Array(xPos, x.a_) |
||||
copyFromUint32Array(yPos, y.a_) |
||||
mod._sheMul(zPos, xPos, yPos) |
||||
copyToUint32Array(z.a_, zPos) |
||||
mod.Runtime.stackRestore(stack) |
||||
return z |
||||
} |
||||
} |
||||
return exports |
||||
}) |
@ -1,55 +0,0 @@ |
||||
<!DOCTYPE html> |
||||
<html lang="ja"> |
||||
<head> |
||||
<meta charset="UTF-8"> |
||||
<title>Two-level Homomorphic Encryption demo</title> |
||||
<link rel="stylesheet" href="style.css"> |
||||
<script src="jquery-3.2.1.min.js"></script> |
||||
<script src="she_c.js"></script> |
||||
<script src="she.js"></script> |
||||
<script src="she-demo2.js"></script> |
||||
</head> |
||||
<body> |
||||
<h1>Two-level Homomorphic Encryption demo</h1> |
||||
<div> |
||||
library status <span name="status">initializing...</span> |
||||
</div> |
||||
|
||||
<div id="left"> |
||||
<h2>Client</h2> |
||||
secretKey : <div name="sec" class="hex"></div> |
||||
|
||||
<table border=1 id="client_table" width="100%" summary="client"> |
||||
<tr id="client_header"><th>x<th>y<th class="hex">EncG1(x)<th class="hex">EncG2(y)</tr> |
||||
</table> |
||||
<input type="text" name="append" value="1,1"> |
||||
<button type="button" onclick="append()">1. append "x,y" and encrypt them (multiple times)</button> |
||||
<br> |
||||
<button type="button" onclick="send()">2. send to server</button> |
||||
<div>received ciphertext</div> |
||||
<div name="encSumC" class="hex">0</div> |
||||
<button type="button" onclick="dec()">6. decode ciphertext</button> |
||||
<div name="ret">0</div> |
||||
|
||||
</div> |
||||
|
||||
<div id="middle"> |
||||
</div> |
||||
|
||||
<div id="right"> |
||||
|
||||
<h2>Server</h2> |
||||
publicKey : <div name="pub" class="hex"></div> |
||||
|
||||
<table border=1 id="server_table" width="100%" summary="server"> |
||||
</table> |
||||
<button type="button" onclick="mul()">3. compute EncGT(x * y) := EncG1(x) * EncG2(y) for each row</button><br> |
||||
<button type="button" onclick="sum()">4. sum of all values in EncGT(x * y) column</button> |
||||
<div>result</div> |
||||
<div name="encSumS" class="hex">0</div> |
||||
<button type="button" onclick="recv()">5. return to client</button> |
||||
|
||||
</div> |
||||
|
||||
</body> |
||||
</html> |
File diff suppressed because one or more lines are too long
Binary file not shown.
@ -1,5 +0,0 @@ |
||||
if (typeof __dirname === 'string') { |
||||
var Module = {} |
||||
Module.wasmBinaryFile = __dirname + '/she_c.wasm' |
||||
} |
||||
|
@ -1 +0,0 @@ |
||||
sheInit|erialize|Str|sheDec |
Loading…
Reference in new issue