Created
March 1, 2018 13:53
-
-
Save gnarula/1d4c8c7b66113dd5a7a765a667f47cb2 to your computer and use it in GitHub Desktop.
Evoting Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const kyber = require('@dedis/kyber-js') | |
const cothority = require('@dedis/cothority') | |
const config = require('./config') | |
const hexToArrayBuffer = require('hex-to-array-buffer') | |
const mockVotes = () => { | |
const promises = [] | |
for (let i = 0; i < voters.length; i++) { | |
const signature = generateSignature(voters[i]) | |
// login | |
promises.push(socket.send('Login', 'LoginReply', { | |
id: config.masterKey, | |
user: voters[i], | |
signature | |
}) | |
.then(reply => { | |
const { token, elections } = reply | |
const lastElection = elections[elections.length - 1] | |
const m = curve.point().embed(Uint8Array.from([228, 124, 4])) | |
const k = curve.scalar().one() | |
const K = curve.point().mul(k) | |
const key = curve.point() | |
key.unmarshalBinary(lastElection.key) | |
const S = curve.point().mul(k, key) | |
const C = S.add(S, m) | |
socket.send('Cast', 'CastReply', { | |
token, | |
id: lastElection.id, | |
ballot: { | |
user: voters[i], | |
alpha: K.marshalBinary(), | |
beta: C.marshalBinary() | |
} | |
}) | |
})) | |
} | |
return promises | |
} | |
const scipersToUint8Array = scipers => { | |
return Uint8Array.from([].concat(...scipers.map(sciper => { | |
const ret = [] | |
let tmp = parseInt(sciper) | |
for (let i = 0; i < 3; i++) { | |
ret.push(tmp & 0xff) | |
tmp = tmp >> 8 | |
} | |
return ret | |
}))) | |
} | |
const curve = new kyber.curve.edwards25519.Curve() | |
const { net } = cothority | |
const socket = new net.Socket('ws://127.0.0.1:7003', 'evoting') | |
const generateSignature = sciper => { | |
sciper = typeof sciper === 'string' ? sciper : sciper.toString() | |
const message = new Uint8Array(config.masterKey.length + sciper.length) | |
message.set(config.masterKey) | |
for (let i = 0; i < sciper.length; i++) { | |
message[i + config.masterKey.length] = sciper[i] - '0' | |
} | |
const suite = new kyber.curve.edwards25519.Curve() | |
const key = suite.scalar() | |
key.unmarshalBinary(new Uint8Array(hexToArrayBuffer(process.env.PRIVATE_KEY))) | |
return kyber.sign.schnorr.sign(suite, | |
key, | |
message | |
) | |
} | |
const voters = [294116, 294117, 294118] | |
const admin = 294116 | |
socket.send('Login', 'LoginReply', { | |
id: config.masterKey, | |
user: admin, | |
signature: generateSignature(admin) | |
}) | |
.then(reply => { | |
adminToken = reply.token | |
return socket.send('Open', 'OpenReply', { | |
token: adminToken, | |
id: config.masterKey, | |
election: { | |
name: 'foobar' + Math.floor(Math.random() * 100), | |
creator: admin, | |
users: voters, | |
data: scipersToUint8Array([294116, 111443]), | |
description: 'foobar', | |
end: '03/01/2018' | |
} | |
}) | |
}) | |
.then(() => { | |
return Promise.all(mockVotes()) | |
}) | |
.then(() => { | |
return socket.send('Login', 'LoginReply', { | |
id: config.masterKey, | |
user: admin, | |
signature: generateSignature(admin) | |
}) | |
}) | |
.then(loginReply => { | |
// shuffle | |
const { elections } = loginReply | |
const election = elections[elections.length - 1] | |
return socket.send('Shuffle', 'ShuffleReply', { | |
id: election.id, | |
token: adminToken | |
}) | |
.then(() => { | |
// decrypt | |
return socket.send('Decrypt', 'DecryptReply', { | |
id: election.id, | |
token: adminToken | |
}) | |
}) | |
.then(() => { | |
// reconstruct | |
return socket.send('Reconstruct', 'ReconstructReply', { | |
id: election.id, | |
token: adminToken | |
}) | |
}) | |
.then(data => { | |
console.log('data', data) | |
const { points } = data | |
for (let i = 0; i < points.length; i++) { | |
const point = curve.point() | |
point.unmarshalBinary(points[i]) | |
console.log(point.string()) | |
console.log('embedded data', point.data()) | |
} | |
}) | |
}) | |
.catch(e => { | |
console.error(e) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment