Changeset - f2079f566ab5
[Not reviewed]
default
0 3 0
Laman - 4 years ago 2020-12-07 22:36:03

optimized computation by caching the results
3 files changed with 6 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/shamira/benchmark.py
Show inline comments
 
@@ -18,13 +18,13 @@ def measure(args):
 
	print("The reconstruction took {0:.3}s, {1:.3}s per byte.".format(time, time/16))
 

	
 

	
 
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):
 
	parent.set_defaults(func=lambda _: parent.error("missing command"))
 
	subparsers = parent.add_subparsers()
 

	
src/shamira/fft.py
Show inline comments
 
import math
 
import cmath
 
import itertools
 
from functools import cache
 

	
 
from .gf256 import gfmul, gfpow
 

	
 
# divisors of 255 and their factors in natural numbers
 
DIVISORS = [3, 5, 15, 17, 51, 85, 255]
 
FACTORS = {3: [3], 5: [5], 15: [3, 5], 17: [17], 51: [3, 17], 85: [5, 17], 255: [3, 5, 17]}
 
@@ -17,12 +18,13 @@ def ceil_size(n):
 
		if ni >= n:
 
			break
 

	
 
	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."""
 
	assert n in SQUARE_ROOTS, n
 
	w = SQUARE_ROOTS[n]  # primitive N-th square root of 1
 
	return list(itertools.accumulate([1]+[w]*(n-1), gfmul))
src/shamira/gf256.py
Show inline comments
 
# GNU GPLv3, see LICENSE
 

	
 
"""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
 

	
 

	
 
def _gfmul(a, b):
 
	"""Basic multiplication. Russian peasant algorithm."""
 
	res = 0
 
@@ -26,21 +26,23 @@ for i in range(256):
 
	L[acc] = i
 
	acc = _gfmul(acc, g)
 
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
 
	if a==0 or b==0: return 0
 
	t = L[a]+L[b]
 
	if t>255: t -= 255
 
	return E[t]
 

	
 

	
 
@cache
 
def gfpow(x, k):
 
	"""Compute x**k."""
 
	i = 1
 
	res = 1
 
	while i <= k:
 
		if k&i:
0 comments (0 inline, 0 general)