Skip to content

Commit

Permalink
Migrate to move v6 and u16, u32, u256 support (MystenLabs#5659)
Browse files Browse the repository at this point in the history
* move v6 initial & new integers
  • Loading branch information
oxade authored Oct 31, 2022
1 parent 18a155b commit aa807cf
Show file tree
Hide file tree
Showing 28 changed files with 668 additions and 220 deletions.
216 changes: 174 additions & 42 deletions Cargo.lock

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,24 @@ name-variant = "0.1.0"
store = { version = "0.4.0", package = "typed-store" }

# Move dependencies
move-binary-format = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-bytecode-utils = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-bytecode-verifier = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-cli = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-compiler = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-core-types = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030", features = ["address20"] }
move-disassembler = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-package = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-stdlib = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-vm-runtime = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-unit-test = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-vm-test-utils = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-vm-types = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-command-line-common = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-transactional-test-runner = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-ir-types = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-prover = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-prover-boogie-backend = { git = "https://github.com/move-language/move", rev = "1ffd0a3e7bdc4bba7dafb8c814279e750113d030" }
move-binary-format = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-bytecode-utils = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-bytecode-verifier = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-cli = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-compiler = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-core-types = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93", features = ["address20"] }
move-disassembler = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-package = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-stdlib = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-vm-runtime = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-unit-test = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-vm-test-utils = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-vm-types = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-command-line-common = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-transactional-test-runner = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-ir-types = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-prover = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }
move-prover-boogie-backend = { git = "https://github.com/move-language/move", rev = "be52c7118aeb94fbbfa12590e420a75e8ddfec93" }

fastcrypto = { git = "https://github.com/MystenLabs/fastcrypto", rev = "bbb2d02a7a64c27314721748cc4d015b00490dbe" }

