main.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 std::time::Instant;
  19. use tfhe::integer::ciphertext::RadixCiphertext;
  20. use tfhe::integer::keycache::IntegerKeyCache;
  21. use tfhe::integer::{IntegerKeyKind, ServerKey};
  22. use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
  23. mod fhe;
  24. mod improved_parallel_fhe;
  25. mod improved_plain;
  26. mod parallel_fhe;
  27. mod plain;
  28. /// The number of blocks to be used in the Radix.
  29. const NUMBER_OF_BLOCKS: usize = 8;
  30. #[allow(clippy::type_complexity)]
  31. fn test_cases() -> Vec<(String, (Vec<u16>, Vec<u16>, Vec<u16>, Vec<u16>))> {
  32. vec![
  33. (
  34. "empty sell orders".to_owned(),
  35. (vec![], (1..11).collect::<Vec<_>>(), vec![], vec![0; 10]),
  36. ),
  37. (
  38. "empty buy orders".to_owned(),
  39. ((1..11).collect::<Vec<_>>(), vec![], vec![0; 10], vec![]),
  40. ),
  41. (
  42. "exact matching of sell and buy orders".to_owned(),
  43. (
  44. (1..11).collect::<Vec<_>>(),
  45. (1..11).collect::<Vec<_>>(),
  46. (1..11).collect::<Vec<_>>(),
  47. (1..11).collect::<Vec<_>>(),
  48. ),
  49. ),
  50. (
  51. "a case where there are more buy orders than sell orders".to_owned(),
  52. (vec![10; 10], vec![200], vec![10; 10], vec![100]),
  53. ),
  54. (
  55. "a case where there are more sell orders than buy orders".to_owned(),
  56. (vec![200], vec![10; 10], vec![100], vec![10; 10]),
  57. ),
  58. (
  59. "maximum input size for sell and buy orders".to_owned(),
  60. (
  61. vec![100; 499],
  62. vec![100; 499],
  63. vec![100; 499],
  64. vec![100; 499],
  65. ),
  66. ),
  67. ]
  68. }
  69. /// Runs the given [tester] function with the test cases for volume matching algorithm.
  70. fn run_test_cases(tester: impl Fn(&[u16], &[u16], &[u16], &[u16])) {
  71. for (test_name, test_case) in &test_cases() {
  72. println!("Testing {test_name}...");
  73. tester(&test_case.0, &test_case.1, &test_case.2, &test_case.3);
  74. println!();
  75. }
  76. }
  77. fn test_volume_match_plain(function: fn(&mut [u16], &mut [u16])) {
  78. println!("Running test cases for the plain implementation");
  79. run_test_cases(|a, b, c, d| plain::tester(a, b, c, d, function));
  80. }
  81. fn test_volume_match_fhe(
  82. fhe_function: fn(&mut [RadixCiphertext], &mut [RadixCiphertext], &ServerKey),
  83. ) {
  84. println!("Generating keys...");
  85. let time = Instant::now();
  86. let (client_key, server_key) =
  87. IntegerKeyCache.get_from_params(PARAM_MESSAGE_2_CARRY_2_KS_PBS, IntegerKeyKind::Radix);
  88. println!("Keys generated in {:?}", time.elapsed());
  89. println!("Running test cases for the FHE implementation");
  90. run_test_cases(|a, b, c, d| fhe::tester(&client_key, &server_key, a, b, c, d, fhe_function));
  91. }
  92. fn main() {
  93. for argument in std::env::args() {
  94. if argument == "plain" {
  95. println!("Running plain version");
  96. test_volume_match_plain(plain::volume_match);
  97. println!();
  98. }
  99. if argument == "plain-improved" {
  100. println!("Running plain improved version");
  101. test_volume_match_plain(improved_plain::volume_match);
  102. println!();
  103. }
  104. if argument == "fhe" {
  105. println!("Running fhe version");
  106. test_volume_match_fhe(fhe::volume_match);
  107. println!();
  108. }
  109. if argument == "fhe-parallel" {
  110. println!("Running parallelized fhe version");
  111. test_volume_match_fhe(parallel_fhe::volume_match);
  112. println!();
  113. }
  114. if argument == "fhe-improved" {
  115. println!("Running improved parallelized fhe fhe version");
  116. test_volume_match_fhe(improved_parallel_fhe::volume_match);
  117. println!();
  118. }
  119. }
  120. }