-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adds support for host functions to ics23 #90
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,18 @@ members = ["codegen"] | |
prost = { version = "0.10", default-features = false, features = ["prost-derive"] } | ||
bytes = { version = "1.0.1", default-features = false } | ||
hex = { version = "0.4.3", default-features = false, features = [ "alloc" ] } | ||
sha2 = { version = "0.9.3", default-features = false } | ||
sha3 = { version = "0.9.1", default-features = false } | ||
ripemd160 = { version = "0.9.1", default-features = false } | ||
anyhow = { version = "1.0.40", default-features = false } | ||
sp-std = {version = "3.0.0", default-features = false } | ||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22", default-features = false } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really don't like tagging to branches here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unfortunately, this won't work. Substrate doesn't publish new releases to crates.io so parachain teams need to track git branches for new releases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do they do that? do you know? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from the versioning (currently we're at 0.9.24) I'd say they don't want to publish until they hit v1.0.0, which would be considered stable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ethanfrey will you be able to tag a version & publish on crates.io with the dependency specified as git/branch? |
||
sha2 = { version = "0.9.3", optional = true } | ||
sha3 = { version = "0.9.1", optional = true } | ||
ripemd160 = { version = "0.9.1", optional = true } | ||
sp-core = { version = "6.0.0", optional = true } | ||
|
||
[dev-dependencies] | ||
sha2 = { version = "0.9.3" } | ||
sha3 = { version = "0.9.1" } | ||
ripemd160 = { version = "0.9.1" } | ||
sp-core = { version = "6.0.0" } | ||
serde = { version = "1.0.125", features = ["derive"] } | ||
serde_json = "1.0.64" | ||
|
||
|
@@ -32,9 +37,10 @@ std = [ | |
"prost/std", | ||
"bytes/std", | ||
"hex/std", | ||
"sha2/std", | ||
"sha3/std", | ||
"ripemd160/std", | ||
"anyhow/std", | ||
"sp-std/std", | ||
"sha2", | ||
"sha3", | ||
"ripemd160", | ||
"sp-core", | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pub type Result<T> = anyhow::Result<T>; | ||
pub type Hash = std::vec::Vec<u8>; | ||
pub type Hash = sp_std::vec::Vec<u8>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/// If this is to be executed in a blockchain context, then we need to delegate these hashing | ||
/// functions to a native implementation through host function calls. | ||
/// This trait provides that interface. | ||
pub trait HostFunctionsProvider { | ||
/// The SHA-256 hash algorithm | ||
fn sha2_256(message: &[u8]) -> [u8; 32]; | ||
|
||
/// The SHA-512 hash algorithm | ||
fn sha2_512(message: &[u8]) -> [u8; 64]; | ||
|
||
/// The SHA-512 hash algorithm with its output truncated to 256 bits. | ||
fn sha2_512_truncated(message: &[u8]) -> [u8; 32]; | ||
|
||
/// SHA-3-512 hash function. | ||
fn sha3_512(message: &[u8]) -> [u8; 64]; | ||
|
||
/// Ripemd160 hash function. | ||
fn ripemd160(message: &[u8]) -> [u8; 20]; | ||
} | ||
|
||
#[cfg(any(feature = "std", test))] | ||
pub mod host_functions_impl { | ||
use crate::host_functions::HostFunctionsProvider; | ||
use ripemd160::Ripemd160; | ||
use sha2::{Digest, Sha512, Sha512Trunc256}; | ||
use sha3::Sha3_512; | ||
|
||
pub struct HostFunctionsManager; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, it makes sense to move this. However, substrate is not the only production user and this code will be needed outside of tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its available in std now |
||
impl HostFunctionsProvider for HostFunctionsManager { | ||
fn sha2_256(message: &[u8]) -> [u8; 32] { | ||
sp_core::hashing::sha2_256(message) | ||
} | ||
|
||
fn sha2_512(message: &[u8]) -> [u8; 64] { | ||
let digest = Sha512::digest(message); | ||
let mut buf = [0u8; 64]; | ||
buf.copy_from_slice(&digest); | ||
buf | ||
} | ||
|
||
fn sha2_512_truncated(message: &[u8]) -> [u8; 32] { | ||
let digest = Sha512Trunc256::digest(message); | ||
let mut buf = [0u8; 32]; | ||
buf.copy_from_slice(&digest); | ||
buf | ||
} | ||
|
||
fn sha3_512(message: &[u8]) -> [u8; 64] { | ||
let digest = Sha3_512::digest(message); | ||
let mut buf = [0u8; 64]; | ||
buf.copy_from_slice(&digest); | ||
buf | ||
} | ||
|
||
fn ripemd160(message: &[u8]) -> [u8; 20] { | ||
let digest = Ripemd160::digest(message); | ||
let mut buf = [0u8; 20]; | ||
buf.copy_from_slice(&digest); | ||
buf | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks