util.rs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. use crate::params::*;
  2. use rand::{prelude::StdRng, SeedableRng};
  3. use serde_json::Value;
  4. pub fn calc_index(indices: &[usize], lengths: &[usize]) -> usize {
  5. let mut idx = 0usize;
  6. let mut prod = 1usize;
  7. for i in (0..indices.len()).rev() {
  8. idx += indices[i] * prod;
  9. prod *= lengths[i];
  10. }
  11. idx
  12. }
  13. pub fn get_test_params() -> Params {
  14. Params::init(
  15. 2048,
  16. &vec![268369921u64, 249561089u64],
  17. 6.4,
  18. 2,
  19. 256,
  20. 20,
  21. 4,
  22. 8,
  23. 56,
  24. 8,
  25. true,
  26. 9,
  27. 6,
  28. 1,
  29. 2048,
  30. )
  31. }
  32. pub fn get_short_keygen_params() -> Params {
  33. Params::init(
  34. 2048,
  35. &vec![268369921u64, 249561089u64],
  36. 6.4,
  37. 2,
  38. 256,
  39. 20,
  40. 4,
  41. 4,
  42. 4,
  43. 4,
  44. true,
  45. 9,
  46. 6,
  47. 1,
  48. 2048,
  49. )
  50. }
  51. pub fn get_expansion_testing_params() -> Params {
  52. let cfg = r#"
  53. {'n': 2,
  54. 'nu_1': 9,
  55. 'nu_2': 6,
  56. 'p': 256,
  57. 'q_prime_bits': 20,
  58. 's_e': 87.62938774292914,
  59. 't_GSW': 8,
  60. 't_conv': 4,
  61. 't_exp': 8,
  62. 't_exp_right': 56,
  63. 'instances': 1,
  64. 'db_item_size': 256 }
  65. "#;
  66. let cfg = cfg.replace("'", "\"");
  67. let b = params_from_json(&cfg);
  68. b
  69. }
  70. pub fn get_seed() -> [u8; 32] {
  71. [
  72. 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6,
  73. 7, 8,
  74. ]
  75. }
  76. pub fn get_seeded_rng() -> StdRng {
  77. StdRng::from_seed(get_seed())
  78. }
  79. pub const fn get_empty_params() -> Params {
  80. Params {
  81. poly_len: 0,
  82. poly_len_log2: 0,
  83. ntt_tables: Vec::new(),
  84. scratch: Vec::new(),
  85. crt_count: 0,
  86. barrett_cr_0_modulus: 0,
  87. barrett_cr_1_modulus: 0,
  88. barrett_cr_0: [0u64; MAX_MODULI],
  89. barrett_cr_1: [0u64; MAX_MODULI],
  90. mod0_inv_mod1: 0,
  91. mod1_inv_mod0: 0,
  92. moduli: [0u64; MAX_MODULI],
  93. modulus: 0,
  94. modulus_log2: 0,
  95. noise_width: 0f64,
  96. n: 0,
  97. pt_modulus: 0,
  98. q2_bits: 0,
  99. t_conv: 0,
  100. t_exp_left: 0,
  101. t_exp_right: 0,
  102. t_gsw: 0,
  103. expand_queries: false,
  104. db_dim_1: 0,
  105. db_dim_2: 0,
  106. instances: 0,
  107. db_item_size: 0,
  108. }
  109. }
  110. pub fn params_from_json(cfg: &str) -> Params {
  111. let v: Value = serde_json::from_str(cfg).unwrap();
  112. let n = v["n"].as_u64().unwrap() as usize;
  113. let db_dim_1 = v["nu_1"].as_u64().unwrap() as usize;
  114. let db_dim_2 = v["nu_2"].as_u64().unwrap() as usize;
  115. let instances = v["instances"].as_u64().unwrap_or(1) as usize;
  116. let db_item_size = v["db_item_size"].as_u64().unwrap_or(1) as usize;
  117. let p = v["p"].as_u64().unwrap();
  118. let q2_bits = v["q_prime_bits"].as_u64().unwrap();
  119. let t_gsw = v["t_GSW"].as_u64().unwrap() as usize;
  120. let t_conv = v["t_conv"].as_u64().unwrap() as usize;
  121. let t_exp_left = v["t_exp"].as_u64().unwrap() as usize;
  122. let t_exp_right = v["t_exp_right"].as_u64().unwrap() as usize;
  123. let do_expansion = v.get("kinda_direct_upload").is_none();
  124. Params::init(
  125. 2048,
  126. &vec![268369921u64, 249561089u64],
  127. 6.4,
  128. n,
  129. p,
  130. q2_bits,
  131. t_conv,
  132. t_exp_left,
  133. t_exp_right,
  134. t_gsw,
  135. do_expansion,
  136. db_dim_1,
  137. db_dim_2,
  138. instances,
  139. db_item_size,
  140. )
  141. }
  142. pub fn read_arbitrary_bits(data: &[u8], bit_offs: usize, num_bits: usize) -> u64 {
  143. let word_off = bit_offs / 64;
  144. let bit_off_within_word = bit_offs % 64;
  145. if (bit_off_within_word + num_bits) <= 64 {
  146. let idx = word_off * 8;
  147. let val = u64::from_ne_bytes(data[idx..idx + 8].try_into().unwrap());
  148. (val >> bit_off_within_word) & ((1u64 << num_bits) - 1)
  149. } else {
  150. let idx = word_off * 8;
  151. let val = u128::from_ne_bytes(data[idx..idx + 16].try_into().unwrap());
  152. ((val >> bit_off_within_word) & ((1u128 << num_bits) - 1)) as u64
  153. }
  154. }
  155. pub fn write_arbitrary_bits(data: &mut [u8], mut val: u64, bit_offs: usize, num_bits: usize) {
  156. let word_off = bit_offs / 64;
  157. let bit_off_within_word = bit_offs % 64;
  158. val = val & ((1u64 << num_bits) - 1);
  159. if (bit_off_within_word + num_bits) <= 64 {
  160. let idx = word_off * 8;
  161. let mut cur_val = u64::from_ne_bytes(data[idx..idx + 8].try_into().unwrap());
  162. cur_val &= !(((1u64 << num_bits) - 1) << bit_off_within_word);
  163. cur_val |= val << bit_off_within_word;
  164. data[idx..idx + 8].copy_from_slice(&u64::to_ne_bytes(cur_val));
  165. } else {
  166. let idx = word_off * 8;
  167. let mut cur_val = u128::from_ne_bytes(data[idx..idx + 16].try_into().unwrap());
  168. let mask = !(((1u128 << num_bits) - 1) << bit_off_within_word);
  169. cur_val &= mask;
  170. cur_val |= (val as u128) << bit_off_within_word;
  171. data[idx..idx + 16].copy_from_slice(&u128::to_ne_bytes(cur_val));
  172. }
  173. }
  174. #[cfg(test)]
  175. mod test {
  176. use super::*;
  177. #[test]
  178. fn params_from_json_correct() {
  179. let cfg = r#"
  180. {'n': 2,
  181. 'nu_1': 9,
  182. 'nu_2': 6,
  183. 'p': 256,
  184. 'q_prime_bits': 20,
  185. 's_e': 87.62938774292914,
  186. 't_GSW': 8,
  187. 't_conv': 4,
  188. 't_exp': 8,
  189. 't_exp_right': 56,
  190. 'instances': 1,
  191. 'db_item_size': 2048 }
  192. "#;
  193. let cfg = cfg.replace("'", "\"");
  194. let b = params_from_json(&cfg);
  195. let c = Params::init(
  196. 2048,
  197. &vec![268369921u64, 249561089u64],
  198. 6.4,
  199. 2,
  200. 256,
  201. 20,
  202. 4,
  203. 8,
  204. 56,
  205. 8,
  206. true,
  207. 9,
  208. 6,
  209. 1,
  210. 2048,
  211. );
  212. assert_eq!(b, c);
  213. }
  214. #[test]
  215. fn test_read_write_arbitrary_bits() {
  216. let len = 4096;
  217. let num_bits = 9;
  218. let mut data = vec![0u8; len];
  219. let scaled_len = len * 8 / num_bits - 64;
  220. let mut bit_offs = 0;
  221. let get_from = |i: usize| -> u64 { ((i * 7 + 13) % (1 << num_bits)) as u64 };
  222. for i in 0..scaled_len {
  223. write_arbitrary_bits(data.as_mut_slice(), get_from(i), bit_offs, num_bits);
  224. bit_offs += num_bits;
  225. }
  226. bit_offs = 0;
  227. for i in 0..scaled_len {
  228. let val = read_arbitrary_bits(data.as_slice(), bit_offs, num_bits);
  229. assert_eq!(val, get_from(i));
  230. bit_offs += num_bits;
  231. }
  232. }
  233. }