Module 0x1::aptos_hash
Cryptographic hashes:
- Keccak-256: see https://keccak.team/keccak.html
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:
-
SipHash: an add-rotate-xor (ARX) based family of pseudorandom functions created by Jean-Philippe Aumasson and Daniel J. Bernstein in 2012
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;
Function sip_hash
Returns the (non-cryptographic) SipHash of bytes. See https://en.wikipedia.org/wiki/SipHash
public fun sip_hash(bytes: vector<u8>): u64
Function 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)
}
Function keccak256
Returns the Keccak-256 hash of bytes.
public fun keccak256(bytes: vector<u8>): vector<u8>
Function 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)
}
Function sha2_512_value
public fun sha2_512_value<T>(val: &T): vector<u8>
Implementation
public fun sha2_512_value<T>(val: &T): vector<u8> {
let bytes = bcs::to_bytes(val);
sha2_512(bytes)
}
Function 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)
}
Function 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)
}
Function 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)
}
Function 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>;
Function 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>;
Function 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>;
Function 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);
Function sip_hash
public fun sip_hash(bytes: vector<u8>): u64
pragma opaque;
aborts_if [abstract] false;
ensures [abstract] result == spec_sip_hash(bytes);
Function sip_hash_from_value
public fun sip_hash_from_value<MoveValue>(v: &MoveValue): u64
pragma opaque;
ensures result == spec_sip_hash(bcs::serialize(v));
Function keccak256
public fun keccak256(bytes: vector<u8>): vector<u8>
pragma opaque;
aborts_if [abstract] false;
ensures [abstract] result == spec_keccak256(bytes);
Function 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);
Function 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);
Function 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);
Function 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);
Function 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);
Function 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);
Function ripemd160_internal
fun ripemd160_internal(bytes: vector<u8>): vector<u8>
pragma opaque;
aborts_if [abstract] false;
ensures [abstract] result == spec_ripemd160_internal(bytes);
Function blake2b_256_internal
fun blake2b_256_internal(bytes: vector<u8>): vector<u8>
pragma opaque;
aborts_if false;
ensures result == spec_blake2b_256_internal(bytes);