Module type Rfc7748.DH

Signature of the modules implementing the Diffie-Hellman functions for RFC 7748.

Types

type private_key

A private key for this curve. Private keys are, generally speaking, natural numbers for scalar multiplication of curve points.

type public_key

A public key for this curve. Public keys form a curve in 2D space and are uniquely identified by their x-coordinate (up to symmetry).

Constants

val key_size : int

The size of a valid private or public key, in bytes.

val base : public_key

The base point of the curve.

Key Conversion

val public_key_of_string : string -> public_key

Create a public key from a given string. The key is assumed to be encoded in hexadecimal and must have a length of 2*key_size. The string is silently truncated (if too long) or padded (if too short). An exception is raised if the string contains characters that are not valid hexadecimal digits. The curves in RFC 7748 are specially crafted such that public keys received from untrusted sources can be used safely without validation.

Note that the creation includes a canonicalization step and thus, in general, string_of_public_key (public_key_of_string s) <> s.

val private_key_of_string : string -> private_key

Create a private key from a given string. The key is assumed to be encoded in hexadecimal and must have a length of 2*key_size. The string is silently truncated (if too long) or padded (if too short). An exception is raised if the string contains characters that are not valid hexadecimal digits. The curves in RFC 7748 are specially crafted such that any random sequence of bytes of the correct length can be used to create a private key.

Note that the creation includes a canonicalization step and thus, in general, string_of_private_key (private_key_of_string s) <> s.

val string_of_public_key : public_key -> string

Convert the public key to its hex-encoded string representation.

val string_of_private_key : private_key -> string

Convert the private key to its hex-encoded string representation.

val public_key_of_bytes : Stdlib.Bytes.t -> public_key

Create a public key from a byte array of length key_size. An exception is raised if the array has the wrong size. Besides that, the function behaves like public_key_of_string after hex-decoding. The array is not modified by this module, and it must not be modified externally while the public_key object is in scope.

val private_key_of_bytes : Stdlib.Bytes.t -> private_key

Create a private key from a byte array of length key_size. An exception is raised if the array has the wrong size. Besides that, the function behaves like private_key_of_string after hex-decoding. The array is not modified by this module, and it must not be modified externally while the public_key object is in scope.

val bytes_of_public_key : public_key -> Stdlib.Bytes.t

Create a byte array from the given public key.

val bytes_of_private_key : private_key -> Stdlib.Bytes.t

Create a byte array from the given private key.

Curve Operations

val scale : private_key -> public_key -> public_key

scale priv pub is the scalar multiplication of pub by priv.

val public_key_of_private_key : private_key -> public_key

public_key_of_private_key priv is equal to scale priv base