Skip to content

Commit 8f33403

Browse files
author
Memo Ugurbil
committed
fix: sort the shares based on length
1 parent f669aec commit 8f33403

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

src/nilql/nilql.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,8 +948,11 @@ def decrypt(
948948
except Exception as exc:
949949
raise error from exc
950950

951-
bytes_ = bytes(shares[-1])
952-
for share_ in shares[:-1]:
951+
lens = [len(share) for share in shares]
952+
indices = sorted(range(len(lens)), key=lambda i: -lens[i])
953+
shares = [shares[i] for i in indices]
954+
bytes_ = bytes(shares[0])
955+
for share_ in shares[1:]:
953956
if len(bytes_) != len(share_):
954957
share_ = _random_bytes(len(bytes_), share_)
955958
bytes_ = bytes(a ^ b for (a, b) in zip(bytes_, share_))

test/test_nilql.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -313,25 +313,25 @@ def test_encrypt_decrypt_for_seed(self):
313313
"""
314314
Test encryption and decryption for storing.
315315
"""
316-
for cluster in [{'nodes': [{}, {}, {}]}]:
317-
sk = nilql.SecretKey.generate(cluster, {'store': True})
318-
319-
plaintext = ("Bart Simpson is a fictional character in the American animated television series The Simpsons"
320-
" who is part of the Simpson family. Described as one of the 100 most important people of the "
321-
"20th century by Time, Bart was created and designed by Matt Groening in James L. Brooks's "
322-
"office. Bart, alongside the rest of the family, debuted in the short 'Good Night' on The "
323-
"Tracey Ullman Show on April 19, 1987. Two years later, the family received their own series, "
324-
"which premiered on Fox on December 17, 1989. Born on April Fools' Day according to Groening, "
325-
"Bart is ten years old; he is the eldest child and only son of Homer and Marge Simpson, and "
326-
"has two sisters, Lisa and Maggie. Voiced by Nancy Cartwright (pictured), Bart is known for "
327-
"his mischievousness, rebelliousness, and disrespect for authority, as well as his prank calls"
328-
" to Moe, chalkboard gags in the opening sequence, and catchphrases. Bart is considered an "
329-
"iconic fictional television character of the 1990s and has been called an American cultural "
330-
"icon.")
331-
e = nilql.encrypt(sk, plaintext)
332-
self.assertEqual((len(e[0]), len(e[1]), len(e[2])), (140, 140, 1408))
333-
decrypted = nilql.decrypt(sk, e)
334-
self.assertEqual(decrypted, plaintext)
316+
cluster = {'nodes': [{}, {}, {}]}
317+
sk = nilql.SecretKey.generate(cluster, {'store': True})
318+
319+
plaintext = ("Bart Simpson is a fictional character in the American animated television series The Simpsons "
320+
"who is part of the Simpson family. Described as one of the 100 most important people of the "
321+
"20th century by Time, Bart was created and designed by Matt Groening in James L. Brooks's "
322+
"office. Bart, alongside the rest of the family, debuted in the short 'Good Night' on The "
323+
"Tracey Ullman Show on April 19, 1987. Two years later, the family received their own series, "
324+
"which premiered on Fox on December 17, 1989. Born on April Fools' Day according to Groening, "
325+
"Bart is ten years old; he is the eldest child and only son of Homer and Marge Simpson, and "
326+
"has two sisters, Lisa and Maggie. Voiced by Nancy Cartwright (pictured), Bart is known for "
327+
"his mischievousness, rebelliousness, and disrespect for authority, as well as his prank calls "
328+
"to Moe, chalkboard gags in the opening sequence, and catchphrases. Bart is considered an "
329+
"iconic fictional television character of the 1990s and has been called an American cultural "
330+
"icon.")
331+
e = nilql.encrypt(sk, plaintext)
332+
self.assertEqual((len(e[0]), len(e[1]), len(e[2])), (140, 140, 1408))
333+
decrypted = nilql.decrypt(sk, [e[1], e[2], e[0]])
334+
self.assertEqual(decrypted, plaintext)
335335

336336
def test_encrypt_for_match(self):
337337
"""

0 commit comments

Comments
 (0)