Bläddra i källkod

[consensus/utils] fixed bug in fbig2ibig, added extra test case

mohab metwally 3 år sedan
förälder
incheckning
36f61bb3f2
1 ändrade filer med 41 tillägg och 27 borttagningar
  1. 41 27
      src/consensus/utils.rs

+ 41 - 27
src/consensus/utils.rs

@@ -19,6 +19,9 @@
 use darkfi_sdk::pasta::pallas;
 use darkfi_sdk::pasta::pallas;
 use dashu::integer::{IBig, Sign};
 use dashu::integer::{IBig, Sign};
 use log::debug;
 use log::debug;
+use dashu::integer::UBig;
+use darkfi_sdk::pasta::group::ff::PrimeField;
+use crate::consensus::{constants::RADIX_BITS};
 
 
 use super::Float10;
 use super::Float10;
 
 
@@ -26,12 +29,32 @@ pub fn fbig2ibig(f: Float10) -> IBig {
     let rad = IBig::try_from(10).unwrap();
     let rad = IBig::try_from(10).unwrap();
     let sig = f.repr().significand();
     let sig = f.repr().significand();
     let exp = f.repr().exponent();
     let exp = f.repr().exponent();
-    let val: IBig = if exp >= 0 { sig.clone() * rad.pow(exp as usize) } else { sig.clone() };
-    debug!(target: "consensus::utils", "fbig2ibig (f): {}", f);
-    debug!(target: "consensus::utils", "fbig2ibig (i): {}", val);
+
+    let val: IBig = if exp >= 0 {
+        sig.clone() * rad.pow(exp as usize)
+    } else {
+        sig.clone() / rad.pow(exp.abs() as usize)
+    };
+
     val
     val
 }
 }
-/*
+
+/// note! nagative values in pallas field won't wraps, and won't
+/// convert back to same value.
+pub fn fbig2base(f: Float10) -> pallas::Base {
+    debug!(target: "consensus::utils", "fbig -> base (f): {}", f);
+    let val: IBig = fbig2ibig(f);
+    let (sign, word) = val.as_sign_words();
+    let mut words: [u64; 4] = [0, 0, 0, 0];
+    words[..word.len()].copy_from_slice(word);
+    match sign {
+        Sign::Positive => pallas::Base::from_raw(words),
+        Sign::Negative => pallas::Base::from_raw(words).neg(),
+    }
+}
+
+/// note! only support positive conversion, and zero.
+/// used for testing purpose on non-negative values at the moment.
 pub fn base2ibig(base: pallas::Base) -> IBig {
 pub fn base2ibig(base: pallas::Base) -> IBig {
     //
     //
     let byts: [u8; 32] = base.to_repr();
     let byts: [u8; 32] = base.to_repr();
@@ -42,28 +65,16 @@ pub fn base2ibig(base: pallas::Base) -> IBig {
         u64::from_le_bytes(byts[24..32].try_into().expect("")),
         u64::from_le_bytes(byts[24..32].try_into().expect("")),
     ];
     ];
     let uparts = UBig::from_words(&words);
     let uparts = UBig::from_words(&words);
-    //TODO both y, and t are positive, but workout the sign for general use
     let ibig = IBig::from_parts(Sign::Positive, uparts);
     let ibig = IBig::from_parts(Sign::Positive, uparts);
     ibig
     ibig
 }
 }
-*/
-pub fn fbig2base(f: Float10) -> pallas::Base {
-    debug!(target: "consensus::utils", "fbig -> base (f): {}", f);
-    let val: IBig = fbig2ibig(f);
-    let (sign, word) = val.as_sign_words();
-    let mut words: [u64; 4] = [0, 0, 0, 0];
-    words[..word.len()].copy_from_slice(word);
-    match sign {
-        Sign::Positive => pallas::Base::from_raw(words),
-        Sign::Negative => pallas::Base::from_raw(words).neg(),
-    }
-}
-
 #[cfg(test)]
 #[cfg(test)]
 mod tests {
 mod tests {
     use dashu::integer::IBig;
     use dashu::integer::IBig;
 
 
-    use crate::consensus::{types::Float10, utils::fbig2ibig};
+    use darkfi_sdk::pasta::{pallas, group::ff::PrimeField};
+    use crate::consensus::{constants::RADIX_BITS, types::Float10, utils::{fbig2ibig, fbig2base, base2ibig}};
+
 
 
     #[test]
     #[test]
     fn dashu_fbig2ibig() {
     fn dashu_fbig2ibig() {
@@ -73,20 +84,23 @@ mod tests {
         assert_eq!(i, sig);
         assert_eq!(i, sig);
     }
     }
 
 
-    /*
     #[test]
     #[test]
     fn dashu_test_base2ibig() {
     fn dashu_test_base2ibig() {
         //
         //
-        let fbig: Float10 = Float10::from_str_native(
-            "28948022309329048855892746252171976963363056481941560715954676764349967630337",
-        )
-        .unwrap()
-        .with_precision(RADIX_BITS)
-        .value();
+        let fbig: Float10 = Float10::from_str_native("289480223093290488558927462521719769633630564819415607159546767643499676303").unwrap().with_precision(RADIX_BITS).value();
         let ibig = fbig2ibig(fbig.clone());
         let ibig = fbig2ibig(fbig.clone());
         let res_base: pallas::Base = fbig2base(fbig.clone());
         let res_base: pallas::Base = fbig2base(fbig.clone());
         let res_ibig: IBig = base2ibig(res_base);
         let res_ibig: IBig = base2ibig(res_base);
         assert_eq!(res_ibig, ibig);
         assert_eq!(res_ibig, ibig);
     }
     }
-    */
+
+    #[test]
+    fn dashu_test2_base2ibig() {
+        //assert that field wrapping for negative values won't hold during conversions.
+        let fbig: Float10 = Float10::from_str_native("-20065240046497827215558476051577517633529246907153511707181011345840062564.87").unwrap().with_precision(RADIX_BITS).value();
+        let ibig = fbig2ibig(fbig.clone());
+        let res_base: pallas::Base = fbig2base(fbig.clone());
+        let res_ibig: IBig = base2ibig(res_base);
+        assert_ne!(res_ibig, ibig);
+    }
 }
 }