Skip to content
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

keyring: remove lazy_static public keys hash maps #2387

Merged
merged 38 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8ac1a1a
keyring: remove lazy_static hash maps
michalkucharczyk Nov 17, 2023
599045c
license fix
michalkucharczyk Nov 17, 2023
a76af76
no static cache
michalkucharczyk Nov 17, 2023
97bca05
missing change
michalkucharczyk Nov 17, 2023
e2121ca
sr25519+bandersnatch
michalkucharczyk Nov 20, 2023
8d2ea05
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Nov 20, 2023
88ae185
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Nov 21, 2023
25a0a89
public() method uses raw bytes
michalkucharczyk Nov 23, 2023
1a227a0
fix
michalkucharczyk Nov 24, 2023
981eade
Merge remote-tracking branch 'origin/master' into mku-remove-lazy-sta…
michalkucharczyk Nov 24, 2023
ebd6260
const fn hex2array util added
michalkucharczyk Nov 24, 2023
7d6d327
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Nov 24, 2023
8d94b6b
hex2arr moved to sp_core
michalkucharczyk Nov 24, 2023
192a021
const keys added
michalkucharczyk Nov 25, 2023
84fccb0
one more try
michalkucharczyk Nov 25, 2023
006452b
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Nov 25, 2023
9b90651
debugs removed
michalkucharczyk Nov 25, 2023
b1a0f00
".git/.scripts/commands/fmt/fmt.sh"
Nov 25, 2023
ae69b21
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Nov 27, 2023
2e716dd
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Nov 28, 2023
bcaacc6
hex2array: more tests + better doc
michalkucharczyk Nov 28, 2023
bfd8f05
Apply suggestions from code review
michalkucharczyk Nov 28, 2023
1406b91
better test
michalkucharczyk Nov 28, 2023
ab13181
doc fixed
michalkucharczyk Nov 28, 2023
f01fe2f
test simplified
michalkucharczyk Nov 28, 2023
1372cef
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Dec 1, 2023
1649b92
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Dec 1, 2023
8aab10a
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Dec 6, 2023
96c3574
macro added
michalkucharczyk Dec 8, 2023
5697147
Merge remote-tracking branch 'origin/master' into mku-remove-lazy-sta…
michalkucharczyk Dec 8, 2023
62ba4f4
Apply suggestions from code review
michalkucharczyk Dec 8, 2023
b278916
fixes
michalkucharczyk Dec 8, 2023
0f7cccc
better naming
michalkucharczyk Dec 8, 2023
b725ac4
Apply suggestions from code review
michalkucharczyk Dec 8, 2023
b38e666
".git/.scripts/commands/update-ui/update-ui.sh"
Dec 8, 2023
ac8361f
Revert "".git/.scripts/commands/update-ui/update-ui.sh""
michalkucharczyk Dec 11, 2023
c8b2c2d
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Dec 11, 2023
8e0d00e
Merge branch 'master' into mku-remove-lazy-static-from-keyring
michalkucharczyk Dec 11, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

162 changes: 162 additions & 0 deletions substrate/primitives/core/src/const_hex2array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// This file is part of Substrate.

// Copyright (C) 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.

//! Provides a const function for converting a hex string to a `u8` array at compile time, when used
//! in the proper context.

/// Provides a const array from given string literal.
///
/// Valid characters are `[0-9a-fA-F]`, and the hex string should not start
/// with the `0x` prefix.
#[macro_export]
macro_rules! hex2array {
($input:expr) => {{
const BYTES: [u8; $input.len() >> 1] = $crate::const_hex2array::private_hex2array($input);
michalkucharczyk marked this conversation as resolved.
Show resolved Hide resolved
michalkucharczyk marked this conversation as resolved.
Show resolved Hide resolved
BYTES
}};
}

/// Generates array from (static) string literal.
///
/// Valid characters are `[0-9a-fA-F]`, and the hex string should not start
/// with the `0x` prefix.
///
/// # Panics
///
/// The function will panic at compile time when used in a const context if:
/// - The given hex string has an invalid length.
/// - It contains invalid characters.
///
/// The function will panic at runtime when used in a non-const context if the above conditions are
/// met.
#[doc(hidden)]
pub const fn private_hex2array<const N: usize>(hex: &str) -> [u8; N] {
const fn c2b(c: u8) -> u8 {
match c as char {
'0'..='9' => c - 48,
'a'..='f' => c - 87,
'A'..='F' => c - 55,
michalkucharczyk marked this conversation as resolved.
Show resolved Hide resolved
_ => panic!("hex string contains invalid character"),
}
}
let mut output = [0; N];
let mut i = 0;
if hex.len() != 2 * N {
panic!("hex string length is not valid");
}
while i < N {
output[i] = 16 * c2b(hex.as_bytes()[2 * i]) + c2b(hex.as_bytes()[2 * i + 1]);
i += 1;
}
output
}

#[cfg(test)]
mod testh2b {
use super::private_hex2array;

#[test]
fn t00() {
const T0: [u8; 0] = private_hex2array("");
const EMPTY: [u8; 0] = [];
assert_eq!(T0, EMPTY);
}

macro_rules! test_byte {
($a:expr, $b:expr) => {{
const X: [u8; 1] = private_hex2array($a);
assert_eq!(X, [$b]);
}};
}

#[test]
fn t01() {
test_byte!("00", 0);
test_byte!("01", 1);
test_byte!("02", 2);
test_byte!("03", 3);
test_byte!("04", 4);
test_byte!("05", 5);
test_byte!("06", 6);
test_byte!("07", 7);
test_byte!("08", 8);
test_byte!("09", 9);
test_byte!("0a", 10);
test_byte!("0A", 10);
test_byte!("0b", 11);
test_byte!("0B", 11);
test_byte!("0c", 12);
test_byte!("0C", 12);
test_byte!("0d", 13);
test_byte!("0D", 13);
test_byte!("0e", 14);
test_byte!("0E", 14);
test_byte!("0f", 15);
test_byte!("0F", 15);
}

#[test]
fn t02() {
const T0: [u8; 2] = private_hex2array("0a10");
assert_eq!(T0, [10, 16]);
const T1: [u8; 2] = private_hex2array("4545");
assert_eq!(T1, [69, 69]);
}

#[test]
fn t02m() {
assert_eq!(hex2array!("0a10"), [10, 16]);
assert_eq!(hex2array!("4545"), [69, 69]);
assert_eq!(
hex2array!("000102030405060708090a0b0c0d0e0f"),
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
);
}

#[test]
fn t16() {
const T16: [u8; 16] = private_hex2array("000102030405060708090a0b0c0d0e0f");

assert_eq!(T16, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
}

#[test]
fn t33() {
michalkucharczyk marked this conversation as resolved.
Show resolved Hide resolved
const T33: [u8; 33] =
private_hex2array("9c8af77d3a4e3f6f076853922985b9e6724fc9675329087f47aff1ceaaae772180");

assert_eq!(
T33,
[
156, 138, 247, 125, 58, 78, 63, 111, 7, 104, 83, 146, 41, 133, 185, 230, 114, 79,
201, 103, 83, 41, 8, 127, 71, 175, 241, 206, 170, 174, 119, 33, 128
]
);
}

#[test]
#[should_panic = "hex string length is not valid"]
fn t_panic_incorrect_length2() {
let _ = private_hex2array::<2>("454");
}

#[test]
#[should_panic = "hex string contains invalid character"]
fn t_panic_invalid_character() {
let _ = private_hex2array::<2>("45ag");
}
}
1 change: 1 addition & 0 deletions substrate/primitives/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub mod hashing;

#[cfg(feature = "full_crypto")]
pub use hashing::{blake2_128, blake2_256, keccak_256, twox_128, twox_256, twox_64};
pub mod const_hex2array;
pub mod crypto;
pub mod hexdisplay;
pub use paste;
Expand Down
1 change: 0 additions & 1 deletion substrate/primitives/keyring/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ readme = "README.md"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
lazy_static = "1.4.0"
strum = { version = "0.24.1", features = ["derive"], default-features = false }
sp-core = { path = "../core" }
sp-runtime = { path = "../runtime" }
Expand Down
69 changes: 26 additions & 43 deletions substrate/primitives/keyring/src/bandersnatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ pub use sp_core::bandersnatch;
use sp_core::{
bandersnatch::{Pair, Public, Signature},
crypto::UncheckedFrom,
ByteArray, Pair as PairT,
hex2array, ByteArray, Pair as PairT,
};

use lazy_static::lazy_static;
use std::{collections::HashMap, ops::Deref, sync::Mutex};

/// Set of test accounts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter)]
pub enum Keyring {
Expand Down Expand Up @@ -74,7 +71,7 @@ impl Keyring {
}

pub fn public(self) -> Public {
self.pair().public()
Public::from(self)
}

pub fn to_seed(self) -> String {
Expand Down Expand Up @@ -129,20 +126,9 @@ impl std::str::FromStr for Keyring {
}
}

lazy_static! {
static ref PRIVATE_KEYS: Mutex<HashMap<Keyring, Pair>> =
Mutex::new(Keyring::iter().map(|who| (who, who.pair())).collect());
static ref PUBLIC_KEYS: HashMap<Keyring, Public> = PRIVATE_KEYS
.lock()
.unwrap()
.iter()
.map(|(&who, pair)| (who, pair.public()))
.collect();
}

impl From<Keyring> for Public {
fn from(k: Keyring) -> Self {
*(*PUBLIC_KEYS).get(&k).unwrap()
Public::unchecked_from(<[u8; PUBLIC_RAW_LEN]>::from(k))
}
}

Expand All @@ -154,32 +140,24 @@ impl From<Keyring> for Pair {

impl From<Keyring> for [u8; PUBLIC_RAW_LEN] {
fn from(k: Keyring) -> Self {
*(*PUBLIC_KEYS).get(&k).unwrap().as_ref()
}
}

impl From<Keyring> for &'static [u8; PUBLIC_RAW_LEN] {
fn from(k: Keyring) -> Self {
PUBLIC_KEYS.get(&k).unwrap().as_ref()
}
}

impl AsRef<[u8; PUBLIC_RAW_LEN]> for Keyring {
fn as_ref(&self) -> &[u8; PUBLIC_RAW_LEN] {
PUBLIC_KEYS.get(self).unwrap().as_ref()
}
}

impl AsRef<Public> for Keyring {
fn as_ref(&self) -> &Public {
PUBLIC_KEYS.get(self).unwrap()
}
}

impl Deref for Keyring {
type Target = [u8; PUBLIC_RAW_LEN];
fn deref(&self) -> &[u8; PUBLIC_RAW_LEN] {
PUBLIC_KEYS.get(self).unwrap().as_ref()
match k {
Keyring::Alice =>
hex2array!("9c8af77d3a4e3f6f076853922985b9e6724fc9675329087f47aff1ceaaae772180"),
Keyring::Bob =>
hex2array!("1abfbb76dc8374a1a6d93d59a5c81f07c18835f4681a6258aa0f514d363bff4780"),
Keyring::Charlie =>
hex2array!("0f4a9990aca3d39a7cd8bf187e2e81a9ea6f9cedb2db405f2fffff384c5dd02680"),
Keyring::Dave =>
hex2array!("bd7a87d4dfa89926a408b5acbed554ae3b053fa3532531053295cbabf07d337000"),
Keyring::Eve =>
hex2array!("f992d5b8eac8fc004d521bee6edc1174cfa7fae3a1baec8262511ee351f9f85e00"),
Keyring::Ferdie =>
hex2array!("1ce2613e89bc5c8e358aad884099cfb576a61176f2f9968cd0d486a04457245180"),
Keyring::One =>
hex2array!("a29e03ac273e521274d8e501a6242abd2ab393d7e197221a9113bdf8e2e5b34d00"),
Keyring::Two =>
hex2array!("f968d47e819ddb18a9d0f2ebd16501680b1a3f07ee375c6f81310e5f99a04f4d00"),
}
}
}

Expand All @@ -206,4 +184,9 @@ mod tests {
&Keyring::Bob.public(),
));
}
#[test]
fn verify_static_public_keys() {
assert!(Keyring::iter()
.all(|k| { k.pair().public().as_ref() == <[u8; PUBLIC_RAW_LEN]>::from(k) }));
}
}
Loading