Commit f39fb982 authored by Bijun Li's avatar Bijun Li
Browse files

Split createSecret function

parent 194102ad
Showing with 49 additions and 27 deletions
+49 -27
/**
* creates a secret to share, an array of shares to share and a verification vector
* creates a secret to share and a verification vector
* @param {Object} bls - an instance of [bls-wasm](https://github.com/herumi/bls-wasm)
* @param {Number} numOfShares - the number of share to create
* @param {Number} threshold - the number of share needed to recover the secret
* @returns {Object} the return value contains `verificationVector`, an array of `shares` and a random `secret`
* @returns {Object} the return value includes `secretVector` that contains the secret, `verificationVector` and the secret
*/
exports.createShare = function (bls, numOfShares, threshold) {
// import secret
const svec = []
const vvec = []
const idVec = []
const secVec = []
const shares = []
exports.createSecret = function (bls, threshold) {
const sVec = []
const vVec = []
// setup master secret key
for (let i = 0; i < threshold; i++) {
const sk = new bls.SecretKey()
sk.setByCSPRNG()
svec.push(sk)
sVec.push(sk)
const pk = sk.getPublicKey()
vvec.push(pk)
vVec.push(pk)
}
const results = {
verificationVector: vVec.map(pk => pk.serialize()),
secretVector: sVec.map(sk => sk.serialize()),
secret: sVec[0].serialize()
}
return results
}
/**
* creates a secret to share, an array of shares to share and a verification vector
* @param {Object} bls - an instance of [bls-wasm](https://github.com/herumi/bls-wasm)
* @param {Array} sVec - `secretVector` to generate shares
* @param {Number} numOfShares - the number of share to create
* @returns {Object} the return value contains an array of `shares`
*/
exports.createShare = function (bls, sVec, numOfShares) {
const nsVec = []
const idVec = []
const secVec = []
const shares = []
sVec.forEach(s => {
const sk = new bls.SecretKey()
sk.deserialize(s)
nsVec.push(sk)
})
// generate key shares
for (let i = 0; i < numOfShares; i++) {
const id = new bls.Id()
......@@ -31,7 +52,7 @@ exports.createShare = function (bls, numOfShares, threshold) {
idVec.push(id)
const sk = new bls.SecretKey()
sk.share(svec, idVec[i])
sk.share(nsVec, idVec[i])
secVec.push(sk)
shares.push({
......@@ -41,9 +62,7 @@ exports.createShare = function (bls, numOfShares, threshold) {
}
const results = {
verificationVector: vvec.map(pk => pk.serialize()),
shares: shares,
secret: svec[0].serialize()
shares: shares
}
return results
}
......
......@@ -8,34 +8,36 @@ const curveTest = (curveType, name) => {
console.log((`name=${name} curve order=${bls.getCurveOrder()}`))
const threshold = 4
const numOfPlayers = 7
const setup = vss.createShare(bls, numOfPlayers, threshold)
const result = vss.createSecret(bls, threshold)
const sec = new bls.SecretKey()
sec.deserialize(setup.secret)
sec.deserialize(result.secret)
console.log('secret:', sec.serializeToHexStr())
const setup = vss.createShare(bls, result.secretVector, numOfPlayers)
setup.shares.forEach(share => {
const verified = vss.verifyShare(bls, share, setup.verificationVector)
const verified = vss.verifyShare(bls, share, result.verificationVector)
assert.strict.deepEqual(verified, true, 'should verify share')
})
var secret = vss.recoverSecret(bls, setup.shares.slice(0, threshold))
var reSecret = vss.recoverSecret(bls, setup.shares.slice(0, threshold))
const sec1 = new bls.SecretKey()
sec1.deserialize(secret)
sec1.deserialize(reSecret)
console.log('recovered:', sec1.serializeToHexStr())
assert.strict.deepEqual(secret, setup.secret, 'should recover the secret')
assert.strict.deepEqual(reSecret, result.secret, 'should recover the secret')
const renewal = vss.renewShare(bls, setup.shares, threshold, setup.verificationVector)
const renewal = vss.renewShare(bls, setup.shares, threshold, result.verificationVector)
renewal.shares.forEach(share => {
const verified = vss.verifyShare(bls, share, renewal.verificationVector)
assert.strict.deepEqual(verified, true, 'should verify new share')
})
secret = vss.recoverSecret(bls, renewal.shares.slice(0, threshold))
reSecret = vss.recoverSecret(bls, renewal.shares.slice(0, threshold))
const sec2 = new bls.SecretKey()
sec2.deserialize(secret)
sec2.deserialize(reSecret)
console.log('recovered:', sec2.serializeToHexStr())
assert.strict.deepEqual(secret, setup.secret, 'secret should not change after share renewal')
assert.strict.deepEqual(reSecret, result.secret, 'secret should not change after share renewal')
} catch (e) {
console.log(`TEST FAIL ${e}`)
assert(false)
......@@ -45,6 +47,7 @@ const curveTest = (curveType, name) => {
async function curveTestAll () {
await curveTest(bls.BN254, 'BN254')
await curveTest(bls.BLS12_381, 'BLS12_381')
}
curveTestAll()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment