Skip to content
Merged
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
6 changes: 3 additions & 3 deletions packages/common-types/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { serializeError } from "serialize-error";

import { EncryptedMessage } from "./baseTypes/commonTypes";

export const generatePrivate = (curve: EllipticCurve): string => {
export const generatePrivate = (curve: EllipticCurve): BN => {
const key = curve.genKeyPair();
return key.getPrivate("hex");
return key.getPrivate();
};

export const ecCurve = new EllipticCurve("secp256k1");
Expand Down Expand Up @@ -76,7 +76,7 @@ export function normalize(input: number | string): string {
}

export function generatePrivateExcludingIndexes(shareIndexes: Array<BN>, curve: EllipticCurve): BN {
const key = new BN(generatePrivate(curve));
const key = generatePrivate(curve);
if (shareIndexes.find((el) => el.eq(key))) {
return generatePrivateExcludingIndexes(shareIndexes, curve);
}
Expand Down
30 changes: 17 additions & 13 deletions packages/common-types/test/test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { generatePrivate } from "@toruslabs/eccrypto";
import { fail } from "assert";
import BN from "bn.js";

import { getPubKeyPoint, Point, Polynomial } from "../src/base";
import { ecCurve } from "../src/utils";
import { KeyType, keyTypeToCurve } from "../src/baseTypes/commonTypes";
import { generatePrivate } from "../src/utils"

const testKeyType = KeyType.secp256k1;
describe("polynomial", function () {
it("#should polyEval indexes correctly", async function () {
const polyArr = [new BN(5), new BN(2)];
const poly = new Polynomial(polyArr);
const poly = new Polynomial(polyArr, testKeyType);
const result = poly.polyEval(new BN(1));
if (result.cmp(new BN(7)) !== 0) {
fail("poly result should equal 7");
Expand All @@ -18,17 +19,19 @@ describe("polynomial", function () {

describe("Point", function () {
it("#should encode into elliptic format on encode", async function () {
const secret = new BN(generatePrivate());
const point = getPubKeyPoint(secret);
const result = point.encode("elliptic-compressed", { ec: ecCurve });
const ecCurve = keyTypeToCurve(testKeyType);
const secret = generatePrivate(ecCurve);
const point = Point.fromPrivate(secret, testKeyType);
const result = point.toSEC1(true);
if (result.toString().slice(2) !== point.x.toString("hex", 64)) {
fail(`elliptic format x should be equal ${secret} ${result.toString()} ${point.x.toString("hex")} ${secret.umod(ecCurve.n)}`);
}
});
it("#should decode into point for elliptic format compressed", async function () {
const secret = new BN(generatePrivate());
const point = getPubKeyPoint(secret);
const result = point.encode("elliptic-compressed", { ec: ecCurve });
const ecCurve = keyTypeToCurve(testKeyType);
const secret = generatePrivate(ecCurve);
const point = Point.fromPrivate(secret, testKeyType);
const result = point.toSEC1(true);
if (result.toString().slice(2) !== point.x.toString("hex", 64)) {
fail("elliptic format x should be equal");
}
Expand All @@ -41,14 +44,15 @@ describe("Point", function () {
}
});
it("#should decode into point for fromCompressedPub", async function () {
const secret = new BN(generatePrivate());
const point = getPubKeyPoint(secret);
const result = point.encode("elliptic-compressed", { ec: ecCurve });
const ecCurve = keyTypeToCurve(testKeyType);
const secret = generatePrivate(ecCurve);
const point = Point.fromPrivate(secret, testKeyType);
const result = point.toSEC1(true);
if (result.toString().slice(2) !== point.x.toString("hex", 64)) {
fail("elliptic format x should be equal");
}

const key = Point.fromCompressedPub(result.toString());
const key = Point.fromSEC1(result.toString(), testKeyType);
if (point.x.cmp(key.x) !== 0) {
fail(" x should be equal");
}
Expand Down
19 changes: 13 additions & 6 deletions packages/core/test/authMetadata.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generatePrivateExcludingIndexes, getPubKeyPoint } from "@tkey/common-types";
import { generatePrivateExcludingIndexes, getPubKeyPoint, KeyType, keyTypeToCurve } from "@tkey/common-types";
import { generatePrivate } from "@toruslabs/eccrypto";
import { deepStrictEqual } from "assert";
import BN from "bn.js";
Expand All @@ -7,19 +7,26 @@ import stringify from "json-stable-stringify";
import { AuthMetadata, generateRandomPolynomial, Metadata } from "../src/index";

const PRIVATE_KEY = generatePrivate().toString("hex");
const testKeyType = KeyType.secp256k1;

describe("AuthMetadata", function () {
describe.only("AuthMetadata", function () {
it("#should authenticate and serialize and deserialize into JSON seamlessly", async function () {
const privKeyBN = new BN(PRIVATE_KEY, 16);
// create a random poly and respective shares
const ecCurve = keyTypeToCurve(testKeyType);
console.log("reach here")
const shareIndexes = [new BN(1), new BN(2)];
shareIndexes.push(generatePrivateExcludingIndexes(shareIndexes));
const poly = generateRandomPolynomial(1, privKeyBN);
shareIndexes.push(generatePrivateExcludingIndexes(shareIndexes, ecCurve));
console.log("reach here")
const poly = generateRandomPolynomial(1, ecCurve, privKeyBN);
const shares = poly.generateShares(shareIndexes);
const metadata = new Metadata(getPubKeyPoint(privKeyBN));
console.log("reach here")
const metadata = new Metadata(getPubKeyPoint(privKeyBN), testKeyType);
metadata.addFromPolynomialAndShares(poly, shares);
metadata.setGeneralStoreDomain("something", { test: "oh this is an object" });
const a = new AuthMetadata(metadata, privKeyBN);
const a = new AuthMetadata(testKeyType, metadata, privKeyBN);
console.log(a)

const stringified = stringify(a);
const metadataSerialized = Metadata.fromJSON(JSON.parse(stringify(metadata)));
const final = AuthMetadata.fromJSON(JSON.parse(stringified));
Expand Down
5 changes: 4 additions & 1 deletion packages/core/test/lagrange.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Polynomial } from "@tkey/common-types";
import { KeyType, Polynomial, keyTypeToCurve } from "@tkey/common-types";
import { generatePrivate } from "@toruslabs/eccrypto";
import { fail } from "assert";
import BN from "bn.js";

import { generateRandomPolynomial, lagrangeInterpolation } from "../src/index";

const testKeyType = KeyType.secp256k1;

describe("lagrange interpolate", function () {
it("#should interpolate secret correctly", async function () {
const ecCurve = keyTypeToCurve(testKeyType);
const polyArr = [new BN(5), new BN(2)];
const poly = new Polynomial(polyArr);
const share1 = poly.polyEval(new BN(1));
Expand Down