Initial commit of DEMOS2 with the upgraded 'Create Event' UI. However, there is no input validation currently

This commit is contained in:
vince0656 2018-06-12 13:31:38 +01:00
commit 7084bd1b16
155 changed files with 8102 additions and 0 deletions

231
Node/demos2-booth.js Executable file
View file

@ -0,0 +1,231 @@
/*
Cryptography functions written by Bingsheng Zhang
Uses the milagro-crypto-js library at:
https://github.com/milagro-crypto/milagro-crypto-js
*/
//Group parameter generator: returns rng object and generators g1,g2 for G1,G2 as well as order
gpGen = function(){
//init, and base generators
var ctx = new CTX("BN254CX");
var n=new ctx.BIG(0); n.rcopy(ctx.ROM_CURVE.CURVE_Order);
//get generator P for G1
P = new ctx.ECP(0);
gx = new ctx.BIG(0);
gx.rcopy(ctx.ROM_CURVE.CURVE_Gx);
if (ctx.ECP.CURVETYPE != ctx.ECP.MONTGOMERY) {
gy = new ctx.BIG(0);
gy.rcopy(ctx.ROM_CURVE.CURVE_Gy);
P.setxy(gx, gy);
} else P.setx(gx);
//get generator Q for G2
var A=new ctx.BIG(0);
var B=new ctx.BIG(0);
A.rcopy(ctx.ROM_CURVE.CURVE_Pxa);
B.rcopy(ctx.ROM_CURVE.CURVE_Pxb);
var Qx=new ctx.FP2(0); Qx.bset(A,B);
A.rcopy(ctx.ROM_CURVE.CURVE_Pya);
B.rcopy(ctx.ROM_CURVE.CURVE_Pyb);
var Qy=new ctx.FP2(0); Qy.bset(A,B);
var Q=new ctx.ECP2();
Q.setxy(Qy,Qy);
return{
n:n,
g1:P,
g2:Q
}
}
//creates ElGamal public and secret key
keyGen=function(params){
var ctx = new CTX("BN254CX");
//set rng
var RAW = [];
var d = new Date();//time for seed, not secure
var rng = new ctx.RAND();
rng.clean();
RAW[0] = d.getSeconds();
RAW[1] = d.getMinutes();
RAW[2] = d.getMilliseconds();
rng.seed(3, RAW);
//ElGamal
var sk = new ctx.BIG(0);
sk = ctx.BIG.randomnum(params.n,rng);
var pk = new ctx.ECP(0);
pk = ctx.PAIR.G1mul(params.g1,sk);
return{
PK:pk,
SK:sk
}
}
//combine multiple public key together
//the input is an array of PKs
combine=function(PKs){
var ctx = new CTX("BN254CX");
var pk=new ctx.ECP();
//copy the first pk
pk.copy(PKs[0]);
//multiple the rest PKs
for(i=1;i<PKs.length;i++){
pk.add(PKs[i]);
}
return{
PK:pk
}
}
//ElGamal encryption
encrypt=function(params,PK, m){
var ctx = new CTX("BN254CX");
//set rand
var RAW = [];
var d = new Date();//time for seed, not secure
var rng = new ctx.RAND();
rng.clean();
RAW[0] = d.getSeconds();
RAW[1] = d.getMinutes();
RAW[2] = d.getMilliseconds();
rng.seed(3, RAW);
var r=new ctx.BIG.randomnum(params.n,rng);
var M=new ctx.BIG(m);
var C1=new ctx.ECP();
C1 = ctx.PAIR.G1mul(params.g1,r);
var gM=new ctx.ECP();
gM = ctx.PAIR.G1mul(params.g1,M);
var C2=new ctx.ECP();
C2 = ctx.PAIR.G1mul(PK,r);
C2.mul(r);
C2.add(gM);
return{
C1:C1,
C2:C2
}
}
//add ciphertexts
add=function(Ciphers){
var ctx = new CTX("BN254CX");
var s1=new ctx.ECP();
var s2=new ctx.ECP();
//copy the first cipher
s1.copy(Ciphers[0].C1);
s2.copy(Ciphers[0].C2);
//multiple the rest ciphertexts
for(i=1;i<Ciphers.length;i++){
s1.add(Ciphers[i].C1);
}
//no idea why I need two loops
for(j=1;j<Ciphers.length;j++){
s2.add(Ciphers[j].C2);
}
return{
C1:s1,
C2:s2
}
}
//ElGamal decryption
decrypt=function(params,SK, C){
var ctx = new CTX("BN254CX");
var D=new ctx.ECP();
D = ctx.PAIR.G1mul(C.C1,SK);
var gM=new ctx.ECP();
gM.copy(C.C2);
gM.sub(D);
//search for message by brute force
var B;
for (j = 0; j < 1000; j++) {
//use D as temp var
B = new ctx.BIG(j);
D = ctx.PAIR.G1mul(params.g1,B);
if (D.equals(gM))
return{
M:j
}
};
return{
M: "Error"
}
}
//ElGamal partial decryption
partDec=function(SK, C){
var ctx = new CTX("BN254CX");
var D=new ctx.ECP();
D = ctx.PAIR.G1mul(C.C1,SK);
return{
D: D
}
}
//Tally, combine partial decryption
//Ds is the array of partial decryptions; C is the ciphertext.
tally=function(params,Ds, C){
var ctx = new CTX("BN254CX");
var D=new ctx.ECP();
D.copy(Ds[0].D);
//combine D
for(i=1;i<Ds.length;i++){
D.add(Ds[i].D);
}
var gM=new ctx.ECP();
gM.copy(C.C2);
gM.sub(D);
//search for message by brute force
var B;
for (j = 0; j < 1000; j++) {
//use D as temp var
B = new ctx.BIG(j);
D = ctx.PAIR.G1mul(params.g1,B);
if (D.equals(gM))
return{
M:j
}
};
return{
M: "Error"
}
}

173
Node/example.html Executable file
View file

@ -0,0 +1,173 @@
<!DOCTYPE html>
<!--- Example code written by Bingsheng Zhang -->
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset="utf-8">
<title>Demos2</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">
<style type="text/css">
/* Sticky footer styles */
html,
body {
height: 100%;
/* The html and body elements cannot have any padding or margin. */
}
/* Wrapper for page content to push down footer */
#wrap {
min-height: 100%;
height: auto;
/* Negative indent footer by its height */
margin: 0 auto -60px;
/* Pad bottom by footer height */
padding: 0 0 60px;
}
</style>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- Wrap all page content here -->
<div id="wrap">
<div class="form-group">
<input type="text" id="numa" name="A" placeholder="Message" class="form-control">
</br>
<input type="text" id="numc" name="C" placeholder="Key" class="form-control">
</br>
<button type="button" id="add" class="btn btn-success">Encrypt</button>
</div>
<input type="text" id="numb" name="B" placeholder="Ciphertext" class="form-control">
</br>
</br>
<ul id="DecomList">
</ul>
</div>
<!-- Footer -->
<div id="footer">
<div class="container">
<p class="text-muted">Test trustee API</p>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- Bootstrap core JavaScript -->
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<!-- Crypto JS -->
<script type="text/javascript" src=./core/rand.js></script>
<script type="text/javascript" src=./core/rom_curve.js></script>
<script type="text/javascript" src=./core/rom_field.js></script>
<script type="text/javascript" src=./core/uint64.js></script>
<script type="text/javascript" src=./core/aes.js></script>
<script type="text/javascript" src=./core/big.js></script>
<script type="text/javascript" src=./core/gcm.js></script>
<script type="text/javascript" src=./core/hash256.js></script>
<script type="text/javascript" src=./core/hash384.js></script>
<script type="text/javascript" src=./core/hash512.js></script>
<script type="text/javascript" src=./core/sha3.js></script>
<script type="text/javascript" src=./core/newhope.js></script>
<script type="text/javascript" src=./core/nhs.js></script>
<script type="text/javascript" src=./core/fp.js></script>
<script type="text/javascript" src=./core/fp2.js></script>
<script type="text/javascript" src=./core/fp4.js></script>
<script type="text/javascript" src=./core/fp12.js></script>
<script type="text/javascript" src=./core/ff.js></script>
<script type="text/javascript" src=./core/rsa.js></script>
<script type="text/javascript" src=./core/ecp.js></script>
<script type="text/javascript" src=./core/ecp2.js></script>
<script type="text/javascript" src=./core/ecdh.js></script>
<script type="text/javascript" src=./core/pair.js></script>
<script type="text/javascript" src=./core/mpin.js></script>
<script type="text/javascript" src=./core/ctx.js></script>
<script type="text/javascript" src="demos2-booth.js"></script>
</body>
<script type="text/javascript">
$(document).ready(function(){
$("#add").click(function(){
var params = gpGen();
var keys = keyGen(params);
console.log(keys.PK.toString());
console.log(keys.SK.toString());
var cipher = encrypt(params,keys.PK, 5);
var string_c1 = cipher.C1.toString();
console.log(cipher.C1.toString());
console.log(cipher.C2.toString());
//test decrypt
var message = decrypt(params, keys.SK, cipher);
console.log(message);
//test combine key
var k1 = keyGen(params);
var k2 = keyGen(params);
console.log(k1.PK.toString());
console.log(k2.PK.toString());
var PKs = new Array(k1.PK,k2.PK);
var pk = combine(PKs);
console.log(pk.PK.toString());
//test add cipher
var c1 = encrypt(params,keys.PK, 3);
var c2 = encrypt(params,keys.PK, 4);
var cArray = new Array(c1,c2);
var S = add(cArray);
console.log(S.C1.toString());
console.log(S.C2.toString());
var msg = decrypt(params, keys.SK, S);
console.log(msg);
//test tally
var Tc = encrypt(params,pk.PK, 3);
var D1 = partDec(k1.SK, Tc);
var D2 = partDec(k2.SK, Tc);
var Ds = new Array(D1,D2);
var tar = tally(params, Ds, Tc);
console.log(tar);
//window.document.write("Testing ElGamal encryption"+ "<br>");
//window.document.write("C1: "+ string_c1 + "<br>");
//window.document.write("C1: "+ string_c1 + "<br>");
});
});
</script>
</html>

469
Node/index.js Executable file
View file

@ -0,0 +1,469 @@
/*
Code by Thomas Smith
*/
var port = 8080;
var express = require('express');
var Buffer = require('buffer').Buffer;
var CTX = require('milagro-crypto-js')
var app = express();
/*
var cors = require('cors')
app.use(cors());
*/
app.use(express.static('test'));
//default test
app.get('/', function(request, response){
var data = {
message: 'hello world',
value: 5
}
//response.send('Hey there'+request.ip);
response.json(data);
console.log('request from'+request.ip);
});
//parameter generation function
app.get('/param', function(request, response){
var param = gpGen();
console.log('Generated Param:' + param);
response.json(param);
})
//combine public keys and return the full combined one - JSON Version
app.get('/combpk', function(request, response){
var partials = request.query['PK']
var parsed = [];
console.log('Combining...');
for (var i = partials.length - 1; i >= 0; i--) {
console.log('PK' +i+ ': '+partials[i]);
parsed.push(JSON.parse(partials[i]));
}
var PK = combine(parsed);
response.json(PK);
})
//byte array version
app.get('/cmpkstring', function(request, response){
var ctx = new CTX("BN254CX");
var partials = request.query['PK']
//if there is only one key, partials will be an array of the individual bytes
//if more than one, it will be an array of arrays
//we need to factor for this in code
var noOfKeys = request.query['number'];
var parsed = [];
if(noOfKeys == partials.length)//if we're submitting more than one key
{
console.log('Combining' + noOfKeys + " keys...");
for (var i = partials.length - 1; i >= 0; i--) {
console.log('PK' +i+ ': '+partials[i]);
var bytes = Buffer.from(partials[i].split(','), 'hex');
console.log(bytes)
var pk = new ctx.ECP.fromBytes(bytes);
parsed.push(pk);
}
}
else if(noOfKeys == 1)
{
console.log("Combining just one key");
var bytes = Buffer.from(partials.split(','), 'hex');
console.log(bytes);
var pk = new ctx.ECP.fromBytes(bytes);
parsed.push(pk);
}
response.json(combine(parsed));
})
//addition function on homomorphically encrypted variables
//this may need some work, different method of serialisation maybe?
app.get('/addec', function(request, response){
var c1 = request.query['C1'];
var c2 = request.query['C2'];
var number = request.query['number']; //number of ciphertexts to add
//all the list of ciphertext objects to give to the function
var parsed = [];
var ctx = new CTX("BN254CX");
console.log('Addec:');
if(number == c1.length)
{
for (var i = 0; i < c1.length; i++) {
console.log(i + ".C1: " + c1[i]);
var c1Bytes = Buffer.from(c1[i].split(','), 'hex');
var newC1 = new ctx.ECP.fromBytes(c1Bytes);
var cipher =
{
C1:newC1,
C2:null
};
parsed.push(cipher);
}
for (var j = 0; j < c2.length; j++) {
console.log(j + ".C2: " + c2[j]);
var c2Bytes = Buffer.from(c2[j].split(','), 'hex');
var newC2 = new ctx.ECP.fromBytes(c2Bytes);
parsed[j].C2 = newC2;
}
}
else if(number == 1)
{
console.log("only one cipher");
var c1Bytes = Buffer.from(c1.split(','), 'hex');
var newC1 = new ctx.ECP.fromBytes(c1Bytes);
console.log("C1: " + c1);
var c2Bytes = Buffer.from(c2.split(','), 'hex');
var newC2 = new ctx.ECP.fromBytes(c2Bytes);
console.log("C2: " + c2);
var cipher =
{
C1:newC1,
C2:newC2
};
parsed.push(cipher);
}
response.json(add(parsed));
})
//tally partially decrypted ciphertexts
app.get('/tally', function(request, response){
console.log("called tally");
var amount = request.query['number'];//number of decryptions taking in
var paramString = request.query['param'];//event group parameter in JSON
var partialsStrings = request.query['decs'];//array of partial decryption(s) in bytes
var ciphertextString = request.query['cipher'];//ciphertext being decrypted in JSON
//re-build parameters
var tempParams = JSON.parse(paramString);
var ctx = new CTX("BN254CX"); //new context we can use
var n = new ctx.BIG();
var g1 = new ctx.ECP();
var g2 = new ctx.ECP2();
//copying the values
n.copy(tempParams.n);
g1.copy(tempParams.g1);
g2.copy(tempParams.g2);
var params = {
n:n,
g1:g1,
g2:g2
}
//re-build partial decryptions
var partials = []
if(amount == partialsStrings.length)
{
console.log(amount + " partial decryptions");
for(var i = 0; i < partialsStrings.length; i++)
{
var bytes = Buffer.from(partialsStrings[i].split(','), 'hex');
var dec = {
D:new ctx.ECP.fromBytes(bytes)
}
partials.push(dec);
}
}
else if(amount == 1)
{
console.log("Only one partial decryption received")
console.log(paramString)
var bytes = Buffer.from(partialsStrings.split(','), 'hex');
var dec = {
D:new ctx.ECP.fromBytes(bytes)
}
partials.push(dec);
}
//re-build combined ciphertext
var tempCipher = JSON.parse(ciphertextString);
cipher = {
C1: new ctx.ECP(),
C2: new ctx.ECP()
}
cipher.C1.copy(tempCipher.C1);
cipher.C2.copy(tempCipher.C2);
response.json(tally(params, partials, cipher))
})
var server = app.listen(port, function(){
var host = server.address().address;
var appPort = server.address().port;
console.log('Server listening on ' + host + ':'+ port);
});
/*
Cryptography functions written by Bingsheng Zhang
Uses the milagro-crypto-js library at:
https://github.com/milagro-crypto/milagro-crypto-js
*/
//Group parameter generator: returns rng object and generators g1,g2 for G1,G2 as well as order
gpGen = function(){
//init, and base generators
var ctx = new CTX("BN254CX");
var n=new ctx.BIG(0); n.rcopy(ctx.ROM_CURVE.CURVE_Order);
//get generator P for G1
P = new ctx.ECP(0);
gx = new ctx.BIG(0);
gx.rcopy(ctx.ROM_CURVE.CURVE_Gx);
if (ctx.ECP.CURVETYPE != ctx.ECP.MONTGOMERY) {
gy = new ctx.BIG(0);
gy.rcopy(ctx.ROM_CURVE.CURVE_Gy);
P.setxy(gx, gy);
} else P.setx(gx);
//get generator Q for G2
var A=new ctx.BIG(0);
var B=new ctx.BIG(0);
A.rcopy(ctx.ROM_CURVE.CURVE_Pxa);
B.rcopy(ctx.ROM_CURVE.CURVE_Pxb);
var Qx=new ctx.FP2(0); Qx.bset(A,B);
A.rcopy(ctx.ROM_CURVE.CURVE_Pya);
B.rcopy(ctx.ROM_CURVE.CURVE_Pyb);
var Qy=new ctx.FP2(0); Qy.bset(A,B);
var Q=new ctx.ECP2();
Q.setxy(Qy,Qy);
return{
n:n,
g1:P,
g2:Q
}
}
//creates ElGamal public and secret key
keyGen=function(params){
var ctx = new CTX("BN254CX");
//set rng
var RAW = [];
var d = new Date();//time for seed, not secure
var rng = new ctx.RAND();
rng.clean();
RAW[0] = d.getSeconds();
RAW[1] = d.getMinutes();
RAW[2] = d.getMilliseconds();
rng.seed(3, RAW);
//ElGamal
var sk = new ctx.BIG(0);
sk = ctx.BIG.randomnum(params.n,rng);
var pk = new ctx.ECP(0);
pk = ctx.PAIR.G1mul(params.g1,sk);
return{
PK:pk,
SK:sk
}
}
//combine multiple public key together
//the input is an array of PKs
combine=function(PKs){
var ctx = new CTX("BN254CX");
var pk=new ctx.ECP();
//copy the first pk
pk.copy(PKs[0]);
//multiple the rest PKs
for(i=1;i<PKs.length;i++){
pk.add(PKs[i]);
}
return{
PK:pk
}
}
//ElGamal encryption
encrypt=function(params,PK, m){
var ctx = new CTX("BN254CX");
//set rand
var RAW = [];
var d = new Date();//time for seed, not secure
var rng = new ctx.RAND();
rng.clean();
RAW[0] = d.getSeconds();
RAW[1] = d.getMinutes();
RAW[2] = d.getMilliseconds();
rng.seed(3, RAW);
var r=new ctx.BIG.randomnum(params.n,rng);
var M=new ctx.BIG(m);
var C1=new ctx.ECP();
C1 = ctx.PAIR.G1mul(params.g1,r);
var gM=new ctx.ECP();
gM = ctx.PAIR.G1mul(params.g1,M);
var C2=new ctx.ECP();
C2 = ctx.PAIR.G1mul(PK,r);
C2.mul(r);
C2.add(gM);
return{
C1:C1,
C2:C2
}
}
//add ciphertexts
add=function(Ciphers){
var ctx = new CTX("BN254CX");
var s1=new ctx.ECP();
var s2=new ctx.ECP();
//copy the first cipher
s1.copy(Ciphers[0].C1);
s2.copy(Ciphers[0].C2);
//multiple the rest ciphertexts
for(i=1;i<Ciphers.length;i++){
s1.add(Ciphers[i].C1);
}
//no idea why I need two loops
for(j=1;j<Ciphers.length;j++){
s2.add(Ciphers[j].C2);
}
return{
C1:s1,
C2:s2
}
}
//ElGamal decryption
decrypt=function(params,SK, C){
var ctx = new CTX("BN254CX");
var D=new ctx.ECP();
D = ctx.PAIR.G1mul(C.C1,SK);
var gM=new ctx.ECP();
gM.copy(C.C2);
gM.sub(D);
//search for message by brute force
var B;
for (j = 0; j < 1000; j++) {
//use D as temp var
B = new ctx.BIG(j);
D = ctx.PAIR.G1mul(params.g1,B);
if (D.equals(gM))
return{
M:j
}
};
return{
M: "Error"
}
}
//ElGamal partial decryption
partDec=function(SK, C){
var ctx = new CTX("BN254CX");
var D=new ctx.ECP();
D = ctx.PAIR.G1mul(C.C1,SK);
return{
D: D
}
}
//Tally, combine partial decryption
//Ds is the array of partial decryptions; C is the ciphertext.
tally=function(params,Ds, C){
var ctx = new CTX("BN254CX");
var D=new ctx.ECP();
D.copy(Ds[0].D);
//combine D
for(i=1;i<Ds.length;i++){
D.add(Ds[i].D);
}
var gM=new ctx.ECP();
gM.copy(C.C2);
gM.sub(D);
//search for message by brute force
var B;
for (j = 0; j < 1000; j++) {
//use D as temp var
B = new ctx.BIG(j);
D = ctx.PAIR.G1mul(params.g1,B);
if (D.equals(gM))
return{
M:j
}
};
return{
M: "Error"
}
}

