Skip to content

Commit 2d8c53d

Browse files
committed
test: more representation, Paillier, and Shamir's tests
1 parent 1734846 commit 2d8c53d

File tree

1 file changed

+184
-36
lines changed

1 file changed

+184
-36
lines changed

test/test_blindfold.py

Lines changed: 184 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -387,49 +387,188 @@ def test_encrypt_decrypt_for_sum_with_multiple_nodes(self):
387387
)
388388
self.assertEqual(decrypted, plaintext)
389389

390-
class TestCiphertextRepresentations(TestCase):
390+
class TestRepresentations(TestCase):
391391
"""
392-
Tests of the portable representation of ciphertexts.
392+
Tests the portability and compatibility of key and ciphertext representations.
393393
"""
394-
def test_ciphertext_representation_for_store_with_multiple_nodes(self):
394+
def test_representations_for_store_with_single_node(self):
395395
"""
396-
Test that ciphertext representation when storing in a multiple-node cluster.
396+
Confirm ability to handle representation of keys and ciphertexts for
397+
storage in a single-node cluster.
397398
"""
398-
ck = blindfold.ClusterKey.generate(cluster(3), {'store': True})
399399
plaintext = 'abc'
400+
sk = blindfold.SecretKey.load({
401+
'material': 'SnC3NBHUXwCbvpayZy9mNZqM3OZa7DlbF9ocHM4nT8Q=',
402+
'cluster': {'nodes': [{}]}, 'operations': {'store': True}
403+
})
404+
self.assertEqual(
405+
sk,
406+
blindfold.SecretKey.generate(cluster(1), {'store': True}, seed=_SEED)
407+
)
408+
ciphertext = 'eJHSIhn4VxpgLWuvk4/dWVm3bYhyTnmeqiGw33lkvEZJ1vvLn5RodwBdpqo='
409+
self.assertEqual(blindfold.decrypt(sk, ciphertext), plaintext)
410+
411+
def test_representations_for_store_with_multiple_nodes(self):
412+
"""
413+
Confirm ability to handle representation of keys and ciphertexts for
414+
storage in a multiple-node cluster.
415+
"""
416+
plaintext = 'abc'
417+
418+
ck = blindfold.ClusterKey.load({
419+
'cluster': {'nodes': [{}, {}, {}]},
420+
'operations': {'store': True}
421+
})
422+
self.assertEqual(ck, blindfold.ClusterKey.generate(cluster(3), {'store': True}))
400423
ciphertext = ['Ifkz2Q==', '8nqHOQ==', '0uLWgw==']
401-
decrypted = blindfold.decrypt(ck, ciphertext)
402-
self.assertEqual(decrypted, plaintext)
424+
self.assertEqual(blindfold.decrypt(ck, ciphertext), plaintext)
425+
426+
sk = blindfold.SecretKey.load({
427+
'material': 'SnC3NBHUXwCbvpayZy9mNZqM3OZa7DlbF9ocHM4nT8Q=',
428+
'cluster': {'nodes': [{}, {}, {}]},
429+
'operations': {'store': True}
430+
})
431+
self.assertEqual(
432+
sk,
433+
blindfold.SecretKey.generate(cluster(3), {'store': True}, seed=_SEED)
434+
)
435+
ciphertext = [
436+
'ioDjqeotjngxp8XLRBYMToS2rpCFJdFGFhPP28tb0EZrFc087sVGCoDXHuU=',
437+
'3cZW1FAxcRauF/N1x/daEDX5rX7c08N8NgVYtzVhJphXNVuwrN6YA1nbiIM=',
438+
'BPzn43eqMovPECsMzlDRq/sG73lqeprbadWa+SzZ+WlZ5m3Vst24KBpNGgI='
439+
]
440+
self.assertEqual(blindfold.decrypt(sk, ciphertext), plaintext)
403441

