polynomial_evaluation.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. use openfhe::cxx::{CxxVector, SharedPtr};
  2. use openfhe::ffi as ffi;
  3. fn main()
  4. {
  5. use std::time::Instant;
  6. println!("\n======EXAMPLE FOR EVALPOLY========\n");
  7. let mut _cc_params_ckksrns = ffi::GenParamsCKKSRNS();
  8. _cc_params_ckksrns.pin_mut().SetMultiplicativeDepth(6);
  9. _cc_params_ckksrns.pin_mut().SetScalingModSize(50);
  10. let _cc = ffi::GenCryptoContextByParamsCKKSRNS(&_cc_params_ckksrns);
  11. _cc.Enable(ffi::PKESchemeFeature::PKE);
  12. _cc.Enable(ffi::PKESchemeFeature::KEYSWITCH);
  13. _cc.Enable(ffi::PKESchemeFeature::LEVELEDSHE);
  14. _cc.Enable(ffi::PKESchemeFeature::ADVANCEDSHE);
  15. let mut _input = CxxVector::<ffi::ComplexPair>::new();
  16. _input.pin_mut().push(ffi::ComplexPair{re: 0.5, im: 0.0});
  17. _input.pin_mut().push(ffi::ComplexPair{re: 0.7, im: 0.0});
  18. _input.pin_mut().push(ffi::ComplexPair{re: 0.9, im: 0.0});
  19. _input.pin_mut().push(ffi::ComplexPair{re: 0.95, im: 0.0});
  20. _input.pin_mut().push(ffi::ComplexPair{re: 0.93, im: 0.0});
  21. let _encoded_length = _input.len();
  22. let mut _coefficients_1 = CxxVector::<f64>::new();
  23. _coefficients_1.pin_mut().push(0.15);
  24. _coefficients_1.pin_mut().push(0.75);
  25. _coefficients_1.pin_mut().push(0.0);
  26. _coefficients_1.pin_mut().push(1.25);
  27. _coefficients_1.pin_mut().push(0.0);
  28. _coefficients_1.pin_mut().push(0.0);
  29. _coefficients_1.pin_mut().push(1.0);
  30. _coefficients_1.pin_mut().push(0.0);
  31. _coefficients_1.pin_mut().push(1.0);
  32. _coefficients_1.pin_mut().push(2.0);
  33. _coefficients_1.pin_mut().push(0.0);
  34. _coefficients_1.pin_mut().push(1.0);
  35. _coefficients_1.pin_mut().push(0.0);
  36. _coefficients_1.pin_mut().push(0.0);
  37. _coefficients_1.pin_mut().push(0.0);
  38. _coefficients_1.pin_mut().push(0.0);
  39. _coefficients_1.pin_mut().push(1.0);
  40. let mut _coefficients_2 = CxxVector::<f64>::new();
  41. _coefficients_2.pin_mut().push(1.0);
  42. _coefficients_2.pin_mut().push(2.0);
  43. _coefficients_2.pin_mut().push(3.0);
  44. _coefficients_2.pin_mut().push(4.0);
  45. _coefficients_2.pin_mut().push(5.0);
  46. _coefficients_2.pin_mut().push(-1.0);
  47. _coefficients_2.pin_mut().push(-2.0);
  48. _coefficients_2.pin_mut().push(-3.0);
  49. _coefficients_2.pin_mut().push(-4.0);
  50. _coefficients_2.pin_mut().push(-5.0);
  51. _coefficients_2.pin_mut().push(0.1);
  52. _coefficients_2.pin_mut().push(0.2);
  53. _coefficients_2.pin_mut().push(0.3);
  54. _coefficients_2.pin_mut().push(0.4);
  55. _coefficients_2.pin_mut().push(0.5);
  56. _coefficients_2.pin_mut().push(-0.1);
  57. _coefficients_2.pin_mut().push(-0.2);
  58. _coefficients_2.pin_mut().push(-0.3);
  59. _coefficients_2.pin_mut().push(-0.4);
  60. _coefficients_2.pin_mut().push(-0.5);
  61. _coefficients_2.pin_mut().push(0.1);
  62. _coefficients_2.pin_mut().push(0.2);
  63. _coefficients_2.pin_mut().push(0.3);
  64. _coefficients_2.pin_mut().push(0.4);
  65. _coefficients_2.pin_mut().push(0.5);
  66. _coefficients_2.pin_mut().push(-0.1);
  67. _coefficients_2.pin_mut().push(-0.2);
  68. _coefficients_2.pin_mut().push(-0.3);
  69. _coefficients_2.pin_mut().push(-0.4);
  70. _coefficients_2.pin_mut().push(-0.5);
  71. let _plain_text_1 = _cc.MakeCKKSPackedPlaintextByVectorOfComplex(&_input, 1, 0, SharedPtr::<ffi::DCRTPolyParams>::null(), 0);
  72. let _key_pair = _cc.KeyGen();
  73. print!("Generating evaluation key for homomorphic multiplication...");
  74. _cc.EvalMultKeyGen(&_key_pair.GetPrivateKey());
  75. println!("Completed.\n");
  76. let mut _cipher_text_1 = _cc.EncryptByPublicKey(&_key_pair.GetPublicKey(), &_plain_text_1);
  77. let mut _start = Instant::now();
  78. let _result = _cc.EvalPoly(&_cipher_text_1, &_coefficients_1);
  79. let _time_eval_poly_1 = _start.elapsed();
  80. _start = Instant::now();
  81. let _result_2 = _cc.EvalPoly(&_cipher_text_1, &_coefficients_2);
  82. let _time_eval_poly_2 = _start.elapsed();
  83. let mut _plain_text_dec = ffi::GenNullPlainText();
  84. _cc.DecryptByPrivateKeyAndCiphertext(&_key_pair.GetPrivateKey(), &_result, _plain_text_dec.pin_mut());
  85. _plain_text_dec.SetLength(_encoded_length);
  86. let mut _plain_text_dec_2 = ffi::GenNullPlainText();
  87. _cc.DecryptByPrivateKeyAndCiphertext(&_key_pair.GetPrivateKey(), &_result_2, _plain_text_dec_2.pin_mut());
  88. _plain_text_dec_2.SetLength(_encoded_length);
  89. println!("\n Original Plaintext #1:");
  90. println!("{}", _plain_text_1.GetString());
  91. println!("\n Result of evaluating a polynomial with coefficients [{} ]", _coefficients_1.iter().fold(String::new(), |acc, &arg| acc + " " + &arg.to_string()));
  92. println!("{}", _plain_text_dec.GetString());
  93. println!("\n Expected result: (0.70519107, 1.38285078, 3.97211180, 5.60215665, 4.86357575)");
  94. println!("\n Evaluation time: {:.0?}", _time_eval_poly_1);
  95. println!("\n Result of evaluating a polynomial with coefficients [{} ]", _coefficients_2.iter().fold(String::new(), |acc, &arg| acc + " " + &arg.to_string()));
  96. println!("{}\n", _plain_text_dec_2.GetString());
  97. println!(" Expected result: (3.4515092326, 5.3752765397, 4.8993108833, 3.2495023573, 4.0485229982)");
  98. print!("\n Evaluation time: {:.0?}\n", _time_eval_poly_2);
  99. }