From d40de259097c5e8d8fd35539560ca7c3d47523e7 Mon Sep 17 00:00:00 2001 From: Ryan Rueger Date: Sat, 1 Mar 2025 20:25:41 +0100 Subject: Initial Commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Damien Robert Co-Authored-By: Frederik Vercauteren Co-Authored-By: Jonathan Komada Eriksen Co-Authored-By: Pierrick Dartois Co-Authored-By: Riccardo Invernizzi Co-Authored-By: Ryan Rueger [0.01s] Co-Authored-By: Benjamin Wesolowski Co-Authored-By: Arthur Herlédan Le Merdy Co-Authored-By: Boris Fouotsa --- test_key_exchange.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 test_key_exchange.py (limited to 'test_key_exchange.py') 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)) -- cgit v1.2.3-70-g09d2