mini_cryptography package

Submodules

mini_cryptography.ecdsa module

ECDSA file info. The following script allows users to perform ECC (Elliptic Curve Cryptography) arithmetic operations, including ECDSA (Elliptic Curve Digital Signature Algorithm) signature formation and verification.

Pre-requisite : tiny.ec package to be installed within the Python runtime environment

class mini_cryptography.ecdsa.Ecdsa(field: Field, name: str)[source]

Bases: object

A class used to do ECC arithmetic operations and ECDSA (Elliptic Curve Digital Signature Algorithm) signature formation and verification

G_multiplication(multiplyer: int)[source]

Multiplies G point from the given multiplier

Args:

multiplyer (int): Given point multipliers

Returns:

Point: A new point after multiplication

calculate_public_key(private_key: int)[source]

Calculate the public key

Args:

private_key (int): private key value

Returns:

Point: A public key

generate_random_number(lower: int, upper: int)[source]

Generates random number [lower, … upper range]

Args:

lower (int): lower is the lower limit of the range upper (int): upper is the upper limit of the range

Returns:

int: generated number

getName()[source]

Returns ECDSA curve name

Returns:

str: ECDS curve name

k_generator()[source]

Generates random k [1, … n-1]

Returns:

int: random number k

multiply_points(point: Point, multiplyer: int)[source]

Point multiplication from a given multiplier

Args:

point (Point): Curve point multiplyer (int): Given point multiplier

Returns:

Point: A new point after multiplication

private_key_generator()[source]

Generates random x [1, … n-1]

Returns:

int: private key x

sign_message(private_key: int, k: int, hash: str)[source]

Create a signature for the given message.

Args:

private_key (int): private key value k (int): random number k hash (str): message hash value

Returns:

int, int: e. signature component values r, s bool: 0 if wrong r or 1 if wrong s

sum_points(point1: Point, point2: Point)[source]

Points sum

Args:

point1 (Point): First point point2 (Point): Second point

Returns:

Point: A new point that is the sum of the first and second points

verify_signature(r: int, s: int, hash: str, public_key: Point)[source]

Verifies signature validity

Args:

r (int): Signature component r s (int): Signature component s hash (str): Message hash public_key (Point): Public key

Returns:

bool: Verification result

class mini_cryptography.ecdsa.Field(a: int, b: int, n: int, p: int, G: Point)[source]

Bases: object

A class used to represent an ECC finite field

class mini_cryptography.ecdsa.Point(x: int, y: int)[source]

Bases: object

A class used to represent an ECC (Elliptic Curve Cryptography) point

to_array()[source]

Returns point x, y coordinates as array

Returns:

list[int]: Array of coordinates x, y

mini_cryptography.merkle module

Merkle file info. The following script allows users to calculate the Merkle tree root using different hash algorithm functions.

class mini_cryptography.merkle.Merkle(sha: SHA = SHA.SHA256, shake_length: int = 100)[source]

Bases: object

A class that is used to calculate the Merkle tree root

class SHA(value)[source]

Bases: Enum

An enumerator that is used to represent possible hash algorithms

Args:

Enum (constant): Hash algorithm

SHA1 = 1
SHA224 = 2
SHA256 = 3
SHA3_224 = 4
SHA3_256 = 5
SHA3_384 = 6
SHA3_512 = 7
SHA512 = 8
SHAKE_128 = 9
SHAKE_256 = 10
calculate_hash(value: str)[source]

Calculates the hash value from the given text.

Args:

value (str): text

Returns:

str: hash value

merkle_root(transactionList: list[str], hashTransaction: bool = 1)[source]

Calculates the Merkle root of transactions.

Args:

transactionList (list[str]): transaction list. hashTransaction (bool, optional): is transactions hashed. Defaults to 1 (hashed).

Returns:

str: Merkle root value in hexadecimal format

shift_hash(hash: str)[source]

Shifts the hexadecimal hash from little-endian to big-endian, and back.

Args:

hash (str): hash

Returns:

bytes: The return value is a bytes object. This function is also available as “b2a_hex()”.

transaction_hash(transaction: str)[source]

Calculates the transaction hash.

Args:

transaction (str): transaction

Returns:

str: transaction hash

Module contents