API Reference
User-facing functions and classes
DiffieHellmanMerkle
For secure key exchange across an unsecure (public) channel using Finite Field Diffie-Hellman-Merkle Key Exchange (RFC 7919)
Refer to
https://en.wikipedia.org/wiki/Diffie-Hellman_key_exchange#Cryptographic_explanation
Attributes:
Name | Type | Description |
---|---|---|
shared_modulus |
int
|
Publicly-shared modulus number (must be a prime number) |
shared_base |
int
|
Publicly-shared base number |
personal_secret |
int
|
Secret (non-public) number unique to this user |
value_to_share |
int
|
Publicly-shared result of algorithmic calculation for this user (function of |
shared_secret |
int
|
Secure secret (non-public) number to use for encrypted communication |
Source code in diffie_hellman_merkle/user.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
generate_shared_secret(received_value_to_share)
Computes a secret shared secret key
Parameters:
Name | Type | Description | Default |
---|---|---|---|
received_value_to_share |
int
|
|
required |
Returns:
Type | Description |
---|---|
None
|
None (does not return anything) |
Source code in diffie_hellman_merkle/user.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
Internal (non-user-facing) helper functions and classes
is_prime(n: int) -> bool
- ReturnsTrue
if integern
is prime, otherwiseFalse
g_is_primitive_root_modulo_p(g: int, p: int) -> bool
- ReturnsTrue
ifg
is primitive root modulo prime numberp
, otherwiseFalse
modulo_exp(base: int, exp: int, mod: int) -> int
- Memory-efficient calculation of the modulo of an exponentiated number (e.g. the value of 123^456)mod7
g_is_primitive_root_modulo_p(g, p)
Returns True
if g
is primitive root modulo prime number p
, otherwise False
Examples:
>>> g_is_primitive_root_modulo_p(g=69, p=251)
False
>>> g_is_primitive_root_modulo_p(g=6, p=251)
True
Parameters:
Name | Type | Description | Default |
---|---|---|---|
g |
int
|
The potential primitive root number |
required |
p |
int
|
The modulo prime number |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in diffie_hellman_merkle/helpers.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
is_prime(n)
Returns True
if integer n
is prime, otherwise False
This code is taken from a stack overflow answer
https://stackoverflow.com/questions/1801391/how-to-create-the-most-compact-mapping-n-→-isprimen-up-to-a-limit-n
Examples:
>>> is_prime(69)
False
>>> is_prime(440_817_757)
True
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
The integer to check (whether it is prime or not) |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in diffie_hellman_merkle/helpers.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
modulo_exp(base, exp, mod)
Memory-efficient calculation of the modulo of an exponentiated number e.g. the value of (123^456)mod7
This algorithm taken from the wikipedia article
https://en.wikipedia.org/wiki/Modular_exponentiation#Memory-efficient_method
Examples:
>>> modulo_exp(base=6, exp=9, mod=420)
216
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base |
int
|
The base number to be exponentiated |
required |
exp |
int
|
The number in the exponent |
required |
mod |
int
|
The modulus (divisor) in the modulo calculation |
required |
Returns:
Type | Description |
---|---|
int
|
The final result of the modulo calculation |
Source code in diffie_hellman_merkle/helpers.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|