Skip to content

hash - [mainnet]

Cryptographic hashes:

In addition, SHA2-256 and SHA3-256 are available in std::hash. Note that SHA3-256 is a variant of Keccak: it is NOT the same as Keccak-256.

Non-cryptograhic hashes:

use 0x1::bcs;
use 0x1::error;
use 0x1::features;

Constants

A newly-added native function is not yet enabled.

const E_NATIVE_FUN_NOT_AVAILABLE: u64 = 1;

Functions

sip_hash

Returns the (non-cryptographic) SipHash of bytes. See https://en.wikipedia.org/wiki/SipHash

public fun sip_hash(bytes: vector<u8>): u64
Implementation
native public fun sip_hash(bytes: vector<u8>): u64;

sip_hash_from_value

Returns the (non-cryptographic) SipHash of the BCS serialization of v. See https://en.wikipedia.org/wiki/SipHash

public fun sip_hash_from_value<MoveValue>(v: &MoveValue): u64
Implementation
public fun sip_hash_from_value<MoveValue>(v: &MoveValue): u64 {
let bytes = bcs::to_bytes(v);
sip_hash(bytes)
}

keccak256

Returns the Keccak-256 hash of bytes.

public fun keccak256(bytes: vector<u8>): vector<u8>
Implementation
native public fun keccak256(bytes: vector<u8>): vector<u8>;

sha2_512

Returns the SHA2-512 hash of bytes.

public fun sha2_512(bytes: vector<u8>): vector<u8>
Implementation
public fun sha2_512(bytes: vector<u8>): vector<u8> {
if(!features::sha_512_and_ripemd_160_enabled()) {
abort(std::error::invalid_state(E_NATIVE_FUN_NOT_AVAILABLE))
};
sha2_512_internal(bytes)
}

sha3_512

Returns the SHA3-512 hash of bytes.

public fun sha3_512(bytes: vector<u8>): vector<u8>
Implementation
public fun sha3_512(bytes: vector<u8>): vector<u8> {
if(!features::sha_512_and_ripemd_160_enabled()) {
abort(std::error::invalid_state(E_NATIVE_FUN_NOT_AVAILABLE))
};
sha3_512_internal(bytes)
}

ripemd160

Returns the RIPEMD-160 hash of bytes.

WARNING: Only 80-bit security is provided by this function. This means an adversary who can compute roughly 2^80 hashes will, with high probability, find a collision x_1 != x_2 such that RIPEMD-160(x_1) = RIPEMD-160(x_2).

public fun ripemd160(bytes: vector<u8>): vector<u8>
Implementation
public fun ripemd160(bytes: vector<u8>): vector<u8> {
if(!features::sha_512_and_ripemd_160_enabled()) {
abort(std::error::invalid_state(E_NATIVE_FUN_NOT_AVAILABLE))
};
ripemd160_internal(bytes)
}

blake2b_256

Returns the BLAKE2B-256 hash of bytes.

public fun blake2b_256(bytes: vector<u8>): vector<u8>
Implementation
public fun blake2b_256(bytes: vector<u8>): vector<u8> {
if(!features::blake2b_256_enabled()) {
abort(std::error::invalid_state(E_NATIVE_FUN_NOT_AVAILABLE))
};
blake2b_256_internal(bytes)
}

sha2_512_internal

Returns the SHA2-512 hash of bytes.

fun sha2_512_internal(bytes: vector<u8>): vector<u8>
Implementation
native fun sha2_512_internal(bytes: vector<u8>): vector<u8>;

sha3_512_internal

Returns the SHA3-512 hash of bytes.

fun sha3_512_internal(bytes: vector<u8>): vector<u8>
Implementation
native fun sha3_512_internal(bytes: vector<u8>): vector<u8>;

ripemd160_internal

Returns the RIPEMD-160 hash of bytes.

WARNING: Only 80-bit security is provided by this function. This means an adversary who can compute roughly 2^80 hashes will, with high probability, find a collision x_1 != x_2 such that RIPEMD-160(x_1) = RIPEMD-160(x_2).

fun ripemd160_internal(bytes: vector<u8>): vector<u8>
Implementation
native fun ripemd160_internal(bytes: vector<u8>): vector<u8>;

blake2b_256_internal

Returns the BLAKE2B-256 hash of bytes.

