Skip to content

Commit

Permalink
[crypto+move] added feature-gating
Browse files Browse the repository at this point in the history
  • Loading branch information
alinush committed Oct 28, 2022
1 parent 4026229 commit fa58ea7
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 24 deletions.
2 changes: 1 addition & 1 deletion aptos-move/aptos-gas/src/gas_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use std::collections::BTreeMap;
// global operations.
// - V1
// - TBA
pub const LATEST_GAS_FEATURE_VERSION: u64 = 3;
pub const LATEST_GAS_FEATURE_VERSION: u64 = 4;

pub(crate) const EXECUTION_GAS_MULTIPLIER: u64 = 20;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ module std::features {
is_enabled(TREAT_FRIEND_AS_PRIVATE)
}

/// Whether the new SHA2-512, SHA3-512 and RIPEMD-160 hash function natives are enabled.
/// This is needed because of the introduction of new native functions.
/// Lifetime: transient
const SHA_512_AND_RIPEMD_160_NATIVES: u64 = 3;

public fun get_sha_512_and_ripemd_160_feature(): u64 { SHA_512_AND_RIPEMD_160_NATIVES }

public fun sha_512_and_ripemd_160_enabled(): bool acquires Features {
is_enabled(SHA_512_AND_RIPEMD_160_NATIVES)
}

// ============================================================================================
// Feature Flag Implementation

Expand Down
269 changes: 263 additions & 6 deletions aptos-move/framework/aptos-stdlib/doc/hash.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,43 @@ 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


