boolean.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from openfhe import *
  2. ## Sample Program: Step 1: Set CryptoContext
  3. cc = BinFHEContext()
  4. """
  5. STD128 is the security level of 128 bits of security based on LWE Estimator
  6. and HE standard. Other common options are TOY, MEDIUM, STD192, and STD256.
  7. MEDIUM corresponds to the level of more than 100 bits for both quantum and
  8. classical computer attacks
  9. """
  10. cc.GenerateBinFHEContext(STD128,GINX)
  11. ## Sample Program: Step 2: Key Generation
  12. # Generate the secret key
  13. sk = cc.KeyGen()
  14. print("Generating the bootstrapping keys...\n")
  15. # Generate the bootstrapping keys (refresh and switching keys)
  16. cc.BTKeyGen(sk)
  17. # Sample Program: Step 3: Encryption
  18. """
  19. Encrypt two ciphertexts representing Boolean True (1).
  20. By default, freshly encrypted ciphertexts are bootstrapped.
  21. If you wish to get a fresh encryption without bootstrapping, write
  22. ct1 = cc.Encrypt(sk, 1, FRESH)
  23. """
  24. ct1 = cc.Encrypt(sk, 1)
  25. ct2 = cc.Encrypt(sk, 1)
  26. # Sample Program: Step 4: Evaluation
  27. # Compute (1 AND 1) = 1; Other binary gate options are OR, NAND, and NOR
  28. ctAND1 = cc.EvalBinGate(AND, ct1, ct2)
  29. # Compute (NOT 1) = 0
  30. ct2Not = cc.EvalNOT(ct2)
  31. # Compute (1 AND (NOT 1)) = 0
  32. ctAND2 = cc.EvalBinGate(AND, ct2Not, ct1)
  33. # Compute OR of the result in ctAND1 and ctAND2
  34. ctResult = cc.EvalBinGate(OR, ctAND1, ctAND2)
  35. # Sample Program: Step 5: Decryption
  36. result = cc.Decrypt(sk, ctResult)
  37. print(f"Result of encrypted computation of (1 AND 1) OR (1 AND (NOT 1)) = {result}")