404-
def test_ciphertext_representation_for_store_with_multiple_nodes_with_threshold(self):
442+
def test_representations_for_store_with_multiple_nodes_with_threshold(self):
405443
"""
406-
Test that ciphertext representation when storing in a multiple-node cluster.
444+
Confirm ability to handle representation of keys and ciphertexts for
445+
storage (with threshold) in a multiple-node cluster.
407446
"""
408-
ck = blindfold.ClusterKey.generate(cluster(3), {'store': True}, threshold=2)
409447
plaintext = 'abc'
448+
449+
ck = blindfold.ClusterKey.load({
450+
'cluster': {'nodes': [{}, {}, {}]},
451+
'operations': {'store': True},
452+
'threshold': 2
453+
})
454+
self.assertEqual(
455+
ck,
456+
blindfold.ClusterKey.generate(cluster(3), {'store': True}, threshold=2)
457+
)
410458
ciphertext = ['AQAAAAICrcwAdifgFQA=', 'AgAAAAUEWpkA+u1dyAA=', 'AwAAAAgGB2YAb7TbegA=']
411-
decrypted = blindfold.decrypt(ck, ciphertext)
412-
self.assertEqual(decrypted, plaintext)
459+
self.assertEqual(blindfold.decrypt(ck, ciphertext), plaintext)
460+
461+
sk = blindfold.SecretKey.load({
462+
'material': 'SnC3NBHUXwCbvpayZy9mNZqM3OZa7DlbF9ocHM4nT8Q=',
463+
'cluster': {'nodes': [{}, {}, {}]},
464+
'operations': {'store': True},
465+
'threshold': 2
466+
})
467+
self.assertEqual(
468+
sk,
469+
blindfold.SecretKey.generate(cluster(3), {'store': True}, threshold=2, seed=_SEED)
470+
)
471+
ciphertext = [
472+
'gbwfluBqUakTrjEtOREArFjEctKIV1gI8Yv4bQv75MJnN2FN2+kJU+exIuv7yVec/Z/ILu7r',
473+
'R0RPv8fE4vPZKudck1qzrxvg0FOn/HAHSEIX0Io0BFJexMP5V7VvyHg0/94853bUzWTBocmL',
474+
'a2/usuHy69KFodRixaUdnsBxSDPRXikwqt/JqeXjolUSU1l7Hn1atWC0soC6zHdRM+NXreD9'
475+
]
476+
self.assertEqual(blindfold.decrypt(sk, ciphertext), plaintext)
413477

414-
def test_ciphertext_representation_for_sum_with_multiple_nodes(self):
478+
def test_representations_for_sum_with_single_node(self):
415479
"""
416-
Test that ciphertext representation when storing in a multiple-node cluster.
480+
Confirm ability to handle representation of keys and ciphertexts for
481+
summation (with threshold) in a single-node cluster.
417482
"""
418-
ck = blindfold.ClusterKey.generate(cluster(3), {'sum': True})
419483
plaintext = 123
484+
sk = blindfold.SecretKey.load({
485+
'material':{
486+
'l': (
487+
'17180710124328693910455057887214184059303187053517283200908251615178685092277'
488+
'68781003825543371514027055406794542204777828069029196158617836785676131719196'
489+
),
490+
'm': (
491+
'36750926513795853434585168117489663841456031899314231851820160524157189283164'
492+
'50771207416561620439623920688439253141292243122044846050470239308322700782213'
493+
),
494+
'n': (
495+
'10308426074597216346273034732328510435581912232110369920544950969107211055366'
496+
'81739294313759304465108824301069626243406484904984349541681357234446259866326'
497+
'7'
498+
),
499+
'g': (
500+
'80305305698293730896962830440487758915654402490995374612274802412883992221923'
501+
'17259092079214965301856055627777412259469950046153383889046622294722297977903'
502+
'21844769070633792102283544209510902482137967535730134757715877943631913072743'
503+
'01123732060710963981670091105550908978777514231236658174687534680701412538826'
504+
)
505+
},
506+
'cluster': {'nodes': [{}]},
507+
'operations': {'sum': True}
508+
})
509+
ciphertext = (
510+
'55869d61244f52780793eeb7c79b1a681b1c54536041f6703073c93f1e45da8208'
511+
'2e23e5ada2f27819c88fe07a0e2321b9460582fcc6ab8ca62eb3a912ec6e997ab0'
512+
'eb930fdc8fe4035f924bf027d3900db0677e694dbdba50b24cd0fb60a37710a919'
513+
'a4faf5fe43c85d7a4758ae99f1a3162c64d080943605af34b2bfd10d88'
514+
)
515+
self.assertEqual(blindfold.decrypt(sk, ciphertext), plaintext)
516+
517+
def test_representations_for_sum_with_multiple_nodes(self):
518+
"""
519+
Confirm ability to handle representation of keys and ciphertexts for
520+
summation in a multiple-node cluster.
521+
"""
522+
plaintext = 123
523+
524+
ck = blindfold.ClusterKey.load({
525+
'cluster': {'nodes': [{}, {}, {}]},
526+
'operations': {'sum': True}
527+
})
528+
self.assertEqual(ck, blindfold.ClusterKey.generate(cluster(3), {'sum': True}))
420529
ciphertext = [456, 246, 4294967296 + 15 - 123 - 456]
421-
decrypted = blindfold.decrypt(ck, ciphertext)
422-
self.assertEqual(decrypted, plaintext)
530+
self.assertEqual(blindfold.decrypt(ck, ciphertext), plaintext)
423531

