simple_real_numbers.rs 4.2 KB

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