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:
secretstr - a mnemonic string.passphrasestr - 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 onsecp256k1curve.
Returns:
pubkeystr - Compressed and encoded point.
decode
def decode(puk: str) -> tuple
Decompresses a compressed secp256k1 point.
Arguments:
pubkeystr - Compressed and encoded point.
Returns:
tuple- Point onsecp256k1the curve.
b64encode
def b64encode(point: tuple) -> str
Encodes an elliptic curve point or ECDSA signatures as a base64 string.
Arguments:
pointtuple - 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:
rawstr - 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:
kint - The integer to invert.pint - 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:
Ctuple - The first point as a tuple (x, y).Dtuple - 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:
Ctuple - 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:
kint - The scalar multiplier.Ctuple - 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:
messagestr - The message to sign.private_keyint - 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:
messagestr - The signed message.signaturestr - The base64-encoded signature.public_keystr - 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:
datastr - The plaintext data to encrypt.secretstr - 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:
datastr - The encrypted data as a hexadecimal string.secretstr - 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_keystr - The base64-encoded public key.messagestr - 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_keyint - The base64-encoded private key.Rstr - The base64-encoded ephemeral public key.cipheredstr - 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:
secretstr - 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.