Skip to content

Commit

Permalink
[asset-mapping] split starcoin_proof.move to more files
Browse files Browse the repository at this point in the history
  • Loading branch information
welbon committed Jan 6, 2025
1 parent fc5c593 commit c71ae37
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 162 deletions.
4 changes: 2 additions & 2 deletions vm/framework/starcoin-framework/doc/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ This is the reference documentation of the Starcoin framework.
- [`0x1::ring`](ring.md#0x1_ring)
- [`0x1::starcoin_account`](starcoin_account.md#0x1_starcoin_account)
- [`0x1::starcoin_coin`](starcoin_coin.md#0x1_starcoin_coin)
- [`0x1::starcoin_proof_bit`](starcoin_proof.md#0x1_starcoin_proof_bit)
- [`0x1::starcoin_proof_structured_hash`](starcoin_proof.md#0x1_starcoin_proof_structured_hash)
- [`0x1::starcoin_proof_bit`](starcoin_proof_bit.md#0x1_starcoin_proof_bit)
- [`0x1::starcoin_proof_structured_hash`](starcoin_proof_structured_hash.md#0x1_starcoin_proof_structured_hash)
- [`0x1::starcoin_proof_verifier`](starcoin_proof.md#0x1_starcoin_proof_verifier)
- [`0x1::stc_block`](stc_block.md#0x1_stc_block)
- [`0x1::stc_genesis`](stc_genesis.md#0x1_stc_genesis)
Expand Down
137 changes: 5 additions & 132 deletions vm/framework/starcoin-framework/doc/starcoin_proof.md
Original file line number Diff line number Diff line change
@@ -1,131 +1,4 @@

<a id="0x1_starcoin_proof_bit"></a>

# Module `0x1::starcoin_proof_bit`



- [Function `get_bit`](#0x1_starcoin_proof_bit_get_bit)


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



<a id="0x1_starcoin_proof_bit_get_bit"></a>

## Function `get_bit`



<pre><code><b>public</b> <b>fun</b> <a href="starcoin_proof.md#0x1_starcoin_proof_bit_get_bit">get_bit</a>(data: &<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, index: u64): bool
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="starcoin_proof.md#0x1_starcoin_proof_bit_get_bit">get_bit</a>(data: &<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, index: u64): bool {
<b>let</b> pos = index / 8;
<b>let</b> bit = (7 - index % 8);
(*<a href="../../move-stdlib/doc/vector.md#0x1_vector_borrow">vector::borrow</a>(data, pos) &gt;&gt; (bit <b>as</b> u8)) & 1u8 != 0
}
</code></pre>



</details>



<a id="0x1_starcoin_proof_structured_hash"></a>

# Module `0x1::starcoin_proof_structured_hash`



- [Constants](#@Constants_0)
- [Function `hash`](#0x1_starcoin_proof_structured_hash_hash)
- [Function `concat`](#0x1_starcoin_proof_structured_hash_concat)


<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/hash.md#0x1_hash">0x1::hash</a>;
<b>use</b> <a href="../../move-stdlib/doc/vector.md#0x1_vector">0x1::vector</a>;
</code></pre>



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

## Constants


<a id="0x1_starcoin_proof_structured_hash_STARCOIN_HASH_PREFIX"></a>



<pre><code><b>const</b> <a href="starcoin_proof.md#0x1_starcoin_proof_structured_hash_STARCOIN_HASH_PREFIX">STARCOIN_HASH_PREFIX</a>: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt; = [83, 84, 65, 82, 67, 79, 73, 78, 58, 58];
</code></pre>



<a id="0x1_starcoin_proof_structured_hash_hash"></a>

## Function `hash`



<pre><code><b>public</b> <b>fun</b> <a href="../../move-stdlib/doc/hash.md#0x1_hash">hash</a>&lt;MoveValue: store&gt;(structure: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, data: &MoveValue): <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="../../move-stdlib/doc/hash.md#0x1_hash">hash</a>&lt;MoveValue: store&gt;(structure: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, data: &MoveValue): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt; {
<b>let</b> prefix_hash = <a href="../../move-stdlib/doc/hash.md#0x1_hash_sha3_256">hash::sha3_256</a>(<a href="starcoin_proof.md#0x1_starcoin_proof_structured_hash_concat">concat</a>(&<a href="starcoin_proof.md#0x1_starcoin_proof_structured_hash_STARCOIN_HASH_PREFIX">STARCOIN_HASH_PREFIX</a>, structure));
<b>let</b> bcs_bytes = <a href="../../move-stdlib/doc/bcs.md#0x1_bcs_to_bytes">bcs::to_bytes</a>(data);
<a href="../../move-stdlib/doc/hash.md#0x1_hash_sha3_256">hash::sha3_256</a>(<a href="starcoin_proof.md#0x1_starcoin_proof_structured_hash_concat">concat</a>(&prefix_hash, bcs_bytes))
}
</code></pre>



</details>

<a id="0x1_starcoin_proof_structured_hash_concat"></a>

## Function `concat`



<pre><code><b>fun</b> <a href="starcoin_proof.md#0x1_starcoin_proof_structured_hash_concat">concat</a>(v1: &<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, v2: <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>fun</b> <a href="starcoin_proof.md#0x1_starcoin_proof_structured_hash_concat">concat</a>(v1: &<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, v2: <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>let</b> data = *v1;
<a href="../../move-stdlib/doc/vector.md#0x1_vector_append">vector::append</a>(&<b>mut</b> data, v2);
data
}
</code></pre>



</details>



<a id="0x1_starcoin_proof_verifier"></a>

# Module `0x1::starcoin_proof_verifier`
Expand All @@ -142,8 +15,8 @@


<pre><code><b>use</b> <a href="../../move-stdlib/doc/hash.md#0x1_hash">0x1::hash</a>;
<b>use</b> <a href="starcoin_proof.md#0x1_starcoin_proof_bit">0x1::starcoin_proof_bit</a>;
<b>use</b> <a href="starcoin_proof.md#0x1_starcoin_proof_structured_hash">0x1::starcoin_proof_structured_hash</a>;
<b>use</b> <a href="starcoin_proof_bit.md#0x1_starcoin_proof_bit">0x1::starcoin_proof_bit</a>;
<b>use</b> <a href="starcoin_proof_structured_hash.md#0x1_starcoin_proof_structured_hash">0x1::starcoin_proof_structured_hash</a>;
</code></pre>


Expand Down Expand Up @@ -348,18 +221,18 @@
proofs: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;&gt;
): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt; {
<b>let</b> leaf_node = <a href="starcoin_proof.md#0x1_starcoin_proof_verifier_Node">Node</a> { hash1: element_key, hash2: element_blob_hash };
<b>let</b> current_hash = <a href="starcoin_proof.md#0x1_starcoin_proof_structured_hash_hash">starcoin_proof_structured_hash::hash</a>(<a href="starcoin_proof.md#0x1_starcoin_proof_verifier_SPARSE_MERKLE_LEAF_NODE">SPARSE_MERKLE_LEAF_NODE</a>, &leaf_node);
<b>let</b> current_hash = <a href="starcoin_proof_structured_hash.md#0x1_starcoin_proof_structured_hash_hash">starcoin_proof_structured_hash::hash</a>(<a href="starcoin_proof.md#0x1_starcoin_proof_verifier_SPARSE_MERKLE_LEAF_NODE">SPARSE_MERKLE_LEAF_NODE</a>, &leaf_node);
<b>let</b> i = 0;
<b>let</b> proof_length = <a href="../../move-stdlib/doc/vector.md#0x1_vector_length">vector::length</a>(&proofs);
<b>while</b> (i &lt; proof_length) {
<b>let</b> sibling = *<a href="../../move-stdlib/doc/vector.md#0x1_vector_borrow">vector::borrow</a>(&proofs, i);
<b>let</b> bit = <a href="starcoin_proof.md#0x1_starcoin_proof_bit_get_bit">starcoin_proof_bit::get_bit</a>(&element_key, proof_length - i - 1);
<b>let</b> bit = <a href="starcoin_proof_bit.md#0x1_starcoin_proof_bit_get_bit">starcoin_proof_bit::get_bit</a>(&element_key, proof_length - i - 1);
<b>let</b> internal_node = <b>if</b> (bit) {
<a href="starcoin_proof.md#0x1_starcoin_proof_verifier_Node">Node</a> { hash1: sibling, hash2: current_hash }
} <b>else</b> {
<a href="starcoin_proof.md#0x1_starcoin_proof_verifier_Node">Node</a> { hash1: current_hash, hash2: sibling }
};
current_hash = <a href="starcoin_proof.md#0x1_starcoin_proof_structured_hash_hash">starcoin_proof_structured_hash::hash</a>(<a href="starcoin_proof.md#0x1_starcoin_proof_verifier_SPARSE_MERKLE_INTERNAL_NODE">SPARSE_MERKLE_INTERNAL_NODE</a>, &internal_node);
current_hash = <a href="starcoin_proof_structured_hash.md#0x1_starcoin_proof_structured_hash_hash">starcoin_proof_structured_hash::hash</a>(<a href="starcoin_proof.md#0x1_starcoin_proof_verifier_SPARSE_MERKLE_INTERNAL_NODE">SPARSE_MERKLE_INTERNAL_NODE</a>, &internal_node);
i = i + 1;
};
current_hash
Expand Down
42 changes: 42 additions & 0 deletions vm/framework/starcoin-framework/doc/starcoin_proof_bit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

<a id="0x1_starcoin_proof_bit"></a>

# Module `0x1::starcoin_proof_bit`



- [Function `get_bit`](#0x1_starcoin_proof_bit_get_bit)


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



<a id="0x1_starcoin_proof_bit_get_bit"></a>

## Function `get_bit`



<pre><code><b>public</b> <b>fun</b> <a href="starcoin_proof_bit.md#0x1_starcoin_proof_bit_get_bit">get_bit</a>(data: &<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, index: u64): bool
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="starcoin_proof_bit.md#0x1_starcoin_proof_bit_get_bit">get_bit</a>(data: &<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, index: u64): bool {
<b>let</b> pos = index / 8;
<b>let</b> bit = (7 - index % 8);
(*<a href="../../move-stdlib/doc/vector.md#0x1_vector_borrow">vector::borrow</a>(data, pos) &gt;&gt; (bit <b>as</b> u8)) & 1u8 != 0
}
</code></pre>



</details>


[move-book]: https://starcoin.dev/move/book/SUMMARY
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

<a id="0x1_starcoin_proof_structured_hash"></a>

# Module `0x1::starcoin_proof_structured_hash`



- [Constants](#@Constants_0)
- [Function `hash`](#0x1_starcoin_proof_structured_hash_hash)
- [Function `concat`](#0x1_starcoin_proof_structured_hash_concat)


<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/hash.md#0x1_hash">0x1::hash</a>;
<b>use</b> <a href="../../move-stdlib/doc/vector.md#0x1_vector">0x1::vector</a>;
</code></pre>



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

## Constants


<a id="0x1_starcoin_proof_structured_hash_STARCOIN_HASH_PREFIX"></a>



<pre><code><b>const</b> <a href="starcoin_proof_structured_hash.md#0x1_starcoin_proof_structured_hash_STARCOIN_HASH_PREFIX">STARCOIN_HASH_PREFIX</a>: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt; = [83, 84, 65, 82, 67, 79, 73, 78, 58, 58];
</code></pre>



<a id="0x1_starcoin_proof_structured_hash_hash"></a>

## Function `hash`



<pre><code><b>public</b> <b>fun</b> <a href="../../move-stdlib/doc/hash.md#0x1_hash">hash</a>&lt;MoveValue: store&gt;(structure: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, data: &MoveValue): <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="../../move-stdlib/doc/hash.md#0x1_hash">hash</a>&lt;MoveValue: store&gt;(structure: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, data: &MoveValue): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt; {
<b>let</b> prefix_hash = <a href="../../move-stdlib/doc/hash.md#0x1_hash_sha3_256">hash::sha3_256</a>(<a href="starcoin_proof_structured_hash.md#0x1_starcoin_proof_structured_hash_concat">concat</a>(&<a href="starcoin_proof_structured_hash.md#0x1_starcoin_proof_structured_hash_STARCOIN_HASH_PREFIX">STARCOIN_HASH_PREFIX</a>, structure));
<b>let</b> bcs_bytes = <a href="../../move-stdlib/doc/bcs.md#0x1_bcs_to_bytes">bcs::to_bytes</a>(data);
<a href="../../move-stdlib/doc/hash.md#0x1_hash_sha3_256">hash::sha3_256</a>(<a href="starcoin_proof_structured_hash.md#0x1_starcoin_proof_structured_hash_concat">concat</a>(&prefix_hash, bcs_bytes))
}
</code></pre>



</details>

<a id="0x1_starcoin_proof_structured_hash_concat"></a>

## Function `concat`



<pre><code><b>fun</b> <a href="starcoin_proof_structured_hash.md#0x1_starcoin_proof_structured_hash_concat">concat</a>(v1: &<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, v2: <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>fun</b> <a href="starcoin_proof_structured_hash.md#0x1_starcoin_proof_structured_hash_concat">concat</a>(v1: &<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, v2: <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>let</b> data = *v1;
<a href="../../move-stdlib/doc/vector.md#0x1_vector_append">vector::append</a>(&<b>mut</b> data, v2);
data
}
</code></pre>



</details>


[move-book]: https://starcoin.dev/move/book/SUMMARY
Original file line number Diff line number Diff line change
Expand Up @@ -91,32 +91,4 @@ module starcoin_framework::starcoin_proof_verifier {
}
}

module starcoin_framework::starcoin_proof_structured_hash {
use std::bcs;
use std::hash;
use std::vector;

const STARCOIN_HASH_PREFIX: vector<u8> = b"STARCOIN::";

public fun hash<MoveValue: store>(structure: vector<u8>, data: &MoveValue): vector<u8> {
let prefix_hash = hash::sha3_256(concat(&STARCOIN_HASH_PREFIX, structure));
let bcs_bytes = bcs::to_bytes(data);
hash::sha3_256(concat(&prefix_hash, bcs_bytes))
}

fun concat(v1: &vector<u8>, v2: vector<u8>): vector<u8> {
let data = *v1;
vector::append(&mut data, v2);
data
}
}

module starcoin_framework::starcoin_proof_bit {
use std::vector;

public fun get_bit(data: &vector<u8>, index: u64): bool {
let pos = index / 8;
let bit = (7 - index % 8);
(*vector::borrow(data, pos) >> (bit as u8)) & 1u8 != 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module starcoin_framework::starcoin_proof_bit {
use std::vector;

public fun get_bit(data: &vector<u8>, index: u64): bool {
let pos = index / 8;
let bit = (7 - index % 8);
(*vector::borrow(data, pos) >> (bit as u8)) & 1u8 != 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module starcoin_framework::starcoin_proof_structured_hash {
use std::bcs;
use std::hash;
use std::vector;

const STARCOIN_HASH_PREFIX: vector<u8> = b"STARCOIN::";

public fun hash<MoveValue: store>(structure: vector<u8>, data: &MoveValue): vector<u8> {
let prefix_hash = hash::sha3_256(concat(&STARCOIN_HASH_PREFIX, structure));
let bcs_bytes = bcs::to_bytes(data);
hash::sha3_256(concat(&prefix_hash, bcs_bytes))
}

fun concat(v1: &vector<u8>, v2: vector<u8>): vector<u8> {
let data = *v1;
vector::append(&mut data, v2);
data
}
}

0 comments on commit c71ae37

Please sign in to comment.