simple_integers.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. use openfhe::cxx::{CxxVector, SharedPtr};
  2. use openfhe::ffi as ffi;
  3. fn main()
  4. {
  5. let mut _cc_params_bfvrns = ffi::GetParamsBFVRNS();
  6. _cc_params_bfvrns.pin_mut().SetPlaintextModulus(65537);
  7. _cc_params_bfvrns.pin_mut().SetMultiplicativeDepth(2);
  8. let _cc = ffi::GenCryptoContextByParamsBFVRNS(&_cc_params_bfvrns);
  9. _cc.Enable(ffi::PKESchemeFeature::PKE);
  10. _cc.Enable(ffi::PKESchemeFeature::KEYSWITCH);
  11. _cc.Enable(ffi::PKESchemeFeature::LEVELEDSHE);
  12. let _key_pair = _cc.KeyGen();
  13. _cc.EvalMultKeyGen(_key_pair.GetPrivateKey());
  14. let mut _index_list = CxxVector::<i32>::new();
  15. _index_list.pin_mut().push(1);
  16. _index_list.pin_mut().push(2);
  17. _index_list.pin_mut().push(-1);
  18. _index_list.pin_mut().push(-2);
  19. _cc.EvalRotateKeyGen(_key_pair.GetPrivateKey(), &_index_list, SharedPtr::<ffi::PublicKeyImpl>::null());
  20. let mut _vector_of_ints_1 = CxxVector::<i64>::new();
  21. _vector_of_ints_1.pin_mut().push(1);
  22. _vector_of_ints_1.pin_mut().push(2);
  23. _vector_of_ints_1.pin_mut().push(3);
  24. _vector_of_ints_1.pin_mut().push(4);
  25. _vector_of_ints_1.pin_mut().push(5);
  26. _vector_of_ints_1.pin_mut().push(6);
  27. _vector_of_ints_1.pin_mut().push(7);
  28. _vector_of_ints_1.pin_mut().push(8);
  29. _vector_of_ints_1.pin_mut().push(9);
  30. _vector_of_ints_1.pin_mut().push(10);
  31. _vector_of_ints_1.pin_mut().push(11);
  32. _vector_of_ints_1.pin_mut().push(12);
  33. let _plain_text_1 = _cc.MakePackedPlaintext(&_vector_of_ints_1, 1, 0);
  34. let mut _vector_of_ints_2 = CxxVector::<i64>::new();
  35. _vector_of_ints_2.pin_mut().push(3);
  36. _vector_of_ints_2.pin_mut().push(2);
  37. _vector_of_ints_2.pin_mut().push(1);
  38. _vector_of_ints_2.pin_mut().push(4);
  39. _vector_of_ints_2.pin_mut().push(5);
  40. _vector_of_ints_2.pin_mut().push(6);
  41. _vector_of_ints_2.pin_mut().push(7);
  42. _vector_of_ints_2.pin_mut().push(8);
  43. _vector_of_ints_2.pin_mut().push(9);
  44. _vector_of_ints_2.pin_mut().push(10);
  45. _vector_of_ints_2.pin_mut().push(11);
  46. _vector_of_ints_2.pin_mut().push(12);
  47. let _plain_text_2 = _cc.MakePackedPlaintext(&_vector_of_ints_2, 1, 0);
  48. let mut _vector_of_ints_3 = CxxVector::<i64>::new();
  49. _vector_of_ints_3.pin_mut().push(1);
  50. _vector_of_ints_3.pin_mut().push(2);
  51. _vector_of_ints_3.pin_mut().push(5);
  52. _vector_of_ints_3.pin_mut().push(2);
  53. _vector_of_ints_3.pin_mut().push(5);
  54. _vector_of_ints_3.pin_mut().push(6);
  55. _vector_of_ints_3.pin_mut().push(7);
  56. _vector_of_ints_3.pin_mut().push(8);
  57. _vector_of_ints_3.pin_mut().push(9);
  58. _vector_of_ints_3.pin_mut().push(10);
  59. _vector_of_ints_3.pin_mut().push(11);
  60. _vector_of_ints_3.pin_mut().push(12);
  61. let _plain_text_3 = _cc.MakePackedPlaintext(&_vector_of_ints_3, 1, 0);
  62. let _cipher_text_1 = _cc.EncryptByPublicKey(_key_pair.GetPublicKey(), &_plain_text_1);
  63. let _cipher_text_2 = _cc.EncryptByPublicKey(_key_pair.GetPublicKey(), &_plain_text_2);
  64. let _cipher_text_3 = _cc.EncryptByPublicKey(_key_pair.GetPublicKey(), &_plain_text_3);
  65. let _cipher_text_add_1_2 = _cc.EvalAddByCiphertexts(&_cipher_text_1, &_cipher_text_2);
  66. let _cipher_text_add_result = _cc.EvalAddByCiphertexts(&_cipher_text_add_1_2, &_cipher_text_3);
  67. let _cipher_text_mul_1_2 = _cc.EvalMultByCiphertexts(&_cipher_text_1, &_cipher_text_2);
  68. let _cipher_text_mult_result = _cc.EvalMultByCiphertexts(&_cipher_text_mul_1_2, &_cipher_text_3);
  69. let _cipher_text_rot_1 = _cc.EvalRotate(&_cipher_text_1, 1);
  70. let _cipher_text_rot_2 = _cc.EvalRotate(&_cipher_text_1, 2);
  71. let _cipher_text_rot_3 = _cc.EvalRotate(&_cipher_text_1, -1);
  72. let _cipher_text_rot_4 = _cc.EvalRotate(&_cipher_text_1, -2);
  73. let mut _plain_text_add_result = ffi::GenEmptyPlainText();
  74. _cc.DecryptByPrivateKeyAndCiphertext(_key_pair.GetPrivateKey(), &_cipher_text_add_result, _plain_text_add_result.pin_mut());
  75. let mut _plain_text_mult_result = ffi::GenEmptyPlainText();
  76. _cc.DecryptByPrivateKeyAndCiphertext(_key_pair.GetPrivateKey(), &_cipher_text_mult_result, _plain_text_mult_result.pin_mut());
  77. let mut _plain_text_rot_1 = ffi::GenEmptyPlainText();
  78. _cc.DecryptByPrivateKeyAndCiphertext(_key_pair.GetPrivateKey(), &_cipher_text_rot_1, _plain_text_rot_1.pin_mut());
  79. let mut _plain_text_rot_2 = ffi::GenEmptyPlainText();
  80. _cc.DecryptByPrivateKeyAndCiphertext(_key_pair.GetPrivateKey(), &_cipher_text_rot_2, _plain_text_rot_2.pin_mut());
  81. let mut _plain_text_rot_3 = ffi::GenEmptyPlainText();
  82. _cc.DecryptByPrivateKeyAndCiphertext(_key_pair.GetPrivateKey(), &_cipher_text_rot_3, _plain_text_rot_3.pin_mut());
  83. let mut _plain_text_rot_4 = ffi::GenEmptyPlainText();
  84. _cc.DecryptByPrivateKeyAndCiphertext(_key_pair.GetPrivateKey(), &_cipher_text_rot_4, _plain_text_rot_4.pin_mut());
  85. _plain_text_rot_1.SetLength(_vector_of_ints_1.len());
  86. _plain_text_rot_2.SetLength(_vector_of_ints_1.len());
  87. _plain_text_rot_3.SetLength(_vector_of_ints_1.len());
  88. _plain_text_rot_4.SetLength(_vector_of_ints_1.len());
  89. println!("Plaintext #1: {}", _plain_text_1.GetString());
  90. println!("Plaintext #2: {}", _plain_text_2.GetString());
  91. println!("Plaintext #3: {}", _plain_text_3.GetString());
  92. println!("\nResults of homomorphic computations");
  93. println!("#1 + #2 + #3: {}", _plain_text_add_result.GetString());
  94. println!("#1 * #2 * #3: {}", _plain_text_mult_result.GetString());
  95. println!("Left rotation of #1 by 1: {}", _plain_text_rot_1.GetString());
  96. println!("Left rotation of #1 by 2: {}", _plain_text_rot_2.GetString());
  97. println!("Right rotation of #1 by -1: {}", _plain_text_rot_3.GetString());
  98. println!("Right rotation of #1 by -2: {}", _plain_text_rot_4.GetString());
  99. }