Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generate randomness #9

Closed
wants to merge 11 commits into from
25 changes: 21 additions & 4 deletions contribution/contribution.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
const bls = require('@noble/curves/bls12-381');
const crypto = require('crypto');
const os = require('os');

function generateRandom(){
const [seconds, nanoseconds] = process.hrtime();
const seed = os.hostname() + os.freemem() + seconds + nanoseconds;

const hash = crypto.createHash('keccak256');
hash.update(seed);
const seedHash = hash.digest();
const seedInt = seedHash.readInt32LE();

randomBytes = crypto.randomBytes(32);
const randomInt = (parseInt(randomBytes.toString('hex'), 16) + seedInt);
const randomBigInt = BigInt(randomInt);
return randomBigInt % Fr.ORDER;
}

// TODO: Confirm by initialContribution.json
// TODO: Support multi-thread?
Expand All @@ -18,12 +35,11 @@ function contribute(contributions, rand) {
const g1Powers = contributions[i].powersOfTau.G1Powers;
const g2Powers = contributions[i].powersOfTau.G2Powers;

var xi = 1n;
for(var j = 0; j < contributions[i].numG1Powers; j++) {
const g1Affine = g1Powers[j];
const g1PrjPoint = G1Point.fromAffine(g1Affine);

const g1NewPrjPoint = g1PrjPoint.multiply(xi);
const g1NewPrjPoint = g1PrjPoint.multiply(rand[i]);
const g1NewAffine = g1NewPrjPoint.toAffine();

contributions[i].powersOfTau.G1Powers[j] = g1NewAffine;
Expand All @@ -32,7 +48,7 @@ function contribute(contributions, rand) {
const g2Affine = g2Powers[j];
const g2PrjPoint = G2Point.fromAffine(g2Affine);

const g2NewPrjPoint = g2PrjPoint.multiply(xi);
const g2NewPrjPoint = g2PrjPoint.multiply(rand[i]);
const g2NewAffine = g2NewPrjPoint.toAffine();

contributions[i].powersOfTau.G2Powers[j] = g2NewAffine;
Expand Down Expand Up @@ -62,7 +78,7 @@ function updateWitness(contributions, rand) {
console.log(potPubkeyAffine);

const potPubkeyPrj = G2Point.fromAffine(potPubkeyAffine);
const newPubkeyPrj = potPubkeyPrj.multiply(rand)
const newPubkeyPrj = potPubkeyPrj.multiply(rand[i])

const newPotPubkey = util.bytesToHex(G2.toBytes(G2Point, newPubkeyPrj, true));
console.log('New PotPubkey', newPotPubkey);
Expand All @@ -76,4 +92,5 @@ function updateWitness(contributions, rand) {
module.exports = {
contribute: contribute,
updateWitness: updateWitness,
generateRandom: generateRandom,
};
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ program
console.log('Decoding...');
contributions = conversion.decode(resp.contributions);

const randValue = Math.floor(Math.random() * 100000); // TODO:
var rand = [];
for (var i =0; i < contributions.length; i++){
rand[i] = contribute.generateRandom();
}

console.log('Update Power of Tau...');
var newContributions = contribute.contribute(contributions, BigInt(randValue));
var newContributions = contribute.contribute(contributions, rand);

console.log('Update Witnesses...');
newContributions = contribute.updateWitness(newContributions, BigInt(randValue));
newContributions = contribute.updateWitness(newContributions, rand);

rand.length = 0;

console.log('Encoding...');
newContributions = conversion.encode(newContributions);
Expand Down