utils.rs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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::pallas;
  19. use dashu::integer::{IBig, Sign};
  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 { sig.clone() * rad.pow(exp as usize) } else { sig.clone() };
  27. debug!(target: "consensus::utils", "fbig2ibig (f): {}", f);
  28. debug!(target: "consensus::utils", "fbig2ibig (i): {}", val);
  29. val
  30. }
  31. /*
  32. pub fn base2ibig(base: pallas::Base) -> IBig {
  33. //
  34. let byts: [u8; 32] = base.to_repr();
  35. let words: [u64; 4] = [
  36. u64::from_le_bytes(byts[0..8].try_into().expect("")),
  37. u64::from_le_bytes(byts[8..16].try_into().expect("")),
  38. u64::from_le_bytes(byts[16..24].try_into().expect("")),
  39. u64::from_le_bytes(byts[24..32].try_into().expect("")),
  40. ];
  41. let uparts = UBig::from_words(&words);
  42. //TODO both y, and t are positive, but workout the sign for general use
  43. let ibig = IBig::from_parts(Sign::Positive, uparts);
  44. ibig
  45. }
  46. */
  47. pub fn fbig2base(f: Float10) -> pallas::Base {
  48. debug!(target: "consensus::utils", "fbig -> base (f): {}", f);
  49. let val: IBig = fbig2ibig(f);
  50. let (sign, word) = val.as_sign_words();
  51. let mut words: [u64; 4] = [0, 0, 0, 0];
  52. words[..word.len()].copy_from_slice(word);
  53. match sign {
  54. Sign::Positive => pallas::Base::from_raw(words),
  55. Sign::Negative => pallas::Base::from_raw(words).neg(),
  56. }
  57. }
  58. #[cfg(test)]
  59. mod tests {
  60. use dashu::integer::IBig;
  61. use crate::consensus::{types::Float10, utils::fbig2ibig};
  62. #[test]
  63. fn dashu_fbig2ibig() {
  64. let f = Float10::try_from("234234223.000").unwrap();
  65. let i: IBig = fbig2ibig(f);
  66. let sig = IBig::from(234234223);
  67. assert_eq!(i, sig);
  68. }
  69. /*
  70. #[test]
  71. fn dashu_test_base2ibig() {
  72. //
  73. let fbig: Float10 = Float10::from_str_native(
  74. "28948022309329048855892746252171976963363056481941560715954676764349967630337",
  75. )
  76. .unwrap()
  77. .with_precision(RADIX_BITS)
  78. .value();
  79. let ibig = fbig2ibig(fbig.clone());
  80. let res_base: pallas::Base = fbig2base(fbig.clone());
  81. let res_ibig: IBig = base2ibig(res_base);
  82. assert_eq!(res_ibig, ibig);
  83. }
  84. */
  85. }