Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/refactor #16

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
490 changes: 485 additions & 5 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"build": "torus-scripts build",
"release": "torus-scripts release",
"prepack": "npm run build",
"test:secp256k1": "CURVE=secp256k1 mocha",
"test:ed25519": "CURVE=ed25519 mocha",
"test:secp256k1": "CURVE=secp256k1 tsx --tsconfig tsconfig.test.json --test test/test.ts",
"test:ed25519": "CURVE=ed25519 tsx --tsconfig tsconfig.test.json --test test/test.ts",
"test": "npm run test:secp256k1 && npm run test:ed25519",
"lint:ts": "eslint --fix 'src/**/*.ts'",
"prepare": "husky install"
Expand All @@ -26,6 +26,7 @@
"@babel/runtime": "7.x"
},
"dependencies": {
"@noble/curves": "^1.6.0",
"@toruslabs/eccrypto": "^5.0.4",
"@toruslabs/http-helpers": "^7.0.0",
"bn.js": "^5.2.1",
Expand All @@ -36,6 +37,7 @@
"web3-utils": "^4.3.1"
},
"devDependencies": {
"@babel/plugin-syntax-bigint": "^7.8.3",
"@babel/register": "^7.24.6",
"@rollup/plugin-replace": "^5.0.7",
"@toruslabs/config": "^2.2.0",
Expand All @@ -53,6 +55,7 @@
"prettier": "^3.3.3",
"rimraf": "^6.0.1",
"ts-node": "^10.9.2",
"tsx": "^4.19.1",
"typescript": "^5.5.4"
},
"lint-staged": {
Expand Down
125 changes: 125 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
export function hexToBigInt(hex: string): bigint {
// Ensure the hex string starts with '0x'
const hexWithPrefix = hex.startsWith("0x") ? hex : `0x${hex}`;
return BigInt(hexWithPrefix);
}

export function bufferToBigInt(buffer: Buffer): bigint {
return hexToBigInt(buffer.toString("hex"));
}

export function bigIntUmod(a: bigint, m: bigint): bigint {
// return a % m;
return ((a % m) + m) % m;
}

export function bigIntPointToHexPoint(point: { x: bigint; y: bigint }) {
return {
x: point.x.toString(16).padStart(64, "0"),
y: point.y.toString(16).padStart(64, "0"),
};
}

// You'll also need to implement a modular inverse function for BigInt
export function modularInverse(a: bigint, m: bigint): bigint {
let [old_r, r] = [a, m];
let [old_s, s] = [BigInt(1), BigInt(0)];
let [old_t, t] = [BigInt(0), BigInt(1)];

while (r !== BigInt(0)) {
const quotient = old_r / r;
[old_r, r] = [r, old_r - quotient * r];
[old_s, s] = [s, old_s - quotient * s];
[old_t, t] = [t, old_t - quotient * t];
}

if (old_r > BigInt(1)) {
throw new Error("Modular inverse does not exist");
}

if (old_s < BigInt(0)) {
old_s += m;
}

return old_s;
}

export function generatePolynomial(degree: number, yIntercept: bigint, randomElement: () => bigint): bigint[] {
const res: bigint[] = [];
let i = 0;
if (yIntercept !== undefined) {
res.push(yIntercept);
i++;
}
for (; i <= degree; i++) {
res.push(randomElement());
}
return res;
}

export function getShare(polynomial: bigint[], index: bigint, modulus: bigint) {
let res = BigInt(0);
for (let i = 0; i < polynomial.length; i++) {
const term = polynomial[i] * index ** BigInt(i);
res = bigIntUmod(term, modulus) + res;
}
return bigIntUmod(res, modulus);
}

export function dotProduct(arr1: bigint[], arr2: bigint[], modulus?: bigint) {
if (arr1.length !== arr2.length) {
throw new Error("arrays of different lengths");
}
let sum = BigInt(0);
for (let i = 0; i < arr1.length; i++) {
// eslint-disable-next-line prettier/prettier
sum = sum + ( arr1[i] * arr2[i] );
if (modulus) {
sum = bigIntUmod(sum, modulus);
}
}
return sum;
}

export function getLagrangeCoeff(_allIndexes: number[] | bigint[], _myIndex: number | bigint, _target: number | bigint, modulus: bigint) {
const allIndexes: bigint[] = _allIndexes.map((i) => BigInt(i));
const myIndex: bigint = BigInt(_myIndex);
const target: bigint = BigInt(_target);
let upper = BigInt(1);
let lower = BigInt(1);

for (let j = 0; j < allIndexes.length; j += 1) {
if (myIndex !== allIndexes[j]) {
let tempUpper = target - allIndexes[j];
tempUpper = bigIntUmod(tempUpper, modulus);
upper = upper * tempUpper;
upper = bigIntUmod(upper, modulus);
let tempLower = myIndex - allIndexes[j];
tempLower = bigIntUmod(tempLower, modulus);
lower = bigIntUmod(lower * tempLower, modulus);
}
}
return bigIntUmod(upper * modularInverse(lower, modulus), modulus);
}

export function lagrangeInterpolation(shares: bigint[], nodeIndex: bigint[], modulus: bigint) {
if (shares.length !== nodeIndex.length) {
return null;
}
let secret = BigInt(0);
for (let i = 0; i < shares.length; i += 1) {
let upper = BigInt(1);
let lower = BigInt(1);
for (let j = 0; j < shares.length; j += 1) {
if (i !== j) {
upper = bigIntUmod(upper * BigInt(-1) * nodeIndex[j], modulus);
const temp = bigIntUmod(nodeIndex[i] - nodeIndex[j], modulus);
lower = bigIntUmod(lower * temp, modulus);
}
}
let delta = bigIntUmod(upper * modularInverse(lower, modulus), modulus);
delta = bigIntUmod(delta * shares[i], modulus);
secret = bigIntUmod(secret + delta, modulus);
}
return secret;
}
Loading
Loading