boolean-lmkcdey.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from openfhe import *
  2. ## Sample Program: Step 1: Set CryptoContext
  3. cc = BinFHEContext()
  4. # We use the STD128 setting optimized for the LMKCDEY mode.
  5. cc.GenerateBinFHEContext(STD128,LMKCDEY)
  6. ## Sample Program: Step 2: Key Generation
  7. # Generate the secret key
  8. sk = cc.KeyGen()
  9. print("Generating the bootstrapping keys...\n")
  10. # Generate the bootstrapping keys (refresh and switching keys)
  11. cc.BTKeyGen(sk)
  12. print("Completed the key generation.\n")
  13. # Sample Program: Step 3: Encryption
  14. """
  15. Encrypt two ciphertexts representing Boolean True (1).
  16. By default, freshly encrypted ciphertexts are bootstrapped.
  17. If you wish to get a fresh encryption without bootstrapping, write
  18. ct1 = cc.Encrypt(sk, 1, FRESH)
  19. """
  20. ct1 = cc.Encrypt(sk, 1)
  21. ct2 = cc.Encrypt(sk, 1)
  22. # Sample Program: Step 4: Evaluation
  23. # Compute (1 AND 1) = 1; Other binary gate options are OR, NAND, and NOR
  24. ctAND1 = cc.EvalBinGate(AND, ct1, ct2)
  25. # Compute (NOT 1) = 0
  26. ct2Not = cc.EvalNOT(ct2)
  27. # Compute (1 AND (NOT 1)) = 0
  28. ctAND2 = cc.EvalBinGate(AND, ct2Not, ct1)
  29. # Compute OR of the result in ctAND1 and ctAND2
  30. ctResult = cc.EvalBinGate(OR, ctAND1, ctAND2)
  31. # Sample Program: Step 5: Decryption
  32. result = cc.Decrypt(sk, ctResult)
  33. print(f"Result of encrypted computation of (1 AND 1) OR (1 AND (NOT 1)) = {result}")