Scalar

Struct Scalar 

Source
pub struct Scalar(/* private fields */);
Expand description

P-521 scalar value for use in elliptic curve operations. Represents integers modulo the curve order n. Used for private keys and scalar multiplication. Automatically zeroized on drop for security.

Implementations§

Source§

impl Scalar

Source

pub fn new(data: [u8; 66]) -> Result<Self>

Create a scalar from raw bytes with modular reduction. Ensures the scalar is in the valid range [1, n-1] where n is the curve order. Performs modular reduction if the input is >= n. Returns an error if the result would be zero (invalid for cryptographic use).

Source

pub fn from_secret_buffer( buffer: SecretBuffer<P521_SCALAR_SIZE>, ) -> Result<Self>

Create a scalar from an existing SecretBuffer. Performs the same validation and reduction as new() but starts from a SecretBuffer instead of a raw byte array.

Source

pub fn as_secret_buffer(&self) -> &SecretBuffer<P521_SCALAR_SIZE>

Access the underlying SecretBuffer containing the scalar value

Source

pub fn serialize(&self) -> [u8; 66]

Serialize the scalar to a byte array. Returns the scalar in big-endian byte representation. The output is suitable for storage or transmission.

Source

pub fn deserialize(bytes: &[u8]) -> Result<Self>

Deserialize a scalar from bytes with validation. Parses bytes as a big-endian scalar value and ensures it’s in the valid range for P-521 operations.

Source

pub fn is_zero(&self) -> bool

Check if the scalar represents zero. Constant-time check to determine if the scalar is the additive identity (which is invalid for most cryptographic operations).

Source

pub fn add_mod_n(&self, other: &Self) -> Result<Self>

Add two scalars modulo the curve order n

Source

pub fn sub_mod_n(&self, other: &Self) -> Result<Self>

Subtract two scalars modulo the curve order n

Source

pub fn mul_mod_n(&self, other: &Self) -> Result<Self>

Multiply two scalars modulo the curve order n. Uses constant-time double-and-add algorithm for correctness and security. Processes bits from MSB to LSB to ensure correct powers of 2.

Source

pub fn inv_mod_n(&self) -> Result<Self>

Compute multiplicative inverse modulo n using Fermat’s little theorem a^(-1) ≡ a^(n-2) (mod n). Left-to-right binary exponentiation.

Source

pub fn negate(&self) -> Self

Compute the additive inverse (negation) modulo n Returns -self mod n, which is equivalent to n - self when self != 0 Returns 0 when self is 0

Trait Implementations§

Source§

impl Clone for Scalar

Source§

fn clone(&self) -> Scalar

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Scalar

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Scalar

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Zeroize for Scalar

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.

Auto Trait Implementations§

§

impl Freeze for Scalar

§

impl RefUnwindSafe for Scalar

§

impl Send for Scalar

§

impl Sync for Scalar

§

impl Unpin for Scalar

§

impl UnwindSafe for Scalar

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V