utils.rs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 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 super::Float10;
  19. use dashu::integer::{IBig, Sign};
  20. use log::debug;
  21. use pasta_curves::pallas;
  22. //use pasta_curves::{group::ff::PrimeField};
  23. //use dashu::integer::{UBig};
  24. pub fn fbig2ibig(f: Float10) -> IBig {
  25. let rad = IBig::try_from(10).unwrap();
  26. let sig = f.repr().significand();
  27. let exp = f.repr().exponent();
  28. let val: IBig = if exp >= 0 { sig.clone() * rad.pow(exp as usize) } else { sig.clone() };
  29. debug!("fbig2ibig (f): {}", f);
  30. debug!("fbig2ibig (i): {}", val);
  31. val
  32. }
  33. /*
  34. pub fn base2ibig(base: pallas::Base) -> IBig {
  35. //
  36. let byts: [u8; 32] = base.to_repr();
  37. let words: [u64; 4] = [
  38. u64::from_le_bytes(byts[0..8].try_into().expect("")),
  39. u64::from_le_bytes(byts[8..16].try_into().expect("")),
  40. u64::from_le_bytes(byts[16..24].try_into().expect("")),
  41. u64::from_le_bytes(byts[24..32].try_into().expect("")),
  42. ];
  43. let uparts = UBig::from_words(&words);
  44. //TODO both y, and t are positive, but workout the sign for general use
  45. let ibig = IBig::from_parts(Sign::Positive, uparts);
  46. ibig
  47. }
  48. */
  49. pub fn fbig2base(f: Float10) -> pallas::Base {
  50. debug!("fbig -> base (f): {}", f);
  51. let val: IBig = fbig2ibig(f);
  52. let (sign, word) = val.as_sign_words();
  53. let mut words: [u64; 4] = [0, 0, 0, 0];
  54. words[..word.len()].copy_from_slice(word);
  55. match sign {
  56. Sign::Positive => pallas::Base::from_raw(words),
  57. Sign::Negative => pallas::Base::from_raw(words).neg(),
  58. }
  59. }
  60. #[cfg(test)]
  61. mod tests {
  62. use dashu::integer::IBig;
  63. use crate::consensus::{constants::RADIX_BITS, types::Float10, utils::fbig2ibig};
  64. #[test]
  65. fn dashu_fbig2ibig() {
  66. let f =
  67. Float10::from_str_native("234234223.000").unwrap().with_precision(RADIX_BITS).value();
  68. let i: IBig = fbig2ibig(f);
  69. let sig = IBig::from(234234223);
  70. assert_eq!(i, sig);
  71. }
  72. /*
  73. #[test]
  74. fn dashu_test_base2ibig() {
  75. //
  76. let fbig: Float10 = Float10::from_str_native(
  77. "28948022309329048855892746252171976963363056481941560715954676764349967630337",
  78. )
  79. .unwrap()
  80. .with_precision(RADIX_BITS)
  81. .value();
  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. */
  88. }