-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
PRNG
functions to a new Prng
module
These functions do not provide cryptographically strong randomness of the sort cryptographic applications need. Therefore they should not be in the crypto module.
- Loading branch information
1 parent
79cb165
commit 2121193
Showing
7 changed files
with
148 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//! Prng contains functions for pseudo-random functions. | ||
use crate::{ | ||
env::internal, unwrap::UnwrapInfallible, Bytes, BytesN, Env, EnvBase, IntoVal, TryIntoVal, Val, | ||
Vec, | ||
}; | ||
|
||
/// Prng provides access to pseudo-random functions. | ||
pub struct Prng { | ||
env: Env, | ||
} | ||
|
||
impl Prng { | ||
pub(crate) fn new(env: &Env) -> Prng { | ||
Prng { env: env.clone() } | ||
} | ||
|
||
pub fn env(&self) -> &Env { | ||
&self.env | ||
} | ||
|
||
// Reseeds the pseudorandom number generator (PRNG) with the provided `seed` value. | ||
pub fn prng_reseed(&self, seed: &Bytes) { | ||
let env = self.env(); | ||
env.check_same_env(seed.env()); | ||
internal::Env::prng_reseed(env, seed.into()).unwrap_infallible(); | ||
} | ||
|
||
// Returns a random u64 in the range between `lower` and `upper` inclusive. | ||
pub fn prng_u64_in_inclusive_range(&self, lower: u64, upper: u64) -> u64 { | ||
let env = self.env(); | ||
internal::Env::prng_u64_in_inclusive_range(env, lower.into(), upper.into()) | ||
.unwrap_infallible() | ||
.into() | ||
} | ||
|
||
// Shuffles a given vector v using the Fisher-Yates algorithm. | ||
pub fn prng_vec_shuffle<V>(&self, v: V) -> Vec<Val> | ||
where | ||
V: IntoVal<Env, Vec<Val>>, | ||
{ | ||
let env = self.env(); | ||
let v_val = v.into_val(env); | ||
env.check_same_env(v_val.env()); | ||
|
||
internal::Env::prng_vec_shuffle(env, v_val.to_object()) | ||
.unwrap_infallible() | ||
.try_into_val(env) | ||
.unwrap_infallible() | ||
} | ||
/// Verifies an ed25519 signature. | ||
/// | ||
/// The signature is verified as a valid signature of the message by the | ||
/// ed25519 public key. | ||
/// | ||
/// ### Panics | ||
/// | ||
/// If the signature verification fails. | ||
pub fn ed25519_verify(&self, public_key: &BytesN<32>, message: &Bytes, signature: &BytesN<64>) { | ||
let env = self.env(); | ||
env.check_same_env(public_key.env()); | ||
env.check_same_env(message.env()); | ||
env.check_same_env(signature.env()); | ||
let _ = internal::Env::verify_sig_ed25519( | ||
env, | ||
public_key.to_object(), | ||
message.to_object(), | ||
signature.to_object(), | ||
); | ||
} | ||
} |
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,64 @@ | ||
use crate::{self as soroban_sdk}; | ||
use crate::{bytes, vec, Bytes, Env, Val, Vec}; | ||
use soroban_sdk::{contract, contractimpl}; | ||
|
||
#[contract] | ||
pub struct TestPrngContract; | ||
|
||
#[contractimpl] | ||
impl TestPrngContract { | ||
pub fn prng_reseed(env: Env, bytes: Bytes) { | ||
env.prng().prng_reseed(&bytes); | ||
} | ||
|
||
pub fn prng_u64_in_inclusive_range(env: Env, min: u64, max: u64) -> u64 { | ||
env.prng().prng_u64_in_inclusive_range(min, max) | ||
} | ||
|
||
pub fn prng_vec_shuffle(env: Env, vec: Vec<u32>) -> Vec<Val> { | ||
env.prng().prng_vec_shuffle::<Vec<u32>>(vec.into()) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_prng_reseed() { | ||
let env = Env::default(); | ||
let contract_id = env.register_contract(None, TestPrngContract); | ||
env.host().set_base_prng_seed([0; 32]).unwrap(); | ||
let client = TestPrngContractClient::new(&env, &contract_id); | ||
|
||
let seed = bytes!( | ||
&env, | ||
0x0000000000000000000000000000000000000000000000000000000000000001 | ||
); | ||
assert_eq!(client.prng_u64_in_inclusive_range(&0, &9), 6); | ||
|
||
client.prng_reseed(&seed); | ||
|
||
assert_eq!(client.prng_u64_in_inclusive_range(&0, &9), 8); | ||
} | ||
|
||
#[test] | ||
fn test_vec_shuffle() { | ||
let env = Env::default(); | ||
env.host().set_base_prng_seed([0; 32]).unwrap(); | ||
let contract_id = env.register_contract(None, TestPrngContract); | ||
let client = TestPrngContractClient::new(&env, &contract_id); | ||
|
||
let vec = vec![&env, 1, 2, 3]; | ||
|
||
assert_eq!( | ||
client.prng_vec_shuffle(&vec), | ||
vec![&env, Val::from(2u32), Val::from(3u32), Val::from(1u32)] | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_u64_in_inclusive_range() { | ||
let env = Env::default(); | ||
env.host().set_base_prng_seed([0; 32]).unwrap(); | ||
let contract_id = env.register_contract(None, TestPrngContract); | ||
let client = TestPrngContractClient::new(&env, &contract_id); | ||
|
||
assert_eq!(client.prng_u64_in_inclusive_range(&0, &9), 6); | ||
} |