424-
def test_ciphertext_representation_for_sum_with_multiple_nodes_with_threshold(self):
532+
sk = blindfold.SecretKey.load({
533+
'material': [2677312581, 321207441, 2186773557],
534+
'cluster': {'nodes': [{}, {}, {}]},
535+
'operations': {'sum': True}
536+
})
537+
self.assertEqual(sk, blindfold.SecretKey.generate(cluster(3), {'sum': True}, seed=_SEED))
538+
ciphertext = [3874430451, 3116877887, 2318008363]
539+
self.assertEqual(blindfold.decrypt(sk, ciphertext), plaintext)
540+
541+
def test_representations_for_sum_with_multiple_nodes_with_threshold(self):
425542
"""
426-
Test that ciphertext representation when storing in a multiple-node cluster.
543+
Confirm ability to handle representation of keys and ciphertexts for
544+
summation (with threshold) in a multiple-node cluster.
427545
"""
428-
ck = blindfold.ClusterKey.generate(cluster(3), {'sum': True}, threshold=2)
429546
plaintext = 123
547+
548+
ck = blindfold.ClusterKey.load({
549+
'cluster': {'nodes': [{}, {}, {}]},
550+
'operations': {'sum': True},
551+
'threshold': 2
552+
})
553+
self.assertEqual(
554+
ck,
555+
blindfold.ClusterKey.generate(cluster(3), {'sum': True}, threshold=2)
556+
)
430557
ciphertext = [[1, 1382717699], [2, 2765435275], [3, 4148152851]]
431-
decrypted = blindfold.decrypt(ck, ciphertext)
432-
self.assertEqual(decrypted, plaintext)
558+
self.assertEqual(blindfold.decrypt(ck, ciphertext), plaintext)
559+
560+
sk = blindfold.SecretKey.load({
561+
'material': [2677312581, 321207441, 2186773557],
562+
'cluster': {'nodes': [{}, {}, {}]},
563+
'operations': {'sum': True},
564+
'threshold': 2
565+
})
566+
self.assertEqual(
567+
sk,
568+
blindfold.SecretKey.generate(cluster(3), {'sum': True}, threshold=2, seed=_SEED)
569+
)
570+
ciphertext = [(1, 177325002), (2, 986000561), (3, 2621193783)]
571+
self.assertEqual(blindfold.decrypt(sk, ciphertext), plaintext)
433572

