[js] simple demo

dev
MITSUNARI Shigeo 7 years ago
parent 3b41a2891f
commit 796119d857
  1. 51
      docs/demo/index.html
  2. 122
      docs/demo/index.js
  3. 66
      docs/demo/mobile.css

@ -0,0 +1,51 @@
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Two-level Homomorphic Encryption demo</title>
<link rel="stylesheet" href="mobile.css">
<script src="jquery-3.2.1.min.js"></script>
<script src="she_c.js"></script>
<script src="she.js"></script>
<script src="index.js"></script>
</head>
<body>
<h1>内積の秘匿計算デモ</h1>
<h3>ライブラリ:</h3><span name="status">初期化中</span>
<h2>データ提供者</h2>
<h3>秘密鍵:</h3><span name="sec" class="hex"></span>
<table border=1 id="client_table" width="100%" summary="client">
<tr id="client_header"><th>x<th>y<th class="hex">Enc(x)<th class="hex">Enc(y)</tr>
</table>
<input type="text" name="append" value="1,1">
<button type="button" onclick="append()">"x,y"を追加する</button> または
<button type="button" onclick="appendRand()">プリセットを追加する</button>
<br>
<button type="button" onclick="send()">暗号文をサーバに送信</button>
<hr>
<h2>代理秘匿計算サーバ</h2>
<h3>公開鍵:</h3><span name="pub" class="hex"></span>
<table border=1 id="server_table" width="100%" summary="server">
</table>
<button type="button" onclick="mul_sum()">暗号文のまま各行の積を取りその値を集計する</button><br>
<h3>集計結果:</h3><span name="encSumS" class="hex">0</span>
<br>
<button type="button" onclick="recv()">クライアントに送信する</button>
<hr>
<h2>復号クライアント</h2>
<h3>暗号文の計算結果:</h3><span name="encSumC" class="hex">0</span>
<br>
<button type="button" onclick="dec()">復号する</button><br>
<h3>集計結果:</h3><span name="ret">0</span>
</body>
</html>

@ -0,0 +1,122 @@
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(256)
.then(() => {
setText('status', 'OK')
sec = new she.SecretKey()
sec.setByCSPRNG()
setText('sec', sec.toHexStr())
pub = sec.getPublicKey()
setText('pub', pub.toHexStr())
})
function appendXY(x, y) {
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 append() {
let v = getValue('append')
let vs = v.split(',')
let x = parseInt(vs[0])
let y = parseInt(vs[1])
appendXY(x, y)
}
function appendRand() {
const tbl = [
[1,2], [-2,1], [4,3], [5,-2], [6,1]
]
tbl.forEach(p => appendXY(p[0], p[1]))
}
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 = [
'Enc(x)', 'Enc(y)', 'Enc(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 mul_sum() {
mul()
sum()
}
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)
}

@ -0,0 +1,66 @@
h1 {
font-size:xx-large;
text-align: center;
}
h2 {
font-size:xx-large;
}
h3 {
font-size:xx-large;
display: inline-block;
background-color: #eee;
}
li {
list-style-type : none;
padding:5px 10px;
border-top:1px solid black;
border-left:1px solid black;
}
input {
font-size: xx-large;
}
span {
font-size:xx-large;
max-width: 400px;
display: inline-block;
overflow: hidden;
}
td {
text-align: left;
max-width: 200px;
overflow: hidden;
}
#header {
background-color: #ddf;
}
#client_header {
background-color: #ddf;
}
table {
font-size: xx-large;
width: 800px;
}
button {
font-size: xx-large;
margin: 8px;
padding: 8px;
border: 1px;
background-color: #bee;
}
div.ret {
font-size: xx-large;
margin: 5px;
padding: 5px;
border: 1px solid;
text-align: center;
}
Loading…
Cancel
Save