Expand Down

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions crates/sui-core/src/gateway_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,11 @@ where
match type_ {
TypeTag::Bool
| TypeTag::U8
| TypeTag::U16
| TypeTag::U32
| TypeTag::U64
| TypeTag::U128
| TypeTag::U256
| TypeTag::Address
| TypeTag::Signer => (),
TypeTag::Vector(inner) => used_packages(packages, inner),
Expand Down
12 changes: 12 additions & 0 deletions crates/sui-core/tests/staged/sui.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ MoveTypeLayout:
TYPENAME: MoveStructLayout
7:
signer: UNIT
8:
u16: UNIT
9:
u32: UNIT
10:
u256: UNIT
ObjectArg:
ENUM:
0:
Expand Down Expand Up @@ -525,6 +531,12 @@ TypeTag:
struct:
NEWTYPE:
TYPENAME: StructTag
8:
u16: UNIT
9:
u32: UNIT
10:
u256: UNIT
TypedStoreError:
ENUM:
0:
Expand Down
19 changes: 19 additions & 0 deletions crates/sui-cost-tables/src/bytecode_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use move_core_types::gas_algebra::{
AbstractMemorySize, InternalGas, InternalGasPerAbstractMemoryUnit, NumArgs, NumBytes,
};
use move_core_types::language_storage::ModuleId;
use move_core_types::u256::U256;
use move_core_types::vm_status::StatusCode;
use move_vm_types::gas::{GasMeter, SimpleInstruction};
use move_vm_types::views::{TypeView, ValueView};
Expand Down Expand Up @@ -151,8 +152,11 @@ fn get_simple_instruction_opcode(instr: SimpleInstruction) -> Opcodes {

Pop => POP,
LdU8 => LD_U8,
LdU16 => LD_U16,
LdU32 => LD_U32,
LdU64 => LD_U64,
LdU128 => LD_U128,
LdU256 => LD_U256,
LdTrue => LD_TRUE,
LdFalse => LD_FALSE,

Expand All @@ -165,8 +169,11 @@ fn get_simple_instruction_opcode(instr: SimpleInstruction) -> Opcodes {
MutBorrowFieldGeneric => MUT_BORROW_FIELD_GENERIC,

CastU8 => CAST_U8,
CastU16 => CAST_U16,
CastU32 => CAST_U32,
CastU64 => CAST_U64,
CastU128 => CAST_U128,
CastU256 => CAST_U256,

Add => ADD,
Sub => SUB,
Expand Down Expand Up @@ -476,11 +483,17 @@ pub fn zero_cost_instruction_table() -> Vec<(Bytecode, GasCost)> {
(Ret, GasCost::new(0, 0)),
(Lt, GasCost::new(0, 0)),
(LdU8(0), GasCost::new(0, 0)),
(LdU16(0), GasCost::new(0, 0)),
(LdU32(0), GasCost::new(0, 0)),
(LdU64(0), GasCost::new(0, 0)),
(LdU128(0), GasCost::new(0, 0)),
(LdU256(U256::from(0u8)), GasCost::new(0, 0)),
(CastU8, GasCost::new(0, 0)),
(CastU16, GasCost::new(0, 0)),
(CastU32, GasCost::new(0, 0)),
(CastU64, GasCost::new(0, 0)),
(CastU128, GasCost::new(0, 0)),
(CastU256, GasCost::new(0, 0)),
(Abort, GasCost::new(0, 0)),
(MutBorrowLoc(0), GasCost::new(0, 0)),
(ImmBorrowLoc(0), GasCost::new(0, 0)),
Expand Down Expand Up @@ -603,11 +616,17 @@ pub fn legacy_bytecode_instruction_costs() -> Vec<(Bytecode, GasCost)> {
(Ret, GasCost::new(638, 1)),
(Lt, GasCost::new(1, 1)),
(LdU8(0), GasCost::new(1, 1)),
(LdU16(0), GasCost::new(1, 1)),
(LdU32(0), GasCost::new(1, 1)),
(LdU64(0), GasCost::new(1, 1)),
(LdU128(0), GasCost::new(1, 1)),
(LdU256(U256::from(0u8)), GasCost::new(2, 1)),
(CastU8, GasCost::new(2, 1)),
(CastU16, GasCost::new(1, 1)),
(CastU32, GasCost::new(1, 1)),
(CastU64, GasCost::new(1, 1)),
(CastU128, GasCost::new(1, 1)),
(CastU256, GasCost::new(2, 1)),
(Abort, GasCost::new(1, 1)),
(MutBorrowLoc(0), GasCost::new(2, 1)),
(ImmBorrowLoc(0), GasCost::new(1, 1)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/sui-cost/tests/calibration.rs
expression: bytecode_calib
---
// Move bytecode v5
// Move bytecode v6
module 2.bytecode_calibration_tests {
struct ObjectWithU64Field has drop, store {
f0: u64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/sui-cost/tests/calibration.rs
expression: natives_calib
---
// Move bytecode v5
// Move bytecode v6
module 2.natives_calibration_tests {
struct ObjectSimple has copy, drop, store, key {
dummy_field: bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ expression: common_costs_estimate
---
{
"MergeCoin": {
"computation_cost": 5487,
"storage_cost": 8098,
"computation_cost": 5513,
"storage_cost": 8137,
"storage_rebate": 0
},
"Publish": {
"computation_cost": 5475,
"storage_cost": 8033,
"computation_cost": 5502,
"storage_cost": 8072,
"storage_rebate": 0
},
"SharedCounterAssertValue": {
Expand All @@ -29,8 +29,8 @@ expression: common_costs_estimate
"storage_rebate": 0
},
"SplitCoin": {
"computation_cost": 5465,
"storage_cost": 8066,
"computation_cost": 5492,
"storage_cost": 8105,
"storage_rebate": 0
},
"TransferPortionSuiCoin": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ expression: common_costs_actual
---
{
"MergeCoin": {
"computation_cost": 662,
"computation_cost": 665,
"storage_cost": 32,
"storage_rebate": 0
},
"Publish": {
"computation_cost": 719,
"computation_cost": 722,
"storage_cost": 83,
"storage_rebate": 0
},
Expand All @@ -29,7 +29,7 @@ expression: common_costs_actual
"storage_rebate": 15
},
"SplitCoin": {
"computation_cost": 790,
"computation_cost": 792,
"storage_cost": 80,
"storage_rebate": 0
},
Expand Down
64 changes: 64 additions & 0 deletions crates/sui-framework/docs/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Basic math for nicer programmability
- [Function `max`](#0x2_math_max)
- [Function `min`](#0x2_math_min)
- [Function `sqrt`](#0x2_math_sqrt)
- [Function `sqrt_u128`](#0x2_math_sqrt_u128)


<pre><code></code></pre>
Expand Down Expand Up @@ -134,4 +135,67 @@ math::sqrt(8 * 1000000) => 2828; // same as above, 2828 / 1000 (2.828)



</details>

<a name="0x2_math_sqrt_u128"></a>

## Function `sqrt_u128`

Similar to math::sqrt, but for u128 numbers. Get a nearest lower integer Square Root for <code>x</code>. Given that this
function can only operate with integers, it is impossible
to get perfect (or precise) integer square root for some numbers.

Example:
```
math::sqrt_u128(9) => 3
math::sqrt_u128(8) => 2 // the nearest lower square root is 4;
```

In integer math, one of the possible ways to get results with more
precision is to use higher values or temporarily multiply the
value by some bigger number. Ideally if this is a square of 10 or 100.

Example:
```
math::sqrt_u128(8) => 2;
math::sqrt_u128(8 * 10000) => 282;
// now we can use this value as if it was 2.82;
// but to get the actual result, this value needs
// to be divided by 100 (because sqrt_u128(10000)).
math::sqrt_u128(8 * 1000000) => 2828; // same as above, 2828 / 1000 (2.828)
```


<pre><code><b>public</b> <b>fun</b> <a href="math.md#0x2_math_sqrt_u128">sqrt_u128</a>(x: u128): u128
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="math.md#0x2_math_sqrt_u128">sqrt_u128</a>(x: u128): u128 {
<b>let</b> bit = 1u256 &lt;&lt; 128;
<b>let</b> res = 0u256;
<b>let</b> x = (x <b>as</b> u256);

<b>while</b> (bit != 0) {
<b>if</b> (x &gt;= res + bit) {
x = x - (res + bit);
res = (res &gt;&gt; 1) + bit;
} <b>else</b> {
res = res &gt;&gt; 1;
};
bit = bit &gt;&gt; 2;
};

(res <b>as</b> u128)
}
</code></pre>



</details>
43 changes: 43 additions & 0 deletions crates/sui-framework/sources/math.move
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,47 @@ module sui::math {

(res as u64)
}

/// Similar to math::sqrt, but for u128 numbers. Get a nearest lower integer Square Root for `x`. Given that this
/// function can only operate with integers, it is impossible
/// to get perfect (or precise) integer square root for some numbers.
///
/// Example:
/// ```
/// math::sqrt_u128(9) => 3
/// math::sqrt_u128(8) => 2 // the nearest lower square root is 4;
/// ```
///
/// In integer math, one of the possible ways to get results with more
/// precision is to use higher values or temporarily multiply the
/// value by some bigger number. Ideally if this is a square of 10 or 100.
///
/// Example:
/// ```
/// math::sqrt_u128(8) => 2;
/// math::sqrt_u128(8 * 10000) => 282;
/// // now we can use this value as if it was 2.82;
/// // but to get the actual result, this value needs
/// // to be divided by 100 (because sqrt_u128(10000)).
///
///
/// math::sqrt_u128(8 * 1000000) => 2828; // same as above, 2828 / 1000 (2.828)
/// ```
public fun sqrt_u128(x: u128): u128 {
let bit = 1u256 << 128;
let res = 0u256;
let x = (x as u256);

while (bit != 0) {
if (x >= res + bit) {
x = x - (res + bit);
res = (res >> 1) + bit;
} else {
res = res >> 1;
};
bit = bit >> 2;
};

(res as u128)
}
}
3 changes: 3 additions & 0 deletions crates/sui-framework/src/natives/test_scenario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,8 +742,11 @@ fn visit_structs_impl<FVisitTypes>(
let next_depth = depth + 1;
match move_value {
MoveValue::U8(_)
| MoveValue::U16(_)
| MoveValue::U32(_)
| MoveValue::U64(_)
| MoveValue::U128(_)
| MoveValue::U256(_)
| MoveValue::Bool(_)
| MoveValue::Address(_)
| MoveValue::Signer(_) => (),
Expand Down
5 changes: 5 additions & 0 deletions crates/sui-framework/tests/math_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ module sui::math_tests {
while (i < 1000) {
assert!(math::sqrt(i * i) == i, 1);
i = i + 1;
};
let i = 0xFFFFFFFFFu128;
while (i < 0xFFFFFFFFFu128 + 1) {
assert!(math::sqrt_u128(i * i) == i, 1);
i = i + 1;
}
}

Expand Down
Loading

0 comments on commit aa807cf

Please sign in to comment.