Procházet zdrojové kódy

Ensure we have a single representation for zero in I64

Tom Pointon před 3 roky
rodič
revize
e190d9abcc
1 změnil soubory, kde provedl 16 přidání a 4 odebrání
  1. 16 4
      aptos/contracts/sources/i64.move

+ 16 - 4
aptos/contracts/sources/i64.move

@@ -13,6 +13,12 @@ module pyth::i64 {
     public fun new(magnitude: u64, negative: bool): I64 {
         assert!(magnitude <= MAX_MAGNITUDE, error::magnitude_too_large());
 
+        // Ensure we have a single zero representation: (0, false).
+        // (0, true) is invalid.
+        if (magnitude == 0) {
+            negative = false;
+        };
+
         I64 {
             magnitude: magnitude,
             negative: negative,
@@ -40,10 +46,9 @@ module pyth::i64 {
     public fun from_u64(from: u64): I64 {
         // Use the MSB to determine whether the number is negative or not.
         let negative = (from >> 63) == 1;
-        return I64 {
-            negative: negative,
-            magnitude: parse_magnitude(from, negative),
-        }
+        let magnitude = parse_magnitude(from, negative);
+
+        new(magnitude, negative)
     }
 
     fun parse_magnitude(from: u64, negative: bool): u64 {
@@ -106,4 +111,11 @@ module pyth::i64 {
         assert!(get_magnitude_if_negative(&new(7686, false)) == 7686, 1);
     }
 
+    #[test]
+    fun test_single_zero_representation() {
+        assert!(&new(0, true) == &new(0, false), 1);
+        assert!(&new(0, true) == &from_u64(0), 1);
+        assert!(&new(0, false) == &from_u64(0), 1);
+    }
+
 }