advanced-real-numbers.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. from openfhe import *
  2. import time # to enable TIC-TOC timing measurements
  3. def automatic_rescale_demo(scal_tech):
  4. if(scal_tech == ScalingTechnique.FLEXIBLEAUTO):
  5. print("\n\n\n ===== FlexibleAutoDemo =============\n")
  6. else:
  7. print("\n\n\n ===== FixedAutoDemo =============\n")
  8. batch_size = 8
  9. parameters = CCParamsCKKSRNS()
  10. parameters.SetMultiplicativeDepth(5)
  11. parameters.SetScalingModSize(50)
  12. parameters.SetScalingTechnique(scal_tech)
  13. parameters.SetBatchSize(batch_size)
  14. cc = GenCryptoContext(parameters)
  15. print(f"CKKS scheme is using ring dimension {cc.GetRingDimension()}\n")
  16. cc.Enable(PKESchemeFeature.PKE)
  17. cc.Enable(PKESchemeFeature.KEYSWITCH)
  18. cc.Enable(PKESchemeFeature.LEVELEDSHE)
  19. keys = cc.KeyGen()
  20. cc.EvalMultKeyGen(keys.secretKey)
  21. # Input
  22. x = [1.0, 1.01, 1.02, 1.03, 1.04, 1.05, 1.06, 1.07]
  23. ptxt = cc.MakeCKKSPackedPlaintext(x)
  24. print(f"Input x: {ptxt}")
  25. c = cc.Encrypt(keys.publicKey,ptxt)
  26. # Computing f(x) = x^18 + x^9 + 1
  27. #
  28. # In the following we compute f(x) with a computation
  29. # that has a multiplicative depth of 5.
  30. #
  31. # The result is correct, even though there is no call to
  32. # the Rescale() operation.
  33. c2 = cc.EvalMult(c, c) # x^2
  34. c4 = cc.EvalMult(c2, c2) # x^4
  35. c8 = cc.EvalMult(c4, c4) # x^8
  36. c16 = cc.EvalMult(c8, c8) # x^16
  37. c9 = cc.EvalMult(c8, c) # x^9
  38. c18 = cc.EvalMult(c16, c2) # x^18
  39. cRes = cc.EvalAdd(cc.EvalAdd(c18, c9), 1.0) # Final result
  40. result = cc.Decrypt(cRes,keys.secretKey)
  41. print("x^18 + x^9 + 1 = ", result)
  42. result.SetLength(batch_size)
  43. print(f"Result: {result}")
  44. def manual_rescale_demo(scal_tech):
  45. print("\n\n\n ===== FixedManualDemo =============\n")
  46. batch_size = 8
  47. parameters = CCParamsCKKSRNS()
  48. parameters.SetMultiplicativeDepth(5)
  49. parameters.SetScalingModSize(50)
  50. parameters.SetBatchSize(batch_size)
  51. cc = GenCryptoContext(parameters)
  52. print(f"CKKS scheme is using ring dimension {cc.GetRingDimension()}\n")
  53. cc.Enable(PKESchemeFeature.PKE)
  54. cc.Enable(PKESchemeFeature.KEYSWITCH)
  55. cc.Enable(PKESchemeFeature.LEVELEDSHE)
  56. keys = cc.KeyGen()
  57. cc.EvalMultKeyGen(keys.secretKey)
  58. # Input
  59. x = [1.0, 1.01, 1.02, 1.03, 1.04, 1.05, 1.06, 1.07]
  60. ptxt = cc.MakeCKKSPackedPlaintext(x)
  61. print(f"Input x: {ptxt}")
  62. c = cc.Encrypt(keys.publicKey,ptxt)
  63. # Computing f(x) = x^18 + x^9 + 1
  64. #
  65. # Compare the following with the corresponding code
  66. # for FLEXIBLEAUTO. Here we need to track the depth of ciphertexts
  67. # and call Rescale whenever needed. In this instance it's still
  68. # not hard to do so, but this can be quite tedious in other
  69. # complicated computations. (e.g. in bootstrapping)
  70. #
  71. #
  72. # x^2
  73. c2_depth2 = cc.EvalMult(c, c)
  74. c2_depth1 = cc.Rescale(c2_depth2)
  75. # x^4
  76. c4_depth2 = cc.EvalMult(c2_depth1, c2_depth1)
  77. c4_depth1 = cc.Rescale(c4_depth2)
  78. # x^8
  79. c8_depth2 = cc.EvalMult(c4_depth1, c4_depth1)
  80. c8_depth1 = cc.Rescale(c8_depth2)
  81. # x^16
  82. c16_depth2 = cc.EvalMult(c8_depth1, c8_depth1)
  83. c16_depth1 = cc.Rescale(c16_depth2)
  84. # x^9
  85. c9_depth2 = cc.EvalMult(c8_depth1, c)
  86. # x^18
  87. c18_depth2 = cc.EvalMult(c16_depth1, c2_depth1)
  88. # Final result
  89. cRes_depth2 = cc.EvalAdd(cc.EvalAdd(c18_depth2, c9_depth2), 1.0)
  90. cRes_depth1 = cc.Rescale(cRes_depth2)
  91. result = cc.Decrypt(cRes_depth1,keys.secretKey)
  92. result.SetLength(batch_size)
  93. print("x^18 + x^9 + 1 = ", result)
  94. def hybrid_key_switching_demo1():
  95. print("\n\n\n ===== hybrid_key_switching_demo1 =============\n")
  96. dnum = 2
  97. batch_size = 8
  98. parameters = CCParamsCKKSRNS()
  99. parameters.SetMultiplicativeDepth(5)
  100. parameters.SetScalingModSize(50)
  101. parameters.SetBatchSize(batch_size)
  102. if get_native_int()!=128:
  103. parameters.SetScalingTechnique(ScalingTechnique.FLEXIBLEAUTO)
  104. parameters.SetNumLargeDigits(dnum)
  105. cc = GenCryptoContext(parameters)
  106. print(f"CKKS scheme is using ring dimension {cc.GetRingDimension()}\n")
  107. print(f"- Using HYBRID key switching with {dnum} digits\n")
  108. cc.Enable(PKESchemeFeature.PKE)
  109. cc.Enable(PKESchemeFeature.KEYSWITCH)
  110. cc.Enable(PKESchemeFeature.LEVELEDSHE)
  111. keys = cc.KeyGen()
  112. cc.EvalRotateKeyGen(keys.secretKey,[1,-2])
  113. # Input
  114. x = [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7]
  115. ptxt = cc.MakeCKKSPackedPlaintext(x)
  116. print(f"Input x: {ptxt}")
  117. c = cc.Encrypt(keys.publicKey,ptxt)
  118. t = time.time()
  119. c_rot1 = cc.EvalRotate(c,1)
  120. c_rot2 = cc.EvalRotate(c_rot1,-2)
  121. time2digits = time.time() - t
  122. result = cc.Decrypt(c_rot2,keys.secretKey)
  123. result.SetLength(batch_size)
  124. print(f"x rotate by -1 = {result}")
  125. print(f" - 2 rotations with HYBRID (2 digits) took {time2digits*1000} ms")
  126. def hybrid_key_switching_demo2():
  127. print("\n\n\n ===== hybrid_key_switching_demo2 =============\n")
  128. dnum = 3
  129. batch_size = 8
  130. parameters = CCParamsCKKSRNS()
  131. parameters.SetMultiplicativeDepth(5)
  132. parameters.SetScalingModSize(50)
  133. parameters.SetBatchSize(batch_size)
  134. if get_native_int()!=128:
  135. parameters.SetScalingTechnique(ScalingTechnique.FLEXIBLEAUTO)
  136. parameters.SetNumLargeDigits(dnum)
  137. cc = GenCryptoContext(parameters)
  138. # Compare the ring dimension in this demo to the one in the previous
  139. print(f"CKKS scheme is using ring dimension {cc.GetRingDimension()}\n")
  140. print(f"- Using HYBRID key switching with {dnum} digits\n")
  141. cc.Enable(PKESchemeFeature.PKE)
  142. cc.Enable(PKESchemeFeature.KEYSWITCH)
  143. cc.Enable(PKESchemeFeature.LEVELEDSHE)
  144. keys = cc.KeyGen()
  145. cc.EvalRotateKeyGen(keys.secretKey,[1,-2])
  146. # Input
  147. x = [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7]
  148. ptxt = cc.MakeCKKSPackedPlaintext(x)
  149. print(f"Input x: {ptxt}")
  150. c = cc.Encrypt(keys.publicKey,ptxt)
  151. t = time.time()
  152. c_rot1 = cc.EvalRotate(c,1)
  153. c_rot2 = cc.EvalRotate(c_rot1,-2)
  154. time3digits = time.time() - t
  155. # The runtime here is smaller than the previous demo
  156. result = cc.Decrypt(c_rot2,keys.secretKey)
  157. result.SetLength(batch_size)
  158. print(f"x rotate by -1 = {result}")
  159. print(f" - 2 rotations with HYBRID (3 digits) took {time3digits*1000} ms")
  160. def fast_rotation_demo1():
  161. print("\n\n\n ===== fast_rotation_demo1 =============\n")
  162. batch_size = 8
  163. parameters = CCParamsCKKSRNS()
  164. parameters.SetMultiplicativeDepth(5)
  165. parameters.SetScalingModSize(50)
  166. parameters.SetBatchSize(batch_size)
  167. cc = GenCryptoContext(parameters)
  168. N = cc.GetRingDimension()
  169. print(f"CKKS scheme is using ring dimension {N}\n")
  170. cc.Enable(PKESchemeFeature.PKE)
  171. cc.Enable(PKESchemeFeature.KEYSWITCH)
  172. cc.Enable(PKESchemeFeature.LEVELEDSHE)
  173. keys = cc.KeyGen()
  174. cc.EvalRotateKeyGen(keys.secretKey,[1,2,3,4,5,6,7])
  175. # Input
  176. x = [0, 0, 0, 0, 0, 0, 0, 1]
  177. ptxt = cc.MakeCKKSPackedPlaintext(x)
  178. print(f"Input x: {ptxt}")
  179. c = cc.Encrypt(keys.publicKey,ptxt)
  180. # First, we perform 7 regular (non-hoisted) rotations
  181. # and measure the runtime
  182. t = time.time()
  183. c_rot1 = cc.EvalRotate(c,1)
  184. c_rot2 = cc.EvalRotate(c,2)
  185. c_rot3 = cc.EvalRotate(c,3)
  186. c_rot4 = cc.EvalRotate(c,4)
  187. c_rot5 = cc.EvalRotate(c,5)
  188. c_rot6 = cc.EvalRotate(c,6)
  189. c_rot7 = cc.EvalRotate(c,7)
  190. time_no_hoisting = time.time() - t
  191. c_res_no_hoist = c + c_rot1 + c_rot2 + c_rot3 + c_rot4 + c_rot5 + c_rot6 + c_rot7
  192. # M is the cyclotomic order and we need it to call EvalFastRotation
  193. M = 2*N
  194. # Then, we perform 7 rotations with hoisting.
  195. t = time.time()
  196. c_precomp = cc.EvalFastRotationPrecompute(c)
  197. c_rot1 = cc.EvalFastRotation(c,1,M,c_precomp)
  198. c_rot2 = cc.EvalFastRotation(c,2,M,c_precomp)
  199. c_rot3 = cc.EvalFastRotation(c,3,M,c_precomp)
  200. c_rot4 = cc.EvalFastRotation(c,4,M,c_precomp)
  201. c_rot5 = cc.EvalFastRotation(c,5,M,c_precomp)
  202. c_rot6 = cc.EvalFastRotation(c,6,M,c_precomp)
  203. c_rot7 = cc.EvalFastRotation(c,7,M,c_precomp)
  204. time_hoisting = time.time() - t
  205. # The time with hoisting should be faster than without hoisting.
  206. c_res_hoist = c + c_rot1 + c_rot2 + c_rot3 + c_rot4 + c_rot5 + c_rot6 + c_rot7
  207. result = cc.Decrypt(c_res_no_hoist,keys.secretKey)
  208. result.SetLength(batch_size)
  209. print(f"Result without hoisting: {result}")
  210. print(f" - 7 rotations without hoisting took {time_no_hoisting*1000} ms")
  211. result = cc.Decrypt(c_res_hoist,keys.secretKey)
  212. result.SetLength(batch_size)
  213. print(f"Result with hoisting: {result}")
  214. print(f" - 7 rotations with hoisting took {time_hoisting*1000} ms")
  215. def fast_rotation_demo2():
  216. print("\n\n\n ===== fast_rotation_demo2 =============\n")
  217. digit_size = 3
  218. batch_size = 8
  219. parameters = CCParamsCKKSRNS()
  220. parameters.SetMultiplicativeDepth(1)
  221. parameters.SetScalingModSize(50)
  222. parameters.SetBatchSize(batch_size)
  223. if get_native_int()!=128:
  224. parameters.SetScalingTechnique(ScalingTechnique.FLEXIBLEAUTO)
  225. parameters.SetKeySwitchTechnique(KeySwitchTechnique.BV)
  226. parameters.SetFirstModSize(60)
  227. parameters.SetDigitSize(digit_size)
  228. cc = GenCryptoContext(parameters)
  229. N = cc.GetRingDimension()
  230. print(f"CKKS scheme is using ring dimension {N}\n")
  231. cc.Enable(PKESchemeFeature.PKE)
  232. cc.Enable(PKESchemeFeature.KEYSWITCH)
  233. cc.Enable(PKESchemeFeature.LEVELEDSHE)
  234. keys = cc.KeyGen()
  235. cc.EvalRotateKeyGen(keys.secretKey,[1,2,3,4,5,6,7])
  236. # Input
  237. x = [0, 0, 0, 0, 0, 0, 0, 1]
  238. ptxt = cc.MakeCKKSPackedPlaintext(x)
  239. print(f"Input x: {ptxt}")
  240. c = cc.Encrypt(keys.publicKey,ptxt)
  241. # First, we perform 7 regular (non-hoisted) rotations
  242. # and measure the runtime
  243. t = time.time()
  244. c_rot1 = cc.EvalRotate(c,1)
  245. c_rot2 = cc.EvalRotate(c,2)
  246. c_rot3 = cc.EvalRotate(c,3)
  247. c_rot4 = cc.EvalRotate(c,4)
  248. c_rot5 = cc.EvalRotate(c,5)
  249. c_rot6 = cc.EvalRotate(c,6)
  250. c_rot7 = cc.EvalRotate(c,7)
  251. time_no_hoisting = time.time() - t
  252. c_res_no_hoist = c + c_rot1 + c_rot2 + c_rot3 + c_rot4 + c_rot5 + c_rot6 + c_rot7
  253. # M is the cyclotomic order and we need it to call EvalFastRotation
  254. M = 2*N
  255. # Then, we perform 7 rotations with hoisting.
  256. t = time.time()
  257. c_precomp = cc.EvalFastRotationPrecompute(c)
  258. c_rot1 = cc.EvalFastRotation(c,1,M,c_precomp)
  259. c_rot2 = cc.EvalFastRotation(c,2,M,c_precomp)
  260. c_rot3 = cc.EvalFastRotation(c,3,M,c_precomp)
  261. c_rot4 = cc.EvalFastRotation(c,4,M,c_precomp)
  262. c_rot5 = cc.EvalFastRotation(c,5,M,c_precomp)
  263. c_rot6 = cc.EvalFastRotation(c,6,M,c_precomp)
  264. c_rot7 = cc.EvalFastRotation(c,7,M,c_precomp)
  265. time_hoisting = time.time() - t
  266. # The time with hoisting should be faster than without hoisting.
  267. # Also, the benefits from hoisting should be more pronounced in this
  268. # case because we're using BV. Of course, we also observe less
  269. # accurate results than when using HYBRID, because of using
  270. # digitSize = 10 (Users can decrease digitSize to see the accuracy
  271. # increase, and performance decrease).
  272. c_res_hoist = c + c_rot1 + c_rot2 + c_rot3 + c_rot4 + c_rot5 + c_rot6 + c_rot7
  273. result = cc.Decrypt(c_res_no_hoist,keys.secretKey)
  274. result.SetLength(batch_size)
  275. print(f"Result without hoisting: {result}")
  276. print(f" - 7 rotations without hoisting took {time_no_hoisting*1000} ms")
  277. result = cc.Decrypt(c_res_no_hoist,keys.secretKey)
  278. result.SetLength(batch_size)
  279. print(f"Result with hoisting: {result}")
  280. print(f" - 7 rotations with hoisting took {time_hoisting*1000} ms")
  281. def main():
  282. if get_native_int()!=128:
  283. automatic_rescale_demo(ScalingTechnique.FLEXIBLEAUTO)
  284. automatic_rescale_demo(ScalingTechnique.FIXEDAUTO)
  285. manual_rescale_demo(ScalingTechnique.FIXEDMANUAL)
  286. hybrid_key_switching_demo1()
  287. hybrid_key_switching_demo2()
  288. fast_rotation_demo1()
  289. fast_rotation_demo2()
  290. if __name__ == "__main__":
  291. main()