Skip to content

Instantly share code, notes, and snippets.

@RajeshRk18
Last active October 3, 2023 17:23
Show Gist options
  • Save RajeshRk18/9717b67e53e7771a27c5c9a6ff9e3db3 to your computer and use it in GitHub Desktop.
Save RajeshRk18/9717b67e53e7771a27c5c9a6ff9e3db3 to your computer and use it in GitHub Desktop.
#[derive(Clone, Debug)] // Cloneable
pub struct SecretKey {
scalar: [u64; 2] // Copyable
}
impl Drop for SecretKey {
fn drop(&mut self) {
unsafe {
core::ptr::write_volatile(&mut self.scalar, [0u64; 2]);
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}
println!("Zeroized: {:?}", self);
}
}
let (a, ptr) = {
let b = SecretKey {
scalar: [43, 32]
};
let addr_b = b.scalar.as_ptr();
println!("Creation address: {:?}", addr_b);
(b, addr_b)
};
let addr_a = a.scalar.as_ptr();
println!("Returned address: {:?}", addr_a);
println!("Value before zeroizing: {:?}", &a);
drop(a);
let val = unsafe {
core::slice::from_raw_parts(ptr, 2)
};
println!("Value at creation address: {:?}", val);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment