simple_real_numbers.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. use openfhe::cxx::{CxxVector, SharedPtr};
  2. use openfhe::ffi as ffi;
  3. fn main()
  4. {
  5. let _mult_depth: u32 = 1;
  6. let _scale_mod_size: u32 = 50;
  7. let _batch_size: u32 = 8;
  8. let mut _cc_params_ckksrns = ffi::GenParamsCKKSRNS();
  9. _cc_params_ckksrns.pin_mut().SetMultiplicativeDepth(_mult_depth);
  10. _cc_params_ckksrns.pin_mut().SetScalingModSize(_scale_mod_size);
  11. _cc_params_ckksrns.pin_mut().SetBatchSize(_batch_size);
  12. let _cc = ffi::GenCryptoContextByParamsCKKSRNS(&_cc_params_ckksrns);
  13. _cc.Enable(ffi::PKESchemeFeature::PKE);
  14. _cc.Enable(ffi::PKESchemeFeature::KEYSWITCH);
  15. _cc.Enable(ffi::PKESchemeFeature::LEVELEDSHE);
  16. println!("CKKS scheme is using ring dimension {}\n", _cc.GetRingDimension());
  17. let _key_pair = _cc.KeyGen();
  18. _cc.EvalMultKeyGen(&_key_pair.GetPrivateKey());
  19. let mut _index_list = CxxVector::<i32>::new();
  20. _index_list.pin_mut().push(1);
  21. _index_list.pin_mut().push(-2);
  22. _cc.EvalRotateKeyGen(&_key_pair.GetPrivateKey(), &_index_list, &ffi::GenNullPublicKey());
  23. let mut _x_1 = CxxVector::<f64>::new();
  24. _x_1.pin_mut().push(0.25);
  25. _x_1.pin_mut().push(0.5);
  26. _x_1.pin_mut().push(0.75);
  27. _x_1.pin_mut().push(1.0);
  28. _x_1.pin_mut().push(2.0);
  29. _x_1.pin_mut().push(3.0);
  30. _x_1.pin_mut().push(4.0);
  31. _x_1.pin_mut().push(5.0);
  32. let mut _x_2 = CxxVector::<f64>::new();
  33. _x_2.pin_mut().push(5.0);
  34. _x_2.pin_mut().push(4.0);
  35. _x_2.pin_mut().push(3.0);
  36. _x_2.pin_mut().push(2.0);
  37. _x_2.pin_mut().push(1.0);
  38. _x_2.pin_mut().push(0.75);
  39. _x_2.pin_mut().push(0.5);
  40. _x_2.pin_mut().push(0.25);
  41. let _p_txt_1 = _cc.MakeCKKSPackedPlaintext(&_x_1, 1, 0, SharedPtr::<ffi::DCRTPolyParams>::null(), 0);
  42. let _p_txt_2 = _cc.MakeCKKSPackedPlaintext(&_x_2, 1, 0, SharedPtr::<ffi::DCRTPolyParams>::null(), 0);
  43. println!("Input x1: {}", _p_txt_1.GetString());
  44. println!("Input x2: {}", _p_txt_2.GetString());
  45. let _c1 = _cc.EncryptByPublicKey(&_key_pair.GetPublicKey(), &_p_txt_1);
  46. let _c2 = _cc.EncryptByPublicKey(&_key_pair.GetPublicKey(), &_p_txt_2);
  47. let _c_add = _cc.EvalAddByCiphertexts(&_c1, &_c2);
  48. let _c_sub = _cc.EvalSubByCiphertexts(&_c1, &_c2);
  49. let _c_scalar = _cc.EvalMultByCiphertextAndConst(&_c1, 4.0);
  50. let _c_mul = _cc.EvalMultByCiphertexts(&_c1, &_c2);
  51. let _c_rot_1 = _cc.EvalRotate(&_c1, 1);
  52. let _c_rot_2 = _cc.EvalRotate(&_c1, -2);
  53. let mut _result = ffi::GenNullPlainText();
  54. println!("\nResults of homomorphic computations:");
  55. _cc.DecryptByPrivateKeyAndCiphertext(&_key_pair.GetPrivateKey(), &_c1, _result.pin_mut());
  56. _result.SetLength(_batch_size.try_into().unwrap());
  57. println!("x1 = {}Estimated precision in bits: {}", _result.GetString(), _result.GetLogPrecision());
  58. _cc.DecryptByPrivateKeyAndCiphertext(&_key_pair.GetPrivateKey(), &_c_add, _result.pin_mut());
  59. _result.SetLength(_batch_size.try_into().unwrap());
  60. println!("x1 + x2 = {}Estimated precision in bits: {}",_result.GetString(), _result.GetLogPrecision());
  61. _cc.DecryptByPrivateKeyAndCiphertext(&_key_pair.GetPrivateKey(), &_c_sub, _result.pin_mut());
  62. _result.SetLength(_batch_size.try_into().unwrap());
  63. println!("x1 - x2 = {}", _result.GetString());
  64. _cc.DecryptByPrivateKeyAndCiphertext(&_key_pair.GetPrivateKey(), &_c_scalar, _result.pin_mut());
  65. _result.SetLength(_batch_size.try_into().unwrap());
  66. println!("4 * x1 = {}", _result.GetString());
  67. _cc.DecryptByPrivateKeyAndCiphertext(&_key_pair.GetPrivateKey(), &_c_mul, _result.pin_mut());
  68. _result.SetLength(_batch_size.try_into().unwrap());
  69. println!("x1 * x2 = {}", _result.GetString());
  70. _cc.DecryptByPrivateKeyAndCiphertext(&_key_pair.GetPrivateKey(), &_c_rot_1, _result.pin_mut());
  71. _result.SetLength(_batch_size.try_into().unwrap());
  72. println!("\nIn rotations, very small outputs (~10^-10 here) correspond to 0's:");
  73. println!("x1 rotate by 1 = {}", _result.GetString());
  74. _cc.DecryptByPrivateKeyAndCiphertext(&_key_pair.GetPrivateKey(), &_c_rot_2, _result.pin_mut());
  75. _result.SetLength(_batch_size.try_into().unwrap());
  76. println!("x1 rotate by -2 = {}", _result.GetString());
  77. }