Elliptic curve cryptography library for JavaScript with support for Bitcoin, Litecoin and other cryptocurrency curves
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Factory function for accessing predefined standard elliptic curves used in cryptographic applications. The library includes definitions for commonly used curves in cryptocurrency and general cryptographic applications.
Returns a curve instance for a predefined curve name, or null if the curve name is not recognized.
/**
* Returns curve instance for predefined curve name
* @param {string} name - Name of the curve
* @returns {Curve|null} Curve instance or null if unknown curve name
*/
function getCurveByName(name);Usage Example:
const ecurve = require('ecurve');
// Get Bitcoin curve
const secp256k1 = ecurve.getCurveByName('secp256k1');
if (secp256k1) {
console.log('Bitcoin curve loaded');
console.log('Generator point:', secp256k1.G.toString());
}
// Get NIST P-256 curve
const secp256r1 = ecurve.getCurveByName('secp256r1');
if (secp256r1) {
console.log('NIST P-256 curve loaded');
}
// Handle unknown curve
const unknown = ecurve.getCurveByName('nonexistent');
console.log(unknown); // nullThe library includes the following predefined elliptic curves:
/**
* Bitcoin and Ethereum curve - 256-bit Koblitz curve
* Used by: Bitcoin, Ethereum, many cryptocurrencies
*/
const secp256k1 = getCurveByName('secp256k1');The secp256k1 curve is defined by the equation y² = x³ + 7 over a 256-bit prime field. This is the curve used by Bitcoin and many other cryptocurrencies.
Usage Example:
const ecurve = require('ecurve');
const BigInteger = require('bigi');
const curve = ecurve.getCurveByName('secp256k1');
// Generate a private key (random 256-bit number)
const privateKey = new BigInteger('18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725', 16);
// Generate corresponding public key
const publicKey = curve.G.multiply(privateKey);
// Get public key in compressed format (33 bytes)
const compressedPubKey = publicKey.getEncoded(true);
console.log('Compressed public key:', compressedPubKey.toString('hex'));/**
* NIST P-256 curve - 256-bit NIST recommended curve
* Also known as: prime256v1, P-256
* Used by: TLS, many government applications
*/
const secp256r1 = getCurveByName('secp256r1');/**
* 192-bit Koblitz curve
* Smaller key size, faster operations but lower security
*/
const secp192k1 = getCurveByName('secp192k1');/**
* NIST P-192 curve - 192-bit NIST recommended curve
* Also known as: prime192v1, P-192
*/
const secp192r1 = getCurveByName('secp192r1');/**
* 160-bit Koblitz curve
* Lower security, suitable for constrained environments
*/
const secp160k1 = getCurveByName('secp160k1');/**
* 160-bit NIST recommended curve
* Lower security, suitable for constrained environments
*/
const secp160r1 = getCurveByName('secp160r1');/**
* 128-bit NIST recommended curve
* Minimal security, suitable for very constrained environments
*/
const secp128r1 = getCurveByName('secp128r1');| Curve Name | Bit Length | Type | Primary Use Case | Security Level |
|---|---|---|---|---|
| secp256k1 | 256 | Koblitz | Bitcoin, Ethereum | High |
| secp256r1 | 256 | NIST | TLS, Government | High |
| secp192k1 | 192 | Koblitz | Legacy crypto | Medium |
| secp192r1 | 192 | NIST | Legacy systems | Medium |
| secp160k1 | 160 | Koblitz | Constrained devices | Low |
| secp160r1 | 160 | NIST | Constrained devices | Low |
| secp128r1 | 128 | NIST | Very constrained | Minimal |
Usage Example:
const ecurve = require('ecurve');
// Compare different curves
const curves = ['secp256k1', 'secp256r1', 'secp192k1'];
curves.forEach(name => {
const curve = ecurve.getCurveByName(name);
if (curve) {
console.log(`${name}:`);
console.log(` Field size: ${curve.p.bitLength()} bits`);
console.log(` Order: ${curve.n.bitLength()} bits`);
console.log(` Cofactor: ${curve.h.toString()}`);
console.log(` Generator: (${curve.G.affineX.toString(16)}, ${curve.G.affineY.toString(16)})`);
console.log('');
}
});/**
* All supported curve names
*/
type CurveName = 'secp128r1' | 'secp160k1' | 'secp160r1' | 'secp192k1' | 'secp192r1' | 'secp256k1' | 'secp256r1';
/**
* Curve categories
*/
type KoblitzCurve = 'secp160k1' | 'secp192k1' | 'secp256k1';
type NISTCurve = 'secp128r1' | 'secp160r1' | 'secp192r1' | 'secp256r1';Install with Tessl CLI
npx tessl i tessl/npm-ecurve