Skip to content

Commit

Permalink
Add keccak256 and blake2b hash functions to move api (MystenLabs#7986)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-lj authored Feb 9, 2023
1 parent 37fa21e commit 0977fa1
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 80 deletions.
34 changes: 17 additions & 17 deletions crates/sui-framework/docs/dynamic_field.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ Aborts with <code><a href="dynamic_field.md#0x2_dynamic_field_EFieldAlreadyExist
value: Value,
) {
<b>let</b> object_addr = <a href="object.md#0x2_object_uid_to_address">object::uid_to_address</a>(<a href="object.md#0x2_object">object</a>);
<b>let</b> <a href="">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<b>assert</b>!(!<a href="dynamic_field.md#0x2_dynamic_field_has_child_object">has_child_object</a>(object_addr, <a href="">hash</a>), <a href="dynamic_field.md#0x2_dynamic_field_EFieldAlreadyExists">EFieldAlreadyExists</a>);
<b>let</b> <a href="hash.md#0x2_hash">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<b>assert</b>!(!<a href="dynamic_field.md#0x2_dynamic_field_has_child_object">has_child_object</a>(object_addr, <a href="hash.md#0x2_hash">hash</a>), <a href="dynamic_field.md#0x2_dynamic_field_EFieldAlreadyExists">EFieldAlreadyExists</a>);
<b>let</b> field = <a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a> {
id: <a href="object.md#0x2_object_new_uid_from_hash">object::new_uid_from_hash</a>(<a href="">hash</a>),
id: <a href="object.md#0x2_object_new_uid_from_hash">object::new_uid_from_hash</a>(<a href="hash.md#0x2_hash">hash</a>),
name,
value,
};
Expand Down Expand Up @@ -185,8 +185,8 @@ type.
name: Name,
): &Value {
<b>let</b> object_addr = <a href="object.md#0x2_object_uid_to_address">object::uid_to_address</a>(<a href="object.md#0x2_object">object</a>);
<b>let</b> <a href="">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<b>let</b> field = <a href="dynamic_field.md#0x2_dynamic_field_borrow_child_object">borrow_child_object</a>&lt;<a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a>&lt;Name, Value&gt;&gt;(<a href="object.md#0x2_object">object</a>, <a href="">hash</a>);
<b>let</b> <a href="hash.md#0x2_hash">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<b>let</b> field = <a href="dynamic_field.md#0x2_dynamic_field_borrow_child_object">borrow_child_object</a>&lt;<a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a>&lt;Name, Value&gt;&gt;(<a href="object.md#0x2_object">object</a>, <a href="hash.md#0x2_hash">hash</a>);
&field.value
}
</code></pre>
Expand Down Expand Up @@ -219,8 +219,8 @@ type.
name: Name,
): &<b>mut</b> Value {
<b>let</b> object_addr = <a href="object.md#0x2_object_uid_to_address">object::uid_to_address</a>(<a href="object.md#0x2_object">object</a>);
<b>let</b> <a href="">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<b>let</b> field = <a href="dynamic_field.md#0x2_dynamic_field_borrow_child_object_mut">borrow_child_object_mut</a>&lt;<a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a>&lt;Name, Value&gt;&gt;(<a href="object.md#0x2_object">object</a>, <a href="">hash</a>);
<b>let</b> <a href="hash.md#0x2_hash">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<b>let</b> field = <a href="dynamic_field.md#0x2_dynamic_field_borrow_child_object_mut">borrow_child_object_mut</a>&lt;<a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a>&lt;Name, Value&gt;&gt;(<a href="object.md#0x2_object">object</a>, <a href="hash.md#0x2_hash">hash</a>);
&<b>mut</b> field.value
}
</code></pre>
Expand Down Expand Up @@ -254,8 +254,8 @@ type.
name: Name,
): Value {
<b>let</b> object_addr = <a href="object.md#0x2_object_uid_to_address">object::uid_to_address</a>(<a href="object.md#0x2_object">object</a>);
<b>let</b> <a href="">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<b>let</b> <a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a> { id, name: _, value } = <a href="dynamic_field.md#0x2_dynamic_field_remove_child_object">remove_child_object</a>&lt;<a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a>&lt;Name, Value&gt;&gt;(object_addr, <a href="">hash</a>);
<b>let</b> <a href="hash.md#0x2_hash">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<b>let</b> <a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a> { id, name: _, value } = <a href="dynamic_field.md#0x2_dynamic_field_remove_child_object">remove_child_object</a>&lt;<a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a>&lt;Name, Value&gt;&gt;(object_addr, <a href="hash.md#0x2_hash">hash</a>);
<a href="object.md#0x2_object_delete">object::delete</a>(id);
value
}
Expand Down Expand Up @@ -287,8 +287,8 @@ Returns true if and only if the <code><a href="object.md#0x2_object">object</a><
name: Name,
): bool {
<b>let</b> object_addr = <a href="object.md#0x2_object_uid_to_address">object::uid_to_address</a>(<a href="object.md#0x2_object">object</a>);
<b>let</b> <a href="">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<a href="dynamic_field.md#0x2_dynamic_field_has_child_object">has_child_object</a>(object_addr, <a href="">hash</a>)
<b>let</b> <a href="hash.md#0x2_hash">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<a href="dynamic_field.md#0x2_dynamic_field_has_child_object">has_child_object</a>(object_addr, <a href="hash.md#0x2_hash">hash</a>)
}
</code></pre>

