pub struct Int {
pub bo: ByteOrder,
/* private fields */
}Expand description
Int is a generic implementation of finite field arithmetic
on integer finite fields with a given constant modulus,
built using num_bigint_dig crate.
The Scalar trait is implemented for Int,
and hence serves as a basic implementation of Scalar,
e.g., representing discrete-log exponents of Schnorr groups
or scalar multipliers for elliptic curves.
Int offers an API similar to and compatible with BigInt,
but “carries around” the relevant modulus
and automatically normalizes the value to that modulus
after all arithmetic operations, simplifying modular arithmetic.
Binary operations assume that the source(s)
have the same modulus, but do not check this assumption.
Unary and binary arithmetic operations may be performed on uninitialized
target objects, and receive the modulus of the first operand.
For efficiency the modulus field m is a pointer,
whose target is assumed never to change.
Fields§
§bo: ByteOrderendianness which will be used on input and output
Implementations§
Source§impl Int
impl Int
Sourcepub fn little_endian(&self, min: usize, max: usize) -> Result<Vec<u8>, IntError>
pub fn little_endian(&self, min: usize, max: usize) -> Result<Vec<u8>, IntError>
[little_endian()] encodes the value of this Int into a little-endian byte-slice
at least min bytes but no more than max bytes long.
Panics if max != 0 and the Int cannot be represented in max bytes.
Sourcepub fn cmpr(&self, s2: &Self) -> Ordering
pub fn cmpr(&self, s2: &Self) -> Ordering
[cmpr()] compares two ints for equality or inequality
Source§impl Int
impl Int
Sourcepub fn set_uint64(&self, v: u64) -> Self
pub fn set_uint64(&self, v: u64) -> Self
[set_uint64()] sets the Int to an arbitrary u64 value.
The modulus must already be initialized.
Sourcepub fn exp(self, a: &Self, e: &BigInt) -> Self
pub fn exp(self, a: &Self, e: &BigInt) -> Self
[exp()] sets the target to a^e mod m,
where e is an arbitrary BigInt exponent (not necessarily 0 <= e < m).
Sourcepub fn jacobi(&self, a_s: &Self) -> Self
pub fn jacobi(&self, a_s: &Self) -> Self
jacobi() computes the Jacobi symbol of (a/m), which indicates whether a is
zero (0), a positive square in m (1), or a non-square in m (-1).
Trait Implementations§
Source§impl BinaryMarshaler for Int
impl BinaryMarshaler for Int
Source§fn marshal_binary(&self) -> Result<Vec<u8>, MarshallingError>
fn marshal_binary(&self) -> Result<Vec<u8>, MarshallingError>
Source§impl BinaryUnmarshaler for Int
impl BinaryUnmarshaler for Int
Source§fn unmarshal_binary(&mut self, data: &[u8]) -> Result<(), MarshallingError>
fn unmarshal_binary(&mut self, data: &[u8]) -> Result<(), MarshallingError>
Source§impl<'de> Deserialize<'de> for Int
impl<'de> Deserialize<'de> for Int
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 Marshaling for Int
impl Marshaling for Int
Source§fn marshal_size(&self) -> usize
fn marshal_size(&self) -> usize
[marshal_size()] returns the length in bytes of encoded integers with modulus m.
The length of encoded ints depends only on the size of the modulus,
and not on the the value of the encoded integer,
making the encoding is fixed-length for simplicity and security.
Source§fn marshal_to(&self, w: &mut impl Write) -> Result<(), MarshallingError>
fn marshal_to(&self, w: &mut impl Write) -> Result<(), MarshallingError>
io::Write.Source§fn unmarshal_from(&mut self, r: &mut impl Read) -> Result<(), MarshallingError>
fn unmarshal_from(&mut self, r: &mut impl Read) -> Result<(), MarshallingError>
io::Read.
If r is an [XOF], it uses r to pick a valid object pseudo-randomly,
which may entail reading more than len bytes due to retries.fn unmarshal_from_random(&mut self, r: &mut (impl Read + Stream))
Source§fn marshal_id(&self) -> [u8; 8]
fn marshal_id(&self) -> [u8; 8]
marshal_id()] returns the type tag used in encoding/decodingSource§impl Ord for Int
impl Ord for Int
Source§impl PartialOrd for Int
impl PartialOrd for Int
Source§impl Scalar for Int
impl Scalar for Int
Source§fn set(self, a: &Self) -> Self
fn set(self, a: &Self) -> Self
[set()] sets both value and modulus to be equal to another Int.
Since this method copies the modulus as well,
Source§fn set_int64(self, v: i64) -> Self
fn set_int64(self, v: i64) -> Self
[set_int64()] sets the Int to an arbitrary 64-bit “small integer” value.
The modulus must already be initialized.
Source§fn zero(self) -> Self
fn zero(self) -> Self
[zero()] set the Int to the value 0. The modulus must already be initialized.
Source§fn sub(self, a: &Self, b: &Self) -> Self
fn sub(self, a: &Self, b: &Self) -> Self
[sub()] sets the target to a - b mod m.
Target receives a’s modulus.
Source§fn pick(self, rand: &mut impl Stream) -> Self
fn pick(self, rand: &mut impl Stream) -> Self
[pick()] a pseudo-random integer modulo m
using bits from the given stream cipher.
Source§fn set_bytes(self, a: &[u8]) -> Self
fn set_bytes(self, a: &[u8]) -> Self
[set_bytes()] set the value value to a number represented
by a byte string.
Endianness depends on the endianess set in i.
Source§fn one(self) -> Self
fn one(self) -> Self
[one()] sets the Int to the value 1. The modulus must already be initialized.
Source§fn div(self, a: &Self, b: &Self) -> Self
fn div(self, a: &Self, b: &Self) -> Self
[div()] sets the target to a * b^-1 mod m, where b^-1 is the modular inverse of b.
impl Eq for Int
Auto Trait Implementations§
impl Freeze for Int
impl RefUnwindSafe for Int
impl Send for Int
impl Sync for Int
impl Unpin for Int
impl UnwindSafe for Int
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more