434573
class TestFunctionsErrors(TestCase):
435574
"""
@@ -555,7 +694,7 @@ class TestSecureComputations(TestCase):
555694
Tests consisting of end-to-end workflows involving secure computation.
556695
"""
557696
# pylint: disable=protected-access # To access ``SecretKey._modulus`` method.
558-
def test_workflow_for_secure_sum_with_single_node(self):
697+
def test_workflow_for_secure_sum_mul_with_single_node(self):
559698
"""
560699
Test secure summation workflow for a cluster that has a single node.
561700
"""
@@ -568,12 +707,19 @@ def test_workflow_for_secure_sum_with_single_node(self):
568707
a = pailliers.cipher(int(blindfold.encrypt(pk, 123), 16))
569708
b = pailliers.cipher(int(blindfold.encrypt(pk, 456), 16))
570709
c = pailliers.cipher(int(blindfold.encrypt(pk, 789), 16))
571-
r = hex(pailliers.add(pk['material'], a, b, c))
710+
r = hex(
711+
pailliers.add(
712+
pk['material'],
713+
pailliers.mul(pk['material'], a, 2),
714+
pailliers.mul(pk['material'], b, -1),
715+
c
716+
)
717+
)
572718

573719
decrypted = blindfold.decrypt(sk, r)
574-
self.assertEqual(decrypted, 123 + 456 + 789)
720+
self.assertEqual(decrypted, (2 * 123) + (-1 * 456) + 789)
575721

576-
def test_workflow_for_secure_sum_with_multiple_nodes(self):
722+
def test_workflow_for_secure_sum_mul_with_multiple_nodes(self):
577723
"""
578724
Test secure summation workflow for a cluster that has multiple nodes.
579725
"""
@@ -583,28 +729,30 @@ def test_workflow_for_secure_sum_with_multiple_nodes(self):
583729
(b0, b1, b2) = blindfold.encrypt(sk, 456)
584730
(c0, c1, c2) = blindfold.encrypt(sk, 789)
585731
(r0, r1, r2) = (
586-
(a0 + b0 + c0) % sk._modulus(),
587-
(a1 + b1 + c1) % sk._modulus(),
588-
(a2 + b2 + c2) % sk._modulus()
732+
((2 * a0) + (-1 * b0) + c0) % sk._modulus(),
733+
((2 * a1) + (-1 * b1) + c1) % sk._modulus(),
734+
((2 * a2) + (-1 * b2) + c2) % sk._modulus()
589735
)
590736

591737
decrypted = blindfold.decrypt(sk, [r0, r1, r2])
592-
self.assertEqual(decrypted, 123 + 456 + 789)
738+
self.assertEqual(decrypted, (2 * 123) + (-1 * 456) + 789)
593739

594-
def test_workflow_for_secure_sum_with_multiple_nodes_with_threshold(self):
740+
def test_workflow_for_secure_sum_mul_with_multiple_nodes_with_threshold(self):
595741
"""
596742
Test secure summation workflow with a threshold for a cluster that has
597743
multiple nodes.
598744
"""
599745
sk = blindfold.SecretKey.generate(cluster(3), {'sum': True}, threshold=2)
600746

601-
xs = blindfold.encrypt(sk, 123)
602-
ys = blindfold.encrypt(sk, 456)
603-
zs = blindfold.encrypt(sk, 789)
747+
xs = [shamirs.share(*s) for s in blindfold.encrypt(sk, 123)]
748+
ys = [shamirs.share(*s) for s in blindfold.encrypt(sk, 456)]
749+
zs = [shamirs.share(*s) for s in blindfold.encrypt(sk, 789)]
604750
rs = shamirs.add(
605-
[shamirs.share(*s) for s in (xs + ys + zs)],
751+
shamirs.mul(xs, 2, modulus=sk._modulus()),
752+
shamirs.mul(ys, -1, modulus=sk._modulus()),
753+
zs,
606754
modulus=sk._modulus()
607755
)
608756

609757
decrypted = blindfold.decrypt(sk, rs)
610-
self.assertEqual(decrypted, 123 + 456 + 789)
758+
self.assertEqual(decrypted, (2 * 123) + (-1 * 456) + 789)

0 commit comments

Comments
 (0)