pub struct Scalar(/* private fields */);
Expand description
P-256 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
impl Scalar
Sourcepub fn new(data: [u8; 32]) -> Result<Self>
pub fn new(data: [u8; 32]) -> 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).
Sourcepub fn from_secret_buffer(
buffer: SecretBuffer<P256_SCALAR_SIZE>,
) -> Result<Self>
pub fn from_secret_buffer( buffer: SecretBuffer<P256_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.
Sourcepub fn as_secret_buffer(&self) -> &SecretBuffer<P256_SCALAR_SIZE>
pub fn as_secret_buffer(&self) -> &SecretBuffer<P256_SCALAR_SIZE>
Access the underlying SecretBuffer containing the scalar value
Sourcepub fn serialize(&self) -> [u8; 32]
pub fn serialize(&self) -> [u8; 32]
Serialize the scalar to a byte array
Returns the scalar in big-endian byte representation. The output is suitable for storage or transmission.
Sourcepub fn deserialize(bytes: &[u8]) -> Result<Self>
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-256 operations.
Sourcepub fn is_zero(&self) -> bool
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).
Sourcepub fn sub_mod_n(&self, other: &Self) -> Result<Self>
pub fn sub_mod_n(&self, other: &Self) -> Result<Self>
Subtract two scalars modulo the curve order n
Sourcepub fn mul_mod_n(&self, other: &Self) -> Result<Self>
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.