-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
166 lines (139 loc) · 4.47 KB
/
Copy pathindex.js
File metadata and controls
166 lines (139 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* creates a secert to share, an array of shares to share and a verifaction vector
* @param {Object} bls - an instance of [bls-lib](https://github.com/wanderer/bls-lib)
* @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 `verifcationVector`, and array of `shares` and a random `secert`
*/
exports.createShare = function (bls, numOfShares, threshold) {
// import secret
// generate a sk and vvec
const svec = []
const vvec = []
const shares = []
for (let i = 0; i < threshold; i++) {
const sk = bls.secretKey()
bls.secretKeySetByCSPRNG(sk)
svec.push(sk)
const pk = bls.publicKey()
bls.getPublicKey(pk, sk)
vvec.push(pk)
}
// generate key shares
for (let i = 1; i < numOfShares + 1; i++) {
const sk = bls.secretKey()
const id = bls.idImportFromInt(i)
bls.secretKeyShare(sk, svec, id)
shares.push({
sk: bls.secretKeyExport(sk),
id: i
})
bls.free(id)
bls.free(sk)
}
const results = {
verifcationVector: vvec.map(pk => bls.publicKeyExport(pk)),
shares: shares,
secret: bls.secretKeyExport(svec[0])
}
// clean up!
bls.freeArray(vvec)
bls.freeArray(svec)
return results
}
/**
* renew shares and verification vector, while keeping the secret
* @param {Object} bls - an instance of [bls-lib](https://github.com/wanderer/bls-lib)
* @param {Number} shares - the array of shares to be renewed (it is not possible to renew only some shares ; all must be). This array will not be modified, and new shares appear in the return object.
* @param {Number} threshold - the number of share needed to recover the secret
* @returns {Object} the return value contains the new `verifcationVector`, an array containing the new `shares`, and the `secret` (which has not changed in respect to the original shares)
*/
exports.renewShare = function (bls, shares, threshold, oldVvec) {
// import secret
// generate a sk and vvec
const svec = []
const vvec = []
const zeroArray = Buffer.alloc(32)
const zeroSK = bls.secretKeyImport(zeroArray)
svec.push(zeroSK)
const zeroPK = bls.publicKey()
bls.getPublicKey(zeroPK, zeroSK)
vvec.push(zeroPK)
for (let i = 1; i < threshold; i++) {
const sk = bls.secretKey()
bls.secretKeySetByCSPRNG(sk)
svec.push(sk)
const pk = bls.publicKey()
bls.getPublicKey(pk, sk)
vvec.push(pk)
}
// generate key shares
const newShares = []
shares.forEach(share => {
var sk = bls.secretKey()
const id = bls.idImportFromInt(share.id)
bls.secretKeyShare(sk, svec, id)
const originalShare = bls.secretKeyImport(share.sk)
bls.secretKeyAdd(sk, originalShare)
bls.free(originalShare)
newShares.push({
sk: bls.secretKeyExport(sk),
id: share.id
})
bls.free(id)
bls.free(sk)
})
const newVvec = oldVvec.map((pk, i) => {
pk = bls.publicKeyImport(pk)
bls.publicKeyAdd(pk, vvec[i])
return pk
})
const results = {
verifcationVector: newVvec.map(pk => bls.publicKeyExport(pk)),
shares: newShares,
secret: bls.secretKeyExport(svec[0])
}
// clean up!
bls.freeArray(vvec)
bls.freeArray(newVvec)
bls.freeArray(svec)
return results
}
/**
* verifys a share again a verifcation vector
* @param {Object} bls - an instance of [bls-lib](https://github.com/wanderer/bls-lib)
* @param {Object} share - a share
* @param {Array} vvec - the verifcation vector
* @return {Boolean}
*/
exports.verifyShare = function (bls, share, vvec) {
const pk1 = bls.publicKey()
const sk = bls.secretKeyImport(share.sk)
vvec = vvec.map(pk => bls.publicKeyImport(pk))
const id = bls.idImportFromInt(share.id)
bls.publicKeyShare(pk1, vvec, id)
const pk2 = bls.publicKey()
bls.getPublicKey(pk2, sk)
const isEqual = bls.publicKeyIsEqual(pk1, pk2)
bls.freeArray(vvec)
bls.free(pk1)
bls.free(pk2)
bls.free(id)
return Boolean(isEqual)
}
/**
* verifys a share again a verifcation vector
* @param {Array} shares - an array of unque shares containing threshold number of shares
* @returns {Uint8Array} - the recovered secret
*/
exports.recoverSecret = function (bls, shares) {
const sk = bls.secretKey()
const sks = shares.map(s => bls.secretKeyImport(s.sk))
const ids = shares.map(s => bls.idImportFromInt(s.id))
bls.secretKeyRecover(sk, sks, ids)
const result = bls.secretKeyExport(sk)
bls.free(sk)
bls.freeArray(sks)
bls.freeArray(ids)
return result
}