Skip to content

Commit

Permalink
fix: review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
guru-web3 committed Feb 20, 2024
1 parent 312e065 commit 19f276d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
13 changes: 8 additions & 5 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,17 +479,16 @@ class ThresholdKey implements ITKey {
return tssPolyCommits;
}

async getTSSPub(accountIndex?: number): Promise<Point> {
getTSSPub(accountIndex?: number): Point {
const tssCommits = this.getTSSCommits();
if (accountIndex && accountIndex > 0) {
const nonce = await this.computeAccountNonce(accountIndex);
const nonce = this.computeAccountNonce(accountIndex);
// we need to add the pub key nonce to the tssPub
const noncePub = ecCurve.keyFromPrivate(nonce.toString("hex")).getPublic();
const pubKeyPoint = ecCurve.keyFromPublic({ x: tssCommits[0].x.toString("hex"), y: tssCommits[0].y.toString("hex") }).getPublic();
const dervicepubKeyPoint = pubKeyPoint.add(noncePub);
return new Point(dervicepubKeyPoint.getX().toString("hex"), dervicepubKeyPoint.getY().toString("hex"));
}
return tssCommits[0];
}

/**
Expand Down Expand Up @@ -620,6 +619,11 @@ class ThresholdKey implements ITKey {
})
);
}

// assign account salt from tKey store if it exists
const accountSalt = await this.getTKeyStoreItem(TSS_MODULE, "accountSalt");
if (accountSalt && accountSalt?.value) this._accountSalt = accountSalt.value;

return { privKey, ...returnObject };
}

Expand Down Expand Up @@ -2001,9 +2005,8 @@ class ThresholdKey implements ITKey {
return Promise.all(Object.keys(this.modules).map((x) => this.modules[x].initialize()));
}

private async computeAccountNonce(index: number) {
private computeAccountNonce(index: number) {
// generation should occur during tkey.init, fails if accountSalt is absent
this._accountSalt = this._accountSalt || (await this.getTKeyStoreItem(TSS_MODULE, "accountSalt")).value;
if (!this._accountSalt) {
throw CoreError.accountSaltUndefined();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class CoreError extends TkeyError {
1103: "setMetadata errored",
1104: "previouslyFetchedCloudMetadata provided in initialization is outdated",
1105: "previouslyFetchedCloudMetadata.nonce should never be higher than the latestShareDetails, please contact support",
1106: "Account Salt is absent, required for nonce generation",
// tkeystore
1106: "Account Salt is absent, required for nonce generation.Make sure key is reconstructed",
// tKeystore
1201: "Invalid tkeyStore",
1202: "Encryption failed",
1203: "Decryption failed",
Expand Down
22 changes: 11 additions & 11 deletions packages/default/test/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => {
// accountSalt is absent, required for nonce generation
// can be only initialize with tkey.initialize();
rejects(async () => {
await tb1.computeAccountNonce(1);
tb1.computeAccountNonce(1);
});
// factor key needs to passed from outside of tKey
const factorKey = new BN(generatePrivate());
Expand All @@ -111,12 +111,12 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => {
}
const { tssShare: retrievedTSS1, tssIndex: retrievedTSSIndex1 } = await tb1.getTSSShare(factorKey, { accountIndex: 1 });
const tssPrivKey1 = getLagrangeCoeffs([1, retrievedTSSIndex1], 1)
.mul(serverDKGPrivKeys[0].add(await tb1.computeAccountNonce(1)))
.mul(serverDKGPrivKeys[0].add(tb1.computeAccountNonce(1)))
.add(getLagrangeCoeffs([1, retrievedTSSIndex1], retrievedTSSIndex1).mul(retrievedTSS1))
.umod(ecCurve.n);
const tssPubKey1 = ecCurve.keyFromPrivate(tssPrivKey1).getPublic();

const pubKey1 = await tb1.getTSSPub(1);
const pubKey1 = tb1.getTSSPub(1);
strictEqual(tssPubKey1.x.toString(16, 64), pubKey1.x.toString(16, 64));
strictEqual(tssPubKey1.y.toString(16, 64), pubKey1.y.toString(16, 64));

Expand All @@ -141,12 +141,12 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => {

const { tssShare: retrievedTSS2, tssIndex: retrievedTSSIndex2 } = await tb2.getTSSShare(factorKey, { accountIndex: 2 });
const tssPrivKey2 = getLagrangeCoeffs([1, retrievedTSSIndex2], 1)
.mul(serverDKGPrivKeys[0].add(await tb1.computeAccountNonce(2)))
.mul(serverDKGPrivKeys[0].add(tb1.computeAccountNonce(2)))
.add(getLagrangeCoeffs([1, retrievedTSSIndex2], retrievedTSSIndex2).mul(retrievedTSS2))
.umod(ecCurve.n);

const tssPubKey2 = getPubKeyPoint(tssPrivKey2);
const pubKey2 = await tb1.getTSSPub(2);
const pubKey2 = tb1.getTSSPub(2);

strictEqual(tssPubKey2.x.toString(16, 64), pubKey2.x.toString(16, 64));
strictEqual(tssPubKey2.y.toString(16, 64), pubKey2.y.toString(16, 64));
Expand Down Expand Up @@ -174,15 +174,15 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => {
const tssSharePub = ecCurve.keyFromPrivate(retrievedTSS.toString("hex")).getPublic();
const { tssShare: retrievedTSS2 } = await tb2.getTSSShare(factorKey, { accountIndex: 1 });
const tssSharePub2 = ecCurve.keyFromPrivate(retrievedTSS2.toString("hex")).getPublic();
const nonce = await tb1.computeAccountNonce(1);
const nonce = tb1.computeAccountNonce(1);
const noncePub = ecCurve.keyFromPrivate(nonce.toString("hex")).getPublic();
const tssShareDerived = tssSharePub.add(noncePub);
strictEqual(tssShareDerived.getX().toString("hex"), tssSharePub2.getX().toString("hex"));
strictEqual(tssShareDerived.getY().toString("hex"), tssSharePub2.getY().toString("hex"));

const { tssShare: retrievedTSS3 } = await tb2.getTSSShare(factorKey, { accountIndex: 2 });
const tssSharePub3 = ecCurve.keyFromPrivate(retrievedTSS3.toString("hex")).getPublic();
const nonce2 = await tb1.computeAccountNonce(2);
const nonce2 = tb1.computeAccountNonce(2);
const noncePub2 = ecCurve.keyFromPrivate(nonce2.toString("hex")).getPublic();
const tssShareDerived2 = tssSharePub.add(noncePub2);
strictEqual(tssShareDerived2.getX().toString("hex"), tssSharePub3.getX().toString("hex"));
Expand All @@ -192,7 +192,7 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => {
{
const { tssShare: newTSS, tssIndex } = await tb1.getTSSShare(factorKey, { accountIndex: 1 });
const newTSSPrivKey = getLagrangeCoeffs([1, 2], 1)
.mul(new BN(serverDKGPrivKeys[1], "hex").add(await tb1.computeAccountNonce(1)))
.mul(new BN(serverDKGPrivKeys[1], "hex").add(tb1.computeAccountNonce(1)))
.add(getLagrangeCoeffs([1, 2], 2).mul(newTSS))
.umod(ecCurve.n);
strictEqual(tssPrivKey1.toString(16, 64), newTSSPrivKey.toString(16, 64));
Expand All @@ -203,7 +203,7 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => {
{
const { tssShare: newTSS2, tssIndex } = await tb2.getTSSShare(factorKey2, { accountIndex: 1 });
const newTSSPrivKey = getLagrangeCoeffs([1, 3], 1)
.mul(new BN(serverDKGPrivKeys[1], "hex").add(await tb1.computeAccountNonce(1)))
.mul(new BN(serverDKGPrivKeys[1], "hex").add(tb1.computeAccountNonce(1)))
.add(getLagrangeCoeffs([1, 3], 3).mul(newTSS2))
.umod(ecCurve.n);
strictEqual(tssPrivKey1.toString(16, 64), newTSSPrivKey.toString(16, 64));
Expand All @@ -214,7 +214,7 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => {
{
const { tssShare: newTSS, tssIndex } = await tb2.getTSSShare(factorKey, { accountIndex: 2 });
const newTSSPrivKey = getLagrangeCoeffs([1, 2], 1)
.mul(new BN(serverDKGPrivKeys[1], "hex").add(await tb2.computeAccountNonce(2)))
.mul(new BN(serverDKGPrivKeys[1], "hex").add(tb2.computeAccountNonce(2)))
.add(getLagrangeCoeffs([1, 2], 2).mul(newTSS))
.umod(ecCurve.n);
strictEqual(tssPrivKey2.toString(16, 64), newTSSPrivKey.toString(16, 64));
Expand All @@ -225,7 +225,7 @@ export const sharedTestCases = (mode, torusSP, storageLayer) => {
{
const { tssShare: newTSS2, tssIndex } = await tb2.getTSSShare(factorKey2, { accountIndex: 2 });
const newTSSPrivKey = getLagrangeCoeffs([1, 3], 1)
.mul(new BN(serverDKGPrivKeys[1], "hex").add(await tb1.computeAccountNonce(2)))
.mul(new BN(serverDKGPrivKeys[1], "hex").add(tb1.computeAccountNonce(2)))
.add(getLagrangeCoeffs([1, 3], 3).mul(newTSS2))
.umod(ecCurve.n);
strictEqual(tssPrivKey2.toString(16, 64), newTSSPrivKey.toString(16, 64));
Expand Down

0 comments on commit 19f276d

Please sign in to comment.