Expand Down Expand Up @@ -318,8 +318,8 @@ Returns true if and only if the <code><a href="object.md#0x2_object">object</a><
name: Name,
): bool {
<b>let</b> object_addr = <a href="object.md#0x2_object_uid_to_address">object::uid_to_address</a>(<a href="object.md#0x2_object">object</a>);
<b>let</b> <a href="">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<a href="dynamic_field.md#0x2_dynamic_field_has_child_object_with_ty">has_child_object_with_ty</a>&lt;<a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a>&lt;Name, Value&gt;&gt;(object_addr, <a href="">hash</a>)
<b>let</b> <a href="hash.md#0x2_hash">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<a href="dynamic_field.md#0x2_dynamic_field_has_child_object_with_ty">has_child_object_with_ty</a>&lt;<a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a>&lt;Name, Value&gt;&gt;(object_addr, <a href="hash.md#0x2_hash">hash</a>)
}
</code></pre>

Expand Down Expand Up @@ -347,8 +347,8 @@ Returns true if and only if the <code><a href="object.md#0x2_object">object</a><
name: Name,
): (&UID, <b>address</b>) {
<b>let</b> object_addr = <a href="object.md#0x2_object_uid_to_address">object::uid_to_address</a>(<a href="object.md#0x2_object">object</a>);
<b>let</b> <a href="">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<b>let</b> <a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a> { id, name: _, value } = <a href="dynamic_field.md#0x2_dynamic_field_borrow_child_object">borrow_child_object</a>&lt;<a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a>&lt;Name, ID&gt;&gt;(<a href="object.md#0x2_object">object</a>, <a href="">hash</a>);
<b>let</b> <a href="hash.md#0x2_hash">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<b>let</b> <a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a> { id, name: _, value } = <a href="dynamic_field.md#0x2_dynamic_field_borrow_child_object">borrow_child_object</a>&lt;<a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a>&lt;Name, ID&gt;&gt;(<a href="object.md#0x2_object">object</a>, <a href="hash.md#0x2_hash">hash</a>);
(id, <a href="object.md#0x2_object_id_to_address">object::id_to_address</a>(value))
}
</code></pre>
Expand Down Expand Up @@ -377,8 +377,8 @@ Returns true if and only if the <code><a href="object.md#0x2_object">object</a><
name: Name,
): (&<b>mut</b> UID, <b>address</b>) {
<b>let</b> object_addr = <a href="object.md#0x2_object_uid_to_address">object::uid_to_address</a>(<a href="object.md#0x2_object">object</a>);
<b>let</b> <a href="">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<b>let</b> <a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a> { id, name: _, value } = <a href="dynamic_field.md#0x2_dynamic_field_borrow_child_object_mut">borrow_child_object_mut</a>&lt;<a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a>&lt;Name, ID&gt;&gt;(<a href="object.md#0x2_object">object</a>, <a href="">hash</a>);
<b>let</b> <a href="hash.md#0x2_hash">hash</a> = <a href="dynamic_field.md#0x2_dynamic_field_hash_type_and_key">hash_type_and_key</a>(object_addr, name);
<b>let</b> <a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a> { id, name: _, value } = <a href="dynamic_field.md#0x2_dynamic_field_borrow_child_object_mut">borrow_child_object_mut</a>&lt;<a href="dynamic_field.md#0x2_dynamic_field_Field">Field</a>&lt;Name, ID&gt;&gt;(<a href="object.md#0x2_object">object</a>, <a href="hash.md#0x2_hash">hash</a>);
(id, <a href="object.md#0x2_object_id_to_address">object::id_to_address</a>(value))
}
</code></pre>
Expand Down
25 changes: 0 additions & 25 deletions crates/sui-framework/docs/ecdsa_k1.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
- [Constants](#@Constants_0)
- [Function `ecrecover`](#0x2_ecdsa_k1_ecrecover)
- [Function `decompress_pubkey`](#0x2_ecdsa_k1_decompress_pubkey)
- [Function `keccak256`](#0x2_ecdsa_k1_keccak256)
- [Function `secp256k1_verify`](#0x2_ecdsa_k1_secp256k1_verify)
- [Function `secp256k1_verify_recoverable`](#0x2_ecdsa_k1_secp256k1_verify_recoverable)

Expand Down Expand Up @@ -97,30 +96,6 @@ otherwise throw error.



</details>

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

## Function `keccak256`

@param data: arbitrary bytes data to hash
Hash the input bytes using keccak256 and returns 32 bytes.


<pre><code><b>public</b> <b>fun</b> <a href="ecdsa_k1.md#0x2_ecdsa_k1_keccak256">keccak256</a>(data: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>native</b> <b>fun</b> <a href="ecdsa_k1.md#0x2_ecdsa_k1_keccak256">keccak256</a>(data: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;;
</code></pre>



</details>

<a name="0x2_ecdsa_k1_secp256k1_verify"></a>
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-framework/docs/ecvrf.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
Verify a proof for a Ristretto ECVRF. Returns true if the proof is valid and corresponds to the given output.


<pre><code><b>public</b> <b>fun</b> <a href="ecvrf.md#0x2_ecvrf_ecvrf_verify">ecvrf_verify</a>(<a href="">hash</a>: &<a href="">vector</a>&lt;u8&gt;, alpha_string: &<a href="">vector</a>&lt;u8&gt;, public_key: &<a href="">vector</a>&lt;u8&gt;, proof: &<a href="">vector</a>&lt;u8&gt;): bool
<pre><code><b>public</b> <b>fun</b> <a href="ecvrf.md#0x2_ecvrf_ecvrf_verify">ecvrf_verify</a>(<a href="hash.md#0x2_hash">hash</a>: &<a href="">vector</a>&lt;u8&gt;, alpha_string: &<a href="">vector</a>&lt;u8&gt;, public_key: &<a href="">vector</a>&lt;u8&gt;, proof: &<a href="">vector</a>&lt;u8&gt;): bool
</code></pre>


Expand All @@ -65,7 +65,7 @@ Verify a proof for a Ristretto ECVRF. Returns true if the proof is valid and cor
<summary>Implementation</summary>


<pre><code><b>public</b> <b>native</b> <b>fun</b> <a href="ecvrf.md#0x2_ecvrf_ecvrf_verify">ecvrf_verify</a>(<a href="">hash</a>: &<a href="">vector</a>&lt;u8&gt;, alpha_string: &<a href="">vector</a>&lt;u8&gt;, public_key: &<a href="">vector</a>&lt;u8&gt;, proof: &<a href="">vector</a>&lt;u8&gt;): bool;
<pre><code><b>public</b> <b>native</b> <b>fun</b> <a href="ecvrf.md#0x2_ecvrf_ecvrf_verify">ecvrf_verify</a>(<a href="hash.md#0x2_hash">hash</a>: &<a href="">vector</a>&lt;u8&gt;, alpha_string: &<a href="">vector</a>&lt;u8&gt;, public_key: &<a href="">vector</a>&lt;u8&gt;, proof: &<a href="">vector</a>&lt;u8&gt;): bool;
</code></pre>


Expand Down
64 changes: 64 additions & 0 deletions crates/sui-framework/docs/hash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

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

# Module `0x2::hash`

Module which defines hash functions. Note that Sha-256 and Sha3-256 is available in the std::hash module in the
standard library.


- [Function `blake2b256`](#0x2_hash_blake2b256)
- [Function `keccak256`](#0x2_hash_keccak256)


<pre><code></code></pre>



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

## Function `blake2b256`

@param data: Arbitrary binary data to hash
Hash the input bytes using Blake2b-256 and returns 32 bytes.


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x2_hash_blake2b256">blake2b256</a>(data: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>native</b> <b>public</b> <b>fun</b> <a href="hash.md#0x2_hash_blake2b256">blake2b256</a>(data: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;;
</code></pre>



</details>

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

## Function `keccak256`

@param data: Arbitrary binary data to hash
Hash the input bytes using keccak256 and returns 32 bytes.


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x2_hash_keccak256">keccak256</a>(data: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>native</b> <b>public</b> <b>fun</b> <a href="hash.md#0x2_hash_keccak256">keccak256</a>(data: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;;
</code></pre>



</details>
4 changes: 0 additions & 4 deletions crates/sui-framework/sources/crypto/ecdsa_k1.move
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ module sui::ecdsa_k1 {
/// otherwise throw error.
public native fun decompress_pubkey(pubkey: &vector<u8>): vector<u8>;

/// @param data: arbitrary bytes data to hash
/// Hash the input bytes using keccak256 and returns 32 bytes.
public native fun keccak256(data: &vector<u8>): vector<u8>;

/// @param signature: A 64-bytes signature in form (r, s) that is signed using
/// Secp256k1. This is an non-recoverable signature without recovery id.
/// Reference implementation on signature generation using RFC6979:
Expand Down
14 changes: 14 additions & 0 deletions crates/sui-framework/sources/crypto/hash.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

/// Module which defines hash functions. Note that Sha-256 and Sha3-256 is available in the std::hash module in the
/// standard library.
module sui::hash {
/// @param data: Arbitrary binary data to hash
/// Hash the input bytes using Blake2b-256 and returns 32 bytes.
native public fun blake2b256(data: &vector<u8>): vector<u8>;

/// @param data: Arbitrary binary data to hash
/// Hash the input bytes using keccak256 and returns 32 bytes.
native public fun keccak256(data: &vector<u8>): vector<u8>;
}
20 changes: 0 additions & 20 deletions crates/sui-framework/src/natives/crypto/ecdsa_k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
use crate::legacy_empty_cost;
use fastcrypto::{
hash::{HashFunction, Keccak256},
secp256k1::{
recoverable::{Secp256k1RecoverablePublicKey, Secp256k1RecoverableSignature},
Secp256k1PublicKey, Secp256k1Signature,
Expand Down Expand Up @@ -94,25 +93,6 @@ pub fn decompress_pubkey(
}
}

pub fn keccak256(
_context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.is_empty());
debug_assert!(args.len() == 1);

// TODO: implement native gas cost estimation https://github.com/MystenLabs/sui/issues/3593
let cost = legacy_empty_cost();
let msg = pop_arg!(args, VectorRef);
let msg_ref = msg.as_bytes_ref();

Ok(NativeResult::ok(
cost,
smallvec![Value::vector_u8(Keccak256::digest(&*msg_ref).to_vec())],
))
}

pub fn secp256k1_verify(
_context: &mut NativeContext,
ty_args: Vec<Type>,
Expand Down
50 changes: 50 additions & 0 deletions crates/sui-framework/src/natives/crypto/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use crate::legacy_empty_cost;
use fastcrypto::hash::{Blake2b256, HashFunction, Keccak256};
use move_binary_format::errors::PartialVMResult;
use move_vm_runtime::native_functions::NativeContext;
use move_vm_types::{
loaded_data::runtime_types::Type,
natives::function::NativeResult,
pop_arg,
values::{Value, VectorRef},
};
use smallvec::smallvec;
use std::collections::VecDeque;

fn hash<H: HashFunction<DIGEST_SIZE>, const DIGEST_SIZE: usize>(
_context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.is_empty());
debug_assert!(args.len() == 1);

// TODO: implement native gas cost estimation https://github.com/MystenLabs/sui/issues/3593
let cost = legacy_empty_cost();
let msg = pop_arg!(args, VectorRef);

Ok(NativeResult::ok(
cost,
smallvec![Value::vector_u8(
H::digest(msg.as_bytes_ref().as_slice()).digest
)],
))
}

pub fn keccak256(
context: &mut NativeContext,
ty_args: Vec<Type>,
args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
hash::<Keccak256, 32>(context, ty_args, args)
}

pub fn blake2b256(
context: &mut NativeContext,
ty_args: Vec<Type>,
args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
hash::<Blake2b256, 32>(context, ty_args, args)
}
1 change: 1 addition & 0 deletions crates/sui-framework/src/natives/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ pub mod ecvrf;
pub mod ed25519;
pub mod elliptic_curve;
pub mod groth16;
pub mod hash;
pub mod hmac;
pub mod tbls;
5 changes: 3 additions & 2 deletions crates/sui-framework/src/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use move_vm_types::{
use std::sync::Arc;

use self::crypto::{
bls12381, bulletproofs, ecdsa_k1, ecvrf, ed25519, elliptic_curve, groth16, hmac, tbls,
bls12381, bulletproofs, ecdsa_k1, ecvrf, ed25519, elliptic_curve, groth16, hash, hmac, tbls,
};

pub fn all_natives(
Expand All @@ -35,6 +35,7 @@ pub fn all_natives(
("address", "from_bytes", make_native!(address::from_bytes)),
("address", "to_u256", make_native!(address::to_u256)),
("address", "from_u256", make_native!(address::from_u256)),
("hash", "blake2b256", make_native!(hash::blake2b256)),
(
"bls12381",
"bls12381_min_sig_verify",
Expand Down Expand Up @@ -91,7 +92,6 @@ pub fn all_natives(
"decompress_pubkey",
make_native!(ecdsa_k1::decompress_pubkey),
),
("ecdsa_k1", "keccak256", make_native!(ecdsa_k1::keccak256)),
(
"ecdsa_k1",
"secp256k1_verify",
Expand Down Expand Up @@ -149,6 +149,7 @@ pub fn all_natives(
"native_hmac_sha3_256",
make_native!(hmac::hmac_sha3_256),
),
("hash", "keccak256", make_native!(hash::keccak256)),
("object", "delete_impl", make_native!(object::delete_impl)),
("object", "borrow_uid", make_native!(object::borrow_uid)),
(
Expand Down
13 changes: 3 additions & 10 deletions crates/sui-framework/tests/crypto/ecdsa_k1_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
module sui::ecdsa_tests {
use sui::ecdsa_k1;
use std::vector;

use sui::hash;

#[test]
fun test_ecrecover_pubkey() {
// test case generated against https://docs.rs/secp256k1/latest/secp256k1/
Expand Down Expand Up @@ -135,14 +136,6 @@ module sui::ecdsa_tests {
assert!(addr == addr1, 0);
}

#[test]
fun test_keccak256_hash() {
let msg = b"hello world!";
let hashed_msg_bytes = x"57caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd6";
let hashed_msg = ecdsa_k1::keccak256(&msg);
assert!(hashed_msg == hashed_msg_bytes, 0);
}

// Helper Move function to recover signature directly to an ETH address.
fun ecrecover_eth_address(sig: vector<u8>, hashed_msg: vector<u8>): vector<u8> {
// Normalize the last byte of the signature to be 0 or 1.
Expand All @@ -168,7 +161,7 @@ module sui::ecdsa_tests {
};

// Take the last 20 bytes of the hash of the 64-bytes uncompressed pubkey.
let hashed = ecdsa_k1::keccak256(&uncompressed_64);
let hashed = hash::keccak256(&uncompressed_64);
let addr = vector::empty<u8>();
let i = 12;
while (i < 32) {
Expand Down
Loading

0 comments on commit 0977fa1

Please sign in to comment.