-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2003 from dusk-network/issue-1884
Issue 1884 - contract ID generation mechanism
- Loading branch information
Showing
12 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
// Copyright (c) DUSK NETWORK. All rights reserved. | ||
|
||
use crate::hash::Hasher; | ||
use rusk_abi::ContractId; | ||
|
||
/// Generate a [`ContractId`] address from: | ||
/// - slice of bytes, | ||
/// - nonce | ||
/// - owner | ||
pub fn gen_contract_id( | ||
bytes: impl AsRef<[u8]>, | ||
nonce: u64, | ||
owner: impl AsRef<[u8]>, | ||
) -> ContractId { | ||
let mut hasher = Hasher::new(); | ||
hasher.update(bytes.as_ref()); | ||
hasher.update(nonce.to_le_bytes()); | ||
hasher.update(owner.as_ref()); | ||
let hash_bytes = hasher.finalize(); | ||
ContractId::from_bytes(hash_bytes) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use rand::rngs::StdRng; | ||
use rand::{RngCore, SeedableRng}; | ||
|
||
#[test] | ||
fn test_gen_contract_id() { | ||
let mut rng = StdRng::seed_from_u64(42); | ||
|
||
let mut bytes = vec![0; 1000]; | ||
rng.fill_bytes(&mut bytes); | ||
|
||
let nonce = rng.next_u64(); | ||
|
||
let mut owner = vec![0, 100]; | ||
rng.fill_bytes(&mut owner); | ||
|
||
let contract_id = | ||
gen_contract_id(bytes.as_slice(), nonce, owner.as_slice()); | ||
|
||
assert_eq!( | ||
hex::encode(contract_id.as_bytes()), | ||
"a138d3b9c87235dac6f62d1d30b75cffbb94601d9cbe5bd540b3e1e5842c8a7d" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
// Copyright (c) DUSK NETWORK. All rights reserved. | ||
|
||
use blake2b_simd::{Params, State}; | ||
|
||
/// Hashes scalars and arbitrary slices of bytes using Blake2b, returning an | ||
/// array of 32 bytes. | ||
/// | ||
/// This hash cannot be proven inside a circuit, if that is desired, use | ||
/// `poseidon_hash` instead. | ||
pub struct Hasher { | ||
state: State, | ||
} | ||
|
||
impl Default for Hasher { | ||
fn default() -> Self { | ||
Hasher { | ||
state: Params::new().hash_length(64).to_state(), | ||
} | ||
} | ||
} | ||
|
||
impl Hasher { | ||
/// Create new hasher instance. | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
/// Process data, updating the internal state. | ||
pub fn update(&mut self, data: impl AsRef<[u8]>) { | ||
self.state.update(data.as_ref()); | ||
} | ||
|
||
/// Retrieve result and consume hasher instance. | ||
pub fn finalize(self) -> [u8; 32] { | ||
let hash = self.state.finalize(); | ||
let mut a = [0u8; 32]; | ||
a.clone_from_slice(&hash.as_array()[..32]); | ||
a | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters