parent
809389db84
commit
cba0e17a74
File diff suppressed because one or more lines are too long
@ -0,0 +1,150 @@ |
||||
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 } |
||||
|
||||
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 bin2hex(s) { |
||||
var o = '' |
||||
for (var i = 0; i < s.length; i++) { |
||||
n = s.charCodeAt(i).toString(16) |
||||
o += (n.length < 2 ? '0' + n : n) + ' ' |
||||
} |
||||
return o |
||||
} |
||||
|
||||
function strip(s) { |
||||
s = s.trim() |
||||
var begin = 0 |
||||
var end = s.length |
||||
if (end == 0) { |
||||
return s |
||||
} |
||||
if (s[0] == '"') { |
||||
begin++ |
||||
} |
||||
if (begin < end && s[end - 1] == '"') { |
||||
end-- |
||||
} |
||||
return s.substr(begin, end - begin) |
||||
} |
||||
|
||||
|
||||
(function() { |
||||
const range = 2048 |
||||
const tryNum = 100 |
||||
she.init(range, tryNum, function() { |
||||
setText('status', 'ok') |
||||
sec = new she.SecretKey() |
||||
sec.setByCSPRNG() |
||||
setText('sec', sec.toHexStr()) |
||||
pub = sec.getPublicKey() |
||||
setText('pub', pub.toHexStr()) |
||||
}) |
||||
}()) |
||||
|
||||
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()) |
||||
}) |
||||
var obj = $('#server_table') |
||||
obj.html('') |
||||
{ |
||||
var header = [ |
||||
'EncG1(x)', 'EncG2(y)', 'EncGT(x y)' |
||||
] |
||||
var t = $('<tr>').attr('id', 'header') |
||||
for (var i = 0; i < header.length; i++) { |
||||
t.append( |
||||
$('<th>').append(header[i]) |
||||
) |
||||
} |
||||
obj.append(t) |
||||
} |
||||
for (var i = 0; i < ct1.length; i++) { |
||||
var 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() { |
||||
let c1 = new she.CipherTextG1() |
||||
let c2 = new she.CipherTextG2() |
||||
$('.encG1xS').each(function() { |
||||
let o = $(this) |
||||
c1.fromHexStr(o.text()) |
||||
c2.fromHexStr(o.next().text()) |
||||
let ct = she.mul(c1, c2) |
||||
o.next().next().text(ct.toHexStr()) |
||||
}) |
||||
} |
||||
|
||||
function sum() { |
||||
let ct = new she.CipherTextGT() |
||||
let csum = pub.encGT(0) |
||||
$('.encGTxyS').each(function() { |
||||
ct.fromHexStr($(this).text()) |
||||
csum = she.add(csum, ct) |
||||
}) |
||||
setText('encSumS', csum.toHexStr()) |
||||
} |
||||
|
||||
function recv() { |
||||
setText('encSumC', getText('encSumS')) |
||||
} |
||||
|
||||
function dec() { |
||||
let s = getText('encSumC') |
||||
let ct = new she.CipherTextGT() |
||||
ct.fromHexStr(s) |
||||
let v = sec.dec(ct) |
||||
setText('ret', v) |
||||
} |
@ -0,0 +1,59 @@ |
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
||||
<html lang="ja"> |
||||
<head> |
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
||||
<meta http-equiv="Content-Script-Type" content="text/javascript"> |
||||
<meta http-equiv="Content-Style-Type" content="text/css"> |
||||
<title>Two-level Homomorphic Encryption demo</title> |
||||
<link rel="stylesheet" type="text/css" href="style.css"> |
||||
<script type='text/javascript' src="jquery-3.2.1.min.js"></script> |
||||
<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-demo2.js"></script> |
||||
</head> |
||||
<body> |
||||
<h1>Two-level Homorphic 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</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"> |
||||
<tr id="header"><th>ciphertext</tr> |
||||
</table> |
||||
<button type="button" onclick="mul()">3. EncG1(xi) * EncG2(y)</button><br> |
||||
<button type="button" onclick="sum()">4. sum Enc(xi * yi)</button> |
||||
<div>result</div> |
||||
<div name="encSumS" class="hex">0</div> |
||||
<button type="button" onclick="recv()">5. return to client</button> |
||||
|
||||
</div> |
||||
|
||||
</body> |
||||
</html> |
Loading…
Reference in new issue