aboutsummaryrefslogtreecommitdiff
path: root/test/ec/perf_formula.py
blob: 60b2b7d5d3b4332b1c43ded4d5684e1e96da54cd (plain)
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
#!/usr/bin/env python
import click

from pyecsca.ec.mod import has_gmp, has_flint
from pyecsca.ec.params import get_params
from pyecsca.misc.cfg import TemporaryConfig
from test.utils import Profiler


@click.command()
@click.option("-p", "--profiler", type=click.Choice(("py", "c", "raw")), default="py")
@click.option(
    "-m",
    "--mod",
    type=click.Choice(("python", "gmp", "flint")),
    default="flint" if has_flint else "gmp" if has_gmp else "python",
)
@click.option("-o", "--operations", type=click.INT, default=5000)
@click.option(
    "-d",
    "--directory",
    type=click.Path(file_okay=False, dir_okay=True),
    default=None,
    envvar="DIR",
)
def main(profiler, mod, operations, directory):
    with TemporaryConfig() as cfg:
        cfg.ec.mod_implementation = mod
        p256 = get_params("secg", "secp256r1", "projective")
        coords = p256.curve.coordinate_model
        add = coords.formulas["add-2015-rcb"]
        dbl = coords.formulas["dbl-2015-rcb"]
        click.echo(
            f"Profiling {operations} {p256.curve.prime.bit_length()}-bit doubling formula (dbl2015rcb) executions..."
        )
        one_point = p256.generator
        with Profiler(
            profiler, directory, f"formula_dbl2016rcb_p256_{operations}_{mod}"
        ):
            for _ in range(operations):
                one_point = dbl(p256.curve.prime, one_point, **p256.curve.parameters)[0]
        click.echo(
            f"Profiling {operations} {p256.curve.prime.bit_length()}-bit addition formula (add2015rcb) executions..."
        )
        other_point = p256.generator
        with Profiler(
            profiler, directory, f"formula_add2016rcb_p256_{operations}_{mod}"
        ):
            for _ in range(operations):
                one_point = add(
                    p256.curve.prime, one_point, other_point, **p256.curve.parameters
                )[0]
        ed25519 = get_params("other", "Ed25519", "extended")
        ecoords = ed25519.curve.coordinate_model
        dblg = ecoords.formulas["mdbl-2008-hwcd"]
        click.echo(
            f"Profiling {operations} {ed25519.curve.prime.bit_length()}-bit doubling formula (mdbl2008hwcd) executions (with assumption)..."
        )
        eone_point = ed25519.generator
        with Profiler(
            profiler, directory, f"formula_mdbl2008hwcd_ed25519_{operations}_{mod}"
        ):
            for _ in range(operations):
                dblg(ed25519.curve.prime, eone_point, **ed25519.curve.parameters)


if __name__ == "__main__":
    main()