Created
November 2, 2020 12:28
-
-
Save caffeinatedgaze/c3ebf3c77d96398cdf2189af53f64f58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# exercise 2, generating shared key | |
from tinyec import registry | |
import secrets | |
curve = registry.get_curve('brainpoolP256r1') | |
def compress_point(point): | |
return hex(point.x) + hex(point.y % 2)[2:] | |
def generate_shared_key(priv_key, pub_key): | |
return pub_key * priv_key | |
def main(): | |
priv_key = secrets.randbelow(curve.field.n) | |
pub_key = priv_key * curve.g | |
alice_keys = priv_key, pub_key | |
print('Alice\'s key pair') | |
print('private', hex(alice_keys[0])) | |
print('public') | |
print('x', hex(alice_keys[1].x), | |
'\n' | |
'y', hex(alice_keys[1].y)) | |
priv_key = secrets.randbelow(curve.field.n) | |
pub_key = priv_key * curve.g | |
bob_keys = priv_key, pub_key | |
print() | |
print('Bob\'s key pair') | |
print('private', hex(bob_keys[0])) | |
print('public') | |
print('x', hex(bob_keys[1].x), | |
'\n' | |
'y', hex(bob_keys[1].y)) | |
sh_key_1 = generate_shared_key(bob_keys[0], alice_keys[1]) | |
sh_key_2 = generate_shared_key(alice_keys[0], bob_keys[1]) | |
assert sh_key_1 == sh_key_2, 'Error in key pair generation, sh key generation has failed' | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment