diff --git a/src/shamira/benchmark.py b/src/shamira/benchmark.py --- a/src/shamira/benchmark.py +++ b/src/shamira/benchmark.py @@ -21,7 +21,7 @@ def measure(args): def profile(args): t = TestShamira() - cProfile.runctx(r"""t.test_generate_reconstruct()""", globals=globals(), locals=locals()) + cProfile.runctx(r"""t.test_generate_reconstruct()""", globals=globals(), locals=locals(), sort="cumtime") def build_subparsers(parent): diff --git a/src/shamira/fft.py b/src/shamira/fft.py --- a/src/shamira/fft.py +++ b/src/shamira/fft.py @@ -1,6 +1,7 @@ import math import cmath import itertools +from functools import cache from .gf256 import gfmul, gfpow @@ -20,6 +21,7 @@ def ceil_size(n): return ni +@cache def precompute_x(n): """Return a geometric sequence [1, w, w**2, ..., w**(n-1)], where w**n==1. This can be done only for certain values of n.""" diff --git a/src/shamira/gf256.py b/src/shamira/gf256.py --- a/src/shamira/gf256.py +++ b/src/shamira/gf256.py @@ -2,7 +2,7 @@ """Arithmetic operations on Galois Field 2**8. See https://en.wikipedia.org/wiki/Finite_field_arithmetic""" -from functools import reduce +from functools import reduce, cache import operator @@ -29,6 +29,7 @@ L[1] = 0 INV = [E[255-L[i]] if i!=0 else None for i in range(256)] # multiplicative inverse +@cache def gfmul(a, b): """Fast multiplication. Basic multiplication is expensive. a*b==g**(log(a)+log(b))""" assert 0<=a<=255, 0<=b<=255 @@ -38,6 +39,7 @@ def gfmul(a, b): return E[t] +@cache def gfpow(x, k): """Compute x**k.""" i = 1