pub struct SecretScalar<E: Curve>(/* private fields */);
Expand description
Scalar representing sensitive information (like secret key)
Secret scalar should be treated with an extra care. You shouldn’t do any
branching (e.g. Eq
, Ord
) on the secret to avoid timing side-channel
attacks, so it implements only constant time traits (like ConstantTimeEq
).
Also, when alloc
feature is enabled, we enforce extra measures:
- Secret scalar leaves no trace in RAM after it’s dropped
Memory is zeroized after use - All clones of secret scalar refer to the same region in the memory
I.e. there will always be only one instance of the scalar in the memory no matter how many clones you make
All these guarantees can be bypassed by calling .as_ref()
and obtaining
&Scalar<E>
that is not protected from timing attacks, leaving traces in
the memory, etc.
Implementations§
Source§impl<E: Curve> SecretScalar<E>
impl<E: Curve> SecretScalar<E>
Source§impl<E: Curve> SecretScalar<E>
impl<E: Curve> SecretScalar<E>
Sourcepub fn from_hash<D: Digest>(data: &impl Digestable) -> Self
Available on crate feature hash-to-scalar
only.
pub fn from_hash<D: Digest>(data: &impl Digestable) -> Self
hash-to-scalar
only.Hashes the input and outputs scalar
Input can be any structured data that implements Digestable
trait (see udigest crate).
§How it works
It works by instantiating HashRng
CSPRNG seeded from provided data.
Then it’s used to derive the scalar.
§Security considerations
It’s not constant time. It doesn’t follow any existing standards for hash to scalar primitive.
§Example
use generic_ec::{SecretScalar, curves::Secp256k1};
use sha2::Sha256;
#[derive(udigest::Digestable)]
struct Data<'a> {
nonce: &'a [u8],
param_a: &'a str,
param_b: u128,
// ...
}
let scalar = SecretScalar::<Secp256k1>::from_hash::<Sha256>(&Data {
nonce: b"some data",
param_a: "some other data",
param_b: 12345,
// ...
});
Sourcepub fn from_be_bytes(bytes: &[u8]) -> Result<Self, InvalidScalar>
pub fn from_be_bytes(bytes: &[u8]) -> Result<Self, InvalidScalar>
Decodes scalar from its bytes representation in big-endian order
Sourcepub fn from_le_bytes(bytes: &[u8]) -> Result<Self, InvalidScalar>
pub fn from_le_bytes(bytes: &[u8]) -> Result<Self, InvalidScalar>
Decodes scalar from its bytes representation in little-endian order
Trait Implementations§
Source§impl<E: Curve> Add<&NonZero<SecretScalar<E>>> for &SecretScalar<E>
impl<E: Curve> Add<&NonZero<SecretScalar<E>>> for &SecretScalar<E>
Source§impl<E: Curve> Add<&NonZero<SecretScalar<E>>> for SecretScalar<E>
impl<E: Curve> Add<&NonZero<SecretScalar<E>>> for SecretScalar<E>
Source§impl<E: Curve> Add<&SecretScalar<E>> for &NonZero<SecretScalar<E>>
impl<E: Curve> Add<&SecretScalar<E>> for &NonZero<SecretScalar<E>>
Source§impl<E: Curve> Add<&SecretScalar<E>> for NonZero<SecretScalar<E>>
impl<E: Curve> Add<&SecretScalar<E>> for NonZero<SecretScalar<E>>
Source§impl<E: Curve> Add<NonZero<SecretScalar<E>>> for &SecretScalar<E>
impl<E: Curve> Add<NonZero<SecretScalar<E>>> for &SecretScalar<E>
Source§impl<E: Curve> Add<NonZero<SecretScalar<E>>> for SecretScalar<E>
impl<E: Curve> Add<NonZero<SecretScalar<E>>> for SecretScalar<E>
Source§impl<E: Curve> Add<SecretScalar<E>> for &NonZero<SecretScalar<E>>
impl<E: Curve> Add<SecretScalar<E>> for &NonZero<SecretScalar<E>>
Source§impl<E: Curve> Add<SecretScalar<E>> for NonZero<SecretScalar<E>>
impl<E: Curve> Add<SecretScalar<E>> for NonZero<SecretScalar<E>>
Source§impl<E: Curve> AddAssign<&SecretScalar<E>> for Scalar<E>
impl<E: Curve> AddAssign<&SecretScalar<E>> for Scalar<E>
Source§fn add_assign(&mut self, rhs: &SecretScalar<E>)
fn add_assign(&mut self, rhs: &SecretScalar<E>)
+=
operation. Read moreSource§impl<E: Curve> AddAssign<SecretScalar<E>> for Scalar<E>
impl<E: Curve> AddAssign<SecretScalar<E>> for Scalar<E>
Source§fn add_assign(&mut self, rhs: SecretScalar<E>)
fn add_assign(&mut self, rhs: SecretScalar<E>)
+=
operation. Read moreSource§impl<E: Curve> Clone for SecretScalar<E>
impl<E: Curve> Clone for SecretScalar<E>
Source§impl<E: Curve> ConstantTimeEq for SecretScalar<E>
impl<E: Curve> ConstantTimeEq for SecretScalar<E>
Source§impl<E: Curve> Debug for SecretScalar<E>
impl<E: Curve> Debug for SecretScalar<E>
Source§impl<'de, E: Curve> Deserialize<'de> for SecretScalar<E>
Available on crate feature serde
only.
impl<'de, E: Curve> Deserialize<'de> for SecretScalar<E>
serde
only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<'de, E: Curve> DeserializeAs<'de, SecretScalar<E>> for Compact
Available on crate feature serde
only.
impl<'de, E: Curve> DeserializeAs<'de, SecretScalar<E>> for Compact
serde
only.Source§fn deserialize_as<D>(deserializer: D) -> Result<SecretScalar<E>, D::Error>where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<SecretScalar<E>, D::Error>where
D: Deserializer<'de>,
Source§impl<E: Curve> From<NonZero<SecretScalar<E>>> for SecretScalar<E>
impl<E: Curve> From<NonZero<SecretScalar<E>>> for SecretScalar<E>
Source§fn from(secret_scalar: NonZero<SecretScalar<E>>) -> Self
fn from(secret_scalar: NonZero<SecretScalar<E>>) -> Self
Source§impl<E: Curve> MulAssign<&SecretScalar<E>> for Point<E>
impl<E: Curve> MulAssign<&SecretScalar<E>> for Point<E>
Source§fn mul_assign(&mut self, rhs: &SecretScalar<E>)
fn mul_assign(&mut self, rhs: &SecretScalar<E>)
*=
operation. Read moreSource§impl<E: Curve> MulAssign<&SecretScalar<E>> for Scalar<E>
impl<E: Curve> MulAssign<&SecretScalar<E>> for Scalar<E>
Source§fn mul_assign(&mut self, rhs: &SecretScalar<E>)
fn mul_assign(&mut self, rhs: &SecretScalar<E>)
*=
operation. Read moreSource§impl<E: Curve> MulAssign<SecretScalar<E>> for Point<E>
impl<E: Curve> MulAssign<SecretScalar<E>> for Point<E>
Source§fn mul_assign(&mut self, rhs: SecretScalar<E>)
fn mul_assign(&mut self, rhs: SecretScalar<E>)
*=
operation. Read moreSource§impl<E: Curve> MulAssign<SecretScalar<E>> for Scalar<E>
impl<E: Curve> MulAssign<SecretScalar<E>> for Scalar<E>
Source§fn mul_assign(&mut self, rhs: SecretScalar<E>)
fn mul_assign(&mut self, rhs: SecretScalar<E>)
*=
operation. Read moreSource§impl<'s, E: Curve> Product<&'s SecretScalar<E>> for Scalar<E>
impl<'s, E: Curve> Product<&'s SecretScalar<E>> for Scalar<E>
Source§fn product<I: Iterator<Item = &'s SecretScalar<E>>>(iter: I) -> Self
fn product<I: Iterator<Item = &'s SecretScalar<E>>>(iter: I) -> Self
Self
from the elements by multiplying
the items.Source§impl<E: Curve> Product<SecretScalar<E>> for Scalar<E>
impl<E: Curve> Product<SecretScalar<E>> for Scalar<E>
Source§fn product<I: Iterator<Item = SecretScalar<E>>>(iter: I) -> Self
fn product<I: Iterator<Item = SecretScalar<E>>>(iter: I) -> Self
Self
from the elements by multiplying
the items.Source§impl<E: Curve> Samplable for SecretScalar<E>
impl<E: Curve> Samplable for SecretScalar<E>
Source§impl<E: Curve> Serialize for SecretScalar<E>
Available on crate feature serde
only.
impl<E: Curve> Serialize for SecretScalar<E>
serde
only.Source§impl<E: Curve> SerializeAs<SecretScalar<E>> for Compact
Available on crate feature serde
only.
impl<E: Curve> SerializeAs<SecretScalar<E>> for Compact
serde
only.Source§fn serialize_as<S>(
source: &SecretScalar<E>,
serializer: S,
) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize_as<S>(
source: &SecretScalar<E>,
serializer: S,
) -> Result<S::Ok, S::Error>where
S: Serializer,
Source§impl<E: Curve> Sub<&NonZero<SecretScalar<E>>> for &SecretScalar<E>
impl<E: Curve> Sub<&NonZero<SecretScalar<E>>> for &SecretScalar<E>
Source§impl<E: Curve> Sub<&NonZero<SecretScalar<E>>> for SecretScalar<E>
impl<E: Curve> Sub<&NonZero<SecretScalar<E>>> for SecretScalar<E>
Source§impl<E: Curve> Sub<&SecretScalar<E>> for &NonZero<SecretScalar<E>>
impl<E: Curve> Sub<&SecretScalar<E>> for &NonZero<SecretScalar<E>>
Source§impl<E: Curve> Sub<&SecretScalar<E>> for NonZero<SecretScalar<E>>
impl<E: Curve> Sub<&SecretScalar<E>> for NonZero<SecretScalar<E>>
Source§impl<E: Curve> Sub<NonZero<SecretScalar<E>>> for &SecretScalar<E>
impl<E: Curve> Sub<NonZero<SecretScalar<E>>> for &SecretScalar<E>
Source§impl<E: Curve> Sub<NonZero<SecretScalar<E>>> for SecretScalar<E>
impl<E: Curve> Sub<NonZero<SecretScalar<E>>> for SecretScalar<E>
Source§impl<E: Curve> Sub<SecretScalar<E>> for &NonZero<SecretScalar<E>>
impl<E: Curve> Sub<SecretScalar<E>> for &NonZero<SecretScalar<E>>
Source§impl<E: Curve> Sub<SecretScalar<E>> for NonZero<SecretScalar<E>>
impl<E: Curve> Sub<SecretScalar<E>> for NonZero<SecretScalar<E>>
Source§impl<E: Curve> SubAssign<&SecretScalar<E>> for Scalar<E>
impl<E: Curve> SubAssign<&SecretScalar<E>> for Scalar<E>
Source§fn sub_assign(&mut self, rhs: &SecretScalar<E>)
fn sub_assign(&mut self, rhs: &SecretScalar<E>)
-=
operation. Read moreSource§impl<E: Curve> SubAssign<SecretScalar<E>> for Scalar<E>
impl<E: Curve> SubAssign<SecretScalar<E>> for Scalar<E>
Source§fn sub_assign(&mut self, rhs: SecretScalar<E>)
fn sub_assign(&mut self, rhs: SecretScalar<E>)
-=
operation. Read moreSource§impl<'s, E: Curve> Sum<&'s NonZero<SecretScalar<E>>> for SecretScalar<E>
impl<'s, E: Curve> Sum<&'s NonZero<SecretScalar<E>>> for SecretScalar<E>
Source§impl<'s, E: Curve> Sum<&'s SecretScalar<E>> for Scalar<E>
impl<'s, E: Curve> Sum<&'s SecretScalar<E>> for Scalar<E>
Source§fn sum<I: Iterator<Item = &'s SecretScalar<E>>>(iter: I) -> Self
fn sum<I: Iterator<Item = &'s SecretScalar<E>>>(iter: I) -> Self
Self
from the elements by “summing up”
the items.Source§impl<E: Curve> Sum<NonZero<SecretScalar<E>>> for SecretScalar<E>
impl<E: Curve> Sum<NonZero<SecretScalar<E>>> for SecretScalar<E>
Source§impl<E: Curve> Sum<SecretScalar<E>> for Scalar<E>
impl<E: Curve> Sum<SecretScalar<E>> for Scalar<E>
Source§fn sum<I: Iterator<Item = SecretScalar<E>>>(iter: I) -> Self
fn sum<I: Iterator<Item = SecretScalar<E>>>(iter: I) -> Self
Self
from the elements by “summing up”
the items.