util.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. use crate::params::*;
  2. use serde_json::{Value};
  3. pub fn calc_index(indices: &[usize], lengths: &[usize]) -> usize {
  4. let mut idx = 0usize;
  5. let mut prod = 1usize;
  6. for i in (0..indices.len()).rev() {
  7. idx += indices[i] * prod;
  8. prod *= lengths[i];
  9. }
  10. idx
  11. }
  12. pub fn get_test_params() -> Params {
  13. Params::init(
  14. 2048,
  15. &vec![268369921u64, 249561089u64],
  16. 6.4,
  17. 2,
  18. 256,
  19. 20,
  20. 4,
  21. 8,
  22. 56,
  23. 8,
  24. true,
  25. 9,
  26. 6,
  27. )
  28. }
  29. pub fn params_from_json(cfg: &str) -> Params {
  30. let v: Value = serde_json::from_str(cfg).unwrap();
  31. let n = v["n"].as_u64().unwrap() as usize;
  32. let db_dim_1 = v["nu_1"].as_u64().unwrap() as usize;
  33. let db_dim_2 = v["nu_2"].as_u64().unwrap() as usize;
  34. let p = v["p"].as_u64().unwrap();
  35. let q2_bits = v["q_prime_bits"].as_u64().unwrap();
  36. let t_gsw = v["t_GSW"].as_u64().unwrap() as usize;
  37. let t_conv = v["t_conv"].as_u64().unwrap() as usize;
  38. let t_exp_left = v["t_exp"].as_u64().unwrap() as usize;
  39. let t_exp_right = v["t_exp_right"].as_u64().unwrap() as usize;
  40. let do_expansion = v.get("kinda_direct_upload").is_none();
  41. Params::init(
  42. 2048,
  43. &vec![268369921u64, 249561089u64],
  44. 6.4,
  45. n,
  46. p,
  47. q2_bits,
  48. t_conv,
  49. t_exp_left,
  50. t_exp_right,
  51. t_gsw,
  52. do_expansion,
  53. db_dim_1,
  54. db_dim_2,
  55. )
  56. }
  57. pub fn read_arbitrary_bits(data: &[u8], bit_offs: usize, num_bits: usize) -> u64 {
  58. let word_off = bit_offs / 64;
  59. let bit_off_within_word = bit_offs % 64;
  60. if (bit_off_within_word + num_bits) <= 64 {
  61. let idx = word_off * 8;
  62. let val = u64::from_ne_bytes(data[idx..idx + 8].try_into().unwrap());
  63. (val >> bit_off_within_word) & ((1u64 << num_bits) - 1)
  64. } else {
  65. let idx = word_off * 8;
  66. let val = u128::from_ne_bytes(data[idx..idx + 16].try_into().unwrap());
  67. ((val >> bit_off_within_word) & ((1u128 << num_bits) - 1)) as u64
  68. }
  69. }
  70. pub fn write_arbitrary_bits(data: &mut [u8], mut val: u64, bit_offs: usize, num_bits: usize) {
  71. let word_off = bit_offs / 64;
  72. let bit_off_within_word = bit_offs % 64;
  73. val = val & ((1u64 << num_bits) - 1);
  74. if (bit_off_within_word + num_bits) <= 64 {
  75. let idx = word_off * 8;
  76. let mut cur_val = u64::from_ne_bytes(data[idx..idx + 8].try_into().unwrap());
  77. cur_val &= !(((1u64 << num_bits) - 1) << bit_off_within_word);
  78. cur_val |= val << bit_off_within_word;
  79. data[idx..idx + 8].copy_from_slice(&u64::to_ne_bytes(cur_val));
  80. } else {
  81. let idx = word_off * 8;
  82. let mut cur_val = u128::from_ne_bytes(data[idx..idx + 16].try_into().unwrap());
  83. let mask = !(((1u128 << num_bits) - 1) << bit_off_within_word);
  84. cur_val &= mask;
  85. cur_val |= (val as u128) << bit_off_within_word;
  86. data[idx..idx + 16].copy_from_slice(&u128::to_ne_bytes(cur_val));
  87. }
  88. }
  89. #[cfg(test)]
  90. mod test {
  91. use super::*;
  92. #[test]
  93. fn params_from_json_correct() {
  94. let cfg = r#"
  95. {'n': 2,
  96. 'nu_1': 9,
  97. 'nu_2': 6,
  98. 'p': 256,
  99. 'q_prime_bits': 20,
  100. 's_e': 87.62938774292914,
  101. 't_GSW': 8,
  102. 't_conv': 4,
  103. 't_exp': 8,
  104. 't_exp_right': 56}
  105. "#;
  106. let cfg = cfg.replace("'", "\"");
  107. let b = params_from_json(&cfg);
  108. let c = Params::init(
  109. 2048,
  110. &vec![268369921u64, 249561089u64],
  111. 6.4,
  112. 2,
  113. 256,
  114. 20,
  115. 4,
  116. 8,
  117. 56,
  118. 8,
  119. true,
  120. 9,
  121. 6,
  122. );
  123. assert_eq!(b, c);
  124. }
  125. #[test]
  126. fn test_read_write_arbitrary_bits() {
  127. let mut data = vec![0u8; 1024];
  128. let num_bits = 5;
  129. let mut bit_offs = 0;
  130. for i in 0..1000 {
  131. write_arbitrary_bits(data.as_mut_slice(), i % 32, bit_offs, num_bits);
  132. bit_offs += num_bits;
  133. }
  134. bit_offs = 0;
  135. for i in 0..1000 {
  136. let val = read_arbitrary_bits(data.as_slice(), bit_offs, num_bits);
  137. assert_eq!(val, i % 32);
  138. bit_offs += num_bits;
  139. }
  140. }
  141. }