From f39fb9829f2d3728b0f673be8e0d73b820d268b1 Mon Sep 17 00:00:00 2001
From: Bijun Li <bijun.li@polis.global>
Date: Mon, 21 Feb 2022 22:59:29 +0100
Subject: [PATCH] Split createSecret function

---
 src/index.js | 53 +++++++++++++++++++++++++++++++++++-----------------
 test/vss.js  | 23 +++++++++++++----------
 2 files changed, 49 insertions(+), 27 deletions(-)

diff --git a/src/index.js b/src/index.js
index dbae406..4964f11 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,29 +1,50 @@
-
 /**
- * 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
 }
diff --git a/test/vss.js b/test/vss.js
index 43d3812..351ace1 100644
--- a/test/vss.js
+++ b/test/vss.js
@@ -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()
-- 
GitLab