Visual demonstration of Elliptic-curve cryptography in the command prompt.
Clone and install this Github repository
git clone https://github.com/johannes67890/EC-crypto.git
npm install
// Run command prompt application
> npm run cdm
// Run test of the applications functions
> npm run test
// Create and initialize EC & signature context
const ec = new EC(secp256k1);
const signature = new Signature(secp256k1);
// Generate key pair
const keySet = new KeySet(secp256k1);
const {privateKey, publicKey } = keySet;
// Message to sign
const message = "Hello World!";
// Sign: Hashes and signs thhe decopmressed private key to the signature function.
const signatureValue = signature.signMsg(message, ec.decompressPoint(privateKey));
// Verify: Decopmress public key to verify match of signature function.
const verify = signature.verifyMsg(message, signatureValue, ec.decompressPoint(publicKey));
console.log("Verify: ", verify); // Return true or false
// Holds x & y cordinats of the corresponding Point
interface Point {
x: BN;
y: BN;
}
interface signature {
// Holds r & s cordinats of the corresponding signature
r: BN;
s: BN;
}
isOnCurve(Point)
- Check ifPoint
is on curve.isInfinity(Point)
- Check ifPoint
is InfinitypointToBN(Point)
- Converts typePoint
to aBN
instancedecompressPoint(BN)
- ConvertsBN
to typePoint
concatPoint(x: BN, y: BN)
- Concatenates twoBN
instances into typePoint
hashMsgSHA256(string)
- Hashesstring
with SHA256
-
addMod(x: BN, y: BN)
- addMod computesz = (x + y) % p
-
subMod(x: BN, y: BN)
- subMod computesz = (x - y) % p
-
mulMod(x: BN, y: BN)
- mulMod computesz = (x * y) % p
-
expMod(x: BN, y: BN)
- expMod computesz = (x^^y) % p
Where
p
is the prime order of the Elliptic curve
-
pointAdd(Point1, Point2)
- Computes the sum of two points on the elliptic curve. -
pointDouble(Point)
- If two points are coincident, Computes point doubling of the point on the elliptic curve. -
pointMul(k: BN, Point)
- multiplies apoint
by the scalark
signMsg(msg, privateKey, k?)
- Returns asignature
from a messagemsg
, with the correspondingPrivateKey
(Precomputedk
is optional)verifyMsg(msg, signature, publicKey)
- ReturnsTrue
orFalse
if the signature is valied corresponding to thepublicKey
andmsg
generatePrivateKey()
- Generates aBN
instance of a random 32 byte size private Key.generatePublicKey(privateKey: BN)
- Generates aBN
instance of a public key fromprivateKey
NOTE: Generate a key pair by initializing new KeySet(curve)
// Generate key pair
const keySet = new KeySet(secp256k1);
const { privateKey, publicKey } = keySet;
Secp256k1
More under development