Skip to content

Commit

Permalink
Pass a Vec to vec_shuffle instead of a VecObject
Browse files Browse the repository at this point in the history
  • Loading branch information
masonforest committed Jun 28, 2023
1 parent a47bf12 commit 7499739
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
17 changes: 12 additions & 5 deletions soroban-sdk/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
//! Crypto contains functions for cryptographic functions.
use crate::{env::internal, unwrap::UnwrapInfallible, Bytes, BytesN, Env, VecObject};
use crate::{
env::internal, unwrap::UnwrapInfallible, Bytes, BytesN, Env, IntoVal,
TryIntoVal, Val, Vec,
};

/// Crypto provides access to cryptographic functions.
pub struct Crypto {
env: Env,
}


impl Crypto {
pub(crate) fn new(env: &Env) -> Crypto {
Crypto { env: env.clone() }
Expand Down Expand Up @@ -37,13 +41,16 @@ impl Crypto {
}

// Shuffles a given vector v using the Fisher-Yates algorithm.
pub fn vec_shuffle(&self, v: VecObject) -> VecObject {
pub fn vec_shuffle<V>(&self, v: V) -> Vec<Val>
where
V: IntoVal<Env, Vec<Val>>,
{
let env = self.env();
internal::Env::prng_vec_shuffle(env, v.into())
internal::Env::prng_vec_shuffle(env, v.into_val(env).to_object())
.unwrap_infallible()
.try_into_val(env)
.unwrap_infallible()
.into()
}

/// Verifies an ed25519 signature.
///
/// The signature is verified as a valid signature of the message by the
Expand Down
26 changes: 20 additions & 6 deletions soroban-sdk/src/tests/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{self as soroban_sdk};
use crate::{bytes, bytesn, vec, Bytes, BytesN, Env, IntoVal, Vec};
use crate::{bytes, bytesn, vec, Bytes, BytesN, Env, IntoVal, Val, Vec};
use soroban_sdk::{contract, contractimpl};

#[contract]
Expand All @@ -19,8 +19,10 @@ impl TestCryptoContract {
env.crypto().u64_in_inclusive_range(min, max)
}

pub fn vec_shuffle(env: Env, vec: Vec<u32>) -> Vec<u32> {
env.crypto().vec_shuffle(vec.into()).into_val(&env)
pub fn vec_shuffle(env: Env, vec: Vec<u32>) -> Vec<Val> {
env.crypto()
.vec_shuffle::<Vec<u32>>(vec.into())
.into_val(&env)
}

pub fn verify_sig_ed25519(
Expand Down Expand Up @@ -60,7 +62,13 @@ fn test_sha256() {

let bytes = bytes!(&env, 0x01);

assert_eq!(client.sha256(&bytes), bytesn!(&env, 0x4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a));
assert_eq!(
client.sha256(&bytes),
bytesn!(
&env,
0x4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a
)
);
}

#[test]
Expand All @@ -72,7 +80,10 @@ fn test_vec_shuffle() {

let vec = vec![&env, 1, 2, 3];

assert_eq!(client.vec_shuffle(&vec), vec![&env, 2, 3, 1]);
assert_eq!(
client.vec_shuffle(&vec),
vec![&env, Val::from(2u32), Val::from(3u32), Val::from(1u32)]
);
}

#[test]
Expand Down Expand Up @@ -104,7 +115,10 @@ fn test_verify_sig_ed25519() {
);
let message = bytes!(&env, 0x72);

assert_eq!(client.verify_sig_ed25519(&public_key, &message, &signature), ());
assert_eq!(
client.verify_sig_ed25519(&public_key, &message, &signature),
()
);
}

#[test]
Expand Down

0 comments on commit 7499739

Please sign in to comment.