529
Node/package-lock.json generated Executable file
View file

@ -0,0 +1,529 @@
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"accepts": {
"version": "1.3.4",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.4.tgz",
"integrity": "sha1-hiRnWMfdbSGmR0/whKR0DsBesh8=",
"requires": {
"mime-types": "2.1.17",
"negotiator": "0.6.1"
}
},
"append-field": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/append-field/-/append-field-0.1.0.tgz",
"integrity": "sha1-bdxY+gg8e8VF08WZWygwzCNm1Eo="
},
"array-flatten": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
},
"body-parser": {
"version": "1.18.2",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.18.2.tgz",
"integrity": "sha1-h2eKGdhLR9hZuDGZvVm84iKxBFQ=",
"requires": {
"bytes": "3.0.0",
"content-type": "1.0.4",
"debug": "2.6.9",
"depd": "1.1.1",
"http-errors": "1.6.2",
"iconv-lite": "0.4.19",
"on-finished": "2.3.0",
"qs": "6.5.1",
"raw-body": "2.3.2",
"type-is": "1.6.15"
}
},
"busboy": {
"version": "0.2.14",
"resolved": "https://registry.npmjs.org/busboy/-/busboy-0.2.14.tgz",
"integrity": "sha1-bCpiLvz0fFe7vh4qnDetNseSVFM=",
"requires": {
"dicer": "0.2.5",
"readable-stream": "1.1.14"
}
},
"bytes": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz",
"integrity": "sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg="
},
"concat-stream": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.0.tgz",
"integrity": "sha1-CqxmL9Ur54lk1VMvaUeE5wEQrPc=",
"requires": {
"inherits": "2.0.3",
"readable-stream": "2.3.3",
"typedarray": "0.0.6"
},
"dependencies": {
"isarray": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
"integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE="
},
"readable-stream": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.3.tgz",
"integrity": "sha512-m+qzzcn7KUxEmd1gMbchF+Y2eIUbieUaxkWtptyHywrX0rE8QEYqPC07Vuy4Wm32/xE16NcdBctb8S0Xe/5IeQ==",
"requires": {
"core-util-is": "1.0.2",
"inherits": "2.0.3",
"isarray": "1.0.0",
"process-nextick-args": "1.0.7",
"safe-buffer": "5.1.1",
"string_decoder": "1.0.3",
"util-deprecate": "1.0.2"
}
},
"string_decoder": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz",
"integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==",
"requires": {
"safe-buffer": "5.1.1"
}
}
}
},
"content-disposition": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.2.tgz",
"integrity": "sha1-DPaLud318r55YcOoUXjLhdunjLQ="
},
"content-type": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz",
"integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA=="
},
"cookie": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.3.1.tgz",
"integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s="
},
"cookie-parser": {
"version": "1.4.3",
"resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.3.tgz",
"integrity": "sha1-D+MfoZ0AC5X0qt8fU/3CuKIDuqU=",
"requires": {
"cookie": "0.3.1",
"cookie-signature": "1.0.6"
}
},
"cookie-signature": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
"integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
},
"core-util-is": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
},
"cors": {
"version": "2.8.4",
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.4.tgz",
"integrity": "sha1-K9OB8usgECAQXNUOpZ2mMJBpRoY=",
"requires": {
"object-assign": "4.1.1",
"vary": "1.1.2"
},
"dependencies": {
"object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
"integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM="
}
}
},
"debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"requires": {
"ms": "2.0.0"
}
},
"depd": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/depd/-/depd-1.1.1.tgz",
"integrity": "sha1-V4O04cRZ8G+lyif5kfPQbnoxA1k="
},
"destroy": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz",
"integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA="
},
"dicer": {
"version": "0.2.5",
"resolved": "https://registry.npmjs.org/dicer/-/dicer-0.2.5.tgz",
"integrity": "sha1-WZbAhrszIYyBLAkL3cCc0S+stw8=",
"requires": {
"readable-stream": "1.1.14",
"streamsearch": "0.1.2"
}
},
"ee-first": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
"integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0="
},
"encodeurl": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.1.tgz",
"integrity": "sha1-eePVhlU0aQn+bw9Fpd5oEDspTSA="
},
"escape-html": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz",
"integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg="
},
"etag": {
"version": "1.8.1",
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
"integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc="
},
"express": {
"version": "4.16.2",
"resolved": "https://registry.npmjs.org/express/-/express-4.16.2.tgz",
"integrity": "sha1-41xt/i1kt9ygpc1PIXgb4ymeB2w=",
"requires": {
"accepts": "1.3.4",
"array-flatten": "1.1.1",
"body-parser": "1.18.2",
"content-disposition": "0.5.2",
"content-type": "1.0.4",
"cookie": "0.3.1",
"cookie-signature": "1.0.6",
"debug": "2.6.9",
"depd": "1.1.1",
"encodeurl": "1.0.1",
"escape-html": "1.0.3",
"etag": "1.8.1",
"finalhandler": "1.1.0",
"fresh": "0.5.2",
"merge-descriptors": "1.0.1",
"methods": "1.1.2",
"on-finished": "2.3.0",
"parseurl": "1.3.2",
"path-to-regexp": "0.1.7",
"proxy-addr": "2.0.2",
"qs": "6.5.1",
"range-parser": "1.2.0",
"safe-buffer": "5.1.1",
"send": "0.16.1",
"serve-static": "1.13.1",
"setprototypeof": "1.1.0",
"statuses": "1.3.1",
"type-is": "1.6.15",
"utils-merge": "1.0.1",
"vary": "1.1.2"
}
},
"finalhandler": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.0.tgz",
"integrity": "sha1-zgtoVbRYU+eRsvzGgARtiCU91/U=",
"requires": {
"debug": "2.6.9",
"encodeurl": "1.0.1",
"escape-html": "1.0.3",
"on-finished": "2.3.0",
"parseurl": "1.3.2",
"statuses": "1.3.1",
"unpipe": "1.0.0"
}
},
"forwarded": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz",
"integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ="
},
"fresh": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
"integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac="
},
"http-errors": {
"version": "1.6.2",
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.6.2.tgz",
"integrity": "sha1-CgAsyFcHGSp+eUbO7cERVfYOxzY=",
"requires": {
"depd": "1.1.1",
"inherits": "2.0.3",
"setprototypeof": "1.0.3",
"statuses": "1.3.1"
},
"dependencies": {
"setprototypeof": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.0.3.tgz",
"integrity": "sha1-ZlZ+NwQ+608E2RvWWMDL77VbjgQ="
}
}
},
"iconv-lite": {
"version": "0.4.19",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.19.tgz",
"integrity": "sha512-oTZqweIP51xaGPI4uPa56/Pri/480R+mo7SeU+YETByQNhDG55ycFyNLIgta9vXhILrxXDmF7ZGhqZIcuN0gJQ=="
},
"inherits": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
"integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4="
},
"ipaddr.js": {
"version": "1.5.2",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.5.2.tgz",
"integrity": "sha1-1LUFvemUaYfM8PxY2QEP+WB+P6A="
},
"isarray": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz",
"integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8="
},
"media-typer": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
"integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g="
},
"merge-descriptors": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz",
"integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E="
},
"methods": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz",
"integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4="
},
"milagro-crypto-js": {
"version": "file:milagro-crypto-js"
},
"mime": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/mime/-/mime-1.4.1.tgz",
"integrity": "sha512-KI1+qOZu5DcW6wayYHSzR/tXKCDC5Om4s1z2QJjDULzLcmf3DvzS7oluY4HCTrc+9FiKmWUgeNLg7W3uIQvxtQ=="
},
"mime-db": {
"version": "1.30.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.30.0.tgz",
"integrity": "sha1-dMZD2i3Z1qRTmZY0ZbJtXKfXHwE="
},
"mime-types": {
"version": "2.1.17",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.17.tgz",
"integrity": "sha1-Cdejk/A+mVp5+K+Fe3Cp4KsWVXo=",
"requires": {
"mime-db": "1.30.0"
}
},
"minimist": {
"version": "0.0.8",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
},
"mkdirp": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
"integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
"requires": {
"minimist": "0.0.8"
}
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
},
"multer": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/multer/-/multer-1.3.0.tgz",
"integrity": "sha1-CSsmcPaEb6SRSWXvyM+Uwg/sbNI=",
"requires": {
"append-field": "0.1.0",
"busboy": "0.2.14",
"concat-stream": "1.6.0",
"mkdirp": "0.5.1",
"object-assign": "3.0.0",
"on-finished": "2.3.0",
"type-is": "1.6.15",
"xtend": "4.0.1"
}
},
"negotiator": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz",
"integrity": "sha1-KzJxhOiZIQEXeyhWP7XnECrNDKk="
},
"object-assign": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-3.0.0.tgz",
"integrity": "sha1-m+3VygiXlJvKR+f/QIBi1Un1h/I="
},
"on-finished": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
"integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=",
"requires": {
"ee-first": "1.1.1"
}
},
"parseurl": {
"version": "1.3.2",
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.2.tgz",
"integrity": "sha1-/CidTtiZMRlGDBViUyYs3I3mW/M="
},
"path-to-regexp": {
"version": "0.1.7",
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz",
"integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w="
},
"process-nextick-args": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-1.0.7.tgz",
"integrity": "sha1-FQ4gt1ZZCtP5EJPyWk8q2L/zC6M="
},
"proxy-addr": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.2.tgz",
"integrity": "sha1-ZXFQT0e7mI7IGAJT+F3X4UlSvew=",
"requires": {
"forwarded": "0.1.2",
"ipaddr.js": "1.5.2"
}
},
"qs": {
"version": "6.5.1",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.5.1.tgz",
"integrity": "sha512-eRzhrN1WSINYCDCbrz796z37LOe3m5tmW7RQf6oBntukAG1nmovJvhnwHHRMAfeoItc1m2Hk02WER2aQ/iqs+A=="
},
"range-parser": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.0.tgz",
"integrity": "sha1-9JvmtIeJTdxA3MlKMi9hEJLgDV4="
},
"raw-body": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.3.2.tgz",
"integrity": "sha1-vNYMd9Prk83gBQKVw/N5OJvIj4k=",
"requires": {
"bytes": "3.0.0",
"http-errors": "1.6.2",
"iconv-lite": "0.4.19",
"unpipe": "1.0.0"
}
},
"readable-stream": {
"version": "1.1.14",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz",
"integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=",
"requires": {
"core-util-is": "1.0.2",
"inherits": "2.0.3",
"isarray": "0.0.1",
"string_decoder": "0.10.31"
}
},
"safe-buffer": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz",
"integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg=="
},
"send": {
"version": "0.16.1",
"resolved": "https://registry.npmjs.org/send/-/send-0.16.1.tgz",
"integrity": "sha512-ElCLJdJIKPk6ux/Hocwhk7NFHpI3pVm/IZOYWqUmoxcgeyM+MpxHHKhb8QmlJDX1pU6WrgaHBkVNm73Sv7uc2A==",
"requires": {
"debug": "2.6.9",
"depd": "1.1.1",
"destroy": "1.0.4",
"encodeurl": "1.0.1",
"escape-html": "1.0.3",
"etag": "1.8.1",
"fresh": "0.5.2",
"http-errors": "1.6.2",
"mime": "1.4.1",
"ms": "2.0.0",
"on-finished": "2.3.0",
"range-parser": "1.2.0",
"statuses": "1.3.1"
}
},
"serve-static": {
"version": "1.13.1",
"resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.13.1.tgz",
"integrity": "sha512-hSMUZrsPa/I09VYFJwa627JJkNs0NrfL1Uzuup+GqHfToR2KcsXFymXSV90hoyw3M+msjFuQly+YzIH/q0MGlQ==",
"requires": {
"encodeurl": "1.0.1",
"escape-html": "1.0.3",
"parseurl": "1.3.2",
"send": "0.16.1"
}
},
"setprototypeof": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.0.tgz",
"integrity": "sha512-BvE/TwpZX4FXExxOxZyRGQQv651MSwmWKZGqvmPcRIjDqWub67kTKuIMx43cZZrS/cBBzwBcNDWoFxt2XEFIpQ=="
},
"statuses": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.3.1.tgz",
"integrity": "sha1-+vUbnrdKrvOzrPStX2Gr8ky3uT4="
},
"streamsearch": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz",
"integrity": "sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo="
},
"string_decoder": {
"version": "0.10.31",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz",
"integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ="
},
"type-is": {
"version": "1.6.15",
"resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.15.tgz",
"integrity": "sha1-yrEPtJCeRByChC6v4a1kbIGARBA=",
"requires": {
"media-typer": "0.3.0",
"mime-types": "2.1.17"
}
},
"typedarray": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz",
"integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c="
},
"unpipe": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
"integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw="
},
"util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8="
},
"utils-merge": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
"integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
},
"vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
"integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw="
},
"xtend": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz",
"integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68="
}
}
}

