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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions contribution/contribution.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
const bls = require('@noble/curves/bls12-381');
const crypto = require('crypto');
const os = require('os');
const Fr = bls.bls12_381.CURVE.Fr;

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

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

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

// TODO: Confirm by initialContribution.json
// TODO: Support multi-thread?
Expand All @@ -10,15 +28,13 @@ function contribute(contributions, rand) {

const G1 = bls.bls12_381.CURVE.G1;
const G2 = bls.bls12_381.CURVE.G2;
const Fr = bls.bls12_381.CURVE.Fr;

const util = bls.bls12_381.utils;

for(var i = 0; i < contributions.length; i++) {
const g1Powers = contributions[i].powersOfTau.G1Powers;
const g2Powers = contributions[i].powersOfTau.G2Powers;

var xi = 1n;
var xi = BigInt(1);
rootwarp marked this conversation as resolved.
Show resolved Hide resolved
for(var j = 0; j < contributions[i].numG1Powers; j++) {
const g1Affine = g1Powers[j];
const g1PrjPoint = G1Point.fromAffine(g1Affine);
Expand All @@ -37,7 +53,6 @@ function contribute(contributions, rand) {

contributions[i].powersOfTau.G2Powers[j] = g2NewAffine;
}

xi = (xi * rand) % Fr.ORDER;
}
};
Expand Down Expand Up @@ -76,4 +91,5 @@ function updateWitness(contributions, rand) {
module.exports = {
contribute: contribute,
updateWitness: updateWitness,
generateRandom: generateRandom,
};
14 changes: 9 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const fs = require('fs');
const contribute = require('./contribution/contribution.js');
const conversion = require('./contribution/coversion.js');
const seq = require('./sequencerclient/sequencerClient.js');

const bls = require('@noble/curves/bls12-381');
const Fr = bls.bls12_381.CURVE.Fr;

// const url = 'https://seq.ceremony.ethereum.org';
// const url = 'https://kzg-ceremony-sequencer-dev.fly.dev';
Expand Down Expand Up @@ -70,20 +71,23 @@ program
console.log(`Error ${resp.status}`);
return;
}

break;
}

console.log('Decoding...');
contributions = conversion.decode(resp.contributions);
//contributions = conversion.decode('{"contributions": [ '+resp.contributions+'}');

const randValue = Math.floor(Math.random() * 100000); // TODO:
rand = contribute.generateRandom();
rand = Fr.create(rand);
rootwarp marked this conversation as resolved.
Show resolved Hide resolved

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 = null;

console.log('Encoding...');
newContributions = conversion.encode(newContributions);
Expand Down
11 changes: 11 additions & 0 deletions test/bls.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const fs = require('fs');
const assert = require("assert");
const bls = require('@noble/curves/bls12-381');
const contribute = require('../contribution/contribution.js');

const Fr = bls.bls12_381.CURVE.Fr;
const util = bls.bls12_381.utils;
const G1 = bls.bls12_381.CURVE.G1;
const G2 = bls.bls12_381.CURVE.G2;
Expand Down Expand Up @@ -156,3 +158,12 @@ describe('power-of_tau', function() {
// def update_witness(contribution: Contribution, x: int) -> Contribution:
// contribution.pot_pubkey = bls.G2.mul(x, bls.G2.g2)
// return contribution

describe('Random', function() {
it('Generate Random', function(){
var random = contribute.generateRandom();
console.log("rand: "+random);
random = Fr.create(random);
console.log("Fr.create(rand): "+random);
});
});
Loading