-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
34 lines (28 loc) · 869 Bytes
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
some code for use with Residual Number System, may be usefull if you like to play with some cryptography
"""
import operator
def vector_boundary(vector):
return reduce(operator.mul, primes_vector)
def to_RNS(number, vector):
if not vector:
assert isinstance(primes_vector, (list, tuple))
if number>vector_boundary(vector):
raise TypeError, 'to small vector chosen'
return [number%elem for elem in vector]
def generate_vector(system_limit):
numbers = range(3, system_limit, 2)
mroot = system_limit ** 0.5
half = len(numbers)
i = 0
m = 3
while m <= mroot:
if numbers[i]:
j = (m * m - 3)//2
numbers[j] = 0
while j < half:
numbers[j] = 0
j += m
i = i + 1
m = 2 * i + 3
return [2]+[x for x in numbers if x]