From 631e3c67d17dab9650d0bc9cd9daeb6f387dd3fa Mon Sep 17 00:00:00 2001 From: nk_ysg Date: Thu, 2 Mar 2023 14:38:16 +0800 Subject: [PATCH] Revert "Fix sign flag for SignedInteger64 zero (#191)" This reverts commit 722a2980fd01ee89b603235a9448fb7fa0e14351. --- .../bytecode_modules/SignedInteger64.mv | Bin 506 -> 514 bytes .../StarcoinFramework/docs/SignedInteger64.md | 30 +++++++------- .../source_maps/SignedInteger64.mvsm | Bin 5727 -> 6063 bytes integration-tests/signed_integer/add.exp | 4 +- integration-tests/signed_integer/add.move | 15 ------- .../signed_integer/add_overflow.exp | 2 +- integration-tests/signed_integer/divide.exp | 2 +- integration-tests/signed_integer/multiply.exp | 2 +- integration-tests/signed_integer/sub.exp | 4 +- integration-tests/signed_integer/sub.move | 10 ----- sources/SignedInteger64.move | 39 +++++++++--------- 11 files changed, 42 insertions(+), 66 deletions(-) diff --git a/build/StarcoinFramework/bytecode_modules/SignedInteger64.mv b/build/StarcoinFramework/bytecode_modules/SignedInteger64.mv index 366352d8adf83f798d64d8370c8e8ecd8a38e13a..d11b7f7ab288f54013bdda33c829046615de5bef 100644 GIT binary patch delta 237 zcmYL@!41MN5CqR>Uj)uMf+!+ZzA^Ab2>((9J!GqA21}I{rd~5EC!~bFcs#(9(di6a^DeaS7JZS)c9iy_^5g zW9{!)zMs3-`8abGG{n3SNTX{yQjRu9_2Q>BDDrY#3Pfe$$q*H�R^aYmm^NTwAIo vt)gImaoelf>taYoTn!i;0B3lWYYg~r?kMsq`y}|!XPpublic fun multiply_u64(num: u64, multiplier: SignedInteger64): SignedInteger64 { let product = multiplier.value * num; - SignedInteger64 { value: product, is_negative: multiplier.is_negative } + SignedInteger64 { value: (product as u64), is_negative: multiplier.is_negative } } @@ -111,7 +111,7 @@ Divide a u64 integer by a signed integer number.
public fun divide_u64(num: u64, divisor: SignedInteger64): SignedInteger64 {
     let quotient = num / divisor.value;
-    SignedInteger64 { value: quotient, is_negative: divisor.is_negative }
+    SignedInteger64 { value: (quotient as u64), is_negative: divisor.is_negative }
 }
 
@@ -150,14 +150,14 @@ Sub: num - minus
public fun sub_u64(num: u64, minus: SignedInteger64): SignedInteger64 {
     if (minus.is_negative) {
         let result = num + minus.value;
-        SignedInteger64 { value: result, is_negative: false }
+        SignedInteger64 { value: (result as u64), is_negative: false }
     } else {
-        if (num >= minus.value) {
+        if (num > minus.value)  {
             let result = num - minus.value;
-            SignedInteger64 { value: result, is_negative: false }
+            SignedInteger64 { value: (result as u64), is_negative: false }
         }else {
             let result = minus.value - num;
-            SignedInteger64 { value: result, is_negative: true }
+            SignedInteger64 { value: (result as u64), is_negative: true }
         }
     }
 }
@@ -197,16 +197,16 @@ Add: num + addend
 
 
public fun add_u64(num: u64, addend: SignedInteger64): SignedInteger64 {
     if (addend.is_negative) {
-        if (num >= addend.value) {
-            let result = num - addend.value;
-            SignedInteger64 { value: result, is_negative: false }
-        }else {
-            let result = addend.value - num;
-            SignedInteger64 { value: result, is_negative: true }
-        }
+       if (num > addend.value)  {
+           let result = num - addend.value;
+           SignedInteger64 { value: (result as u64), is_negative: false }
+       }else {
+           let result = addend.value - num;
+           SignedInteger64 { value: (result as u64), is_negative: true }
+       }
     } else {
-        let result = num + addend.value;
-        SignedInteger64 { value: result, is_negative: false }
+         let result = num + addend.value;
+         SignedInteger64 { value: (result as u64), is_negative: false }
     }
 }
 
diff --git a/build/StarcoinFramework/source_maps/SignedInteger64.mvsm b/build/StarcoinFramework/source_maps/SignedInteger64.mvsm index 1c9f9d8b237a2ad3f00f554c161e477a854caeb4..61f625963ba20a0676aecb5bc518f36541f5e7fb 100644 GIT binary patch literal 6063 zcma)j0$R7i&Uy0q1IMGV=a9sc~E-s@}MnkQ|;Q64ZeMQV7;yR&%XWV z?63UShx@PGI#+zAxHr~OQIa(Lywy?5dLM70_#@Vrv+!g;A&n<`v}Sq=p)iG-g@ zD19_)4Mg(lYG)`pgV@7}ac3!a3GQn##?j$ta>=aHkDwoe*#CO%boGZBj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$y|#$Oq#V=Uqh}m?Q+PM<|gWG zh5~7B!`cPIoy?ywKal1YtgSFYnsybAlx9Em24IvluV7t=(b7DIbpyso({{c{nt81F z5m4-87GcJv8OIt2A4;G24*@N{9lslOxFegj18|w&6k)}OCKb7V+>fMB?(!7WD2r8s`4(l;Ylcw#w z!|`1k^E@BCaHPGVDdVe4WIgT%$P3!mCjG{SbkHuJN#S>x|KO64V?T-bC+M5!Z>)wOVcEyUzFjky~1^O$=nRRkw-bTI)E*tX!-ZN8N zPPQ>$g8rHw!V0r|^jyEfd=0wG_MP;`EaRUmKyS=BSk<66rZK+)y)pB#>cAl&{K)>@ zYxc0y!r6y;5F*V%GSj9`^B&AzV-p8u`uaATHA&8NiH_GO`m2esZqe~IdN$>45|q)g z7y0;m56Ij*gTZ_bF46G@qxl!~VmIbJ(2LzRL@(heuaygW2^V0E1igffSqwTl=3(W7 zUcz?H^5l_FMZIcMSDMSQR)P+h z(a~;19Uc3qcM5cL`~&MOl*lAJEb9D)nNR zIy&0U>*zR}dR0&=`)SN-D3fLl)?(1naRJss_(ayU=b{do?bQ1YbjaL^)d@Oe)?@7k P9Wrg_b#%1*-Sgf6*P1q> diff --git a/integration-tests/signed_integer/add.exp b/integration-tests/signed_integer/add.exp index f6b3dc0b..ab0e2edd 100644 --- a/integration-tests/signed_integer/add.exp +++ b/integration-tests/signed_integer/add.exp @@ -1,7 +1,7 @@ processed 3 tasks -task 2 'run'. lines 5-41: +task 2 'run'. lines 5-26: { - "gas_used": 100723, + "gas_used": 54451, "status": "Executed" } diff --git a/integration-tests/signed_integer/add.move b/integration-tests/signed_integer/add.move index 12d9505f..efa879cd 100644 --- a/integration-tests/signed_integer/add.move +++ b/integration-tests/signed_integer/add.move @@ -22,20 +22,5 @@ fun main() { let i2 = SignedInteger64::create_from_raw_value(0, false); let z2 = SignedInteger64::add_u64(100, copy i2); assert!(SignedInteger64::get_value(z2) == 100, 6); - - let i3 = SignedInteger64::create_from_raw_value(1, true); - let z3 = SignedInteger64::add_u64(1, i3); - assert!(SignedInteger64::get_value(copy z3) == 0, 7); - assert!(SignedInteger64::is_negative(z3) == false, 8); - - let i4 = SignedInteger64::create_from_raw_value(0, true); - let z4 = SignedInteger64::add_u64(0, i4); - assert!(SignedInteger64::get_value(copy z4) == 0, 9); - assert!(SignedInteger64::is_negative(z4) == false, 10); - - let i5 = SignedInteger64::create_from_raw_value(0, false); - let z5 = SignedInteger64::add_u64(0, i5); - assert!(SignedInteger64::get_value(copy z5) == 0, 11); - assert!(SignedInteger64::is_negative(z5) == false, 12); } } \ No newline at end of file diff --git a/integration-tests/signed_integer/add_overflow.exp b/integration-tests/signed_integer/add_overflow.exp index c0510b88..c3cca6af 100644 --- a/integration-tests/signed_integer/add_overflow.exp +++ b/integration-tests/signed_integer/add_overflow.exp @@ -12,7 +12,7 @@ task 2 'run'. lines 5-16: } }, "function": 0, - "code_offset": 40 + "code_offset": 42 } } } diff --git a/integration-tests/signed_integer/divide.exp b/integration-tests/signed_integer/divide.exp index 663ea9ce..debe8124 100644 --- a/integration-tests/signed_integer/divide.exp +++ b/integration-tests/signed_integer/divide.exp @@ -2,6 +2,6 @@ processed 3 tasks task 2 'run'. lines 5-26: { - "gas_used": 53253, + "gas_used": 53261, "status": "Executed" } diff --git a/integration-tests/signed_integer/multiply.exp b/integration-tests/signed_integer/multiply.exp index 4f28423f..4dd911e9 100644 --- a/integration-tests/signed_integer/multiply.exp +++ b/integration-tests/signed_integer/multiply.exp @@ -2,6 +2,6 @@ processed 3 tasks task 2 'run'. lines 5-23: { - "gas_used": 45720, + "gas_used": 45726, "status": "Executed" } diff --git a/integration-tests/signed_integer/sub.exp b/integration-tests/signed_integer/sub.exp index 927616f9..ab0e2edd 100644 --- a/integration-tests/signed_integer/sub.exp +++ b/integration-tests/signed_integer/sub.exp @@ -1,7 +1,7 @@ processed 3 tasks -task 2 'run'. lines 5-36: +task 2 'run'. lines 5-26: { - "gas_used": 84861, + "gas_used": 54451, "status": "Executed" } diff --git a/integration-tests/signed_integer/sub.move b/integration-tests/signed_integer/sub.move index c32676cd..1cc37569 100644 --- a/integration-tests/signed_integer/sub.move +++ b/integration-tests/signed_integer/sub.move @@ -22,15 +22,5 @@ fun main() { let i2 = SignedInteger64::create_from_raw_value(100, true); let z2 = SignedInteger64::sub_u64(100, copy i2); assert!(SignedInteger64::get_value(z2) == 200, 6); - - let i3 = SignedInteger64::create_from_raw_value(0, true); - let z3 = SignedInteger64::sub_u64(0, i3); - assert!(SignedInteger64::get_value(copy z3) == 0, 1); - assert!(SignedInteger64::is_negative(z3) == false, 2); - - let i4 = SignedInteger64::create_from_raw_value(0, false); - let z4 = SignedInteger64::sub_u64(0, i4); - assert!(SignedInteger64::get_value(copy z4) == 0, 1); - assert!(SignedInteger64::is_negative(z4) == false, 2); } } \ No newline at end of file diff --git a/sources/SignedInteger64.move b/sources/SignedInteger64.move index 5258cc78..b61c83ba 100644 --- a/sources/SignedInteger64.move +++ b/sources/SignedInteger64.move @@ -16,44 +16,43 @@ module SignedInteger64 { /// Multiply a u64 integer by a signed integer number. public fun multiply_u64(num: u64, multiplier: SignedInteger64): SignedInteger64 { let product = multiplier.value * num; - SignedInteger64 { value: product, is_negative: multiplier.is_negative } + SignedInteger64 { value: (product as u64), is_negative: multiplier.is_negative } } /// Divide a u64 integer by a signed integer number. public fun divide_u64(num: u64, divisor: SignedInteger64): SignedInteger64 { let quotient = num / divisor.value; - SignedInteger64 { value: quotient, is_negative: divisor.is_negative } + SignedInteger64 { value: (quotient as u64), is_negative: divisor.is_negative } } /// Sub: `num - minus` public fun sub_u64(num: u64, minus: SignedInteger64): SignedInteger64 { if (minus.is_negative) { let result = num + minus.value; - SignedInteger64 { value: result, is_negative: false } + SignedInteger64 { value: (result as u64), is_negative: false } } else { - if (num >= minus.value) { + if (num > minus.value) { let result = num - minus.value; - SignedInteger64 { value: result, is_negative: false } + SignedInteger64 { value: (result as u64), is_negative: false } }else { let result = minus.value - num; - SignedInteger64 { value: result, is_negative: true } + SignedInteger64 { value: (result as u64), is_negative: true } } } } - /// Add: `num + addend` public fun add_u64(num: u64, addend: SignedInteger64): SignedInteger64 { if (addend.is_negative) { - if (num >= addend.value) { - let result = num - addend.value; - SignedInteger64 { value: result, is_negative: false } - }else { - let result = addend.value - num; - SignedInteger64 { value: result, is_negative: true } - } + if (num > addend.value) { + let result = num - addend.value; + SignedInteger64 { value: (result as u64), is_negative: false } + }else { + let result = addend.value - num; + SignedInteger64 { value: (result as u64), is_negative: true } + } } else { - let result = num + addend.value; - SignedInteger64 { value: result, is_negative: false } + let result = num + addend.value; + SignedInteger64 { value: (result as u64), is_negative: false } } } @@ -74,8 +73,10 @@ module SignedInteger64 { // **************** SPECIFICATIONS **************** + + spec multiply_u64 { - aborts_if multiplier.value * num > max_u64(); + aborts_if multiplier.value * num > max_u64(); } spec divide_u64 { @@ -87,7 +88,7 @@ module SignedInteger64 { } spec add_u64 { - aborts_if !addend.is_negative && num + addend.value > max_u64(); + aborts_if !addend.is_negative && num + addend.value > max_u64(); } spec create_from_raw_value { @@ -104,6 +105,6 @@ module SignedInteger64 { aborts_if false; ensures result == num.is_negative; } - } + }