utils.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. use dashu::{
  2. float::{round::mode::Zero, FBig},
  3. integer::{IBig, Sign},
  4. };
  5. use log::{debug, info};
  6. use pasta_curves::{group::ff::PrimeField, pallas};
  7. pub type Float10 = FBig<Zero, 10>;
  8. pub fn fbig2ibig(f: Float10) -> IBig {
  9. let rad = IBig::try_from(10).unwrap();
  10. let sig = f.repr().significand();
  11. let exp = f.repr().exponent();
  12. let val: IBig = if exp >= 0 {
  13. sig.clone() * rad.pow(exp as usize)
  14. } else {
  15. sig.clone()
  16. };
  17. debug!("fbig2ibig (f): {}", f);
  18. debug!("fbig2ibig (i): {}", val);
  19. val
  20. }
  21. pub fn fbig2base(f: Float10) -> pallas::Base {
  22. info!("fbig -> base (f): {}", f);
  23. let val: IBig = fbig2ibig(f);
  24. let (sign, word) = val.as_sign_words();
  25. //TODO (res) set pallas base sign, i.e sigma1 is negative.
  26. let mut words: [u64; 4] = [0, 0, 0, 0];
  27. for i in 0..word.len() {
  28. words[i] = word[i];
  29. }
  30. let base = match sign {
  31. Sign::Positive => pallas::Base::from_raw(words),
  32. Sign::Negative => pallas::Base::from_raw(words).neg(),
  33. };
  34. base
  35. }
  36. /// Extract leader selection lottery randomness(eta)
  37. /// using the hash of the previous lead proof, converted to pallas base.
  38. pub fn get_eta(proof_tx_hash: blake3::Hash) -> pallas::Base {
  39. let mut bytes: [u8; 32] = *proof_tx_hash.as_bytes();
  40. // read first 254 bits
  41. bytes[30] = 0;
  42. bytes[31] = 0;
  43. pallas::Base::from_repr(bytes).unwrap()
  44. }