Skip to content

Commit

Permalink
add pow method
Browse files Browse the repository at this point in the history
  • Loading branch information
nanne007 committed Nov 25, 2021
1 parent fa8e709 commit d1a332a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
Binary file modified genesis/generated/halley/genesis
Binary file not shown.
Binary file modified vm/stdlib/compiled/latest/stdlib/79_U256.mv
Binary file not shown.
16 changes: 16 additions & 0 deletions vm/stdlib/modules/U256.move
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,22 @@ module U256 {
assert(compare(&Self::rem(copy a, c), &from_u64(1)) == EQUAL, 0);
}

public fun pow(a: U256, b: U256): U256 {
native_pow(&mut a, &b);
a
}

#[test]
fun test_pow() {
let a = Self::from_u128(10);
let b = Self::from_u64(1);
let c = Self::from_u64(2);
let d = Self::zero();
assert(compare(&Self::pow(copy a, b), &from_u64(10)) == EQUAL, 0);
assert(compare(&Self::pow(copy a, c), &from_u64(100)) == EQUAL, 0);
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;
Expand Down
26 changes: 26 additions & 0 deletions vm/stdlib/modules/doc/U256.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Implementation u256.
- [Function `mul`](#0x1_U256_mul)
- [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)
Expand Down Expand Up @@ -471,6 +472,31 @@ vector should always has two elements.



</details>

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

## Function `pow`



<pre><code><b>public</b> <b>fun</b> <a href="U256.md#0x1_U256_pow">pow</a>(a: <a href="U256.md#0x1_U256_U256">U256::U256</a>, b: <a href="U256.md#0x1_U256_U256">U256::U256</a>): <a href="U256.md#0x1_U256_U256">U256::U256</a>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="U256.md#0x1_U256_pow">pow</a>(a: <a href="U256.md#0x1_U256">U256</a>, b: <a href="U256.md#0x1_U256">U256</a>): <a href="U256.md#0x1_U256">U256</a> {
<a href="U256.md#0x1_U256_native_pow">native_pow</a>(&<b>mut</b> a, &b);
a
}
</code></pre>



</details>

<a name="0x1_U256_add_nocarry"></a>
Expand Down
2 changes: 1 addition & 1 deletion vm/stdlib/modules/doc/VMConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ The <code><a href="VMConfig.md#0x1_VMConfig_GasCost">GasCost</a></code> tracks:
<a href="Vector.md#0x1_Vector_push_back">Vector::push_back</a>(&<b>mut</b> table, <a href="VMConfig.md#0x1_VMConfig_new_gas_cost">new_gas_cost</a>(10, 1));
// <a href="U256.md#0x1_U256_rem">U256::rem</a> 28
<a href="Vector.md#0x1_Vector_push_back">Vector::push_back</a>(&<b>mut</b> table, <a href="VMConfig.md#0x1_VMConfig_new_gas_cost">new_gas_cost</a>(4, 1));
// U256::pow 29
// <a href="U256.md#0x1_U256_pow">U256::pow</a> 29
<a href="Vector.md#0x1_Vector_push_back">Vector::push_back</a>(&<b>mut</b> table, <a href="VMConfig.md#0x1_VMConfig_new_gas_cost">new_gas_cost</a>(8, 1));
table
}
Expand Down

0 comments on commit d1a332a

Please sign in to comment.