2018-06-12 12:31:38 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
|
2018-07-07 08:52:47 +00:00
|
|
|
Code by Bingsheng Zhang, Thomas Smith, Vincent de Almeida
|
2018-06-12 12:31:38 +00:00
|
|
|
|
2018-07-07 08:52:47 +00:00
|
|
|
Dependencies can be found in 'package.json' and installed using 'npm install'
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
var port = 8080;
|
|
|
|
|
|
|
|
var Buffer = require('buffer').Buffer;
|
2018-09-04 14:21:31 +00:00
|
|
|
var atob = require("atob");
|
2018-07-07 08:52:47 +00:00
|
|
|
var CTX = require('milagro-crypto-js');
|
|
|
|
|
|
|
|
var express = require('express');
|
|
|
|
var bodyParser = require("body-parser");
|
2018-06-12 12:31:38 +00:00
|
|
|
var app = express();
|
|
|
|
|
2018-07-07 08:52:47 +00:00
|
|
|
// Express server configuration
|
2018-06-12 12:31:38 +00:00
|
|
|
app.use(express.static('test'));
|
2018-07-07 08:52:47 +00:00
|
|
|
app.use(bodyParser.urlencoded({ extended: false }));
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
|
2018-06-12 12:31:38 +00:00
|
|
|
//default test
|
|
|
|
app.get('/', function(request, response){
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
message: 'hello world',
|
|
|
|
value: 5
|
2018-07-07 08:52:47 +00:00
|
|
|
};
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
//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();
|
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
console.log('Generated Group Param');
|
2018-06-12 12:31:38 +00:00
|
|
|
response.json(param);
|
|
|
|
|
2018-07-07 08:52:47 +00:00
|
|
|
});
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
//combine public keys and return the full combined one - JSON Version
|
|
|
|
app.get('/combpk', function(request, response){
|
2018-07-07 08:52:47 +00:00
|
|
|
console.log('\nEndpoint /combpk called');
|
2018-06-12 12:31:38 +00:00
|
|
|
|
2018-07-07 08:52:47 +00:00
|
|
|
var partials = request.query['PK'];
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
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]));
|
|
|
|
}
|
|
|
|
|
2018-07-07 08:52:47 +00:00
|
|
|
var PK = combine_pks(parsed);
|
2018-06-12 12:31:38 +00:00
|
|
|
response.json(PK);
|
|
|
|
|
2018-07-07 08:52:47 +00:00
|
|
|
});
|
2018-06-12 12:31:38 +00:00
|
|
|
|
2018-09-04 14:21:31 +00:00
|
|
|
function getKeyBytes(key, byteArray) {
|
|
|
|
for(let i = 0; i < key.length; i += 4) {
|
|
|
|
let B64EncodedByte = key.substring(i, i + 4);
|
|
|
|
|
|
|
|
byteArray.push(atob(B64EncodedByte));
|
|
|
|
}
|
|
|
|
}
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
//byte array version
|
2018-07-07 08:52:47 +00:00
|
|
|
app.post('/cmpkstring', function(request, response){
|
|
|
|
console.log('\nEndpoint /cmpkstring called');
|
2018-06-12 12:31:38 +00:00
|
|
|
var ctx = new CTX("BN254CX");
|
|
|
|
|
2018-07-07 08:52:47 +00:00
|
|
|
var partials = request.body.PKs;
|
2018-06-12 12:31:38 +00:00
|
|
|
var parsed = [];
|
|
|
|
|
2018-07-07 08:52:47 +00:00
|
|
|
if(partials.length > 1)//if we're submitting more than one key
|
2018-06-12 12:31:38 +00:00
|
|
|
{
|
2018-07-07 08:52:47 +00:00
|
|
|
console.log('Combining ' + partials.length + " public keys into one...");
|
2018-09-04 14:21:31 +00:00
|
|
|
for (let i = partials.length - 1; i >= 0; i--) {
|
|
|
|
console.log('PK' + i + ': ' + partials[i]);
|
|
|
|
|
|
|
|
let rawBytes = [];
|
|
|
|
getKeyBytes(partials[i], rawBytes);
|
|
|
|
|
|
|
|
parsed.push(new ctx.ECP.fromBytes(Buffer.from(rawBytes, 'hex')));
|
2018-06-12 12:31:38 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-07 08:52:47 +00:00
|
|
|
else if(partials.length === 1)
|
2018-06-12 12:31:38 +00:00
|
|
|
{
|
2018-07-07 08:52:47 +00:00
|
|
|
console.log("Combining just one public key...");
|
2018-09-04 14:21:31 +00:00
|
|
|
let PKStr = partials[0];
|
|
|
|
console.log("PK: " + PKStr);
|
|
|
|
|
|
|
|
let rawBytes = [];
|
|
|
|
getKeyBytes(PKStr, rawBytes);
|
|
|
|
|
|
|
|
parsed.push(new ctx.ECP.fromBytes(Buffer.from(rawBytes, 'hex')));
|
2018-06-12 12:31:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 08:52:47 +00:00
|
|
|
response.json(combine_pks(parsed));
|
|
|
|
});
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
//addition function on homomorphically encrypted variables
|
|
|
|
//this may need some work, different method of serialisation maybe?
|
2018-07-11 13:25:36 +00:00
|
|
|
app.post('/add_ciphers', function(request, response){
|
|
|
|
console.log("\nEndpoint /add_ciphers called");
|
|
|
|
const C1s = request.body.ciphers.c1s;
|
|
|
|
const C2s = request.body.ciphers.c2s;
|
|
|
|
const CIPHER_COUNT = C1s.length;
|
2018-06-12 12:31:38 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
// Will store a list of parsed ciphers from the C1s and C2s arrays passed in
|
|
|
|
var parsedCiphers = [];
|
2018-06-12 12:31:38 +00:00
|
|
|
var ctx = new CTX("BN254CX");
|
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
if(CIPHER_COUNT > 1)
|
2018-06-12 12:31:38 +00:00
|
|
|
{
|
2018-07-11 13:25:36 +00:00
|
|
|
console.log("Combining " + CIPHER_COUNT + " ciphers");
|
|
|
|
|
|
|
|
for (var i = 0; i < CIPHER_COUNT; i++) {
|
|
|
|
|
|
|
|
var c1Bytes = Buffer.from(C1s[i].split(','), 'hex');
|
2018-06-12 12:31:38 +00:00
|
|
|
var newC1 = new ctx.ECP.fromBytes(c1Bytes);
|
2018-07-11 13:25:36 +00:00
|
|
|
|
|
|
|
var c2Bytes = Buffer.from(C2s[i].split(','), 'hex');
|
|
|
|
var newC2 = new ctx.ECP.fromBytes(c2Bytes);
|
2018-06-12 12:31:38 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
var cipher = {
|
|
|
|
C1 : newC1,
|
|
|
|
C2 : newC2
|
2018-06-12 12:31:38 +00:00
|
|
|
};
|
2018-07-11 13:25:36 +00:00
|
|
|
|
|
|
|
parsedCiphers.push(cipher);
|
2018-06-12 12:31:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
} else if(CIPHER_COUNT === 1) {
|
|
|
|
console.log("Combining only one cipher");
|
2018-06-12 12:31:38 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
var c1Bytes = Buffer.from(C1s[0].split(','), 'hex');
|
2018-06-12 12:31:38 +00:00
|
|
|
var newC1 = new ctx.ECP.fromBytes(c1Bytes);
|
2018-07-11 13:25:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
var c2Bytes = Buffer.from(C2s[0].split(','), 'hex');
|
2018-06-12 12:31:38 +00:00
|
|
|
var newC2 = new ctx.ECP.fromBytes(c2Bytes);
|
|
|
|
|
|
|
|
var cipher =
|
|
|
|
{
|
2018-07-11 13:25:36 +00:00
|
|
|
C1 : newC1,
|
|
|
|
C2 : newC2
|
2018-06-12 12:31:38 +00:00
|
|
|
};
|
2018-07-07 08:52:47 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
parsedCiphers.push(cipher);
|
2018-06-12 12:31:38 +00:00
|
|
|
}
|
2018-07-07 08:52:47 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
// Combine the ciphers here
|
|
|
|
var combinedCipher = add(parsedCiphers);
|
2018-07-07 08:52:47 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
// Get the byte string of the C1 and C2 part for transmission
|
|
|
|
var C1Bytes = [];
|
|
|
|
combinedCipher.C1.toBytes(C1Bytes);
|
2018-06-12 12:31:38 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
var C2Bytes = [];
|
|
|
|
combinedCipher.C2.toBytes(C2Bytes);
|
2018-06-12 12:31:38 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
var responseData = {
|
|
|
|
C1: C1Bytes.toString(),
|
|
|
|
C2: C2Bytes.toString()
|
2018-07-07 08:52:47 +00:00
|
|
|
};
|
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
response.json(responseData);
|
2018-07-07 08:52:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/get_tally', function(request, response){
|
2018-07-11 13:25:36 +00:00
|
|
|
console.log("\nEndpoint /get_tally called");
|
2018-06-12 12:31:38 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
// Extract the data from the request
|
|
|
|
const TEMP_PARAMS = JSON.parse(JSON.parse(request.body.param).crypto);
|
|
|
|
const BALLOT_CIPHER = request.body.ballot_cipher;
|
|
|
|
const PART_DECS = request.body.part_decs;
|
|
|
|
const VOTERS_COUNT = request.body.voters_count;
|
2018-06-12 12:31:38 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
// Re-build parameters
|
|
|
|
var ctx = new CTX("BN254CX");
|
2018-07-07 08:52:47 +00:00
|
|
|
var n = new ctx.BIG();
|
|
|
|
var g1 = new ctx.ECP();
|
|
|
|
var g2 = new ctx.ECP2();
|
|
|
|
|
|
|
|
n.copy(TEMP_PARAMS.n);
|
|
|
|
g1.copy(TEMP_PARAMS.g1);
|
|
|
|
g2.copy(TEMP_PARAMS.g2);
|
|
|
|
|
|
|
|
var params = {
|
2018-07-11 13:25:36 +00:00
|
|
|
n : n,
|
|
|
|
g1 : g1,
|
|
|
|
g2 : g2
|
2018-07-07 08:52:47 +00:00
|
|
|
};
|
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
// Initialise the ballot cipher
|
|
|
|
var c1Bytes = Buffer.from(BALLOT_CIPHER.C1.split(','), 'hex');
|
|
|
|
var newC1 = new ctx.ECP.fromBytes(c1Bytes);
|
2018-07-07 08:52:47 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
var c2Bytes = Buffer.from(BALLOT_CIPHER.C2.split(','), 'hex');
|
|
|
|
var newC2 = new ctx.ECP.fromBytes(c2Bytes);
|
2018-07-07 08:52:47 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
var cipher =
|
|
|
|
{
|
|
|
|
C1 : newC1,
|
|
|
|
C2 : newC2
|
|
|
|
};
|
2018-07-07 08:52:47 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
// Initialise all of the partial decryptions
|
|
|
|
var partials = [];
|
|
|
|
for(var i = 0; i < PART_DECS.length; i++)
|
|
|
|
{
|
|
|
|
var bytes = Buffer.from(PART_DECS[i].split(','), 'hex');
|
2018-07-07 08:52:47 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
var dec = {
|
|
|
|
D : new ctx.ECP.fromBytes(bytes)
|
|
|
|
};
|
2018-07-07 08:52:47 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
partials.push(dec);
|
|
|
|
}
|
2018-07-07 08:52:47 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
// Send the decrypted cipher value (vote tally for an option)
|
|
|
|
response.send("" + getCipherVal(params, partials, cipher, VOTERS_COUNT).M);
|
2018-07-07 08:52:47 +00:00
|
|
|
});
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
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
|
2018-07-11 13:25:36 +00:00
|
|
|
gpGen = function() {
|
2018-06-12 12:31:38 +00:00
|
|
|
//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
|
|
|
|
}
|
2018-07-07 08:52:47 +00:00
|
|
|
};
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
//creates ElGamal public and secret key
|
2018-07-11 13:25:36 +00:00
|
|
|
keyGen = function(params) {
|
2018-06-12 12:31:38 +00:00
|
|
|
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
|
|
|
|
}
|
2018-07-07 08:52:47 +00:00
|
|
|
};
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
//combine multiple public key together
|
|
|
|
//the input is an array of PKs
|
2018-07-11 13:25:36 +00:00
|
|
|
combine_pks = function(PKs) {
|
2018-06-12 12:31:38 +00:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2018-07-07 08:52:47 +00:00
|
|
|
return {
|
|
|
|
PK : pk
|
2018-06-12 12:31:38 +00:00
|
|
|
}
|
2018-07-07 08:52:47 +00:00
|
|
|
};
|
2018-06-12 12:31:38 +00:00
|
|
|
|
2018-07-07 08:52:47 +00:00
|
|
|
// Written by Vincent de Almeida: Combines multiple secret keys together
|
|
|
|
// The SKs in the SKs array should already have been initialised using 'new ctx.BIG.fromBytes()'
|
2018-07-11 13:25:36 +00:00
|
|
|
combine_sks = function(SKs) {
|
2018-07-07 08:52:47 +00:00
|
|
|
// 'add' the rest of the sks to the first
|
|
|
|
var sk = SKs[0];
|
|
|
|
|
|
|
|
for(var i = 1; i < SKs.length; i++) {
|
|
|
|
sk.add(SKs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
SK: sk
|
|
|
|
}
|
|
|
|
};
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
//ElGamal encryption
|
2018-07-11 13:25:36 +00:00
|
|
|
encrypt = function(params,PK, m) {
|
2018-06-12 12:31:38 +00:00
|
|
|
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
|
|
|
|
}
|
2018-07-07 08:52:47 +00:00
|
|
|
};
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
//add ciphertexts
|
2018-07-11 13:25:36 +00:00
|
|
|
add = function(Ciphers) {
|
2018-06-12 12:31:38 +00:00
|
|
|
var ctx = new CTX("BN254CX");
|
2018-07-11 13:25:36 +00:00
|
|
|
var s1 = new ctx.ECP();
|
|
|
|
var s2 = new ctx.ECP();
|
|
|
|
|
2018-06-12 12:31:38 +00:00
|
|
|
//copy the first cipher
|
|
|
|
s1.copy(Ciphers[0].C1);
|
|
|
|
s2.copy(Ciphers[0].C2);
|
2018-07-11 13:25:36 +00:00
|
|
|
|
2018-06-12 12:31:38 +00:00
|
|
|
//multiple the rest ciphertexts
|
2018-07-11 13:25:36 +00:00
|
|
|
for(var i = 1; i < Ciphers.length; i++){
|
2018-06-12 12:31:38 +00:00
|
|
|
s1.add(Ciphers[i].C1);
|
2018-07-11 13:25:36 +00:00
|
|
|
s2.add(Ciphers[i].C2);
|
2018-06-12 12:31:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
return {
|
|
|
|
C1 : s1,
|
|
|
|
C2 : s2
|
2018-06-12 12:31:38 +00:00
|
|
|
}
|
2018-07-07 08:52:47 +00:00
|
|
|
};
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
//ElGamal decryption
|
2018-07-11 13:25:36 +00:00
|
|
|
decrypt = function(params,SK, C, votersCount) {
|
2018-06-12 12:31:38 +00:00
|
|
|
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);
|
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
// Search for value based on the number of voters
|
2018-06-12 12:31:38 +00:00
|
|
|
var B;
|
2018-07-11 13:25:36 +00:00
|
|
|
for (var j = 0; j <= votersCount; j++) {
|
2018-06-12 12:31:38 +00:00
|
|
|
//use D as temp var
|
|
|
|
B = new ctx.BIG(j);
|
|
|
|
D = ctx.PAIR.G1mul(params.g1,B);
|
|
|
|
if (D.equals(gM))
|
|
|
|
return{
|
|
|
|
M:j
|
|
|
|
}
|
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
}
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
return{
|
|
|
|
M: "Error"
|
|
|
|
}
|
2018-07-07 08:52:47 +00:00
|
|
|
};
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
//ElGamal partial decryption
|
2018-07-11 13:25:36 +00:00
|
|
|
partDec = function(SK, C) {
|
2018-06-12 12:31:38 +00:00
|
|
|
var ctx = new CTX("BN254CX");
|
2018-07-11 13:25:36 +00:00
|
|
|
var D = new ctx.ECP();
|
2018-06-12 12:31:38 +00:00
|
|
|
D = ctx.PAIR.G1mul(C.C1,SK);
|
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
return {
|
2018-06-12 12:31:38 +00:00
|
|
|
D: D
|
|
|
|
}
|
2018-07-07 08:52:47 +00:00
|
|
|
};
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
// Combines partial decryptions to enable the decryption of a cipher text which will be an int val representing
|
|
|
|
// a tally of votes for an option. Ds is the array of partial decryptions; C is the ciphertext.
|
|
|
|
getCipherVal = function(params, Ds, C, votersCount) {
|
|
|
|
// Create a context and initialise the first decryption part
|
|
|
|
var ctx = new CTX("BN254CX");
|
|
|
|
var D = new ctx.ECP();
|
2018-06-12 12:31:38 +00:00
|
|
|
D.copy(Ds[0].D);
|
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
// Combine the decryptions (in Ds array) into a single decryption by adding them to D
|
|
|
|
for(var i = 1; i < Ds.length; i++){
|
2018-06-12 12:31:38 +00:00
|
|
|
D.add(Ds[i].D);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var gM=new ctx.ECP();
|
|
|
|
gM.copy(C.C2);
|
|
|
|
gM.sub(D);
|
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
// Search for the value based on the number of voters
|
2018-06-12 12:31:38 +00:00
|
|
|
var B;
|
2018-07-11 13:25:36 +00:00
|
|
|
for (var j = 0; j <= votersCount; j++) {
|
2018-06-12 12:31:38 +00:00
|
|
|
//use D as temp var
|
|
|
|
B = new ctx.BIG(j);
|
|
|
|
D = ctx.PAIR.G1mul(params.g1,B);
|
|
|
|
if (D.equals(gM))
|
|
|
|
return{
|
2018-07-07 08:52:47 +00:00
|
|
|
M: j
|
2018-06-12 12:31:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 08:52:47 +00:00
|
|
|
}
|
2018-06-12 12:31:38 +00:00
|
|
|
|
2018-07-11 13:25:36 +00:00
|
|
|
// If the search failed
|
2018-06-12 12:31:38 +00:00
|
|
|
return{
|
|
|
|
M: "Error"
|
|
|
|
}
|
2018-07-07 08:52:47 +00:00
|
|
|
};
|
2018-06-12 12:31:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|