From 7f02308f748eb09ddd8e1fd0d130a9bdccb7b90f Mon Sep 17 00:00:00 2001 From: Johannes Date: Thu, 8 Dec 2022 14:09:18 +0100 Subject: [PATCH] terminal program created --- package-lock.json | 28 ++++++++++++++------------ package.json | 5 ++++- src/cdm.ts | 51 +++++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 7 +++++++ 4 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 src/cdm.ts diff --git a/package-lock.json b/package-lock.json index 4762b08..e686bc9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,11 +13,11 @@ "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^14.4.3", "@types/jest": "^29.2.0", - "@types/node": "^18.11.0", "@types/react": "^18.0.21", "@types/react-dom": "^18.0.6", "bn.js": "^5.2.1", "buffer": "^6.0.3", + "chalk": "^4.1.2", "crypto-browserify": "^3.12.0", "process": "^0.11.10", "react": "^18.2.0", @@ -29,12 +29,14 @@ }, "devDependencies": { "@types/bn.js": "^5.1.1", + "@types/node": "^18.11.11", "@webpack-cli/generators": "^2.5.0", "hash.js": "^1.1.7", "react-app-rewired": "^2.2.1", "tailwindcss": "^3.1.8", "ts-jest": "^29.0.3", "ts-loader": "^9.2.5", + "tslib": "^2.4.1", "typescript": "^4.9.3", "webpack": "^5.74.0", "webpack-cli": "^4.10.0" @@ -4192,9 +4194,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "18.11.3", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.3.tgz", - "integrity": "sha512-fNjDQzzOsZeKZu5NATgXUPsaFaTxeRgFXoosrHivTl8RGeV733OLawXsGfEk9a8/tySyZUyiZ6E8LcjPFZ2y1A==" + "version": "18.11.11", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.11.tgz", + "integrity": "sha512-KJ021B1nlQUBLopzZmPBVuGU9un7WJd/W4ya7Ih02B4Uwky5Nja0yGYav2EfYIk0RR2Q9oVhf60S2XR1BCWJ2g==" }, "node_modules/@types/normalize-package-data": { "version": "2.4.1", @@ -21464,9 +21466,9 @@ } }, "node_modules/tslib": { - "version": "2.4.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz", - "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==" + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.1.tgz", + "integrity": "sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==" }, "node_modules/tsutils": { "version": "3.21.0", @@ -27177,9 +27179,9 @@ "dev": true }, "@types/node": { - "version": "18.11.3", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.3.tgz", - "integrity": "sha512-fNjDQzzOsZeKZu5NATgXUPsaFaTxeRgFXoosrHivTl8RGeV733OLawXsGfEk9a8/tySyZUyiZ6E8LcjPFZ2y1A==" + "version": "18.11.11", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.11.tgz", + "integrity": "sha512-KJ021B1nlQUBLopzZmPBVuGU9un7WJd/W4ya7Ih02B4Uwky5Nja0yGYav2EfYIk0RR2Q9oVhf60S2XR1BCWJ2g==" }, "@types/normalize-package-data": { "version": "2.4.1", @@ -40070,9 +40072,9 @@ } }, "tslib": { - "version": "2.4.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz", - "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==" + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.1.tgz", + "integrity": "sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==" }, "tsutils": { "version": "3.21.0", diff --git a/package.json b/package.json index 0527eec..5349cb0 100644 --- a/package.json +++ b/package.json @@ -8,11 +8,11 @@ "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^14.4.3", "@types/jest": "^29.2.0", - "@types/node": "^18.11.0", "@types/react": "^18.0.21", "@types/react-dom": "^18.0.6", "bn.js": "^5.2.1", "buffer": "^6.0.3", + "chalk": "^4.1.2", "crypto-browserify": "^3.12.0", "process": "^0.11.10", "react": "^18.2.0", @@ -24,6 +24,7 @@ }, "scripts": { "start": "react-app-rewired start", + "cdm": "ts-node ./src/cdm.ts", "build": "webpack --mode=production --node-env=production", "test": "jest", "eject": "react-scripts eject", @@ -51,12 +52,14 @@ }, "devDependencies": { "@types/bn.js": "^5.1.1", + "@types/node": "^18.11.11", "@webpack-cli/generators": "^2.5.0", "hash.js": "^1.1.7", "react-app-rewired": "^2.2.1", "tailwindcss": "^3.1.8", "ts-jest": "^29.0.3", "ts-loader": "^9.2.5", + "tslib": "^2.4.1", "typescript": "^4.9.3", "webpack": "^5.74.0", "webpack-cli": "^4.10.0" diff --git a/src/cdm.ts b/src/cdm.ts new file mode 100644 index 0000000..a534732 --- /dev/null +++ b/src/cdm.ts @@ -0,0 +1,51 @@ +import { EC } from './curve/EDDSA/EC'; +import KeySet from "./curve/EDDSA/keyGeneration"; +import Signature from "./curve/EDDSA/signature"; +import { secp256k1 } from './curve/curvesDefined'; +import { hashMsgSHA256 } from './util'; +import chalk from 'chalk'; + +/** + * Run the CDM + * To run this script: `npm run cdm` + */ + +function CDMrunECDSA() { + console.log(chalk.green("Elliptic Curve Digital Signature Algorithm")); + console.log(chalk.gray("Curve: ", secp256k1.name, "\n")); + + // Create and initialize EC context & signature + const ec = new EC(secp256k1); + const signature = new Signature(secp256k1); + + // Generate key pair + const keySet = new KeySet(secp256k1); + const {privateKey, publicKey } = keySet; + + console.log("Private key: ", `(${privateKey.byteLength()} bytes)`, "\n", chalk.redBright(privateKey.toString("hex"),"\n")); + console.log("Public key: ", `(${publicKey.byteLength()} bytes)`, "\n", + chalk.blueBright( "X: ", ec.decompressPoint(publicKey).x.toString("hex"), "\n"), + chalk.redBright("Y: ", ec.decompressPoint(publicKey).y.toString("hex"), "\n")); + + // Sign message + const message = "Hello World!"; + console.log("Message: ",chalk.cyan(message), "\n"); + // Hashed message with SHA256. + console.log("Message hash (SHA256):", `(${hashMsgSHA256(message).byteLength()} bytes)`, "\n", `${hashMsgSHA256(message).toString('hex')}`, "\n"); + + + const signatureValue = signature.signMsg(message, ec.decompressPoint(privateKey)); // Decopmress private key to match signature function. + console.log(`Signature:`, signatureValue, "\n"); // Return signature {r,s} as hex string + + // Verify signature + const verify = signature.verifyMsg(message, signatureValue, ec.decompressPoint(publicKey)); // Decopmress public key to match signature function. + console.log("Verify: ", verify, "\n"); // Return true or false +} + + + + +CDMrunECDSA(); + + + diff --git a/tsconfig.json b/tsconfig.json index eae2ff8..68278f6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,4 +1,10 @@ { + "ts-node": { + // these options are overrides used only by ts-node + "compilerOptions": { + "module": "commonjs" + } + }, "compilerOptions": { "allowSyntheticDefaultImports": true, "moduleResolution": "node", @@ -9,5 +15,6 @@ "allowJs": true, "jsx": "react" }, + "include": ["src/**/*"], "files": ["src/index.tsx"] }