Ryan Rueger

ryan@rueg.re / picture / key / home
aboutsummaryrefslogtreecommitdiffhomepage
path: root/test_key_exchange.py
diff options
context:
space:
mode:
authorRyan Rueger <git@rueg.re>2025-03-01 20:25:41 +0100
committerRyan Rueger <git@rueg.re>2025-03-01 22:11:11 +0100
commitd40de259097c5e8d8fd35539560ca7c3d47523e7 (patch)
tree18e0f94350a2329060c2a19b56b0e3e2fdae56f1 /test_key_exchange.py
downloadpegasis-d40de259097c5e8d8fd35539560ca7c3d47523e7.tar.gz
pegasis-d40de259097c5e8d8fd35539560ca7c3d47523e7.tar.bz2
pegasis-d40de259097c5e8d8fd35539560ca7c3d47523e7.zip
Initial Commit
Co-Authored-By: Damien Robert <Damien.Olivier.Robert+git@gmail.com> Co-Authored-By: Frederik Vercauteren <frederik.vercauteren@gmail.com> Co-Authored-By: Jonathan Komada Eriksen <jonathan.eriksen97@gmail.com> Co-Authored-By: Pierrick Dartois <pierrickdartois@icloud.com> Co-Authored-By: Riccardo Invernizzi <nidadoni@gmail.com> Co-Authored-By: Ryan Rueger <git@rueg.re> [0.01s] Co-Authored-By: Benjamin Wesolowski <benjamin@pasch.umpa.ens-lyon.fr> Co-Authored-By: Arthur Herlédan Le Merdy <ahlm@riseup.net> Co-Authored-By: Boris Fouotsa <tako.fouotsa@epfl.ch>
Diffstat (limited to 'test_key_exchange.py')
-rw-r--r--test_key_exchange.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/test_key_exchange.py b/test_key_exchange.py
new file mode 100644
index 0000000..220a3c2
--- /dev/null
+++ b/test_key_exchange.py
@@ -0,0 +1,83 @@
+#!/usr/bin/env python3
+from argparse import ArgumentParser, RawTextHelpFormatter
+from datetime import datetime, timedelta
+
+from pegasis import PEGASIS
+
+description = """
+Testing the key exchange from the pegasis action.
+
+The test does the following
+
+ 1) Randomly samples ideals a, b
+ 2) Computes Ea, Eb as the action of a and b on the starting curve E
+ 3) Computes Eab, Eba as the action of b on Ea and a on Eb
+ 4) Verifies that Eab == Eba
+"""
+
+
+def action(EGA, E, ideal):
+ global stats
+ start = datetime.now()
+ E_ideal = EGA.action(E, ideal)
+ end = datetime.now()
+ stats["time"] += (end - start) / timedelta(microseconds=1) / 10**6
+ stats["actions"] += 1
+ return E_ideal
+
+
+if __name__ == "__main__":
+ print("Beginning tests...")
+ parser = ArgumentParser(prog="test_key_exchange.py", description=description, formatter_class=RawTextHelpFormatter)
+ parser.add_argument(
+ "--loglevel",
+ default="INFO",
+ help="Set loglevel to one of 'DEBUG', 'INFO' (default), 'WARNING', 'ERROR', 'CRITICAL'",
+ choices=("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"),
+ )
+ parser.add_argument("-p", type=int, help="Prime size", choices=(500, 1000, 1500, 2000, 4000), default=500)
+ args = parser.parse_args()
+
+ stats = {
+ # Tries
+ "tries": 0,
+ # Successes
+ "success": 0,
+ # Number of action computations done so far
+ "actions": 0,
+ # Total action time (seconds)
+ "time": 0.0,
+ }
+
+ while True:
+ stats["tries"] += 1
+
+ # Get two instances of PEGASIS to prove that there is no stateful
+ # information in the class that could interfere
+ alice = PEGASIS(args.p)
+ bob = PEGASIS(args.p)
+
+ assert alice.E_start == bob.E_start
+
+ alice_seckey = alice.sample_ideal()
+ bob_seckey = bob.sample_ideal()
+
+ alice_pubkey = action(alice, alice.E_start, alice_seckey)
+ bob_pubkey = action(bob, bob.E_start, bob_seckey)
+
+ alice_shared_secret = action(alice, bob_pubkey, alice_seckey)
+ bob_shared_secret = action(bob, alice_pubkey, bob_seckey)
+
+ if alice_shared_secret != bob_shared_secret:
+ status = "Failure"
+ else:
+ status = "Success!"
+ stats["success"] += 1
+
+ _log = []
+ _log += [f"#{stats['tries']:>3} {status}"]
+ _log += [f"Success rate: {stats['success'] / stats['tries'] * 100:>4.1f}%"]
+ _log += [f"Actions computed: {stats['actions']:>4}"]
+ _log += [f"Time per action: {stats["time"] / stats["actions"]:4>.2f}s"]
+
+ print(" | ".join(_log))