Changeset - f90dd9a4f5a4
[Not reviewed]
default
0 4 0
Laman - 4 years ago 2020-12-07 21:53:15

integrated fft evaluation into core functions
4 files changed with 36 insertions and 10 deletions:
0 comments (0 inline, 0 general)
src/shamira/core.py
Show inline comments
 
@@ -3,39 +3,45 @@
 
import os
 
import re
 
import base64
 
import binascii
 

	
 
from . import gf256
 
from . import fft
 

	
 

	
 
class SException(Exception): pass
 
class InvalidParams(SException): pass
 
class DetectionException(SException): pass
 
class DecodingException(SException): pass
 
class MalformedShare(SException): pass
 

	
 

	
 
def compute_x(n):
 
	return fft.precompute_x(fft.ceil_size(n))[:n]
 

	
 

	
 
def _share_byte(secret_b, k, n):
 
	if not k<=n<255:
 
		raise InvalidParams("Failed k<=n<255, k={0}, n={1}".format(k, n))
 
	# we might be concerned with zero coefficients degenerating our polynomial, but there's no reason - we still need k shares to determine it is the case
 
	coefs = [int(b) for b in os.urandom(k-1)]+[int(secret_b)]
 
	points = [gf256.evaluate(coefs, i) for i in range(1, n+1)]
 
	return points
 
	# we might be concerned with zero coefficients degenerating our polynomial,
 
	# but there's no reason - we still need k shares to determine it is the case
 
	coefs = [int(secret_b)]+[int(b) for b in os.urandom(k-1)]
 
	return fft.evaluate(coefs, n)
 

	
 

	
 
def generate_raw(secret, k, n):
 
	"""Splits secret into shares.
 

	
 
	:param secret: (bytes)
 
	:param k: number of shares necessary for secret recovery. 1 <= k <= n
 
	:param n: (int) number of shares generated. 1 <= n < 255
 
	:return: [(i, (bytes) share), ...]"""
 
	xs = compute_x(n)
 
	shares = [_share_byte(b, k, n) for b in secret]
 
	return [(i+1, bytes([s[i] for s in shares])) for i in range(n)]
 
	return [(xi, bytes([s[i] for s in shares])) for (i, xi) in enumerate(xs)]
 

	
 

	
 
def reconstruct_raw(*shares):
 
	"""Tries to recover the secret from its shares.
 

	
 
	:param shares: (((int) i, (bytes) share), ...)
src/shamira/fft.py
Show inline comments
 
import math
 
import cmath
 
import itertools
 

	
 
from .gf256 import gfmul, gfpow
 

	
 
# values of n-th square roots
 
# 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]}
 
# values of n-th square roots in GF256
 
SQUARE_ROOTS = {3: 189, 5: 12, 15: 225, 17: 53, 51: 51, 85: 15, 255: 3}
 

	
 

	
 
def ceil_size(n):
 
	assert n <= DIVISORS[-1]
 
	for (i, ni) in enumerate(DIVISORS):
 
		if ni >= n:
 
			break
 

	
 
	return ni
 

	
 

	
 
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))
 
@@ -70,6 +82,14 @@ def prime_fft(p, divisors, basic_dft=dft
 
	# remap and output
 
	res = [0]*N
 
	for k1 in range(N1):
 
		for k2 in range(N2):
 
			res[(k1*N2*N2_inv+k2*N1*N1_inv) % N] = ys[k1][k2]
 
	return res
 

	
 

	
 
def evaluate(coefs, n):
 
	ni = ceil_size(n)
 
	extended_coefs = coefs + [0]*(ni-len(coefs))
 
	ys = prime_fft(extended_coefs, FACTORS[ni])
 

	
 
	return ys[:n]
src/shamira/tests/test_fft.py
Show inline comments
 
@@ -2,18 +2,18 @@
 

	
 
import random
 
import functools
 
import operator
 
from unittest import TestCase
 

	
 
from ..gf256 import evaluate
 
from .. import gf256
 
from ..fft import *
 

	
 

	
 
def batch_evaluate(coefs, xs):
 
	return [evaluate(coefs, x) for x in xs]
 
	return [gf256.evaluate(coefs, x) for x in xs]
 

	
 

	
 
class TestFFT(TestCase):
 
	def test_complex_dft(self):
 
		self.assertEqual(complex_dft([0]), [0+0j])
 
		self.assertEqual(complex_dft([1]), [1+0j])
src/shamira/tests/test_shamira.py
Show inline comments
 
@@ -2,13 +2,13 @@
 
import os
 
import random
 
from unittest import TestCase
 

	
 
from .. import *
 
from .. import gf256
 
from ..core import encode, decode,detect_encoding, _share_byte
 
from ..core import encode, decode, detect_encoding, _share_byte, compute_x
 

	
 

	
 
class TestShamira(TestCase):
 
	_urandom = os.urandom
 

	
 
	@classmethod
 
@@ -26,13 +26,13 @@ class TestShamira(TestCase):
 
		with self.assertRaises(InvalidParams):  # too many shares
 
			_share_byte(b"a", 5, 255)
 
		with self.assertRaises(ValueError):  # not castable to int
 
			_share_byte("x", 2, 3)
 

	
 
		ys = _share_byte(ord(b"a"), 2, 3)
 
		xs = list(range(1, 4))
 
		xs = compute_x(3)
 

	
 
		weights = gf256.compute_weights(xs)
 
		self.assertEqual(gf256.get_constant_coef(weights, ys), ord(b"a"))
 

	
 
		weights = gf256.compute_weights(xs[:2])
 
		self.assertEqual(gf256.get_constant_coef(weights, ys[:2]), ord(b"a"))
0 comments (0 inline, 0 general)