aboutsummaryrefslogtreecommitdiff
path: root/test/sca/perf_epa.py
blob: 32648c95db32eba6a8705f0677fce05866cffdba (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
import click

from pyecsca.ec.mod import Mod
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.ec.mult import LTRMultiplier
from pyecsca.misc.cfg import TemporaryConfig
from pyecsca.sca.re.rpa import multiple_graph
from pyecsca.sca.re.epa import evaluate_checks, graph_to_check_inputs
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=100)
@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")

        scalars = [int(Mod.random(p128.order)) for _ in range(operations)]
        ops = [
            multiple_graph(scalar, p128, LTRMultiplier, LTRMultiplier)
            for scalar in scalars
        ]

        click.echo(
            f"Profiling {operations} {p128.curve.prime.bit_length()}-bit graph_to_check_inputs + evaluate_checks computations..."
        )
        with Profiler(profiler, directory, f"epa_p128_ltr_{operations}_{mod}"):
            for precomp_ctx, full_ctx, out in ops:
                check_inputs = graph_to_check_inputs(
                    precomp_ctx,
                    full_ctx,
                    out,
                    check_condition="all",
                    precomp_to_affine=True,
                    use_init=True,
                    use_multiply=True,
                    check_formulas={"add"},
                )
                c = 0
                for j in range(3220):
                    i = 0

                    def check_add(x, y):
                        nonlocal i, c
                        i += 1
                        c += 1
                        return (i % (30 * (j+1))) == 0

                    def check_affine(x):
                        return False

                    evaluate_checks(
                        check_funcs={
                            "add": check_add,
                            "affine": check_affine,
                        },
                        check_inputs=check_inputs,
                    )


if __name__ == "__main__":
    main()