generic_ec/secret_scalar/
definition.rs1#[cfg(feature = "alloc")]
2#[cfg_attr(docsrs, doc(cfg(all())))]
3mod with_alloc {
4 use alloc::sync::Arc;
5 use zeroize::{Zeroize, Zeroizing};
6
7 use crate::{Curve, Scalar};
8
9 #[doc = include_str!("docs.md")]
10 pub struct SecretScalar<E: Curve>(Arc<Zeroizing<Scalar<E>>>);
11
12 impl<E: Curve> SecretScalar<E> {
13 #[doc = include_str!("docs-constructor.md")]
14 pub fn new(scalar: &mut Scalar<E>) -> Self {
15 let mut scalar_on_heap = Arc::<Zeroizing<Scalar<E>>>::default();
16 let scalar_mut = Arc::make_mut(&mut scalar_on_heap);
17 core::mem::swap(&mut **scalar_mut, scalar);
18 scalar.zeroize();
19 Self(scalar_on_heap)
20 }
21 }
22
23 impl<E: Curve> AsRef<Scalar<E>> for SecretScalar<E> {
24 fn as_ref(&self) -> &Scalar<E> {
25 &self.0
26 }
27 }
28
29 impl<E: Curve> Clone for SecretScalar<E> {
30 fn clone(&self) -> Self {
31 Self(self.0.clone())
32 }
33 }
34}
35
36#[cfg(not(feature = "alloc"))]
37#[cfg_attr(docsrs, doc(cfg(all())))]
38mod without_alloc {
39 use zeroize::{Zeroize, Zeroizing};
40
41 use crate::{Curve, Scalar};
42
43 #[doc = include_str!("docs.md")]
44 pub struct SecretScalar<E: Curve>(Zeroizing<Scalar<E>>);
45
46 impl<E: Curve> SecretScalar<E> {
47 #[doc = include_str!("docs-constructor.md")]
48 pub fn new(scalar: &mut Scalar<E>) -> Self {
49 let scalar_new = Self(Zeroizing::new(*scalar));
50 scalar.zeroize();
51 scalar_new
52 }
53 }
54
55 impl<E: Curve> AsRef<Scalar<E>> for SecretScalar<E> {
56 fn as_ref(&self) -> &Scalar<E> {
57 &self.0
58 }
59 }
60
61 impl<E: Curve> Clone for SecretScalar<E> {
62 fn clone(&self) -> Self {
63 Self(self.0.clone())
64 }
65 }
66}
67
68mod secret_scalar {
69 #[cfg(feature = "alloc")]
70 #[cfg_attr(docsrs, doc(cfg(all())))]
71 pub use super::with_alloc::SecretScalar;
72 #[cfg(not(feature = "alloc"))]
73 #[cfg_attr(docsrs, doc(cfg(all())))]
74 pub use super::without_alloc::SecretScalar;
75}
76
77pub use self::secret_scalar::SecretScalar;