- [Constants](#@Constants_0)
- [Function `sip_hash`](#0x1_aptos_hash_sip_hash)
- [Function `sip_hash_from_value`](#0x1_aptos_hash_sip_hash_from_value)
- [Function `keccak256`](#0x1_aptos_hash_keccak256)
- [Specification](#@Specification_0)
- [Function `sip_hash`](#@Specification_0_sip_hash)
- [Function `sip_hash_from_value`](#@Specification_0_sip_hash_from_value)
- [Function `sha2_512`](#0x1_aptos_hash_sha2_512)
- [Function `sha3_512`](#0x1_aptos_hash_sha3_512)
- [Function `ripemd160`](#0x1_aptos_hash_ripemd160)
- [Function `sha2_512_internal`](#0x1_aptos_hash_sha2_512_internal)
- [Function `sha3_512_internal`](#0x1_aptos_hash_sha3_512_internal)
- [Function `ripemd160_internal`](#0x1_aptos_hash_ripemd160_internal)
- [Specification](#@Specification_1)
- [Function `sip_hash`](#@Specification_1_sip_hash)
- [Function `sip_hash_from_value`](#@Specification_1_sip_hash_from_value)
- [Function `keccak256`](#@Specification_1_keccak256)
- [Function `sha2_512`](#@Specification_1_sha2_512)
- [Function `sha3_512`](#@Specification_1_sha3_512)
- [Function `ripemd160`](#@Specification_1_ripemd160)


<pre><code><b>use</b> <a href="../../move-stdlib/doc/bcs.md#0x1_bcs">0x1::bcs</a>;
<b>use</b> <a href="../../move-stdlib/doc/error.md#0x1_error">0x1::error</a>;
<b>use</b> <a href="../../move-stdlib/doc/features.md#0x1_features">0x1::features</a>;
</code></pre>



<a name="@Constants_0"></a>

## Constants


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

A newly-added native function is not yet enabled.


<pre><code><b>const</b> <a href="hash.md#0x1_aptos_hash_E_NATIVE_FUN_NOT_AVAILABLE">E_NATIVE_FUN_NOT_AVAILABLE</a>: u64 = 1;
</code></pre>


Expand All @@ -30,6 +58,7 @@ Non-cryptograhic hashes:

## Function `sip_hash`

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


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_sip_hash">sip_hash</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): u64
Expand All @@ -52,6 +81,7 @@ Non-cryptograhic hashes:

## Function `sip_hash_from_value`

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


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_sip_hash_from_value">sip_hash_from_value</a>&lt;MoveValue&gt;(v: &MoveValue): u64
Expand All @@ -78,6 +108,7 @@ Non-cryptograhic hashes:

## Function `keccak256`

Returns the Keccak-256 hash of <code>bytes</code>.


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_keccak256">keccak256</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
Expand All @@ -96,12 +127,174 @@ Non-cryptograhic hashes:

</details>

<a name="@Specification_0"></a>
<a name="0x1_aptos_hash_sha2_512"></a>

## Function `sha2_512`

Returns the SHA2-512 hash of <code>bytes</code>.


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_sha2_512">sha2_512</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_sha2_512">sha2_512</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt; {
<b>if</b>(!<a href="../../move-stdlib/doc/features.md#0x1_features_sha_512_and_ripemd_160_enabled">features::sha_512_and_ripemd_160_enabled</a>()) {
<b>abort</b>(std::error::invalid_state(<a href="hash.md#0x1_aptos_hash_E_NATIVE_FUN_NOT_AVAILABLE">E_NATIVE_FUN_NOT_AVAILABLE</a>))
};

<a href="hash.md#0x1_aptos_hash_sha2_512_internal">sha2_512_internal</a>(bytes)
}
</code></pre>



</details>

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

## Function `sha3_512`

Returns the SHA3-512 hash of <code>bytes</code>.


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_sha3_512">sha3_512</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_sha3_512">sha3_512</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt; {
<b>if</b>(!<a href="../../move-stdlib/doc/features.md#0x1_features_sha_512_and_ripemd_160_enabled">features::sha_512_and_ripemd_160_enabled</a>()) {
<b>abort</b>(std::error::invalid_state(<a href="hash.md#0x1_aptos_hash_E_NATIVE_FUN_NOT_AVAILABLE">E_NATIVE_FUN_NOT_AVAILABLE</a>))
};

<a href="hash.md#0x1_aptos_hash_sha3_512_internal">sha3_512_internal</a>(bytes)
}
</code></pre>



</details>

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

## Function `ripemd160`

Returns the RIPEMD-160 hash of <code>bytes</code>.

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).


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_ripemd160">ripemd160</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_ripemd160">ripemd160</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt; {
<b>if</b>(!<a href="../../move-stdlib/doc/features.md#0x1_features_sha_512_and_ripemd_160_enabled">features::sha_512_and_ripemd_160_enabled</a>()) {
<b>abort</b>(std::error::invalid_state(<a href="hash.md#0x1_aptos_hash_E_NATIVE_FUN_NOT_AVAILABLE">E_NATIVE_FUN_NOT_AVAILABLE</a>))
};

<a href="hash.md#0x1_aptos_hash_ripemd160_internal">ripemd160_internal</a>(bytes)
}
</code></pre>



</details>

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

## Function `sha2_512_internal`

Returns the SHA2-512 hash of <code>bytes</code>.


<pre><code><b>fun</b> <a href="hash.md#0x1_aptos_hash_sha2_512_internal">sha2_512_internal</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>native</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_sha2_512_internal">sha2_512_internal</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;;
</code></pre>



</details>

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

## Function `sha3_512_internal`

Returns the SHA3-512 hash of <code>bytes</code>.


<pre><code><b>fun</b> <a href="hash.md#0x1_aptos_hash_sha3_512_internal">sha3_512_internal</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>native</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_sha3_512_internal">sha3_512_internal</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;;
</code></pre>



</details>

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

## Function `ripemd160_internal`

Returns the RIPEMD-160 hash of <code>bytes</code>.

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).


<pre><code><b>fun</b> <a href="hash.md#0x1_aptos_hash_ripemd160_internal">ripemd160_internal</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>native</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_ripemd160_internal">ripemd160_internal</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;;
</code></pre>



</details>

<a name="@Specification_1"></a>

## Specification


<a name="@Specification_0_sip_hash"></a>
<a name="@Specification_1_sip_hash"></a>

### Function `sip_hash`

Expand All @@ -117,7 +310,7 @@ Non-cryptograhic hashes:



<a name="@Specification_0_sip_hash_from_value"></a>
<a name="@Specification_1_sip_hash_from_value"></a>

### Function `sip_hash_from_value`

Expand All @@ -128,6 +321,70 @@ Non-cryptograhic hashes:



<pre><code><b>pragma</b> opaque;
</code></pre>



<a name="@Specification_1_keccak256"></a>

### Function `keccak256`


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_keccak256">keccak256</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>




<pre><code><b>pragma</b> opaque;
</code></pre>



<a name="@Specification_1_sha2_512"></a>

### Function `sha2_512`


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_sha2_512">sha2_512</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>




<pre><code><b>pragma</b> opaque;
</code></pre>



<a name="@Specification_1_sha3_512"></a>

### Function `sha3_512`


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_sha3_512">sha3_512</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>




<pre><code><b>pragma</b> opaque;
</code></pre>



<a name="@Specification_1_ripemd160"></a>

### Function `ripemd160`


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x1_aptos_hash_ripemd160">ripemd160</a>(bytes: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>




<pre><code><b>pragma</b> opaque;
</code></pre>

Expand Down
Loading

0 comments on commit fa58ea7

Please sign in to comment.