utils.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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 darkfi_sdk::pasta::{group::ff::PrimeField, pallas};
  19. use dashu::integer::{IBig, Sign, UBig};
  20. use log::debug;
  21. use super::Float10;
  22. pub fn fbig2ibig(f: Float10) -> IBig {
  23. let rad = IBig::try_from(10).unwrap();
  24. let sig = f.repr().significand();
  25. let exp = f.repr().exponent();
  26. let val: IBig = if exp >= 0 {
  27. sig.clone() * rad.pow(exp.unsigned_abs())
  28. } else {
  29. sig.clone() / rad.pow(exp.unsigned_abs())
  30. };
  31. val
  32. }
  33. /// note! nagative values in pallas field won't wraps, and won't
  34. /// convert back to same value.
  35. pub fn fbig2base(f: Float10) -> pallas::Base {
  36. debug!(target: "consensus::utils", "fbig -> base (f): {}", f);
  37. let val: IBig = fbig2ibig(f);
  38. let (sign, word) = val.as_sign_words();
  39. let mut words: [u64; 4] = [0, 0, 0, 0];
  40. words[..word.len()].copy_from_slice(word);
  41. match sign {
  42. Sign::Positive => pallas::Base::from_raw(words),
  43. Sign::Negative => pallas::Base::from_raw(words).neg(),
  44. }
  45. }
  46. /// note! only support positive conversion, and zero.
  47. /// used for testing purpose on non-negative values at the moment.
  48. pub fn base2ibig(base: pallas::Base) -> IBig {
  49. //
  50. let byts: [u8; 32] = base.to_repr();
  51. let words: [u64; 4] = [
  52. u64::from_le_bytes(byts[0..8].try_into().expect("")),
  53. u64::from_le_bytes(byts[8..16].try_into().expect("")),
  54. u64::from_le_bytes(byts[16..24].try_into().expect("")),
  55. u64::from_le_bytes(byts[24..32].try_into().expect("")),
  56. ];
  57. let uparts = UBig::from_words(&words);
  58. IBig::from_parts(Sign::Positive, uparts)
  59. }
  60. #[cfg(test)]
  61. mod tests {
  62. use dashu::integer::IBig;
  63. use crate::consensus::{
  64. types::Float10,
  65. utils::{base2ibig, fbig2base, fbig2ibig},
  66. };
  67. use darkfi_sdk::pasta::pallas;
  68. #[test]
  69. fn dashu_fbig2ibig() {
  70. let f = Float10::try_from("234234223.000").unwrap();
  71. let i: IBig = fbig2ibig(f);
  72. let sig = IBig::from(234234223);
  73. assert_eq!(i, sig);
  74. }
  75. #[test]
  76. fn dashu_test_base2ibig() {
  77. //
  78. let fbig: Float10 = Float10::try_from(
  79. "289480223093290488558927462521719769633630564819415607159546767643499676303",
  80. )
  81. .unwrap();
  82. let ibig = fbig2ibig(fbig.clone());
  83. let res_base: pallas::Base = fbig2base(fbig.clone());
  84. let res_ibig: IBig = base2ibig(res_base);
  85. assert_eq!(res_ibig, ibig);
  86. }
  87. #[test]
  88. fn dashu_test2_base2ibig() {
  89. //assert that field wrapping for negative values won't hold during conversions.
  90. let fbig: Float10 = Float10::try_from(
  91. "-20065240046497827215558476051577517633529246907153511707181011345840062564.87",
  92. )
  93. .unwrap();
  94. let ibig = fbig2ibig(fbig.clone());
  95. let res_base: pallas::Base = fbig2base(fbig.clone());
  96. let res_ibig: IBig = base2ibig(res_base);
  97. assert_ne!(res_ibig, ibig);
  98. }
  99. }