main.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use tfhe::{
  19. boolean::prelude::{gen_keys as boolean_gen_keys, *},
  20. integer::gen_keys_radix,
  21. shortint::prelude::{gen_keys as shortint_gen_keys, *},
  22. };
  23. fn main() {
  24. // ===============
  25. // Boolean circuit
  26. // ===============
  27. // Generate a set of client/server keys, using the default parameters.
  28. // The client generates both keys. The server key is meant to be published
  29. // so that homomorphic circuits can be computed.
  30. let (client_key, server_key) = boolean_gen_keys();
  31. // Encrypt two messages using the (private) client key:
  32. let msg1 = true;
  33. let msg2 = false;
  34. let ct_1 = client_key.encrypt(msg1);
  35. let ct_2 = client_key.encrypt(msg2);
  36. // We use the server public key to execute a boolean circuit:
  37. // if ((NOT ct_2) NAND (ct_1 AND ct_2)) then (NOT ct_2) else (ct_1 AND ct_2)
  38. let ct_3 = server_key.not(&ct_2);
  39. let ct_4 = server_key.and(&ct_1, &ct_2);
  40. let ct_5 = server_key.nand(&ct_3, &ct_4);
  41. let ct_6 = server_key.mux(&ct_5, &ct_3, &ct_4);
  42. // We use the client key to decrypt the output of the circuit
  43. let output = client_key.decrypt(&ct_6);
  44. assert!(output);
  45. // ================
  46. // Shortint circuit
  47. // ================
  48. // Generate a set of client/server keys
  49. // with 2 bits of message and 2 bits of carry
  50. let (client_key, server_key) = shortint_gen_keys(PARAM_MESSAGE_2_CARRY_2);
  51. let msg1 = 3;
  52. let msg2 = 2;
  53. // Encrypt two messages using the (private) client key:
  54. let ct_1 = client_key.encrypt(msg1);
  55. let ct_2 = client_key.encrypt(msg2);
  56. // Homomorphically compute an addition
  57. let ct_add = server_key.unchecked_add(&ct_1, &ct_2);
  58. // Define the Hamming weight function
  59. // f: x -> sum of the bits of x
  60. let f = |x: u64| x.count_ones() as u64;
  61. // Generate the accumulator for the function
  62. let acc = server_key.generate_accumulator(f);
  63. // Compute the function over the ciphertext using the PBS
  64. let ct_res = server_key.apply_lookup_table(&ct_add, &acc);
  65. // Decrypt the ciphertext using the (private) client key
  66. let output = client_key.decrypt(&ct_res);
  67. assert_eq!(output, f(msg1 + msg2));
  68. // ===============
  69. // Integer circuit
  70. // ===============
  71. // We create keys to create 16 bits integers
  72. // using 8 blocks of 2 bits
  73. let (cks, sks) = gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, 8);
  74. let clear_a = 2382u16;
  75. let clear_b = 29374u16;
  76. let mut a = cks.encrypt(clear_a as u64);
  77. let mut b = cks.encrypt(clear_b as u64);
  78. let encrypted_max = sks.smart_max_parallelized(&mut a, &mut b);
  79. let decrypted_max: u64 = cks.decrypt(&encrypted_max);
  80. assert_eq!(decrypted_max as u16, clear_a.max(clear_b))
  81. }