|
1 | 1 | class Ciphertext(object):
|
2 |
| - def __init__(s, parent, v): |
3 |
| - s.parent = parent |
4 |
| - s.v = v |
5 |
| - s.is_homomorphic = hasattr(s.parent, '_homomorphic_type') |
6 |
| - if s.is_homomorphic: |
7 |
| - s.homomorphic_type = s.parent._homomorphic_type() |
| 2 | + def __init__(s, parent, v): |
| 3 | + s.parent = parent |
| 4 | + s.v = v |
| 5 | + s.is_homomorphic = hasattr(s.parent, "_homomorphic_type") |
| 6 | + if s.is_homomorphic: |
| 7 | + s.homomorphic_type = s.parent._homomorphic_type() |
8 | 8 |
|
9 |
| - def __add__(s, rhs): |
10 |
| - assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption' |
11 |
| - assert '+' in s.homomorphic_type, 'Parents does not support addition' |
12 |
| - if isinstance(rhs, Ciphertext): |
13 |
| - assert rhs.parent == s.parent, 'Invalid Parents: %s != %s' % (rhs.parent, s.parent) |
14 |
| - return Ciphertext(s.parent, s.parent.hom('+', s.v, rhs.v)) |
15 |
| - else: |
16 |
| - return s + s.parent.encrypt(rhs) |
| 9 | + def __add__(s, rhs): |
| 10 | + assert ( |
| 11 | + s.is_homomorphic |
| 12 | + ), "The cryptosystem/cipher does not support homomorphic encryption" |
| 13 | + assert "+" in s.homomorphic_type, "Parents does not support addition" |
| 14 | + if isinstance(rhs, Ciphertext): |
| 15 | + assert rhs.parent == s.parent, "Invalid Parents: %s != %s" % ( |
| 16 | + rhs.parent, |
| 17 | + s.parent, |
| 18 | + ) |
| 19 | + return Ciphertext(s.parent, s.parent.hom("+", s.v, rhs.v)) |
| 20 | + else: |
| 21 | + return s + s.parent.encrypt(rhs) |
17 | 22 |
|
18 |
| - def __mul__(s, rhs): |
19 |
| - assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption' |
20 |
| - if isinstance(rhs, Ciphertext): |
21 |
| - assert '*' in s.homomorphic_type, 'Parents does not support multiplication' |
22 |
| - assert rhs.parent == s.parent, 'Invalid Parents: %s != %s' % (rhs.parent, s.parent) |
23 |
| - return Ciphertext(s.parent, s.parent.hom('*', s.v, rhs.v)) |
24 |
| - else: |
25 |
| - if '*' in s.homomorphic_type: |
26 |
| - return s * s.parent.encrypt(rhs) |
27 |
| - elif '+' in s.homomorphic_type: |
28 |
| - bin_repr = map(int, format(rhs, 'b')) |
29 |
| - t = s.parent.encrypt(0) |
30 |
| - for x in bin_repr: |
31 |
| - if x: |
32 |
| - t += s |
33 |
| - t += t |
34 |
| - return t |
35 |
| - else: |
36 |
| - raise TypeError('Parents does not support multiplication') |
| 23 | + def __mul__(s, rhs): |
| 24 | + assert ( |
| 25 | + s.is_homomorphic |
| 26 | + ), "The cryptosystem/cipher does not support homomorphic encryption" |
| 27 | + if isinstance(rhs, Ciphertext): |
| 28 | + assert "*" in s.homomorphic_type, "Parents does not support multiplication" |
| 29 | + assert rhs.parent == s.parent, "Invalid Parents: %s != %s" % ( |
| 30 | + rhs.parent, |
| 31 | + s.parent, |
| 32 | + ) |
| 33 | + return Ciphertext(s.parent, s.parent.hom("*", s.v, rhs.v)) |
| 34 | + else: |
| 35 | + if "*" in s.homomorphic_type: |
| 36 | + return s * s.parent.encrypt(rhs) |
| 37 | + elif "+" in s.homomorphic_type: |
| 38 | + bin_repr = map(int, format(rhs, "b")) |
| 39 | + t = s.parent.encrypt(0) |
| 40 | + for x in bin_repr: |
| 41 | + if x: |
| 42 | + t += s |
| 43 | + t += t |
| 44 | + return t |
| 45 | + else: |
| 46 | + raise TypeError("Parents does not support multiplication") |
37 | 47 |
|
38 |
| - def __sub__(s, rhs): |
39 |
| - assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption' |
40 |
| - if isinstance(rhs, Ciphertext): |
41 |
| - assert rhs.parent == s.parent, 'Invalid Parents: %s != %s' % (rhs.parent, s.parent) |
42 |
| - if '-' in s.homomorphic_type: |
43 |
| - return Ciphertext(s.parent, s.parent.hom('-', s.v, rhs.v)) |
44 |
| - return s + -rhs |
45 |
| - else: |
46 |
| - return s - s.parent.encrypt(rhs) |
| 48 | + def __sub__(s, rhs): |
| 49 | + assert ( |
| 50 | + s.is_homomorphic |
| 51 | + ), "The cryptosystem/cipher does not support homomorphic encryption" |
| 52 | + if isinstance(rhs, Ciphertext): |
| 53 | + assert rhs.parent == s.parent, "Invalid Parents: %s != %s" % ( |
| 54 | + rhs.parent, |
| 55 | + s.parent, |
| 56 | + ) |
| 57 | + if "-" in s.homomorphic_type: |
| 58 | + return Ciphertext(s.parent, s.parent.hom("-", s.v, rhs.v)) |
| 59 | + return s + -rhs |
| 60 | + else: |
| 61 | + return s - s.parent.encrypt(rhs) |
47 | 62 |
|
48 |
| - def __div__(s, rhs): |
49 |
| - assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption' |
50 |
| - assert '/' in s.homomorphic_type, 'Parents does not support division' |
51 |
| - if isinstance(rhs, Ciphertext): |
52 |
| - assert rhs.parent == s.parent, 'Invalid Parents: %s != %s' % (rhs.parent, s.parent) |
53 |
| - return Ciphertext(s.parent, s.parent.hom('/', s.v, rhs.v)) |
54 |
| - else: |
55 |
| - return s / s.parent.encrypt(rhs) |
| 63 | + def __div__(s, rhs): |
| 64 | + assert ( |
| 65 | + s.is_homomorphic |
| 66 | + ), "The cryptosystem/cipher does not support homomorphic encryption" |
| 67 | + assert "/" in s.homomorphic_type, "Parents does not support division" |
| 68 | + if isinstance(rhs, Ciphertext): |
| 69 | + assert rhs.parent == s.parent, "Invalid Parents: %s != %s" % ( |
| 70 | + rhs.parent, |
| 71 | + s.parent, |
| 72 | + ) |
| 73 | + return Ciphertext(s.parent, s.parent.hom("/", s.v, rhs.v)) |
| 74 | + else: |
| 75 | + return s / s.parent.encrypt(rhs) |
56 | 76 |
|
57 |
| - def __neg__(s): |
58 |
| - assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption' |
59 |
| - if '*' in s.homomorphic_type: |
60 |
| - return s * s.parent.encrypt(-1) |
61 |
| - elif '-' in s.homomorphic_type: |
62 |
| - return s.parent.encrypt(0) - s |
63 |
| - else: |
64 |
| - raise TypeError('Parents does not support negation') |
| 77 | + def __neg__(s): |
| 78 | + assert ( |
| 79 | + s.is_homomorphic |
| 80 | + ), "The cryptosystem/cipher does not support homomorphic encryption" |
| 81 | + if "*" in s.homomorphic_type: |
| 82 | + return s * s.parent.encrypt(-1) |
| 83 | + elif "-" in s.homomorphic_type: |
| 84 | + return s.parent.encrypt(0) - s |
| 85 | + else: |
| 86 | + raise TypeError("Parents does not support negation") |
65 | 87 |
|
66 |
| - def __radd__(s, lhs): |
67 |
| - assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption' |
68 |
| - assert '+' in s.homomorphic_type, 'Parents does not support addition' |
69 |
| - return s.parent.encrypt(lhs) + s |
| 88 | + def __radd__(s, lhs): |
| 89 | + assert ( |
| 90 | + s.is_homomorphic |
| 91 | + ), "The cryptosystem/cipher does not support homomorphic encryption" |
| 92 | + assert "+" in s.homomorphic_type, "Parents does not support addition" |
| 93 | + return s.parent.encrypt(lhs) + s |
70 | 94 |
|
71 |
| - def __rmul__(s, lhs): |
72 |
| - assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption' |
73 |
| - if '*' in s.homomorphic_type: |
74 |
| - return s.parent.encrypt(lhs) * s |
75 |
| - elif '+' in s.homomorphic_type: |
76 |
| - return s * lhs |
77 |
| - else: |
78 |
| - raise TypeError('Parents does not support multiplication') |
| 95 | + def __rmul__(s, lhs): |
| 96 | + assert ( |
| 97 | + s.is_homomorphic |
| 98 | + ), "The cryptosystem/cipher does not support homomorphic encryption" |
| 99 | + if "*" in s.homomorphic_type: |
| 100 | + return s.parent.encrypt(lhs) * s |
| 101 | + elif "+" in s.homomorphic_type: |
| 102 | + return s * lhs |
| 103 | + else: |
| 104 | + raise TypeError("Parents does not support multiplication") |
79 | 105 |
|
80 |
| - def __rsub__(s, lhs): |
81 |
| - assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption' |
82 |
| - assert '-' in s.homomorphic_type, 'Parents does not support subtract' |
83 |
| - return s.parent.encrypt(lhs) - s |
| 106 | + def __rsub__(s, lhs): |
| 107 | + assert ( |
| 108 | + s.is_homomorphic |
| 109 | + ), "The cryptosystem/cipher does not support homomorphic encryption" |
| 110 | + assert "-" in s.homomorphic_type, "Parents does not support subtract" |
| 111 | + return s.parent.encrypt(lhs) - s |
84 | 112 |
|
85 |
| - def __rdiv__(s, lhs): |
86 |
| - assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption' |
87 |
| - assert '/' in s.homomorphic_type, 'Parents does not support division' |
88 |
| - return s.parent.encrypt(lhs) / s |
| 113 | + def __rdiv__(s, lhs): |
| 114 | + assert ( |
| 115 | + s.is_homomorphic |
| 116 | + ), "The cryptosystem/cipher does not support homomorphic encryption" |
| 117 | + assert "/" in s.homomorphic_type, "Parents does not support division" |
| 118 | + return s.parent.encrypt(lhs) / s |
89 | 119 |
|
90 |
| - def __eq__(s, rhs): |
91 |
| - if isinstance(rhs, Ciphertext): |
92 |
| - return s.v == rhs.v |
93 |
| - return s.v == rhs |
| 120 | + def __eq__(s, rhs): |
| 121 | + if isinstance(rhs, Ciphertext): |
| 122 | + return s.v == rhs.v |
| 123 | + return s.v == rhs |
94 | 124 |
|
95 |
| - def __str__(s): |
96 |
| - return 'Ciphertext of %s: %s' % (s.parent, s.v) |
| 125 | + def __str__(s): |
| 126 | + return "Ciphertext of %s: %s" % (s.parent, s.v) |
97 | 127 |
|
98 |
| - def __repr__(s): |
99 |
| - return '%s(%r, %r)' % (s.__class__.__name__, s.parent, s.v) |
| 128 | + def __repr__(s): |
| 129 | + return "%s(%r, %r)" % (s.__class__.__name__, s.parent, s.v) |
0 commit comments