aboutsummaryrefslogtreecommitdiff
path: root/test/sca/perf_zvp.py
blob: eb390e8d6252b01426fb4039fa2bb3a51ad1303a (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
#!/usr/bin/env python
import click

from pyecsca.ec.formula.unroll import unroll_formula
from pyecsca.ec.mod.flint import has_flint
from pyecsca.ec.mod.gmp import has_gmp
from pyecsca.ec.params import get_params
from pyecsca.misc.cfg import TemporaryConfig
from pyecsca.sca.re.zvp import zvp_points, map_to_affine
from test.utils import Profiler


@click.command()
@click.option(
    "-p",
    "--profiler",
    type=click.Choice(("py", "c", "raw")),
    default="py",
    envvar="PROF",
)
@click.option(
    "-m",
    "--mod",
    type=click.Choice(("python", "gmp", "flint")),
    default="flint" if has_flint else "gmp" if has_gmp else "python",
    envvar="MOD",
)
@click.option("-o", "--operations", type=click.INT, default=1)
@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
        p128 = get_params("secg", "secp128r1", "projective")
        formula = p128.curve.coordinate_model.formulas["add-2015-rcb"]
        unrolled = unroll_formula(formula)
        unrolled = map_to_affine(formula, unrolled)
        poly = unrolled[6][1]
        k = 5

        click.echo(
            f"Profiling {operations} {p128.curve.prime.bit_length()}-bit (k = {k}) ZVP computations..."
        )
        with Profiler(profiler, directory, f"zvp_p128_{operations}_{mod}"):
            for _ in range(operations):
                zvp_points(poly, p128.curve, k, p128.order)


if __name__ == "__main__":
    main()