Skip to content

Instantly share code, notes, and snippets.

@melnikaite
Created July 16, 2019 15:10
Show Gist options
  • Save melnikaite/200ed9e4f0f179ce7d53a611fd6b0d31 to your computer and use it in GitHub Desktop.
Save melnikaite/200ed9e4f0f179ce7d53a611fd6b0d31 to your computer and use it in GitHub Desktop.
const bls = require('bls-lib');
bls.onModuleInit(() => {
bls.init();
const numOfPlayers = 7;
const threshold = 3;
const msg = 'hello world';
console.log({numOfPlayers, threshold, msg});
// set up master key share
const masterSecretKey = [];
const masterPublicKey = [];
for (let i = 0; i < threshold; i++) {
const sk = bls.secretKey();
bls.secretKeySetByCSPRNG(sk);
masterSecretKey.push(sk);
const pk = bls.publicKey();
bls.getPublicKey(pk, sk);
masterPublicKey.push(pk);
}
// key sharing
const ids = [];
const secretKeys = [];
console.log('secret key shares:');
for (let i = 0; i < numOfPlayers; i++) {
const id = bls.secretKey();
bls.secretKeySetByCSPRNG(id);
ids.push(id);
const sk = bls.secretKey();
bls.secretKeyShare(sk, masterSecretKey, id);
secretKeys.push(sk);
console.log(`${Buffer.from(bls.secretKeyExport(id)).toString('hex')}: ${Buffer.from(bls.secretKeyExport(sk)).toString('hex')}`);
}
// signing
const subIds = [];
const subSigs = [];
console.log('signatures:');
for (let i = 0; i < threshold; i++) {
const id = numOfPlayers - i - 1;
subIds.push(ids[id]);
const sk = secretKeys[id];
const sig = bls.signature();
bls.sign(sig, sk, msg);
subSigs.push(sig);
console.log(`${Buffer.from(bls.secretKeyExport(ids[id])).toString('hex')}: ${Buffer.from(bls.signatureExport(sig)).toString('hex')}`);
}
// verify
const sig = bls.signature();
bls.signatureRecover(sig, subSigs, subIds);
const result = bls.verify(sig, masterPublicKey[0], msg);
console.log({verified: result});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment