From 796119d8575af0ce2029a0f36792b1eb3d8a4db6 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Sun, 12 Nov 2017 09:05:55 +0900 Subject: [PATCH] [js] simple demo --- docs/demo/index.html | 51 ++++++++++++++++++ docs/demo/index.js | 122 +++++++++++++++++++++++++++++++++++++++++++ docs/demo/mobile.css | 66 +++++++++++++++++++++++ 3 files changed, 239 insertions(+) create mode 100644 docs/demo/index.html create mode 100644 docs/demo/index.js create mode 100644 docs/demo/mobile.css diff --git a/docs/demo/index.html b/docs/demo/index.html new file mode 100644 index 0000000..9da744c --- /dev/null +++ b/docs/demo/index.html @@ -0,0 +1,51 @@ + + + + + Two-level Homomorphic Encryption demo + + + + + + + + +

内積の秘匿計算デモ

+

ライブラリ:

初期化中 + +

データ提供者

+

秘密鍵:

+ + + +
xyEnc(x)Enc(y)
+ + または + +
+ + +
+ +

代理秘匿計算サーバ

+

公開鍵:

+ + +
+
+

集計結果:

0 +
+ + + +
+

復号クライアント

+ +

暗号文の計算結果:

0 +
+
+

集計結果:

0 + + + diff --git a/docs/demo/index.js b/docs/demo/index.js new file mode 100644 index 0000000..9cc346f --- /dev/null +++ b/docs/demo/index.js @@ -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( + $('').append( + $('').text(x) + ).append( + $('').text(y) + ).append( + $('').text(c1.toHexStr()) + ).append( + $('').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 = $('').attr('id', 'header') + for (let i = 0; i < header.length; i++) { + t.append( + $('').append(header[i]) + ) + } + obj.append(t) + } + for (let i = 0; i < ct1.length; i++) { + let t = $('') + t.append( + $('').append(ct1[i]) + ).append( + $('').append(ct2[i]) + ).append( + $('').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) +} diff --git a/docs/demo/mobile.css b/docs/demo/mobile.css new file mode 100644 index 0000000..94a22b5 --- /dev/null +++ b/docs/demo/mobile.css @@ -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; +}