Changeset - 329ff9ed7905
[Not reviewed]
default
0 1 1
Laman - 4 years ago 2020-04-10 21:45:46

added benchmark script
2 files changed with 78 insertions and 2 deletions:
0 comments (0 inline, 0 general)
readme.md
Show inline comments
 
# Shamira #
 

	
 
Implements Shamir's secret sharing algorithm. Splits a string or a byte sequence byte-per-byte into n<255 shares, with any k of them sufficient for reconstruction of the original input.
 
Implements Shamir's secret sharing algorithm. Splits a string or a byte sequence byte-per-byte into _n_<255 shares, with any _k_ of them sufficient for reconstruction of the original input.
 

	
 
Outputs the shares as hexadecimal, Base32 or Base64 encoded strings.
 

	
 
Can be used on its own from the command line by invoking shamira.py or as a library by importing shamira.py.
 
Can be used on its own from the command line by invoking `shamira.py` or as a library by importing `shamira.py`.
 

	
 
## Performance ##
 

	
 
As it is, the code is not very fast. Splitting takes _n_ evaluations of a polynomial of order _k_ over Galois field 256, leading to _O(n*k)_ finite field multiplications. Reconstruction of the constant parameters during joining similarly takes _O(k*k)_ multiplications.
 

	
 
Benchmark results, all values mean _seconds per byte_ of the secret length:
 
<table>
 
    <tr>
 
        <th>k / n parameters</th>
 
        <th>Split</th>
 
        <th>Join</th>
 
    </tr>
 
    <tr>
 
        <td>2 / 3 (a Raspberry Pi 3)</td>
 
        <td>7.99e-05</td>
 
        <td>0.000428</td>
 
    </tr>
 
    <tr>
 
        <td>2 / 3 (a laptop)</td>
 
        <td>1e-05</td>
 
        <td>6.7e-05</td>
 
    </tr>
 
    <tr>
 
        <td>254 / 254 (a Raspberry Pi 3)</td>
 
        <td>0.417</td>
 
        <td>0.471</td>
 
    </tr>
 
    <tr>
 
        <td>254 / 254 (a laptop)</td>
 
        <td>0.0431</td>
 
        <td>0.0542</td>
 
    </tr>
 
</table>
 

	
 
While the speeds are not awful, for longer secrets I recommend encrypting them with a random key of your choice and splitting only the key. Anyway, you can run your own benchmark with `benchmark.py`
src/benchmark.py
Show inline comments
 
new file 100644
 
from argparse import ArgumentParser
 
import cProfile
 
import timeit
 

	
 
from shamira import generate, reconstruct
 
from tests.test_shamira import TestShamira
 

	
 

	
 
def measure(args):
 
	secret = "1234567890123456"
 
	shares = generate(secret, args.k, args.n)
 
	symbols = globals()
 
	symbols.update(locals())
 

	
 
	time = timeit.timeit("""generate(secret, args.k, args.n)""", number=1, globals=symbols)
 
	print("The generation took {0:.3}s, {1:.3} per byte.".format(time, time/16))
 

	
 
	time = timeit.timeit("""reconstruct(*shares)""", number=1, globals=symbols)
 
	print("The reconstruction took {0:.3}s, {1:.3} per byte.".format(time, time/16))
 

	
 

	
 
def profile(args):
 
	t = TestShamira()
 

	
 
	cProfile.runctx(r"""t.test_generate_reconstruct()""", globals=globals(), locals=locals())
 

	
 

	
 
parser = ArgumentParser()
 
parser.set_defaults(func=lambda _: parser.error("missing command"))
 
subparsers = parser.add_subparsers()
 

	
 
profile_parser = subparsers.add_parser("profile")
 
profile_parser.set_defaults(func=profile)
 

	
 
measure_parser = subparsers.add_parser("measure")
 
measure_parser.add_argument("-k", type=int, required=True)
 
measure_parser.add_argument("-n", type=int, required=True)
 
measure_parser.set_defaults(func=measure)
 

	
 
args = parser.parse_args()
 
args.func(args)
0 comments (0 inline, 0 general)