pub struct Scalar<E: Curve>(/* private fields */);
Expand description
Scalar modulo curve E
group order
Scalar is an integer modulo curve E
group order.
Implementations§
Source§impl<E: Curve> Scalar<E>
impl<E: Curve> Scalar<E>
Sourcepub fn zero() -> Self
pub fn zero() -> Self
Returns scalar $S = 0$
use generic_ec::{Scalar, curves::Secp256k1};
use rand::rngs::OsRng;
let s = Scalar::<Secp256k1>::random(&mut OsRng);
assert_eq!(s * Scalar::zero(), Scalar::zero());
assert_eq!(s + Scalar::zero(), s);
Sourcepub fn one() -> Self
pub fn one() -> Self
Returns scalar $S = 1$
use generic_ec::{Scalar, curves::Secp256k1};
use rand::rngs::OsRng;
let s = Scalar::<Secp256k1>::random(&mut OsRng);
assert_eq!(s * Scalar::one(), s);
Sourcepub fn invert(&self) -> Option<Self>
pub fn invert(&self) -> Option<Self>
Returns scalar inverse $S^{-1}$
Inverse of scalar $S$ is a scalar $S^{-1}$ such as $S \cdot S^{-1} = 1$. Inverse doesn’t
exist only for scalar $S = 0$, so function returns None
if scalar is zero.
use generic_ec::{Scalar, curves::Secp256k1};
use rand::rngs::OsRng;
let s = Scalar::<Secp256k1>::random(&mut OsRng);
let s_inv = s.invert()?;
assert_eq!(s * s_inv, Scalar::one());
Sourcepub fn ct_invert(&self) -> CtOption<Self>
pub fn ct_invert(&self) -> CtOption<Self>
Returns scalar inverse $S^{-1}$ (constant time)
Same as Scalar::invert
but performs constant-time check on whether it’s zero
scalar
Sourcepub fn to_be_bytes(&self) -> EncodedScalar<E>
pub fn to_be_bytes(&self) -> EncodedScalar<E>
Encodes scalar as bytes in big-endian order
use generic_ec::{Scalar, curves::Secp256k1};
use rand::rngs::OsRng;
let s = Scalar::<Secp256k1>::random(&mut OsRng);
let bytes = s.to_be_bytes();
println!("Scalar hex representation: {}", hex::encode(bytes));
Sourcepub fn to_le_bytes(&self) -> EncodedScalar<E>
pub fn to_le_bytes(&self) -> EncodedScalar<E>
Encodes scalar as bytes in little-endian order
Sourcepub fn from_be_bytes(bytes: impl AsRef<[u8]>) -> Result<Self, InvalidScalar>
pub fn from_be_bytes(bytes: impl AsRef<[u8]>) -> Result<Self, InvalidScalar>
Decodes scalar from its representation as bytes in big-endian order
Returns error if encoded integer is larger than group order.
use generic_ec::{Scalar, curves::Secp256k1};
use rand::rngs::OsRng;
let s = Scalar::<Secp256k1>::random(&mut OsRng);
let s_bytes = s.to_be_bytes();
let s_decoded = Scalar::from_be_bytes(&s_bytes)?;
assert_eq!(s, s_decoded);
Sourcepub fn from_le_bytes(bytes: impl AsRef<[u8]>) -> Result<Self, InvalidScalar>
pub fn from_le_bytes(bytes: impl AsRef<[u8]>) -> Result<Self, InvalidScalar>
Decodes scalar from its representation as bytes in little-endian order
Returns error if encoded integer is larger than group order.
Sourcepub fn from_be_bytes_mod_order(bytes: impl AsRef<[u8]>) -> Self
pub fn from_be_bytes_mod_order(bytes: impl AsRef<[u8]>) -> Self
Interprets provided bytes as integer $i$ in big-endian order, returns scalar $s = i \mod q$
Sourcepub fn from_le_bytes_mod_order(bytes: impl AsRef<[u8]>) -> Self
pub fn from_le_bytes_mod_order(bytes: impl AsRef<[u8]>) -> Self
Interprets provided bytes as integer $i$ in little-endian order, returns scalar $s = i \mod q$
Sourcepub fn random<R: RngCore>(rng: &mut R) -> Self
pub fn random<R: RngCore>(rng: &mut R) -> Self
Generates random non-zero scalar
Algorithm is based on rejection sampling: we sample a scalar, if it’s zero try again. It may be considered constant-time as zero scalar appears with $2^{-256}$ probability which is considered to be negligible.
§Panics
Panics if randomness source returned 100 zero scalars in a row. It happens with $2^{-25600}$ probability, which practically means that randomness source is broken.
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::{Scalar, curves::Secp256k1};
use sha2::Sha256;
#[derive(udigest::Digestable)]
struct Data<'a> {
nonce: &'a [u8],
param_a: &'a str,
param_b: u128,
// ...
}
let scalar = Scalar::<Secp256k1>::from_hash::<Sha256>(&Data {
nonce: b"some data",
param_a: "some other data",
param_b: 12345,
// ...
});
Sourcepub fn serialized_len() -> usize
pub fn serialized_len() -> usize
Returns size of bytes buffer that can fit serialized scalar
Sourcepub fn as_radix16_be(&self) -> Radix16Iter<E> ⓘ
pub fn as_radix16_be(&self) -> Radix16Iter<E> ⓘ
Returns scalar big-endian representation in radix $2^4 = 16$
Radix 16 representation is defined as sum:
$$s = s_0 + s_1 16^1 + s_2 16^2 + \dots + s_{\log_{16}(s) - 1} 16^{\log_{16}(s) - 1}$$
(note: we typically work with 256 bit scalars, so $\log_{16}(s) = \log_{16}(2^{256}) = 64$)
Returns iterator over coefficients from most to least significant: $s_{\log_{16}(s) - 1}, \dots, s_1, s_0$
Sourcepub fn as_radix16_le(&self) -> Radix16Iter<E> ⓘ
pub fn as_radix16_le(&self) -> Radix16Iter<E> ⓘ
Returns scalar little-endian representation in radix $2^4 = 16$
Radix 16 representation is defined as sum:
$$s = s_0 + s_1 16^1 + s_2 16^2 + \dots + s_{\log_{16}(s) - 1} 16^{\log_{16}(s) - 1}$$
(note: we typically work with 256 bit scalars, so $\log_{16}(s) = \log_{16}(2^{256}) = 64$)
Returns iterator over coefficients from least to most significant: $s_0, s_1, \dots, s_{\log_{16}(s) - 1}$
Sourcepub fn multiscalar_mul<S, P>(
scalar_points: impl ExactSizeIterator<Item = (S, P)>,
) -> Point<E>
pub fn multiscalar_mul<S, P>( scalar_points: impl ExactSizeIterator<Item = (S, P)>, ) -> Point<E>
Performs multiscalar multiplication
Takes iterator of pairs (scalar, point)
. Returns sum of scalar * point
. Uses
Default
algorithm.
See multiscalar module docs for more info.
Trait Implementations§
Source§impl<E: Curve> AddAssign<&NonZero<SecretScalar<E>>> for Scalar<E>
impl<E: Curve> AddAssign<&NonZero<SecretScalar<E>>> for Scalar<E>
Source§fn add_assign(&mut self, rhs: &NonZero<SecretScalar<E>>)
fn add_assign(&mut self, rhs: &NonZero<SecretScalar<E>>)
+=
operation. Read moreSource§impl<E: Curve> AddAssign<&Scalar<E>> for Scalar<E>
impl<E: Curve> AddAssign<&Scalar<E>> for Scalar<E>
Source§fn add_assign(&mut self, rhs: &Scalar<E>)
fn add_assign(&mut self, rhs: &Scalar<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> AddAssign<NonZero<SecretScalar<E>>> for Scalar<E>
impl<E: Curve> AddAssign<NonZero<SecretScalar<E>>> for Scalar<E>
Source§fn add_assign(&mut self, rhs: NonZero<SecretScalar<E>>)
fn add_assign(&mut self, rhs: NonZero<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> AddAssign for Scalar<E>
impl<E: Curve> AddAssign for Scalar<E>
Source§fn add_assign(&mut self, rhs: Scalar<E>)
fn add_assign(&mut self, rhs: Scalar<E>)
+=
operation. Read moreSource§impl<E: Curve> ConditionallySelectable for Scalar<E>
impl<E: Curve> ConditionallySelectable for Scalar<E>
Source§fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self
Source§fn conditional_assign(&mut self, other: &Self, choice: Choice)
fn conditional_assign(&mut self, other: &Self, choice: Choice)
Source§fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)
fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)
self
and other
if choice == 1
; otherwise,
reassign both unto themselves. Read moreSource§impl<E: Curve> ConstantTimeEq for Scalar<E>
impl<E: Curve> ConstantTimeEq for Scalar<E>
Source§impl<'de, E: Curve> Deserialize<'de> for Scalar<E>
Available on crate feature serde
only.
impl<'de, E: Curve> Deserialize<'de> for Scalar<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, Scalar<E>> for Compact
Available on crate feature serde
only.
impl<'de, E: Curve> DeserializeAs<'de, Scalar<E>> for Compact
serde
only.Source§fn deserialize_as<D>(deserializer: D) -> Result<Scalar<E>, D::Error>where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<Scalar<E>, D::Error>where
D: Deserializer<'de>,
Source§impl<E: Curve> Digestable for Scalar<E>
Available on crate feature udigest
only.
impl<E: Curve> Digestable for Scalar<E>
udigest
only.Source§fn unambiguously_encode<B>(&self, encoder: EncodeValue<'_, B>)where
B: Buffer,
fn unambiguously_encode<B>(&self, encoder: EncodeValue<'_, B>)where
B: Buffer,
Source§impl<E: Curve> MulAssign<&NonZero<SecretScalar<E>>> for Scalar<E>
impl<E: Curve> MulAssign<&NonZero<SecretScalar<E>>> for Scalar<E>
Source§fn mul_assign(&mut self, rhs: &NonZero<SecretScalar<E>>)
fn mul_assign(&mut self, rhs: &NonZero<SecretScalar<E>>)
*=
operation. Read moreSource§impl<E: Curve> MulAssign<&Scalar<E>> for Point<E>
impl<E: Curve> MulAssign<&Scalar<E>> for Point<E>
Source§fn mul_assign(&mut self, rhs: &Scalar<E>)
fn mul_assign(&mut self, rhs: &Scalar<E>)
*=
operation. Read moreSource§impl<E: Curve> MulAssign<&Scalar<E>> for Scalar<E>
impl<E: Curve> MulAssign<&Scalar<E>> for Scalar<E>
Source§fn mul_assign(&mut self, rhs: &Scalar<E>)
fn mul_assign(&mut self, rhs: &Scalar<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<NonZero<SecretScalar<E>>> for Scalar<E>
impl<E: Curve> MulAssign<NonZero<SecretScalar<E>>> for Scalar<E>
Source§fn mul_assign(&mut self, rhs: NonZero<SecretScalar<E>>)
fn mul_assign(&mut self, rhs: NonZero<SecretScalar<E>>)
*=
operation. Read moreSource§impl<E: Curve> MulAssign<Scalar<E>> for Point<E>
impl<E: Curve> MulAssign<Scalar<E>> for Point<E>
Source§fn mul_assign(&mut self, rhs: Scalar<E>)
fn mul_assign(&mut self, rhs: Scalar<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 for Scalar<E>
impl<E: Curve> MulAssign for Scalar<E>
Source§fn mul_assign(&mut self, rhs: Scalar<E>)
fn mul_assign(&mut self, rhs: Scalar<E>)
*=
operation. Read moreSource§impl<E: Curve> Ord for Scalar<E>
impl<E: Curve> Ord for Scalar<E>
Source§impl<E: Curve> PartialOrd<NonZero<Scalar<E>>> for Scalar<E>
impl<E: Curve> PartialOrd<NonZero<Scalar<E>>> for Scalar<E>
Source§impl<E: Curve> PartialOrd for Scalar<E>
impl<E: Curve> PartialOrd for Scalar<E>
Source§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, const N: usize> Reduce<N> for Scalar<E>
impl<E: Curve, const N: usize> Reduce<N> for Scalar<E>
Source§fn from_be_array_mod_order(bytes: &[u8; N]) -> Self
fn from_be_array_mod_order(bytes: &[u8; N]) -> Self
bytes
as big-endian encoding of an integer, returns this
integer modulo curve (prime) orderSource§fn from_le_array_mod_order(bytes: &[u8; N]) -> Self
fn from_le_array_mod_order(bytes: &[u8; N]) -> Self
bytes
as little-endian encoding of an integer, returns this
integer modulo curve (prime) orderSource§impl<E: Curve> SerializeAs<Scalar<E>> for Compact
Available on crate feature serde
only.
impl<E: Curve> SerializeAs<Scalar<E>> for Compact
serde
only.Source§fn serialize_as<S>(source: &Scalar<E>, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize_as<S>(source: &Scalar<E>, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
Source§impl<E: Curve> SubAssign<&NonZero<SecretScalar<E>>> for Scalar<E>
impl<E: Curve> SubAssign<&NonZero<SecretScalar<E>>> for Scalar<E>
Source§fn sub_assign(&mut self, rhs: &NonZero<SecretScalar<E>>)
fn sub_assign(&mut self, rhs: &NonZero<SecretScalar<E>>)
-=
operation. Read moreSource§impl<E: Curve> SubAssign<&Scalar<E>> for Scalar<E>
impl<E: Curve> SubAssign<&Scalar<E>> for Scalar<E>
Source§fn sub_assign(&mut self, rhs: &Scalar<E>)
fn sub_assign(&mut self, rhs: &Scalar<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<E: Curve> SubAssign<NonZero<SecretScalar<E>>> for Scalar<E>
impl<E: Curve> SubAssign<NonZero<SecretScalar<E>>> for Scalar<E>
Source§fn sub_assign(&mut self, rhs: NonZero<SecretScalar<E>>)
fn sub_assign(&mut self, rhs: NonZero<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<E: Curve> SubAssign for Scalar<E>
impl<E: Curve> SubAssign for Scalar<E>
Source§fn sub_assign(&mut self, rhs: Scalar<E>)
fn sub_assign(&mut self, rhs: Scalar<E>)
-=
operation. Read moreSource§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<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.