secp256k1

EncryptionError Objects

class EncryptionError(Exception)

Custom exception for encryption errors.

bip39_hash

def bip39_hash(secret: str, passphrase: str = "SALT") -> bytes

Returns bip39 hash bytes string. This function does not check mnemonic integrity.

Arguments:

  • secret str - a mnemonic string.
  • passphrase str - salt string.

Returns:

  • bytes - 64 length bytes string.

y_from_x

def y_from_x(x: int) -> int

Computes y from x according to secp256k1 equation.

encode

def encode(point: tuple) -> str

Compresses secp256k1 point.

Arguments:

  • tuple - Point on secp256k1 curve.

Returns:

  • pubkey str - Compressed and encoded point.

decode

def decode(puk: str) -> tuple

Decompresses a compressed secp256k1 point.

Arguments:

  • pubkey str - Compressed and encoded point.

Returns:

  • tuple - Point on secp256k1 the curve.

b64encode

def b64encode(point: tuple) -> str

Encodes an elliptic curve point or ECDSA signatures as a base64 string.

Arguments:

  • point tuple - The elliptic curve point as a tuple (x, y), where x and y are integers.

Returns:

  • str - The base64-encoded string representing the point.

b64decode

def b64decode(raw: str) -> tuple

Decodes a base64-encoded string into an elliptic curve point or ECDSA signature.

Arguments:

  • raw str - The base64-encoded string.

Returns:

  • tuple - The elliptic curve point as a tuple (x, y).

mod_inverse

def mod_inverse(k: int, p: int) -> int

Computes the modular inverse using the extended Euclidean algorithm.

Arguments:

  • k int - The integer to invert.
  • p int - The modulus.

Returns:

  • int - The modular inverse of k modulo p.

Raises:

  • ZeroDivisionError - If k is zero.

point_add

def point_add(C: tuple, D: tuple) -> tuple

Adds two points on the elliptic curve.

Arguments:

  • C tuple - The first point as a tuple (x, y).
  • D tuple - The second point as a tuple (x, y).

Returns:

  • tuple - The resulting point after addition.

point_double

def point_double(C: tuple) -> tuple

Doubles a point on the elliptic curve.

Arguments:

  • C tuple - The point to double as a tuple (x, y).

Returns:

  • tuple - The resulting point after doubling.

point_multiply

def point_multiply(k: int, C: tuple) -> tuple

Multiplies a point P by a scalar k on the elliptic curve.

Arguments:

  • k int - The scalar multiplier.
  • C tuple - The point to multiply as a tuple (x, y).

Returns:

  • tuple - The resulting point after multiplication.

generate_keypair

def generate_keypair(secret: str = None)

Generates a private and public key pair for SECP256k1.

Returns:

  • tuple - A tuple containing the private key (int) and the base64-encoded public key.

sign

def sign(message: str, private_key: int) -> str

Generates an ECDSA message signature using a private key.

Arguments:

  • message str - The message to sign.
  • private_key int - The private key used for signing.

Returns:

  • str - The base64-encoded signature.

verify

def verify(message: str, signature: str, public_key: str) -> bool

Verifies an ECDSA signature using a public key.

Arguments:

  • message str - The signed message.
  • signature str - The base64-encoded signature.
  • public_key str - The base64-encoded public key.

Returns:

  • bool - True if the signature is valid, False otherwise.

aes_encrypt

def aes_encrypt(data: str, secret: str) -> str

Encrypts data using AES with a secret.

Arguments:

  • data str - The plaintext data to encrypt.
  • secret str - The secret key for encryption.

Returns:

  • str - The encrypted data as a hexadecimal string.

aes_decrypt

def aes_decrypt(data: str, secret: str) -> str

Decrypts AES-encrypted data using a secret.

Arguments:

  • data str - The encrypted data as a hexadecimal string.
  • secret str - The secret key for decryption.

Returns:

  • str|bool - The decrypted plaintext data, or False if decryption fails.

encrypt

def encrypt(public_key: str, message: str) -> typing.Tuple[str, str]

Encrypts a message using a public key.

Arguments:

  • public_key str - The base64-encoded public key.
  • message str - The plaintext message to encrypt.

Returns:

  • tuple - A tuple containing the base64-encoded R value and the encrypted message as a hexadecimal string.

decrypt

def decrypt(private_key: int, R: str, ciphered: str) -> str

Decrypts a message using a private key.

Arguments:

  • private_key int - The base64-encoded private key.
  • R str - The base64-encoded ephemeral public key.
  • ciphered str - The ciphered message to decrypt.

Returns:

  • str - Message as plaintext.

dump_secret

def dump_secret(secret: str = None) -> None

Securely stores a secret using a PIN.

The secret is encrypted with AES using a key derived from a PIN. The encrypted file is saved in a specified directory.

Arguments:

  • secret str - The secret to be encrypted and stored.

load_secret

def load_secret() -> typing.Optional[str]

Loads and decrypts a secret using a PIN.

The file containing the secret is identified by a SHA256 hash of the PIN. If the file exists, its contents are decrypted and returned.

Returns:

  • Optional[str] - The decrypted secret, or None if the file does not exist.