Ryan Rueger

ryan@rueg.re / picture / key / home
aboutsummaryrefslogtreecommitdiffhomepage
path: root/test_key_exchange.py
blob: 220a3c2942c2d67d5e7d2666269d01ad669baa97 (plain) (blame)
1
2
3
4
5
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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))