aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rwxr-xr-xtest/ec/perf_formula.py2
-rwxr-xr-xtest/ec/perf_mod.py2
-rwxr-xr-xtest/ec/perf_mult.py2
-rw-r--r--test/ec/test_regress.py2
-rw-r--r--test/ec/utils.py74
-rw-r--r--test/plots/plot_perf.py10
-rw-r--r--test/sca/perf_combine.py49
-rw-r--r--test/sca/test_rpa.py4
-rw-r--r--test/utils.py75
10 files changed, 141 insertions, 83 deletions
diff --git a/Makefile b/Makefile
index 26954d0..1dcaa1f 100644
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,7 @@ sca.test_sampling sca.test_target sca.test_test sca.test_trace sca.test_traceset
TESTS = ${EC_TESTS} ${SCA_TESTS}
-PERF_SCRIPTS = test/ec/perf_mod.py test/ec/perf_formula.py test/ec/perf_mult.py
+PERF_SCRIPTS = test.ec.perf_mod test.ec.perf_formula test/ec/perf_mult.py
test:
nose2 -s test -E "not slow and not disabled" -C -v ${TESTS}
@@ -41,7 +41,7 @@ black-all:
perf: ${PERF_SCRIPTS}
mkdir -p .perf
- echo $^ | env DIR=".perf" xargs -n 1 python
+ echo $^ | env DIR=".perf" xargs -n 1 python -m
perf-plots:
python test/plots/plot_perf.py -d .perf
diff --git a/test/ec/perf_formula.py b/test/ec/perf_formula.py
index 2cc4513..b49daab 100755
--- a/test/ec/perf_formula.py
+++ b/test/ec/perf_formula.py
@@ -4,7 +4,7 @@ import click
from pyecsca.ec.mod import has_gmp
from pyecsca.ec.params import get_params
from pyecsca.misc.cfg import TemporaryConfig
-from utils import Profiler
+from test.utils import Profiler
@click.command()
diff --git a/test/ec/perf_mod.py b/test/ec/perf_mod.py
index 49fe9a7..6a83314 100755
--- a/test/ec/perf_mod.py
+++ b/test/ec/perf_mod.py
@@ -3,7 +3,7 @@ import click
from pyecsca.ec.mod import Mod, has_gmp
from pyecsca.misc.cfg import TemporaryConfig
-from utils import Profiler
+from test.utils import Profiler
@click.command()
diff --git a/test/ec/perf_mult.py b/test/ec/perf_mult.py
index 36f004b..51fb5a9 100755
--- a/test/ec/perf_mult.py
+++ b/test/ec/perf_mult.py
@@ -5,7 +5,7 @@ from pyecsca.ec.mod import has_gmp
from pyecsca.ec.mult import LTRMultiplier
from pyecsca.ec.params import get_params
from pyecsca.misc.cfg import TemporaryConfig
-from utils import Profiler
+from test.utils import Profiler
@click.command()
diff --git a/test/ec/test_regress.py b/test/ec/test_regress.py
index 4cb5f3c..13e6655 100644
--- a/test/ec/test_regress.py
+++ b/test/ec/test_regress.py
@@ -103,7 +103,7 @@ class RegressionTests(TestCase):
c = Mod(2, p)
d = Mod(10, p)
curve = EllipticCurve(model, coords, p, InfinityPoint(coords), {"c": c, "d": d})
- Paff = Point(affine, x=Mod(0xd, p), y=Mod(0x9, p))
+ Paff = Point(affine, x=Mod(0xD, p), y=Mod(0x9, p))
P = Paff.to_model(coords, curve)
Qaff = Point(affine, x=Mod(0x4, p), y=Mod(0x12, p))
Q = Qaff.to_model(coords, curve)
diff --git a/test/ec/utils.py b/test/ec/utils.py
index c702d5b..67a9cc0 100644
--- a/test/ec/utils.py
+++ b/test/ec/utils.py
@@ -1,12 +1,5 @@
-import pstats
-import sys
-
-from pathlib import Path
-from subprocess import run, PIPE, DEVNULL
from itertools import product
from functools import reduce
-from pyinstrument import Profiler as PyProfiler
-from cProfile import Profile as cProfiler
def slow(func):
@@ -17,70 +10,3 @@ def slow(func):
def cartesian(*items):
for cart in product(*items):
yield reduce(lambda x, y: x + y, cart)
-
-
-class Profiler(object):
- def __init__(self, prof_type, output_directory, benchmark_name):
- self._prof = PyProfiler() if prof_type == "py" else cProfiler()
- self._prof_type = prof_type
- self._root_frame = None
- self._state = None
- self._output_directory = output_directory
- self._benchmark_name = benchmark_name
-
- def __enter__(self):
- self._prof.__enter__()
- self._state = "in"
- return self
-
- def __exit__(self, exc_type, exc_val, exc_tb):
- self._prof.__exit__(exc_type, exc_val, exc_tb)
- if self._prof_type == "py":
- self._root_frame = self._prof.last_session.root_frame()
- self._state = "out"
- self.output()
- self.save()
-
- def save(self):
- if self._state != "out":
- raise ValueError
- if self._output_directory is None or self._benchmark_name is None:
- return
- git_commit = (
- run(
- ["git", "rev-parse", "--short", "HEAD"],
- stdout=PIPE,
- stderr=DEVNULL,
- check=False,
- )
- .stdout.strip()
- .decode()
- )
- git_dirty = (
- run(
- ["git", "diff", "--quiet"], stdout=DEVNULL, stderr=DEVNULL, check=False
- ).returncode
- != 0
- )
- version = git_commit + ("-dirty" if git_dirty else "")
- output_path = Path(self._output_directory) / (self._benchmark_name + ".csv")
- with output_path.open("a") as f:
- f.write(
- f"{version},{'.'.join(map(str, sys.version_info[:3]))},{self.get_time()}\n"
- )
-
- def output(self):
- if self._state != "out":
- raise ValueError
- if self._prof_type == "py":
- print(self._prof.output_text(unicode=True, color=True))
- else:
- self._prof.print_stats("cumtime")
-
- def get_time(self):
- if self._state != "out":
- raise ValueError
- if self._prof_type == "py":
- return self._root_frame.time()
- else:
- return pstats.Stats(self._prof).total_tt
diff --git a/test/plots/plot_perf.py b/test/plots/plot_perf.py
index 7054543..631ef07 100644
--- a/test/plots/plot_perf.py
+++ b/test/plots/plot_perf.py
@@ -13,13 +13,19 @@ import numpy as np
type=click.Path(file_okay=False, dir_okay=True),
default=None,
envvar="DIR",
- required=True
+ required=True,
)
def main(directory):
directory = Path(directory)
for f in directory.glob("*.csv"):
pname = str(f).removesuffix(".csv")
- d = np.genfromtxt(f, delimiter=",", dtype=None, encoding="ascii", names=("commit", "pyversion", "time"))
+ d = np.genfromtxt(
+ f,
+ delimiter=",",
+ dtype=None,
+ encoding="ascii",
+ names=("commit", "pyversion", "time"),
+ )
if len(d.shape) == 0:
d.shape = (1,)
line = hv.Curve(zip(d["commit"], d["time"]), "commit", "duration")
diff --git a/test/sca/perf_combine.py b/test/sca/perf_combine.py
new file mode 100644
index 0000000..9b934d7
--- /dev/null
+++ b/test/sca/perf_combine.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+import click
+
+from test.utils import Profiler
+from pyecsca.sca import (
+ InspectorTraceSet,
+ average,
+ variance,
+ standard_deviation,
+ add,
+ subtract,
+ conditional_average,
+)
+
+
+@click.command()
+@click.option("-p", "--profiler", type=click.Choice(("py", "c")), default="py")
+@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, operations, directory):
+ traces = InspectorTraceSet.read("test/data/example.trs")
+ with Profiler(profiler, directory, f"combine_average_example_{operations}"):
+ for _ in range(operations):
+ average(*traces)
+ with Profiler(profiler, directory, f"combine_condavg_example_{operations}"):
+ for _ in range(operations):
+ conditional_average(*traces, condition=lambda trace: trace[0] > 0)
+ with Profiler(profiler, directory, f"combine_variance_example_{operations}"):
+ for _ in range(operations):
+ variance(*traces)
+ with Profiler(profiler, directory, f"combine_stddev_example_{operations}"):
+ for _ in range(operations):
+ standard_deviation(*traces)
+ with Profiler(profiler, directory, f"combine_add_example_{operations}"):
+ for _ in range(operations):
+ add(*traces)
+ with Profiler(profiler, directory, f"combine_subtract_example_{operations}"):
+ for _ in range(operations):
+ subtract(traces[0], traces[1])
+
+
+if __name__ == "__main__":
+ main()
diff --git a/test/sca/test_rpa.py b/test/sca/test_rpa.py
index 1f06e92..995099d 100644
--- a/test/sca/test_rpa.py
+++ b/test/sca/test_rpa.py
@@ -65,7 +65,9 @@ class MultipleContextTests(TestCase):
self.assertListEqual(muls, [1, 2, 3, 5])
def test_window(self):
- mult = WindowNAFMultiplier(self.add, self.dbl, self.neg, 3, precompute_negation=True)
+ mult = WindowNAFMultiplier(
+ self.add, self.dbl, self.neg, 3, precompute_negation=True
+ )
with local(MultipleContext()):
mult.init(self.secp128r1, self.base)
mult.multiply(5)
diff --git a/test/utils.py b/test/utils.py
new file mode 100644
index 0000000..eeac169
--- /dev/null
+++ b/test/utils.py
@@ -0,0 +1,75 @@
+import pstats
+import sys
+
+from pathlib import Path
+from subprocess import run, PIPE, DEVNULL
+
+from pyinstrument import Profiler as PyProfiler
+from cProfile import Profile as cProfiler
+
+
+class Profiler(object):
+ def __init__(self, prof_type, output_directory, benchmark_name):
+ self._prof = PyProfiler() if prof_type == "py" else cProfiler()
+ self._prof_type = prof_type
+ self._root_frame = None
+ self._state = None
+ self._output_directory = output_directory
+ self._benchmark_name = benchmark_name
+
+ def __enter__(self):
+ self._prof.__enter__()
+ self._state = "in"
+ return self
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ self._prof.__exit__(exc_type, exc_val, exc_tb)
+ if self._prof_type == "py":
+ self._root_frame = self._prof.last_session.root_frame()
+ self._state = "out"
+ self.output()
+ self.save()
+
+ def save(self):
+ if self._state != "out":
+ raise ValueError
+ if self._output_directory is None or self._benchmark_name is None:
+ return
+ git_commit = (
+ run(
+ ["git", "rev-parse", "--short", "HEAD"],
+ stdout=PIPE,
+ stderr=DEVNULL,
+ check=False,
+ )
+ .stdout.strip()
+ .decode()
+ )
+ git_dirty = (
+ run(
+ ["git", "diff", "--quiet"], stdout=DEVNULL, stderr=DEVNULL, check=False
+ ).returncode
+ != 0
+ )
+ version = git_commit + ("-dirty" if git_dirty else "")
+ output_path = Path(self._output_directory) / (self._benchmark_name + ".csv")
+ with output_path.open("a") as f:
+ f.write(
+ f"{version},{'.'.join(map(str, sys.version_info[:3]))},{self.get_time()}\n"
+ )
+
+ def output(self):
+ if self._state != "out":
+ raise ValueError
+ if self._prof_type == "py":
+ print(self._prof.output_text(unicode=True, color=True))
+ else:
+ self._prof.print_stats("cumtime")
+
+ def get_time(self):
+ if self._state != "out":
+ raise ValueError
+ if self._prof_type == "py":
+ return self._root_frame.time()
+ else:
+ return pstats.Stats(self._prof).total_tt