Skip to content

Commit

Permalink
sdk: Add concurrent support for rand 0.7 and 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque committed Aug 17, 2023
1 parent a9ecdc0 commit be1e6db
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 12 deletions.
2 changes: 2 additions & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ num-traits = { workspace = true }
num_enum = { workspace = true }
pbkdf2 = { workspace = true }
qstring = { workspace = true }
rand0-7 = { package = "rand", version = "0.7" } # remove with upgrade to ed25519-dalek v2
rand = { workspace = true, optional = true }
rand_chacha = { workspace = true, optional = true }
rand_core = { workspace = true, optional = true }
rustversion = { workspace = true }
serde = { workspace = true }
serde_bytes = { workspace = true }
Expand Down
8 changes: 4 additions & 4 deletions sdk/program/src/serde_varint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ mod tests {
let mut rng = rand::thread_rng();
for _ in 0..100_000 {
let dummy = Dummy {
a: rng.gen::<u32>() >> rng.gen_range(0, u32::BITS),
b: rng.gen::<u64>() >> rng.gen_range(0, u64::BITS),
c: rng.gen::<u64>() >> rng.gen_range(0, u64::BITS),
d: rng.gen::<u32>() >> rng.gen_range(0, u32::BITS),
a: rng.gen::<u32>() >> rng.gen_range(0..u32::BITS),
b: rng.gen::<u64>() >> rng.gen_range(0..u64::BITS),
c: rng.gen::<u64>() >> rng.gen_range(0..u64::BITS),
d: rng.gen::<u32>() >> rng.gen_range(0..u32::BITS),
};
let bytes = bincode::serialize(&dummy).unwrap();
let other: Dummy = bincode::deserialize(&bytes).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions sdk/program/src/vote/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1266,8 +1266,8 @@ mod tests {

fn run_serde_compact_vote_state_update<R: Rng>(rng: &mut R) {
let lockouts: VecDeque<_> = std::iter::repeat_with(|| {
let slot = 149_303_885_u64.saturating_add(rng.gen_range(0, 10_000));
let confirmation_count = rng.gen_range(0, 33);
let slot = 149_303_885_u64.saturating_add(rng.gen_range(0..10_000));
let confirmation_count = rng.gen_range(0..33);
Lockout::new_with_confirmation_count(slot, confirmation_count)
})
.take(32)
Expand All @@ -1276,7 +1276,7 @@ mod tests {
let root = rng.gen_ratio(1, 2).then(|| {
lockouts[0]
.slot()
.checked_sub(rng.gen_range(0, 1_000))
.checked_sub(rng.gen_range(0..1_000))
.expect("All slots should be greater than 1_000")
});
let timestamp = rng.gen_ratio(1, 2).then(|| rng.gen());
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/ed25519_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub mod test {
signature::{Keypair, Signer},
transaction::Transaction,
},
rand::{thread_rng, Rng},
rand0_7::{thread_rng, Rng},
};

fn test_case(
Expand Down
13 changes: 12 additions & 1 deletion sdk/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@
//! [SHA-256]: https://en.wikipedia.org/wiki/SHA-2
//! [`Hash`]: struct@Hash
use rand::Rng;
pub use solana_program::hash::*;

/// Random hash value for tests and benchmarks.
#[deprecated(since = "1.17.0", note = "Please use `new_with_thread_rng`")]
#[cfg(feature = "full")]
pub fn new_rand<R: ?Sized>(rng: &mut R) -> Hash
where
R: rand::Rng,
R: rand0_7::Rng,
{
let mut buf = [0u8; HASH_BYTES];
rng.fill(&mut buf);
Hash::new(&buf)
}

/// Random hash value for tests and benchmarks.
#[cfg(feature = "full")]
pub fn new_with_thread_rng() -> Hash {
let mut rng = rand::thread_rng();
let mut buf = [0u8; HASH_BYTES];
rng.fill(&mut buf);
Hash::new(&buf)
}
4 changes: 2 additions & 2 deletions sdk/src/secp256k1_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@
//! // Sign some messages.
//! let mut signatures = vec![];
//! for idx in 0..2 {
//! let secret_key = libsecp256k1::SecretKey::random(&mut rand::thread_rng());
//! let secret_key = libsecp256k1::SecretKey::random(&mut rand0_7::thread_rng());
//! let message = format!("hello world {}", idx).into_bytes();
//! let message_hash = {
//! let mut hasher = keccak::Hasher::default();
Expand Down Expand Up @@ -1059,7 +1059,7 @@ pub mod test {
signature::{Keypair, Signer},
transaction::Transaction,
},
rand::{thread_rng, Rng},
rand0_7::{thread_rng, Rng},
};

fn test_case(
Expand Down
24 changes: 23 additions & 1 deletion sdk/src/signer/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {
ed25519_dalek::Signer as DalekSigner,
ed25519_dalek_bip32::Error as Bip32Error,
hmac::Hmac,
rand::{rngs::OsRng, CryptoRng, RngCore},
rand0_7::{rngs::OsRng, CryptoRng, RngCore},
std::{
error,
io::{Read, Write},
Expand Down Expand Up @@ -78,6 +78,20 @@ impl Keypair {
public: self.0.public,
})
}

/// Generates a `Keypair` from the secret key bytes only
pub fn from_secret_key_bytes(bytes: &[u8]) -> Result<Self, ed25519_dalek::SignatureError> {
let secret_key = ed25519_dalek::SecretKey::from_bytes(bytes)?;
let public_key = ed25519_dalek::PublicKey::from(&secret_key);
ed25519_dalek::Keypair::from_bytes(
&[
secret_key.as_bytes().as_slice(),
public_key.as_bytes().as_slice(),
]
.concat(),
)
.map(Self)
}
}

impl Signer for Keypair {
Expand Down Expand Up @@ -364,4 +378,12 @@ mod tests {
let keypair2 = keypair_from_seed(&[0u8; 32]).unwrap();
assert_eq!(keypair, keypair2);
}

#[test]
fn test_from_secret_key_bytes() {
let keypair = Keypair::new();
let keypair_from_bytes = Keypair::from_secret_key_bytes(keypair.secret().as_bytes()).unwrap();
assert_eq!(keypair_from_bytes.secret().as_bytes(), keypair.secret().as_bytes());
assert_eq!(keypair_from_bytes.pubkey(), keypair.pubkey());
}
}

0 comments on commit be1e6db

Please sign in to comment.