boolean-truth-tables.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. from openfhe import *
  2. # Sample Program: Step 1: Set CryptoContext
  3. cc = BinFHEContext()
  4. print("Generate cryptocontext\n")
  5. """
  6. STD128 is the security level of 128 bits of security based on LWE Estimator
  7. and HE standard. Other common options are TOY, MEDIUM, STD192, and STD256. MEDIUM
  8. corresponds to the level of more than 100 bits for both quantum and
  9. classical computer attacks
  10. """
  11. cc.GenerateBinFHEContext(STD128)
  12. print("Finished generating cryptocontext\n")
  13. # Sample Program: Step 2: Key Generation
  14. # Generate the secret key
  15. sk = cc.KeyGen()
  16. print("Generating the bootstrapping keys...\n")
  17. # Generate the bootstrapping keys (refresh and switching keys)
  18. cc.BTKeyGen(sk)
  19. print("Completed the key generation.\n\n")
  20. # Sample Program: Step 3: Encryption
  21. # Encrypt two ciphertexts representing Boolean True (1).
  22. ct10 = cc.Encrypt(sk, 1)
  23. ct11 = cc.Encrypt(sk, 1)
  24. # Encrypt two ciphertexts representing Boolean False (0).
  25. ct00 = cc.Encrypt(sk, 0)
  26. ct01 = cc.Encrypt(sk, 0)
  27. # Sample Program: Step 4: Evaluation of NAND gates
  28. ctNAND1 = cc.EvalBinGate(NAND, ct10, ct11)
  29. ctNAND2 = cc.EvalBinGate(NAND, ct10, ct01)
  30. ctNAND3 = cc.EvalBinGate(NAND, ct00, ct01)
  31. ctNAND4 = cc.EvalBinGate(NAND, ct00, ct11)
  32. result = cc.Decrypt(sk, ctNAND1)
  33. print(f"1 NAND 1 = {result}")
  34. result = cc.Decrypt(sk, ctNAND2)
  35. print(f"1 NAND 0 = {result}")
  36. result = cc.Decrypt(sk, ctNAND3)
  37. print(f"0 NAND 0 = {result}")
  38. result = cc.Decrypt(sk, ctNAND4)
  39. print(f"0 NAND 1 = {result}")
  40. # Sample Program: Step 5: Evaluation of AND gates
  41. ctAND1 = cc.EvalBinGate(AND, ct10, ct11)
  42. ctAND2 = cc.EvalBinGate(AND, ct10, ct01)
  43. ctAND3 = cc.EvalBinGate(AND, ct00, ct01)
  44. ctAND4 = cc.EvalBinGate(AND, ct00, ct11)
  45. result = cc.Decrypt(sk, ctAND1)
  46. print(f"1 AND 1 = {result}")
  47. result = cc.Decrypt(sk, ctAND2)
  48. print(f"1 AND 0 = {result}")
  49. result = cc.Decrypt(sk, ctAND3)
  50. print(f"0 AND 0 = {result}")
  51. result = cc.Decrypt(sk, ctAND4)
  52. print(f"0 AND 1 = {result}")
  53. # Sample Program: Step 6: Evaluation of OR gates
  54. ctOR1 = cc.EvalBinGate(OR, ct10, ct11)
  55. ctOR2 = cc.EvalBinGate(OR, ct10, ct01)
  56. ctOR3 = cc.EvalBinGate(OR, ct00, ct01)
  57. ctOR4 = cc.EvalBinGate(OR, ct00, ct11)
  58. result = cc.Decrypt(sk, ctOR1)
  59. print(f"1 OR 1 = {result}")
  60. result = cc.Decrypt(sk, ctOR2)
  61. print(f"1 OR 0 = {result}")
  62. result = cc.Decrypt(sk, ctOR3)
  63. print(f"0 OR 0 = {result}")
  64. result = cc.Decrypt(sk, ctOR4)
  65. print(f"0 OR 1 = {result}")
  66. # Sample Program: Step 7: Evaluation of NOR gates
  67. ctNOR1 = cc.EvalBinGate(NOR, ct10, ct11)
  68. ctNOR2 = cc.EvalBinGate(NOR, ct10, ct01)
  69. ctNOR3 = cc.EvalBinGate(NOR, ct00, ct01)
  70. ctNOR4 = cc.EvalBinGate(NOR, ct00, ct11)
  71. result = cc.Decrypt(sk, ctNOR1)
  72. print(f"1 NOR 1 = {result}")
  73. result = cc.Decrypt(sk, ctNOR2)
  74. print(f"1 NOR 0 = {result}")
  75. result = cc.Decrypt(sk, ctNOR3)
  76. print(f"0 NOR 0 = {result}")
  77. result = cc.Decrypt(sk, ctNOR4)
  78. print(f"0 NOR 1 = {result}")
  79. # Sample Program: Step 8: Evaluation of XOR gates
  80. ctXOR1 = cc.EvalBinGate(XOR, ct10, ct11)
  81. ctXOR2 = cc.EvalBinGate(XOR, ct10, ct01)
  82. ctXOR3 = cc.EvalBinGate(XOR, ct00, ct01)
  83. ctXOR4 = cc.EvalBinGate(XOR, ct00, ct11)
  84. result = cc.Decrypt(sk, ctXOR1)
  85. print(f"1 XOR 1 = {result}")
  86. result = cc.Decrypt(sk, ctXOR2)
  87. print(f"1 XOR 0 = {result}")
  88. result = cc.Decrypt(sk, ctXOR3)
  89. print(f"0 XOR 0 = {result}")
  90. result = cc.Decrypt(sk, ctXOR4)
  91. print(f"0 XOR 1 = {result}")
  92. # Sample Program: Step 9: Evaluation of XNOR gates
  93. ctXNOR1 = cc.EvalBinGate(XNOR, ct10, ct11)
  94. ctXNOR2 = cc.EvalBinGate(XNOR, ct10, ct01)
  95. ctXNOR3 = cc.EvalBinGate(XNOR, ct00, ct01)
  96. ctXNOR4 = cc.EvalBinGate(XNOR, ct00, ct11)
  97. result = cc.Decrypt(sk, ctXNOR1)
  98. print(f"1 XNOR 1 = {result}")
  99. result = cc.Decrypt(sk, ctXNOR2)
  100. print(f"1 XNOR 0 = {result}")
  101. result = cc.Decrypt(sk, ctXNOR3)
  102. print(f"0 XNOR 0 = {result}")
  103. result = cc.Decrypt(sk, ctXNOR4)
  104. print(f"0 XNOR 1 = {result}")
  105. # Sample Program: Step 90: Evaluation of NOR gates
  106. # using XOR_FAT (1 boostrap but the probability of failure is higher)
  107. ctNOR1_FAST = cc.EvalBinGate(XOR_FAST, ct10, ct11)
  108. ctNOR2_FAST = cc.EvalBinGate(XOR_FAST, ct10, ct01)
  109. ctNOR3_FAST = cc.EvalBinGate(XOR_FAST, ct00, ct01)
  110. ctNOR4_FAST = cc.EvalBinGate(XOR_FAST, ct00, ct11)
  111. result = cc.Decrypt(sk, ctNOR1_FAST)
  112. print(f"1 XOR_FAST 1 = {result}")
  113. result = cc.Decrypt(sk, ctNOR2_FAST)
  114. print(f"1 XOR_FAST 0 = {result}")
  115. result = cc.Decrypt(sk, ctNOR3_FAST)
  116. print(f"0 XOR_FAST 0 = {result}")
  117. result = cc.Decrypt(sk, ctNOR4_FAST)
  118. print(f"0 XOR_FAST 1 = {result}")
  119. # Sample Program: Step 10: Evaluation of XNOR gates
  120. # using XNOR_FAT (1 boostrap but the probability of failure is higher)
  121. ctXNOR1_FAST = cc.EvalBinGate(XNOR_FAST, ct10, ct11)
  122. ctXNOR2_FAST = cc.EvalBinGate(XNOR_FAST, ct10, ct01)
  123. ctXNOR3_FAST = cc.EvalBinGate(XNOR_FAST, ct00, ct01)
  124. ctXNOR4_FAST = cc.EvalBinGate(XNOR_FAST, ct00, ct11)
  125. result = cc.Decrypt(sk, ctXNOR1_FAST)
  126. print(f"1 XNOR_FAST 1 = {result}")
  127. result = cc.Decrypt(sk, ctXNOR2_FAST)
  128. print(f"1 XNOR_FAST 0 = {result}")
  129. result = cc.Decrypt(sk, ctXNOR3_FAST)
  130. print(f"0 XNOR_FAST 0 = {result}")
  131. result = cc.Decrypt(sk, ctXNOR4_FAST)
  132. print(f"0 XNOR_FAST 1 = {result}")