forked from paritytech/substrate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hashing proc macro utils (paritytech#9875)
* hashing macro * fmt * use in easy place, and fix blake sizes * fix * Fixes, docs. Allow ident as input. * fix doc tests * update error in test (nmapkey and key are same type). * hashing crates under sp_core * Doc updates and format. * use all existing hashing functions. * return array of u8 * Update primitives/core/hashing/proc-macro/src/impls.rs Co-authored-by: Bastian Köcher <[email protected]> * ToTokeen for an array of u8 * fix * re * Improve impls * complete doc tests * fmt * fix doctest format * fix ui test (nmap key type alias) Co-authored-by: Bastian Köcher <[email protected]> Co-authored-by: Bastian Köcher <[email protected]>
- Loading branch information
1 parent
0210ea0
commit 0465b0b
Showing
14 changed files
with
553 additions
and
149 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,31 @@ | ||
[package] | ||
name = "sp-core-hashing" | ||
version = "4.0.0-dev" | ||
authors = ["Parity Technologies <[email protected]>"] | ||
edition = "2018" | ||
license = "Apache-2.0" | ||
homepage = "https://substrate.dev" | ||
repository = "https://github.com/paritytech/substrate/" | ||
description = "Primitive core crate hashing implementation." | ||
documentation = "https://docs.rs/sp-core-hashing" | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] | ||
|
||
[dependencies] | ||
sp-std = { version = "4.0.0-dev", default-features = false, path = "../../std" } | ||
byteorder = { version = "1.3.2", default-features = false } | ||
|
||
blake2-rfc = { version = "0.2.18", default-features = false } | ||
tiny-keccak = { version = "2.0.1", features = ["keccak"] } | ||
sha2 = { version = "0.9.2", default-features = false } | ||
twox-hash = { version = "1.5.0", default-features = false } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"blake2-rfc/std", | ||
"sha2/std", | ||
"sp-std/std", | ||
"twox-hash/std", | ||
] |
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,22 @@ | ||
[package] | ||
name = "sp-core-hashing-proc-macro" | ||
version = "4.0.0-dev" | ||
authors = ["Parity Technologies <[email protected]>"] | ||
edition = "2018" | ||
license = "Apache-2.0" | ||
homepage = "https://substrate.dev" | ||
repository = "https://github.com/paritytech/substrate/" | ||
description = "This crate provides procedural macros for calculating static hash." | ||
documentation = "https://docs.rs/sp-core-hashing-proc-macro" | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
syn = { version = "1.0.77", features = ["full", "parsing"] } | ||
quote = "1.0.6" | ||
proc-macro2 = "1.0.29" | ||
sp-core-hashing = { version = "4.0.0-dev", path = "../", default-features = false } |
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,124 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2021 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use quote::quote; | ||
use syn::parse::{Parse, ParseStream}; | ||
|
||
use proc_macro::TokenStream; | ||
|
||
pub(super) struct InputBytes(pub Vec<u8>); | ||
|
||
pub(super) struct MultipleInputBytes(pub Vec<Vec<u8>>); | ||
|
||
impl MultipleInputBytes { | ||
pub(super) fn concatenated(mut self) -> Vec<u8> { | ||
if self.0.len() == 0 { | ||
Vec::new() | ||
} else { | ||
let mut result = core::mem::take(&mut self.0[0]); | ||
for other in self.0[1..].iter_mut() { | ||
result.append(other); | ||
} | ||
result | ||
} | ||
} | ||
} | ||
|
||
impl Parse for InputBytes { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
match syn::ExprArray::parse(input) { | ||
Ok(array) => { | ||
let mut bytes = Vec::<u8>::new(); | ||
for expr in array.elems.iter() { | ||
match expr { | ||
syn::Expr::Lit(lit) => match &lit.lit { | ||
syn::Lit::Int(b) => bytes.push(b.base10_parse()?), | ||
syn::Lit::Byte(b) => bytes.push(b.value()), | ||
_ => | ||
return Err(syn::Error::new( | ||
input.span(), | ||
"Expected array of u8 elements.".to_string(), | ||
)), | ||
}, | ||
_ => | ||
return Err(syn::Error::new( | ||
input.span(), | ||
"Expected array of u8 elements.".to_string(), | ||
)), | ||
} | ||
} | ||
return Ok(InputBytes(bytes)) | ||
}, | ||
Err(_e) => (), | ||
} | ||
// use rust names as a vec of their utf8 bytecode. | ||
match syn::Ident::parse(input) { | ||
Ok(ident) => return Ok(InputBytes(ident.to_string().as_bytes().to_vec())), | ||
Err(_e) => (), | ||
} | ||
Ok(InputBytes(syn::LitByteStr::parse(input)?.value())) | ||
} | ||
} | ||
|
||
impl Parse for MultipleInputBytes { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
let elts = | ||
syn::punctuated::Punctuated::<InputBytes, syn::token::Comma>::parse_terminated(input)?; | ||
Ok(MultipleInputBytes(elts.into_iter().map(|elt| elt.0).collect())) | ||
} | ||
} | ||
|
||
pub(super) fn twox_64(bytes: Vec<u8>) -> TokenStream { | ||
bytes_to_array(sp_core_hashing::twox_64(bytes.as_slice())) | ||
} | ||
|
||
pub(super) fn twox_128(bytes: Vec<u8>) -> TokenStream { | ||
bytes_to_array(sp_core_hashing::twox_128(bytes.as_slice())) | ||
} | ||
|
||
pub(super) fn blake2b_512(bytes: Vec<u8>) -> TokenStream { | ||
bytes_to_array(sp_core_hashing::blake2_512(bytes.as_slice())) | ||
} | ||
|
||
pub(super) fn blake2b_256(bytes: Vec<u8>) -> TokenStream { | ||
bytes_to_array(sp_core_hashing::blake2_256(bytes.as_slice())) | ||
} | ||
|
||
pub(super) fn blake2b_64(bytes: Vec<u8>) -> TokenStream { | ||
bytes_to_array(sp_core_hashing::blake2_64(bytes.as_slice())) | ||
} | ||
|
||
pub(super) fn keccak_256(bytes: Vec<u8>) -> TokenStream { | ||
bytes_to_array(sp_core_hashing::keccak_256(bytes.as_slice())) | ||
} | ||
|
||
pub(super) fn keccak_512(bytes: Vec<u8>) -> TokenStream { | ||
bytes_to_array(sp_core_hashing::keccak_512(bytes.as_slice())) | ||
} | ||
|
||
pub(super) fn sha2_256(bytes: Vec<u8>) -> TokenStream { | ||
bytes_to_array(sp_core_hashing::sha2_256(bytes.as_slice())) | ||
} | ||
|
||
fn bytes_to_array(bytes: impl IntoIterator<Item = u8>) -> TokenStream { | ||
let bytes = bytes.into_iter(); | ||
|
||
quote!( | ||
[ #( #bytes ),* ] | ||
) | ||
.into() | ||
} |
Oops, something went wrong.