232
Node/test/demos2-booth.js Executable file
View file

@ -0,0 +1,232 @@
/*
Cryptography functions written by Bingsheng Zhang
Uses the milagro-crypto-js library at:
https://github.com/milagro-crypto/milagro-crypto-js
*/
//Group parameter generator: returns rng object and generators g1,g2 for G1,G2 as well as order
gpGen = function(){
//init, and base generators
var ctx = new CTX("BN254CX");
var n=new ctx.BIG(0); n.rcopy(ctx.ROM_CURVE.CURVE_Order);
//get generator P for G1
P = new ctx.ECP(0);
gx = new ctx.BIG(0);
gx.rcopy(ctx.ROM_CURVE.CURVE_Gx);
if (ctx.ECP.CURVETYPE != ctx.ECP.MONTGOMERY) {
gy = new ctx.BIG(0);
gy.rcopy(ctx.ROM_CURVE.CURVE_Gy);
P.setxy(gx, gy);
} else P.setx(gx);
//get generator Q for G2
var A=new ctx.BIG(0);
var B=new ctx.BIG(0);
A.rcopy(ctx.ROM_CURVE.CURVE_Pxa);
B.rcopy(ctx.ROM_CURVE.CURVE_Pxb);
var Qx=new ctx.FP2(0); Qx.bset(A,B);
A.rcopy(ctx.ROM_CURVE.CURVE_Pya);
B.rcopy(ctx.ROM_CURVE.CURVE_Pyb);
var Qy=new ctx.FP2(0); Qy.bset(A,B);
var Q=new ctx.ECP2();
Q.setxy(Qy,Qy);
return{
n:n,
g1:P,
g2:Q
}
}
//creates ElGamal public and secret key
keyGen=function(params){
var ctx = new CTX("BN254CX");
//set rng
var RAW = [];
var d = new Date();//time for seed, not secure
var rng = new ctx.RAND();
rng.clean();
RAW[0] = d.getSeconds();
RAW[1] = d.getMinutes();
RAW[2] = d.getMilliseconds();
rng.seed(3, RAW);
//ElGamal
var sk = new ctx.BIG(0);
sk = ctx.BIG.randomnum(params.n,rng);
var pk = new ctx.ECP(0);
pk = ctx.PAIR.G1mul(params.g1,sk);
return{
PK:pk,
SK:sk
}
}
//combine multiple public key together
//the input is an array of PKs
combine=function(PKs){
var ctx = new CTX("BN254CX");
var pk=new ctx.ECP();
//copy the first pk
pk.copy(PKs[0]);
//multiple the rest PKs
for(i=1;i<PKs.length;i++){
pk.add(PKs[i]);
}
return{
PK:pk
}
}
//ElGamal encryption
encrypt=function(params,PK, m){
var ctx = new CTX("BN254CX");
//set rand
var RAW = [];
var d = new Date();//time for seed, not secure
var rng = new ctx.RAND();
rng.clean();
RAW[0] = d.getSeconds();
RAW[1] = d.getMinutes();
RAW[2] = d.getMilliseconds();
rng.seed(3, RAW);
var r=new ctx.BIG.randomnum(params.n,rng);
var M=new ctx.BIG(m);
var C1=new ctx.ECP();
C1 = ctx.PAIR.G1mul(params.g1,r);
var gM=new ctx.ECP();
gM = ctx.PAIR.G1mul(params.g1,M);
var C2=new ctx.ECP();
C2 = ctx.PAIR.G1mul(PK,r);
C2.mul(r);
C2.add(gM);
return{
C1:C1,
C2:C2
}
}
//add ciphertexts
add=function(Ciphers){
var ctx = new CTX("BN254CX");
var s1=new ctx.ECP();
var s2=new ctx.ECP();
//copy the first cipher
s1.copy(Ciphers[0].C1);
s2.copy(Ciphers[0].C2);
//multiple the rest ciphertexts
for(i=1;i<Ciphers.length;i++){
s1.add(Ciphers[i].C1);
}
//no idea why I need two loops
for(j=1;j<Ciphers.length;j++){
s2.add(Ciphers[j].C2);
}
return{
C1:s1,
C2:s2
}
}
//ElGamal decryption
decrypt=function(params,SK, C){
var ctx = new CTX("BN254CX");
var D=new ctx.ECP();
D = ctx.PAIR.G1mul(C.C1,SK);
var gM=new ctx.ECP();
gM.copy(C.C2);
gM.sub(D);
//search for message by brute force
var B;
for (j = 0; j < 1000; j++) {
//use D as temp var
B = new ctx.BIG(j);
D = ctx.PAIR.G1mul(params.g1,B);
if (D.equals(gM))
return{
M:j
}
};
return{
M: "Error"
}
}
//ElGamal partial decryption
partDec=function(SK, C){
var ctx = new CTX("BN254CX");
var D=new ctx.ECP();
D = ctx.PAIR.G1mul(C.C1,SK);
return{
D: D
}
}
//Tally, combine partial decryption
//Ds is the array of partial decryptions; C is the ciphertext.
tally=function(params,Ds, C){
var ctx = new CTX("BN254CX");
var D=new ctx.ECP();
D.copy(Ds[0].D);
//combine D
for(i=1;i<Ds.length;i++){
D.add(Ds[i].D);
}
var gM=new ctx.ECP();
gM.copy(C.C2);
gM.sub(D);
//search for message by brute force
var B;
for (j = 0; j < 1000; j++) {
//use D as temp var
B = new ctx.BIG(j);
D = ctx.PAIR.G1mul(params.g1,B);
if (D.equals(gM))
return{
M:j
}
};
return{
M: "Error"
}
}

288
Node/test/test_webserver.html Executable file
View file

@ -0,0 +1,288 @@
<!DOCTYPE html>
<html>
<head>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- Bootstrap core JavaScript -->
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
</head>
<body>
<!-- Crypto JS -->
<script type="text/javascript" src=./core/rand.js></script>
<script type="text/javascript" src=./core/rom_curve.js></script>
<script type="text/javascript" src=./core/rom_field.js></script>
<script type="text/javascript" src=./core/uint64.js></script>
<script type="text/javascript" src=./core/aes.js></script>
<script type="text/javascript" src=./core/big.js></script>
<script type="text/javascript" src=./core/gcm.js></script>
<script type="text/javascript" src=./core/hash256.js></script>
<script type="text/javascript" src=./core/hash384.js></script>
<script type="text/javascript" src=./core/hash512.js></script>
<script type="text/javascript" src=./core/sha3.js></script>
<script type="text/javascript" src=./core/newhope.js></script>
<script type="text/javascript" src=./core/nhs.js></script>
<script type="text/javascript" src=./core/fp.js></script>
<script type="text/javascript" src=./core/fp2.js></script>
<script type="text/javascript" src=./core/fp4.js></script>
<script type="text/javascript" src=./core/fp12.js></script>
<script type="text/javascript" src=./core/ff.js></script>
<script type="text/javascript" src=./core/rsa.js></script>
<script type="text/javascript" src=./core/ecp.js></script>
<script type="text/javascript" src=./core/ecp2.js></script>
<script type="text/javascript" src=./core/ecdh.js></script>
<script type="text/javascript" src=./core/pair.js></script>
<script type="text/javascript" src=./core/mpin.js></script>
<script type="text/javascript" src=./core/ctx.js></script>
<script type="text/javascript" src="demos2-booth.js"></script>
<h1>DEMOS2 Node.js Server testing page</h1>
<button type="button" id="gpGen" class="btn">gpGen</button>
<p id="gpGenLocal">GPGen Local results</p>
<p id="gpGenServer">GPGen Server results</p>
<button type="button" id="addec" class="btn">addec</button>
<p id="addecLocal">addec Local results</p>
<p id="addecServer">addec Server results</p>
<p id="addecResult">addec comparison results</p>
<button type="button" id="combpk" class="btn">combpk</button>
<p id="combpkLocal">combpk Local results</p>
<p id="combpkServer">combpk Server results</p>
<p id="combpkResult">compk comparison results</p>
<button type="button" id="combpkOne" class="btn">combpk (one key)</button>
<p id="combpkLocalOne">combpk Local results</p>
<p id="combpkServerOne">combpk Server results</p>
<p id="combpkResultOne">compk comparison results</p>
<button type="button" id="tally" class="btn">tally</button>
<p id="tallyLocal">tally Local results</p>
<p id="tallyServer">tally Server results</p>
<p id="tallyResult">compk comparison results</p>
<script>
$(document).ready(function(){
$("#gpGen").click(function(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("gpGenServer").innerHTML = this.responseText;
var params = gpGen();
document.getElementById("gpGenLocal").innerHTML = JSON.stringify(params);
}
};
xhttp.open("GET", "/param", true);
xhttp.send();
})
var params = gpGen();
var keys = keyGen(params);
//encrypt number 5
var cipher = encrypt(params,keys.PK, 5);
//test decrypt
var message = decrypt(params, keys.SK, cipher);
$("#addec").click(function(){
var c1 = encrypt(params,keys.PK, 3);
var c2 = encrypt(params,keys.PK, 4);
var cArray = new Array(c1,c2);
var bytes = [];
var queryparams = "?number=2"
queryparams += "&C1=";
c1.C1.toBytes(bytes);
queryparams += bytes.toString();
queryparams += "&C2=";
c1.C2.toBytes(bytes);
queryparams += bytes.toString();
queryparams += '&C1=';
c2.C1.toBytes(bytes);
queryparams += bytes.toString();
queryparams += '&C2=';
c2.C2.toBytes(bytes);
queryparams += bytes.toString();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var localAdd = add(cArray);
var localM = decrypt(params, keys.SK, localAdd);
console.log(localM);
document.getElementById("addecLocal").innerHTML = JSON.stringify(localAdd);
console.log("Request: /addec"+queryparams);
document.getElementById("addecServer").innerHTML = this.responseText;
//build object from server
var temp = JSON.parse(this.responseText);
//the values need to be copied in otherwise decryption doesn't work
var ctx = new CTX("BN254CX");
var s1=new ctx.ECP();
var s2=new ctx.ECP();
//copy the first cipher
s1.copy(temp.C1);
s2.copy(temp.C2);
var serverAdd = {
C1:s1,
C2:s2
}
var serverM = decrypt(params, keys.SK, serverAdd);
console.log(serverM);
document.getElementById("addecResult").innerHTML = "Local results: " + localM.M + ", Server results: " + serverM.M + "\n JSON string match: " + (JSON.stringify(localAdd) == this.responseText);
}
};
xhttp.open("GET", "/addec"+queryparams, true);
xhttp.send();
})
$("#combpk").click(function(){
var k1 = keyGen(params);
var k2 = keyGen(params);
var bytes = [];
var queryparams = "?number=2";
queryparams += "&PK=";
k1.PK.toBytes(bytes);
queryparams += bytes.toString();
queryparams += "&PK=";
k2.PK.toBytes(bytes);
queryparams += bytes.toString();
console.log(queryparams);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//test combine key
var PKs = new Array(k1.PK,k2.PK);
var pk = combine(PKs);
document.getElementById("combpkServer").innerHTML = this.responseText;
document.getElementById("combpkLocal").innerHTML = JSON.stringify(pk);
var match = (this.responseText == JSON.stringify(pk));
document.getElementById("combpkResult").innerHTML = "Matching strings: " + match;
}
};
xhttp.open("GET", "/cmpkstring"+queryparams, true);
xhttp.send();
})
$("#combpkOne").click(function(){
var k1 = keyGen(params);
var bytes = [];
var queryparams = "?number=1";
queryparams += "&PK=";
k1.PK.toBytes(bytes);
queryparams += bytes.toString();
console.log(queryparams);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//test combine key
var PKs = new Array(k1.PK);
var pk = combine(PKs);
document.getElementById("combpkServerOne").innerHTML = this.responseText;
document.getElementById("combpkLocalOne").innerHTML = JSON.stringify(pk);
var match = (this.responseText == JSON.stringify(pk));
document.getElementById("combpkResultOne").innerHTML = "Matching strings: " + match;
}
};
xhttp.open("GET", "/cmpkstring"+queryparams, true);
xhttp.send();
})
$("#tally").click(function(){
//combine some keys first to test tally
var k1 = keyGen(params);
var k2 = keyGen(params);
var PKs = new Array(k1.PK,k2.PK);
var pk = combine(PKs);
var Tc = encrypt(params,pk.PK, 3);
var D1 = partDec(k1.SK, Tc);
var D2 = partDec(k2.SK, Tc);
var Ds = new Array(D1,D2);
var queryparams = "?number=2";
queryparams += "&param=";//JSON format
queryparams += JSON.stringify(params);
queryparams +="&decs=";//will be in byte array format
var bytes = [];
D1.D.toBytes(bytes);
queryparams += bytes.toString();
queryparams +="&decs=";
D2.D.toBytes(bytes);
queryparams += bytes.toString();
queryparams +="&cipher=";//JSON, since it came from the server before
queryparams += JSON.stringify(Tc);
console.log(queryparams);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//test tally results
var tar = tally(params, Ds, Tc);
document.getElementById("tallyServer").innerHTML = this.responseText;
document.getElementById("tallyLocal").innerHTML = JSON.stringify(tar);
var match = (this.responseText == JSON.stringify(tar));
document.getElementById("tallyResult").innerHTML = "Matching strings: " + match;
}
};
xhttp.open("GET", "/tally"+queryparams, true);
xhttp.send();
})
});
</script>
</body>
</html>