Skip to content

Commit bc78e0d

Browse files
committed
use an internal CSPRNG for uuid generation internally in Azle
1 parent 1948df1 commit bc78e0d

File tree

15 files changed

+112
-4
lines changed

15 files changed

+112
-4
lines changed
7.52 KB
Binary file not shown.
4.7 KB
Binary file not shown.

src/experimental/build/commands/build/wasm_binary/rust/experimental_canister_template/src/ic/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ mod stable_b_tree_map_remove;
4242
mod stable_b_tree_map_values;
4343
mod time;
4444
mod trap;
45+
mod uuid;
4546

4647
use wasmedge_quickjs::AsObject;
4748

@@ -347,5 +348,10 @@ pub fn register(context: &mut wasmedge_quickjs::Context) {
347348
context.new_function::<trap::NativeFunction>("").into(),
348349
);
349350

351+
ic.set(
352+
"uuid",
353+
context.new_function::<uuid::NativeFunction>("").into(),
354+
);
355+
350356
context.get_global().set("_azleIcExperimental", ic.into());
351357
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use rand::RngCore;
2+
use wasmedge_quickjs::{Context, JsFn, JsValue};
3+
4+
use crate::INTERNAL_CSPRNG;
5+
6+
pub struct NativeFunction;
7+
impl JsFn for NativeFunction {
8+
fn call(context: &mut Context, _this_val: JsValue, _argv: &[JsValue]) -> JsValue {
9+
INTERNAL_CSPRNG.with(|csprng_refcell| {
10+
let mut rng = csprng_refcell.borrow_mut();
11+
12+
let mut bytes = [0u8; 16];
13+
14+
rng.fill_bytes(&mut bytes);
15+
16+
let hex = format!("{:032x}", u128::from_be_bytes(bytes));
17+
18+
context.new_string(&hex).into()
19+
})
20+
}
21+
}

src/experimental/build/commands/build/wasm_binary/rust/experimental_canister_template/src/init_and_post_upgrade.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use wasmedge_quickjs::AsObject;
1010
use crate::{
1111
MEMORY_MANAGER_REF_CELL, RUNTIME, WASM_DATA_REF_CELL, execute_method_js, ic,
1212
ic::{drain_microtasks, rand_seed::rand_seed},
13+
seed_internal_csprng, upload_file,
1314
wasm_binary_manipulation::{WasmData, get_js_code, get_wasm_data},
15+
web_assembly,
1416
};
1517

16-
use crate::{upload_file, web_assembly};
17-
1818
#[inline(never)]
1919
#[unsafe(no_mangle)]
2020
pub extern "C" fn init(function_index: i32) {
@@ -195,6 +195,13 @@ fn seed_from_raw_rand() {
195195
.candid()?;
196196

197197
rand_seed(
198+
randomness
199+
.clone()
200+
.try_into()
201+
.map_err(|_| "seed must be exactly 32 bytes in length")?,
202+
);
203+
204+
seed_internal_csprng(
198205
randomness
199206
.try_into()
200207
.map_err(|_| "seed must be exactly 32 bytes in length")?,

src/experimental/build/commands/build/wasm_binary/rust/experimental_canister_template/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ mod guards;
1616
mod ic;
1717
mod init_and_post_upgrade;
1818
mod internal_canister_methods;
19+
mod seed_internal_csprng;
1920
mod stable_b_tree_map;
2021
mod upload_file;
2122
mod wasm_binary_manipulation;
2223
mod web_assembly;
2324

25+
pub use seed_internal_csprng::seed_internal_csprng;
26+
2427
#[global_allocator]
2528
static PEAK_ALLOC: PeakAlloc = PeakAlloc;
2629

@@ -29,6 +32,8 @@ type Memory = VirtualMemory<DefaultMemoryImpl>;
2932

3033
thread_local! {
3134
static CSPRNG: RefCell<StdRng> = RefCell::new(StdRng::from_seed([0;32]));
35+
// Internal CSPRNG used exclusively for Azle internal UUID generation. It cannot be reseeded by user code.
36+
static INTERNAL_CSPRNG: RefCell<StdRng> = RefCell::new(StdRng::from_seed([0;32]));
3237
static MEMORY_MANAGER_REF_CELL: RefCell<MemoryManager<DefaultMemoryImpl>> = RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));
3338
static RUNTIME: RefCell<Option<wasmedge_quickjs::Runtime>> = RefCell::new(None);
3439
static WASM_DATA_REF_CELL: RefCell<Option<wasm_binary_manipulation::WasmData>> = RefCell::new(None);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use rand::{SeedableRng, rngs::StdRng};
2+
3+
use crate::INTERNAL_CSPRNG;
4+
5+
/// Seeds the internal Azle CSPRNG used for UUID generation. Not exposed to user code.
6+
pub fn seed_internal_csprng(seed: [u8; 32]) {
7+
INTERNAL_CSPRNG.with(|csprng_refcell| {
8+
let mut rng = csprng_refcell.borrow_mut();
9+
*rng = StdRng::from_seed(seed);
10+
});
11+
}

src/experimental/lib/ic/azle_ic_experimental.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export type AzleIcExperimental = {
3737
msgRejectCode: () => number;
3838
msgReply: (bytes: ArrayBuffer) => void;
3939
randBytes: (byteLength: number) => ArrayBuffer;
40+
uuid: () => string;
4041
randSeed: (seed: ArrayBuffer) => void;
4142
certifiedDataSet: (dataBytes: ArrayBuffer) => void;
4243
setTimer: (delayString: string) => string;

src/stable/build/commands/build/wasm_binary/rust/stable_canister_template/src/ic/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ mod stable_b_tree_map_remove;
4949
mod stable_b_tree_map_values;
5050
mod time;
5151
mod trap;
52+
mod uuid;
5253

5354
#[allow(unused)]
5455
pub fn register(ctx: Ctx) -> Result<()> {
@@ -203,6 +204,8 @@ pub fn register(ctx: Ctx) -> Result<()> {
203204

204205
ic.set("trap", trap::get_function(ctx.clone()))?;
205206

207+
ic.set("uuid", uuid::get_function(ctx.clone()))?;
208+
206209
ctx.globals().set("_azleIc", ic)?;
207210

208211
Ok(())
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use rand::RngCore;
2+
use rquickjs::{Ctx, Function, Result};
3+
4+
use crate::INTERNAL_CSPRNG;
5+
6+
pub fn get_function(ctx: Ctx) -> Result<Function> {
7+
Function::new(ctx.clone(), move || -> Result<String> {
8+
INTERNAL_CSPRNG.with(|csprng_refcell| {
9+
let mut rng = csprng_refcell.borrow_mut();
10+
11+
let mut bytes = [0u8; 16];
12+
13+
rng.fill_bytes(&mut bytes);
14+
15+
let hex = format!("{:032x}", u128::from_be_bytes(bytes));
16+
17+
Ok(hex)
18+
})
19+
})
20+
}

0 commit comments

Comments
 (0)