kryptos/ec
Elliptic Curve Cryptography key generation and management.
This module provides key pair generation for elliptic curve cryptography, supporting standard NIST curves and secp256k1. EC keys can be used for both ECDSA signatures and ECDH key agreement.
Key Generation
import kryptos/ec
let #(private_key, public_key) = ec.generate_key_pair(ec.P256)
Import/Export
import kryptos/ec
let #(private_key, _public_key) = ec.generate_key_pair(ec.P256)
let assert Ok(pem) = ec.to_pem(private_key)
let assert Ok(#(imported_private, _)) = ec.from_pem(pem)
Types
Supported elliptic curves for key generation.
pub type Curve {
P256
P384
P521
Secp256k1
}
Constructors
-
P256NIST P-256 curve (secp256r1, prime256v1). 256-bit key size.
-
P384NIST P-384 curve (secp384r1). 384-bit key size.
-
P521NIST P-521 curve (secp521r1). 521-bit key size.
-
Secp256k1Koblitz curve used by Bitcoin and Ethereum. 256-bit key size.
An elliptic curve private key.
pub type PrivateKey
Values
pub fn coordinate_size(curve: Curve) -> Int
Returns the coordinate size in bytes for the given curve.
This is the size of each coordinate (x or y) in an EC point.
pub fn from_der(
der: BitArray,
) -> Result(#(PrivateKey, PublicKey), Nil)
Imports an EC private key from DER-encoded data.
The key must be in PKCS#8 format.
Parameters
der: DER-encoded key data
Returns
Ok(#(private_key, public_key)) on success, Error(Nil) on failure.
pub fn from_pem(
pem: String,
) -> Result(#(PrivateKey, PublicKey), Nil)
Imports an EC private key from PEM-encoded data.
The key must be in PKCS#8 format.
Parameters
pem: PEM-encoded key string
Returns
Ok(#(private_key, public_key)) on success, Error(Nil) on failure.
pub fn generate_key_pair(
curve: Curve,
) -> #(PrivateKey, PublicKey)
Generates a new elliptic curve key pair.
The private key should be kept secret and used for signing or ECDH key agreement. The public key can be shared and is used for signature verification or ECDH key agreement.
Parameters
curve: The elliptic curve to use for key generation
Returns
A tuple of #(private_key, public_key).
pub fn public_key_from_der(
der: BitArray,
) -> Result(PublicKey, Nil)
Imports an EC public key from DER-encoded data.
The key must be in SPKI format.
Parameters
der: DER-encoded key data
Returns
Ok(public_key) on success, Error(Nil) on failure.
pub fn public_key_from_pem(pem: String) -> Result(PublicKey, Nil)
Imports an EC public key from PEM-encoded data.
The key must be in SPKI format.
Parameters
pem: PEM-encoded key string
Returns
Ok(public_key) on success, Error(Nil) on failure.
pub fn public_key_from_private_key(key: PrivateKey) -> PublicKey
Derives the public key from an EC private key.
Parameters
key: The private key
Returns
The corresponding public key.
pub fn public_key_from_raw_point(
curve: Curve,
point: BitArray,
) -> Result(PublicKey, Nil)
Imports an EC public key from an uncompressed SEC1 point.
The point must be in uncompressed format: 0x04 || x || y
where x and y are the coordinates padded to the curve’s coordinate size.
Parameters
curve: The elliptic curve (P256, P384, P521, or Secp256k1)point: The uncompressed point bytes (1 + 2 * coordinate_size bytes)
Returns
Ok(public_key) on success, Error(Nil) if the format is invalid
or the point is not on the curve.
pub fn public_key_to_der(key: PublicKey) -> Result(BitArray, Nil)
Exports an EC public key to DER format.
The key is exported in SPKI format.
Parameters
key: The public key to export
Returns
Ok(der_data) on success, Error(Nil) on failure.
pub fn public_key_to_pem(key: PublicKey) -> Result(String, Nil)
Exports an EC public key to PEM format.
The key is exported in SPKI format.
Parameters
key: The public key to export
Returns
Ok(pem_string) on success, Error(Nil) on failure.
pub fn public_key_to_raw_point(key: PublicKey) -> BitArray
Exports a public key to uncompressed SEC1 point format.
Returns a BitArray in the format: 0x04 || X || Y where X and Y are
the coordinates of the public key point, each padded to the curve’s
coordinate size.
If the key was imported with a compressed point format, it will be automatically decompressed.
This is the inverse of public_key_from_raw_point.
pub fn to_der(key: PrivateKey) -> Result(BitArray, Nil)
Exports an EC private key to DER format.
The key is exported in PKCS#8 format.
Parameters
key: The private key to export
Returns
Ok(der_data) on success, Error(Nil) on failure.
pub fn to_pem(key: PrivateKey) -> Result(String, Nil)
Exports an EC private key to PEM format.
The key is exported in PKCS#8 format.
Parameters
key: The private key to export
Returns
Ok(pem_string) on success, Error(Nil) on failure.