Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cmp #190

Merged
merged 2 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified build/StarcoinFramework/bytecode_modules/Compare.mv
Binary file not shown.
Binary file modified build/StarcoinFramework/bytecode_modules/U256.mv
Binary file not shown.
20 changes: 13 additions & 7 deletions build/StarcoinFramework/docs/Compare.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ Keep this in mind when using this function to compare addresses.
<b>while</b> (i1 &gt; 0 && i2 &gt; 0) {
i1 = i1 - 1;
i2 = i2 - 1;
<b>let</b> elem_cmp = <a href="Compare.md#0x1_Compare_cmp_u8">cmp_u8</a>(*<a href="Vector.md#0x1_Vector_borrow">Vector::borrow</a>(v1, i1), *<a href="Vector.md#0x1_Vector_borrow">Vector::borrow</a>(v2, i2));
<b>let</b> v1 = *<a href="Vector.md#0x1_Vector_borrow">Vector::borrow</a>(v1, i1);
<b>let</b> v2 = *<a href="Vector.md#0x1_Vector_borrow">Vector::borrow</a>(v2, i2);
<b>let</b> elem_cmp = <b>if</b> (v1 == v2) <a href="Compare.md#0x1_Compare_EQUAL">EQUAL</a>
<b>else</b> <b>if</b> (v1 &lt; v2) <a href="Compare.md#0x1_Compare_LESS_THAN">LESS_THAN</a>
<b>else</b> <a href="Compare.md#0x1_Compare_GREATER_THAN">GREATER_THAN</a>;
<b>if</b> (elem_cmp != 0) <b>return</b> elem_cmp
// <b>else</b>, compare next element
};
Expand Down Expand Up @@ -147,16 +151,18 @@ Keep this in mind when using this function to compare addresses.
<b>let</b> l1 = <a href="Vector.md#0x1_Vector_length">Vector::length</a>(v1);
<b>let</b> l2 = <a href="Vector.md#0x1_Vector_length">Vector::length</a>(v2);
<b>let</b> len_cmp = <a href="Compare.md#0x1_Compare_cmp_u64">cmp_u64</a>(l1, l2);
<b>let</b> i1 = 0;
<b>let</b> i2 = 0;
<b>while</b> (i1 &lt; l1 && i2 &lt; l2) {
<b>let</b> elem_cmp = <a href="Compare.md#0x1_Compare_cmp_u8">cmp_u8</a>(*<a href="Vector.md#0x1_Vector_borrow">Vector::borrow</a>(v1, i1), *<a href="Vector.md#0x1_Vector_borrow">Vector::borrow</a>(v2, i2));
<b>let</b> i = 0;
<b>while</b> (i &lt; l1 && i &lt; l2) {
<b>let</b> v1 = *<a href="Vector.md#0x1_Vector_borrow">Vector::borrow</a>(v1, i);
<b>let</b> v2 = *<a href="Vector.md#0x1_Vector_borrow">Vector::borrow</a>(v2, i);
<b>let</b> elem_cmp = <b>if</b> (v1 == v2) <a href="Compare.md#0x1_Compare_EQUAL">EQUAL</a>
<b>else</b> <b>if</b> (v1 &lt; v2) <a href="Compare.md#0x1_Compare_LESS_THAN">LESS_THAN</a>
<b>else</b> <a href="Compare.md#0x1_Compare_GREATER_THAN">GREATER_THAN</a>;
<b>if</b> (elem_cmp != 0) {
<b>return</b> elem_cmp
};
// <b>else</b>, compare next element
i1 = i1 + 1;
i2 = i2 + 1;
i = i + 1;
};
// all compared elements equal; <b>use</b> length comparison <b>to</b> <b>break</b> the tie
len_cmp
Expand Down
78 changes: 1 addition & 77 deletions build/StarcoinFramework/docs/U256.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ Implementation u256.
- [Function `div`](#0x1_U256_div)
- [Function `rem`](#0x1_U256_rem)
- [Function `pow`](#0x1_U256_pow)
- [Function `add_nocarry`](#0x1_U256_add_nocarry)
- [Function `sub_noborrow`](#0x1_U256_sub_noborrow)
- [Function `from_bytes`](#0x1_U256_from_bytes)
- [Function `native_add`](#0x1_U256_native_add)
- [Function `native_sub`](#0x1_U256_native_sub)
Expand All @@ -34,8 +32,7 @@ Implementation u256.
- [Module Specification](#@Module_Specification_1)


<pre><code><b>use</b> <a href="U256.md#0x1_Arith">0x1::Arith</a>;
<b>use</b> <a href="Errors.md#0x1_Errors">0x1::Errors</a>;
<pre><code><b>use</b> <a href="Errors.md#0x1_Errors">0x1::Errors</a>;
<b>use</b> <a href="Vector.md#0x1_Vector">0x1::Vector</a>;
</code></pre>

Expand Down Expand Up @@ -678,79 +675,6 @@ vector should always has two elements.



</details>

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

## Function `add_nocarry`

move implementation of native_add.


<pre><code><b>fun</b> <a href="U256.md#0x1_U256_add_nocarry">add_nocarry</a>(a: &<b>mut</b> <a href="U256.md#0x1_U256_U256">U256::U256</a>, b: &<a href="U256.md#0x1_U256_U256">U256::U256</a>)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>fun</b> <a href="U256.md#0x1_U256_add_nocarry">add_nocarry</a>(a: &<b>mut</b> <a href="U256.md#0x1_U256">U256</a>, b: &<a href="U256.md#0x1_U256">U256</a>) {
<b>let</b> carry = 0;
<b>let</b> idx = 0;
<b>let</b> len = (<a href="U256.md#0x1_U256_WORD">WORD</a> <b>as</b> u64);
<b>while</b> (idx &lt; len) {
<b>let</b> a_bit = <a href="Vector.md#0x1_Vector_borrow_mut">Vector::borrow_mut</a>(&<b>mut</b> a.bits, idx);
<b>let</b> b_bit = <a href="Vector.md#0x1_Vector_borrow">Vector::borrow</a>(&b.bits, idx);
*a_bit = StarcoinFramework::Arith::adc(*a_bit, *b_bit, &<b>mut</b> carry);
idx = idx + 1;
};

// check overflow
<b>assert</b>!(carry == 0, 100);
}
</code></pre>



</details>

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

## Function `sub_noborrow`

move implementation of native_sub.


<pre><code><b>fun</b> <a href="U256.md#0x1_U256_sub_noborrow">sub_noborrow</a>(a: &<b>mut</b> <a href="U256.md#0x1_U256_U256">U256::U256</a>, b: &<a href="U256.md#0x1_U256_U256">U256::U256</a>)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>fun</b> <a href="U256.md#0x1_U256_sub_noborrow">sub_noborrow</a>(a: &<b>mut</b> <a href="U256.md#0x1_U256">U256</a>, b: &<a href="U256.md#0x1_U256">U256</a>) {
<b>let</b> borrow = 0;
<b>let</b> idx = 0;
<b>let</b> len = (<a href="U256.md#0x1_U256_WORD">WORD</a> <b>as</b> u64);
<b>while</b> (idx &lt; len) {
<b>let</b> a_bit = <a href="Vector.md#0x1_Vector_borrow_mut">Vector::borrow_mut</a>(&<b>mut</b> a.bits, idx);
<b>let</b> b_bit = <a href="Vector.md#0x1_Vector_borrow">Vector::borrow</a>(&b.bits, idx);
*a_bit = StarcoinFramework::Arith::sbb(*a_bit, *b_bit, &<b>mut</b> borrow);
idx = idx + 1;
};

// check overflow
<b>assert</b>!(borrow == 0, 100);

}
</code></pre>



</details>

<a name="0x1_U256_from_bytes"></a>
Expand Down
Binary file modified build/StarcoinFramework/source_maps/Arith.mvsm
Binary file not shown.
Binary file modified build/StarcoinFramework/source_maps/Compare.mvsm
Binary file not shown.
Binary file modified build/StarcoinFramework/source_maps/U256.mvsm
Binary file not shown.
2 changes: 1 addition & 1 deletion integration-tests/compare/compare.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ processed 3 tasks

task 2 'run'. lines 5-62:
{
"gas_used": 2338303,
"gas_used": 1789891,
"status": "Executed"
}
12 changes: 6 additions & 6 deletions integration-tests/incubator/SortedLinkedList.exp
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ task 10 'run'. lines 293-303:

task 11 'run'. lines 305-317:
{
"gas_used": 359425,
"gas_used": 295841,
"status": "Executed"
}

task 12 'run'. lines 319-331:
{
"gas_used": 495218,
"gas_used": 399842,
"status": "Executed"
}

task 13 'run'. lines 333-343:
{
"gas_used": 595306,
"gas_used": 468138,
"status": "Executed"
}

Expand Down Expand Up @@ -72,19 +72,19 @@ task 19 'run'. lines 504-512:

task 20 'run'. lines 514-523:
{
"gas_used": 221094,
"gas_used": 213146,
"status": "Executed"
}

task 21 'run'. lines 525-534:
{
"gas_used": 259964,
"gas_used": 248042,
"status": "Executed"
}

task 22 'run'. lines 536-545:
{
"gas_used": 315232,
"gas_used": 299336,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/list/linked_list.exp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ task 9 'run'. lines 386-395:

task 10 'run'. lines 397-410:
{
"gas_used": 144048,
"gas_used": 136100,
"status": "Executed"
}

Expand Down
20 changes: 13 additions & 7 deletions sources/Compare.move
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ module Compare {
while (i1 > 0 && i2 > 0) {
i1 = i1 - 1;
i2 = i2 - 1;
let elem_cmp = cmp_u8(*Vector::borrow(v1, i1), *Vector::borrow(v2, i2));
pause125 marked this conversation as resolved.
Show resolved Hide resolved
let v1 = *Vector::borrow(v1, i1);
let v2 = *Vector::borrow(v2, i2);
let elem_cmp = if (v1 == v2) EQUAL
else if (v1 < v2) LESS_THAN
else GREATER_THAN;
if (elem_cmp != 0) return elem_cmp
// else, compare next element
};
Expand All @@ -58,16 +62,18 @@ module Compare {
let l1 = Vector::length(v1);
let l2 = Vector::length(v2);
let len_cmp = cmp_u64(l1, l2);
let i1 = 0;
let i2 = 0;
while (i1 < l1 && i2 < l2) {
let elem_cmp = cmp_u8(*Vector::borrow(v1, i1), *Vector::borrow(v2, i2));
let i = 0;
while (i < l1 && i < l2) {
let v1 = *Vector::borrow(v1, i);
let v2 = *Vector::borrow(v2, i);
let elem_cmp = if (v1 == v2) EQUAL
else if (v1 < v2) LESS_THAN
else GREATER_THAN;
if (elem_cmp != 0) {
return elem_cmp
};
// else, compare next element
i1 = i1 + 1;
i2 = i2 + 1;
i = i + 1;
};
// all compared elements equal; use length comparison to break the tie
len_cmp
Expand Down
109 changes: 0 additions & 109 deletions sources/U256.move
Original file line number Diff line number Diff line change
Expand Up @@ -378,115 +378,6 @@ module U256 {
assert!(compare(&Self::pow(copy a, d), &from_u64(1)) == EQUAL, 0);
}

/// move implementation of native_add.
fun add_nocarry(a: &mut U256, b: &U256) {
let carry = 0;
let idx = 0;
let len = (WORD as u64);
while (idx < len) {
let a_bit = Vector::borrow_mut(&mut a.bits, idx);
let b_bit = Vector::borrow(&b.bits, idx);
*a_bit = StarcoinFramework::Arith::adc(*a_bit, *b_bit, &mut carry);
idx = idx + 1;
};

// check overflow
assert!(carry == 0, 100);
}

// TODO: MVP find false examples that violate the spec
// spec add_nocarry {
// aborts_if value_of_U256(a) + value_of_U256(b) >= P64 * P64 * P64 * P64;
// ensures value_of_U256(a) == value_of_U256(old(a)) + value_of_U256(b);
// }

#[test]
#[expected_failure]
fun test_add_nocarry_overflow() {
let va = Vector::empty();
Vector::push_back(&mut va, 15891);
Vector::push_back(&mut va, 0);
Vector::push_back(&mut va, 0);
Vector::push_back(&mut va, 0);

let vb = Vector::empty();
Vector::push_back(&mut vb, 18446744073709535725);
Vector::push_back(&mut vb, 18446744073709551615);
Vector::push_back(&mut vb, 18446744073709551615);
Vector::push_back(&mut vb, 18446744073709551615);

let a = U256 { bits: va };
let b = U256 { bits: vb };
add_nocarry(&mut a, &b); // MVP thinks this won't abort
}

#[test]
fun test_add_nocarry_like_native_1() {
let va = Vector::empty();
Vector::push_back(&mut va, 15891);
Vector::push_back(&mut va, 0);
Vector::push_back(&mut va, 0);
Vector::push_back(&mut va, 0);

let vb = Vector::empty();
Vector::push_back(&mut vb, 18446744073709535724);
Vector::push_back(&mut vb, 18446744073709551615);
Vector::push_back(&mut vb, 18446744073709551615);
Vector::push_back(&mut vb, 18446744073709551615);

let a1 = U256 { bits: va };
let a2 = copy a1;
let b = U256 { bits: vb };
add_nocarry(&mut a1, &b);
native_add(&mut a2, &b);
assert!(compare(&a1, &a2) == EQUAL, 0); // MVP thinks this doesn't hold
}

#[test]
fun test_add_nocarry_like_native_2() {
let va = Vector::empty();
Vector::push_back(&mut va, 26962);
Vector::push_back(&mut va, 24464);
Vector::push_back(&mut va, 6334);
Vector::push_back(&mut va, 19169);

let vb = Vector::empty();
Vector::push_back(&mut vb, 29358);
Vector::push_back(&mut vb, 26500);
Vector::push_back(&mut vb, 15724);
Vector::push_back(&mut vb, 11478);

let a1 = U256 { bits: va };
let a2 = copy a1;
let b = U256 { bits: vb };
add_nocarry(&mut a1, &b); // MVP thinks this abort
native_add(&mut a2, &b);
assert!(compare(&a1, &a2) == EQUAL, 0);
}

/// move implementation of native_sub.
fun sub_noborrow(a: &mut U256, b: &U256) {
let borrow = 0;
let idx = 0;
let len = (WORD as u64);
while (idx < len) {
let a_bit = Vector::borrow_mut(&mut a.bits, idx);
let b_bit = Vector::borrow(&b.bits, idx);
*a_bit = StarcoinFramework::Arith::sbb(*a_bit, *b_bit, &mut borrow);
idx = idx + 1;
};

// check overflow
assert!(borrow == 0, 100);

}

// TODO: Similar situation with `add_nocarry`
// spec sub_noborrow {
// aborts_if value_of_U256(a) < value_of_U256(b);
// ensures value_of_U256(a) == value_of_U256(old(a)) - value_of_U256(b);
// }

native fun from_bytes(data: &vector<u8>, be: bool): U256;
native fun native_add(a: &mut U256, b: &U256);
native fun native_sub(a: &mut U256, b: &U256);
Expand Down