fun blake2b_256_internal(bytes: vector<u8>): vector<u8>
Implementation
native fun blake2b_256_internal(bytes: vector<u8>): vector<u8>;

Specification

spec_sip_hash is not assumed to be injective.

fun spec_sip_hash(bytes: vector<u8>): u64;

spec_keccak256 is an injective function.

fun spec_keccak256(bytes: vector<u8>): vector<u8>;
axiom forall b1: vector<u8>, b2: vector<u8>:
(spec_keccak256(b1) == spec_keccak256(b2) ==> b1 == b2);

spec_sha2_512_internal is an injective function.

fun spec_sha2_512_internal(bytes: vector<u8>): vector<u8>;
axiom forall b1: vector<u8>, b2: vector<u8>:
(spec_sha2_512_internal(b1) == spec_sha2_512_internal(b2) ==> b1 == b2);

spec_sha3_512_internal is an injective function.

fun spec_sha3_512_internal(bytes: vector<u8>): vector<u8>;
axiom forall b1: vector<u8>, b2: vector<u8>:
(spec_sha3_512_internal(b1) == spec_sha3_512_internal(b2) ==> b1 == b2);

spec_ripemd160_internal is an injective function.

fun spec_ripemd160_internal(bytes: vector<u8>): vector<u8>;
axiom forall b1: vector<u8>, b2: vector<u8>:
(spec_ripemd160_internal(b1) == spec_ripemd160_internal(b2) ==> b1 == b2);

spec_blake2b_256_internal is an injective function.

fun spec_blake2b_256_internal(bytes: vector<u8>): vector<u8>;
axiom forall b1: vector<u8>, b2: vector<u8>:
(spec_blake2b_256_internal(b1) == spec_blake2b_256_internal(b2) ==> b1 == b2);

sip_hash

public fun sip_hash(bytes: vector<u8>): u64
pragma opaque;
aborts_if [abstract] false;
ensures [abstract] result == spec_sip_hash(bytes);

sip_hash_from_value

public fun sip_hash_from_value<MoveValue>(v: &MoveValue): u64
pragma opaque;
ensures result == spec_sip_hash(bcs::serialize(v));

keccak256

public fun keccak256(bytes: vector<u8>): vector<u8>
pragma opaque;
aborts_if [abstract] false;
ensures [abstract] result == spec_keccak256(bytes);

sha2_512

public fun sha2_512(bytes: vector<u8>): vector<u8>
pragma opaque;
aborts_if !features::spec_is_enabled(features::SHA_512_AND_RIPEMD_160_NATIVES);
ensures result == spec_sha2_512_internal(bytes);

sha3_512

public fun sha3_512(bytes: vector<u8>): vector<u8>
pragma opaque;
aborts_if !features::spec_is_enabled(features::SHA_512_AND_RIPEMD_160_NATIVES);
ensures result == spec_sha3_512_internal(bytes);

ripemd160

public fun ripemd160(bytes: vector<u8>): vector<u8>
pragma opaque;
aborts_if !features::spec_is_enabled(features::SHA_512_AND_RIPEMD_160_NATIVES);
ensures result == spec_ripemd160_internal(bytes);

blake2b_256

public fun blake2b_256(bytes: vector<u8>): vector<u8>
pragma opaque;
aborts_if !features::spec_is_enabled(features::BLAKE2B_256_NATIVE);
ensures result == spec_blake2b_256_internal(bytes);

sha2_512_internal

fun sha2_512_internal(bytes: vector<u8>): vector<u8>
pragma opaque;
aborts_if [abstract] false;
ensures [abstract] result == spec_sha2_512_internal(bytes);

sha3_512_internal

fun sha3_512_internal(bytes: vector<u8>): vector<u8>
pragma opaque;
aborts_if [abstract] false;
ensures [abstract] result == spec_sha3_512_internal(bytes);

ripemd160_internal

fun ripemd160_internal(bytes: vector<u8>): vector<u8>
pragma opaque;
aborts_if [abstract] false;
ensures [abstract] result == spec_ripemd160_internal(bytes);

blake2b_256_internal

fun blake2b_256_internal(bytes: vector<u8>): vector<u8>
pragma opaque;
aborts_if false;
ensures result == spec_blake2b_256_internal(bytes);