Skip to content
Open
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
7 changes: 6 additions & 1 deletion src/functions/shamir_secret_sharing.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ module.exports = function (S) {


S.__split_secret = (threshold, total, secret, indexBits=8) => {
if ((typeof threshold !== "number") || (threshold % 1 !== 0) ||
(typeof total !== "number") || (total % 1 !== 0))
throw new TypeError("threshold and total should be integers");
if (threshold < 2) throw new Error("threshold must be at least 2");
if (total < 2) throw new Error("total must be at least 2");
if (threshold > 255) throw new Error("threshold limit 255");
if (total > 255) throw new Error("total limit 255");
let index_mask = 2**indexBits - 1;
Expand Down Expand Up @@ -167,4 +172,4 @@ module.exports = function (S) {
};

S.__precompute_GF256_expLog(S);
};
};
19 changes: 17 additions & 2 deletions test/jsbgl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ describe(`${(browser) ? 'Browser' : 'Node'} test jsbgl library`, function () {
s = splitMnemonic(2, 255, m, {embeddedIndex: true});
equal(m, combineMnemonic(s));

s = splitMnemonic(1, 255, m, {embeddedIndex: true});
equal(m, combineMnemonic(s));
assert.throws(() => splitMnemonic(1, 255, m, {embeddedIndex: true}),
/threshold must be at least 2/);

m = "stage amused wasp estate tomorrow outer satoshi version verb pudding ghost slender";
s = splitMnemonic(3, 8, m, {embeddedIndex: true});
Expand Down Expand Up @@ -589,6 +589,21 @@ describe(`${(browser) ? 'Browser' : 'Node'} test jsbgl library`, function () {

}
}).timeout(14000);

it('Reject invalid secret split parameters', () => {
let secret = Buffer.from("secret", 'utf8');

assert.throws(() => __split_secret(1, 2, secret),
/threshold must be at least 2/);
assert.throws(() => __split_secret(0, 2, secret),
/threshold must be at least 2/);
assert.throws(() => __split_secret(2, 1, secret),
/total must be at least 2/);
assert.throws(() => __split_secret(2.5, 3, secret),
/threshold and total should be integers/);
assert.throws(() => __split_secret(2, 3.5, secret),
/threshold and total should be integers/);
});
});

describe("Private/Public key functions:", function () {
Expand Down