Created
August 18, 2022 08:18
-
-
Save shuax/7ef96ad9ef7b9e3171e1d0879285a84a to your computer and use it in GitHub Desktop.
This file contains 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
from tinyec import registry | |
import secrets | |
def compress(pubKey): | |
return hex(pubKey.x) + hex(pubKey.y)[2:] | |
curve = registry.get_curve('secp256r1') | |
alicePrivKey = secrets.randbelow(curve.field.n) | |
alicePubKey = alicePrivKey * curve.g | |
print("Alice public key:", compress(alicePubKey)) | |
bobPrivKey = secrets.randbelow(curve.field.n) | |
bobPubKey = bobPrivKey * curve.g | |
print("Bob public key:", compress(bobPubKey)) | |
print("Now exchange the public keys (e.g. through Internet)") | |
aliceSharedKey = alicePrivKey * bobPubKey | |
print("Alice shared key:", compress(aliceSharedKey)) | |
bobSharedKey = bobPrivKey * alicePubKey | |
print("Bob shared key:", compress(bobSharedKey)) | |
print("Equal shared keys:", aliceSharedKey == bobSharedKey) | |
# # mallory | |
# alicePrivKey = secrets.randbelow(curve.field.n) | |
# alicePubKey = alicePrivKey * curve.g | |
# bobPrivKey = secrets.randbelow(curve.field.n) | |
# bobPubKey = bobPrivKey * curve.g | |
# mallory1PrivKey = secrets.randbelow(curve.field.n) | |
# mallory1PubKey = mallory1PrivKey * curve.g | |
# mallory2PrivKey = secrets.randbelow(curve.field.n) | |
# mallory2PubKey = mallory2PrivKey * curve.g | |
# aliceMallorySharedKey = alicePrivKey * mallory1PubKey | |
# print("Alice Mallory shared key:", compress(aliceMallorySharedKey)) | |
# malloryAliceSharedKey = mallory1PrivKey * alicePubKey | |
# print("Mallory Alice shared key:", compress(malloryAliceSharedKey)) | |
# print("Equal shared keys:", aliceMallorySharedKey == malloryAliceSharedKey) | |
# bobSharedKey = bobPrivKey * mallory2PubKey | |
# print("Bob shared key:", compress(bobSharedKey)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment