Skip to content

Commit 28af416

Browse files
more comments resolved
1 parent bdadda5 commit 28af416

File tree

3 files changed

+22
-35
lines changed

3 files changed

+22
-35
lines changed

src/Polynomial.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { mod } from "@noble/curves/abstract/modular.js";
22

3-
import { bigintToHex, Curve, toBigIntBE } from "./helpers/common";
4-
import { BigIntString } from "./interfaces";
3+
import { bigintToHex, Curve } from "./helpers/common";
54
import Share from "./Share";
65

76
export type ShareMap = {
@@ -22,36 +21,23 @@ class Polynomial {
2221
return this.polynomial.length;
2322
}
2423

25-
polyEval(x: BigIntString): bigint {
24+
polyEval(x: bigint): bigint {
2625
const n = this.ecCurve.Point.CURVE().n;
27-
const tmpX = toBigIntBE(x);
28-
let xi = tmpX;
26+
let xi = x;
2927
let sum = this.polynomial[0];
3028
for (let i = 1; i < this.polynomial.length; i += 1) {
3129
const tmp = xi * this.polynomial[i];
3230
sum = mod(sum + tmp, n);
33-
xi = mod(xi * tmpX, n);
31+
xi = mod(xi * x, n);
3432
}
3533
return sum;
3634
}
3735

38-
generateShares(shareIndexes: BigIntString[]): ShareMap {
39-
const newShareIndexes = shareIndexes.map((index) => {
40-
if (typeof index === "number") {
41-
return BigInt(index);
42-
}
43-
if (typeof index === "bigint") {
44-
return index;
45-
}
46-
if (typeof index === "string") {
47-
return toBigIntBE(index);
48-
}
49-
return index;
50-
});
51-
36+
generateShares(shareIndexes: bigint[]): ShareMap {
5237
const shares: ShareMap = {};
53-
for (let x = 0; x < newShareIndexes.length; x += 1) {
54-
shares[bigintToHex(newShareIndexes[x])] = new Share(newShareIndexes[x], this.polyEval(newShareIndexes[x]));
38+
for (let x = 0; x < shareIndexes.length; x += 1) {
39+
const idx = shareIndexes[x];
40+
shares[bigintToHex(idx)] = new Share(idx, this.polyEval(idx));
5541
}
5642
return shares;
5743
}

src/Share.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { bigintToHex, toBigIntBE } from "./helpers/common";
2-
import { BigIntString, ShareJSON, StringifiedType } from "./interfaces";
2+
import { ShareJSON, StringifiedType } from "./interfaces";
33

44
class Share {
55
share: bigint;
66

77
shareIndex: bigint;
88

9-
constructor(shareIndex: BigIntString, share: BigIntString) {
10-
this.share = toBigIntBE(share);
11-
this.shareIndex = toBigIntBE(shareIndex);
9+
constructor(shareIndex: bigint, share: bigint) {
10+
this.share = share;
11+
this.shareIndex = shareIndex;
1212
}
1313

1414
static fromJSON(value: StringifiedType): Share {
1515
const { share, shareIndex } = value as ShareJSON;
16-
return new Share(shareIndex, share);
16+
return new Share(toBigIntBE(shareIndex), toBigIntBE(share));
1717
}
1818

1919
toJSON(): ShareJSON {

src/helpers/common.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,13 @@ export function keccak256Bytes(a: Uint8Array): Uint8Array {
6969
return keccakHash(a);
7070
}
7171

72-
export const generatePrivateKey = (keyType: KeyType): Uint8Array => {
73-
if (keyType === KEY_TYPE.SECP256K1) {
74-
return secp256k1.utils.randomSecretKey();
75-
} else if (keyType === KEY_TYPE.ED25519) {
76-
return ed25519.utils.randomSecretKey();
77-
}
78-
throw new Error(`Invalid keyType: ${keyType}`);
79-
};
72+
/** Generate a random private key. Prefer passing ecCurve when you already have it (better for tree-shaking). */
73+
export function generatePrivateKey(keyType: KeyType): Uint8Array;
74+
export function generatePrivateKey(ecCurve: Curve): Uint8Array;
75+
export function generatePrivateKey(ecCurveOrKeyType: Curve | KeyType): Uint8Array {
76+
const ec = typeof ecCurveOrKeyType === "string" ? getKeyCurve(ecCurveOrKeyType) : ecCurveOrKeyType;
77+
return ec.utils.randomSecretKey();
78+
}
8079

8180
export const getSecp256k1 = () => secp256k1;
8281
export const getEd25519 = () => ed25519;
@@ -170,6 +169,7 @@ export const thresholdSame = <T>(arr: T[], t: number): T | undefined => {
170169
return undefined;
171170
};
172171

172+
/** ECIES params: bytes → hex. \@toruslabs/eccrypto v7 does not export these; we keep them local. */
173173
export function encParamsBufToHex(encParams: Ecies): EciesHex {
174174
return {
175175
iv: bytesToHex(encParams.iv),
@@ -180,6 +180,7 @@ export function encParamsBufToHex(encParams: Ecies): EciesHex {
180180
};
181181
}
182182

183+
/** ECIES params: hex → bytes. \@toruslabs/eccrypto v7 does not export these; we keep them local. */
183184
export function encParamsHexToBuf(eciesData: Omit<EciesHex, "ciphertext">): Omit<Ecies, "ciphertext"> {
184185
return {
185186
ephemPublicKey: hexToBytes(eciesData.ephemPublicKey),

0 commit comments

Comments
 (0)