function_evaluation.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #![allow(non_snake_case)]
  2. use openfhe::cxx::{CxxVector, SharedPtr};
  3. use openfhe::ffi as ffi;
  4. fn EvalLogisticExample()
  5. {
  6. println!("--------------------------------- EVAL LOGISTIC FUNCTION ---------------------------------");
  7. let mut _cc_params_ckksrns = ffi::GenParamsCKKSRNS();
  8. _cc_params_ckksrns.pin_mut().SetSecurityLevel(ffi::SecurityLevel::HEStd_NotSet);
  9. _cc_params_ckksrns.pin_mut().SetRingDim(1 << 10);
  10. // #if NATIVEINT == 128
  11. // let _scaling_mod_size: u32 = 78;
  12. // let _first_mod_size: u32 = 89;
  13. let _scaling_mod_size: u32 = 50;
  14. let _first_mod_size: u32 = 60;
  15. _cc_params_ckksrns.pin_mut().SetScalingModSize(_scaling_mod_size);
  16. _cc_params_ckksrns.pin_mut().SetFirstModSize(_first_mod_size);
  17. let _poly_degree: u32 = 16;
  18. let _mult_depth: u32 = 6;
  19. _cc_params_ckksrns.pin_mut().SetMultiplicativeDepth(_mult_depth);
  20. let _cc = ffi::GenCryptoContextByParamsCKKSRNS(&_cc_params_ckksrns);
  21. _cc.Enable(ffi::PKESchemeFeature::PKE);
  22. _cc.Enable(ffi::PKESchemeFeature::KEYSWITCH);
  23. _cc.Enable(ffi::PKESchemeFeature::LEVELEDSHE);
  24. _cc.Enable(ffi::PKESchemeFeature::ADVANCEDSHE);
  25. let _key_pair = _cc.KeyGen();
  26. _cc.EvalMultKeyGen(&_key_pair.GetPrivateKey());
  27. let mut _input = CxxVector::<ffi::ComplexPair>::new();
  28. _input.pin_mut().push(ffi::ComplexPair{re: -4.0, im: 0.0});
  29. _input.pin_mut().push(ffi::ComplexPair{re: -3.0, im: 0.0});
  30. _input.pin_mut().push(ffi::ComplexPair{re: -2.0, im: 0.0});
  31. _input.pin_mut().push(ffi::ComplexPair{re: -1.0, im: 0.0});
  32. _input.pin_mut().push(ffi::ComplexPair{re: 0.0, im: 0.0});
  33. _input.pin_mut().push(ffi::ComplexPair{re: 1.0, im: 0.0});
  34. _input.pin_mut().push(ffi::ComplexPair{re: 2.0, im: 0.0});
  35. _input.pin_mut().push(ffi::ComplexPair{re: 3.0, im: 0.0});
  36. _input.pin_mut().push(ffi::ComplexPair{re: 4.0, im: 0.0});
  37. let _encoded_length: usize = _input.len();
  38. let _plain_text = _cc.MakeCKKSPackedPlaintextByVectorOfComplex(&_input, 1, 0, SharedPtr::<ffi::DCRTPolyParams>::null(), 0);
  39. let _cipher_text = _cc.EncryptByPublicKey(&_key_pair.GetPublicKey(), &_plain_text);
  40. let _lower_bound: f64 = -5.0;
  41. let _upper_bound: f64 = 5.0;
  42. let _result = _cc.EvalLogistic(&_cipher_text, _lower_bound, _upper_bound, _poly_degree);
  43. let mut _plain_text_dec = ffi::GenNullPlainText();
  44. _cc.DecryptByPrivateKeyAndCiphertext(&_key_pair.GetPrivateKey(), &_result, _plain_text_dec.pin_mut());
  45. _plain_text_dec.SetLength(_encoded_length);
  46. let mut _expected_output = CxxVector::<ffi::ComplexPair>::new();
  47. _expected_output.pin_mut().push(ffi::ComplexPair{re: 0.0179885, im: 0.0});
  48. _expected_output.pin_mut().push(ffi::ComplexPair{re: 0.0474289, im: 0.0});
  49. _expected_output.pin_mut().push(ffi::ComplexPair{re: 0.119205, im: 0.0});
  50. _expected_output.pin_mut().push(ffi::ComplexPair{re: 0.268936, im: 0.0});
  51. _expected_output.pin_mut().push(ffi::ComplexPair{re: 0.5, im: 0.0});
  52. _expected_output.pin_mut().push(ffi::ComplexPair{re: 0.731064, im: 0.0});
  53. _expected_output.pin_mut().push(ffi::ComplexPair{re: 0.880795, im: 0.0});
  54. _expected_output.pin_mut().push(ffi::ComplexPair{re: 0.952571, im: 0.0});
  55. _expected_output.pin_mut().push(ffi::ComplexPair{re: 0.982011, im: 0.0});
  56. println!("Expected output\n\t[{} ]", _expected_output.iter().fold(String::new(), |acc, arg| acc + " " + "(" + &arg.re.to_string() + "," + &arg.im.to_string() + ")"));
  57. let _final_result = _plain_text_dec.GetCopyOfCKKSPackedValue();
  58. println!("Actual output\n\t[{} ]", _final_result.iter().fold(String::new(), |acc, arg| acc + " " + "(" + &arg.re.to_string() + "," + &arg.im.to_string() + ")"));
  59. }
  60. fn GetSqrt(x: f64, ret: &mut f64)
  61. {
  62. *ret = x.sqrt();
  63. }
  64. fn EvalFunctionExample()
  65. {
  66. println!("--------------------------------- EVAL SQUARE ROOT FUNCTION ---------------------------------");
  67. let mut _cc_params_ckksrns = ffi::GenParamsCKKSRNS();
  68. _cc_params_ckksrns.pin_mut().SetSecurityLevel(ffi::SecurityLevel::HEStd_NotSet);
  69. _cc_params_ckksrns.pin_mut().SetRingDim(1 << 10);
  70. // #if NATIVEINT == 128
  71. // let _scaling_mod_size: u32 = 78;
  72. // let _first_mod_size: u32 = 89;
  73. let _scaling_mod_size: u32 = 50;
  74. let _first_mod_size: u32 = 60;
  75. _cc_params_ckksrns.pin_mut().SetScalingModSize(_scaling_mod_size);
  76. _cc_params_ckksrns.pin_mut().SetFirstModSize(_first_mod_size);
  77. let _poly_degree: u32 = 50;
  78. let _mult_depth: u32 = 7;
  79. _cc_params_ckksrns.pin_mut().SetMultiplicativeDepth(_mult_depth);
  80. let _cc = ffi::GenCryptoContextByParamsCKKSRNS(&_cc_params_ckksrns);
  81. _cc.Enable(ffi::PKESchemeFeature::PKE);
  82. _cc.Enable(ffi::PKESchemeFeature::KEYSWITCH);
  83. _cc.Enable(ffi::PKESchemeFeature::LEVELEDSHE);
  84. _cc.Enable(ffi::PKESchemeFeature::ADVANCEDSHE);
  85. let _key_pair = _cc.KeyGen();
  86. _cc.EvalMultKeyGen(&_key_pair.GetPrivateKey());
  87. let mut _input = CxxVector::<ffi::ComplexPair>::new();
  88. _input.pin_mut().push(ffi::ComplexPair{re: 1.0, im: 0.0});
  89. _input.pin_mut().push(ffi::ComplexPair{re: 2.0, im: 0.0});
  90. _input.pin_mut().push(ffi::ComplexPair{re: 3.0, im: 0.0});
  91. _input.pin_mut().push(ffi::ComplexPair{re: 4.0, im: 0.0});
  92. _input.pin_mut().push(ffi::ComplexPair{re: 5.0, im: 0.0});
  93. _input.pin_mut().push(ffi::ComplexPair{re: 6.0, im: 0.0});
  94. _input.pin_mut().push(ffi::ComplexPair{re: 7.0, im: 0.0});
  95. _input.pin_mut().push(ffi::ComplexPair{re: 8.0, im: 0.0});
  96. _input.pin_mut().push(ffi::ComplexPair{re: 9.0, im: 0.0});
  97. let _encoded_length: usize = _input.len();
  98. let _plain_text = _cc.MakeCKKSPackedPlaintextByVectorOfComplex(&_input, 1, 0, SharedPtr::<ffi::DCRTPolyParams>::null(), 0);
  99. let _cipher_text = _cc.EncryptByPublicKey(&_key_pair.GetPublicKey(), &_plain_text);
  100. let _lower_bound: f64 = 0.0;
  101. let _upper_bound: f64 = 10.0;
  102. let _result = _cc.EvalChebyshevFunction(GetSqrt, &_cipher_text, _lower_bound, _upper_bound, _poly_degree);
  103. let mut _plain_text_dec = ffi::GenNullPlainText();
  104. _cc.DecryptByPrivateKeyAndCiphertext(&_key_pair.GetPrivateKey(), &_result, _plain_text_dec.pin_mut());
  105. _plain_text_dec.SetLength(_encoded_length);
  106. let mut _expected_output = CxxVector::<ffi::ComplexPair>::new();
  107. _expected_output.pin_mut().push(ffi::ComplexPair{re: 1.0, im: 0.0});
  108. _expected_output.pin_mut().push(ffi::ComplexPair{re: 1.414213, im: 0.0});
  109. _expected_output.pin_mut().push(ffi::ComplexPair{re: 1.732050, im: 0.0});
  110. _expected_output.pin_mut().push(ffi::ComplexPair{re: 2.0, im: 0.0});
  111. _expected_output.pin_mut().push(ffi::ComplexPair{re: 2.236067, im: 0.0});
  112. _expected_output.pin_mut().push(ffi::ComplexPair{re: 2.449489, im: 0.0});
  113. _expected_output.pin_mut().push(ffi::ComplexPair{re: 2.645751, im: 0.0});
  114. _expected_output.pin_mut().push(ffi::ComplexPair{re: 2.828427, im: 0.0});
  115. _expected_output.pin_mut().push(ffi::ComplexPair{re: 3.0, im: 0.0});
  116. println!("Expected output\n\t[{} ]", _expected_output.iter().fold(String::new(), |acc, arg| acc + " " + "(" + &arg.re.to_string() + "," + &arg.im.to_string() + ")"));
  117. let _final_result = _plain_text_dec.GetCopyOfCKKSPackedValue();
  118. println!("Actual output\n\t[{} ]", _final_result.iter().fold(String::new(), |acc, arg| acc + " " + "(" + &arg.re.to_string() + "," + &arg.im.to_string() + ")"));
  119. }
  120. fn main()
  121. {
  122. EvalLogisticExample();
  123. EvalFunctionExample();
  124. }