Int

Struct Int 

Source
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: ByteOrder

endianness which will be used on input and output

Implementations§

Source§

impl Int

Source

pub fn init64(self, v: i64, m: BigInt) -> Self

[init64()] creates an Int with an i64 value and BigInt modulus.

Source

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.

Source

pub fn new_int(v: BigInt, m: BigInt) -> Int

[new_int()] creates a new Int with a given BigInt and a BigInt modulus.

Source

pub fn new_int64(v: i64, m: BigInt) -> Int

[new_int64()] creates a new Int with a given i64 value and BigInt modulus.

Source

pub fn new_int_bytes(a: &[u8], m: &BigInt, byte_order: ByteOrder) -> Int

[new_int_bytes()] creates a new Int with a given slice of bytes and a BigInt modulus.

Source

pub fn new_int_string(n: String, d: String, base: i32, m: &BigInt) -> Int

[new_int_string()] creates a new Int with a given String and a BigInt modulus. The value is set to a rational fraction n/d in a given base.

Source

pub fn equal(&self, s2: &Self) -> bool

[equal()] returns true if the two ints are equal

Source

pub fn cmpr(&self, s2: &Self) -> Ordering

[cmpr()] compares two ints for equality or inequality

Source

pub fn init_bytes(self, a: &[u8], m: &BigInt, byte_order: ByteOrder) -> Self

[init_bytes()] init the Int to a number represented in a big-endian byte string.

Source

pub fn set_string( self, n: String, d: String, base: i32, ) -> Result<Self, IntError>

[set_string()] sets the Int to a rational fraction n/d represented by a pair of strings. If d == "", then the denominator is taken to be 1. Returns the Int on success or an Error if the string failed to parse

Source§

impl Int

Source

pub fn nonzero(&self) -> bool

[nonzero()] returns true if the integer value is nonzero.

Source

pub fn int64(&self) -> i64

[int64()] returns the i64 representation of the value. If the value is not representable in an i64 the result is undefined.

Source

pub fn set_uint64(&self, v: u64) -> Self

[set_uint64()] sets the Int to an arbitrary u64 value. The modulus must already be initialized.

Source

pub fn uint64(&self) -> u64

[uint64()] returns the u64 representation of the value. If the value is not representable in an u64 the result is undefined.

Source

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).

Source

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).

Source

pub fn sqrt(&mut self, a_s: &Self) -> Result<(), IntError>

[sqrt()] computes some square root of a mod m of ONE exists. Assumes the modulus m is an odd prime. Returns true on success, false if input a is not a square.

Source

pub fn big_endian(&self, min: usize, max: usize) -> Result<Vec<u8>, IntError>

[big_endian()] encodes the value of this Int into a big-endian byte-slice at least min bytes but no more than max bytes long. Returns an Error if max != 0 and the Int cannot be represented in max bytes.

Trait Implementations§

Source§

impl<'a, 'b> Add<&'a Int> for &'b Int

Source§

type Output = Int

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Int) -> Self::Output

Performs the + operation. Read more
Source§

impl<'a> Add<&'a Int> for Int

Source§

type Output = Int

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Int) -> Self::Output

Performs the + operation. Read more
Source§

impl<'a> Add<Int> for &'a Int

Source§

type Output = Int

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Int) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for Int

Source§

type Output = Int

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Int) -> Self::Output

Performs the + operation. Read more
Source§

impl BinaryMarshaler for Int

Source§

fn marshal_binary(&self) -> Result<Vec<u8>, MarshallingError>

[marshal_binary()] encodes the value of this Int into a byte-slice exactly [self.marshal_size()] bytes long. It uses i’s ByteOrder to determine which byte order to output.

Source§

impl BinaryUnmarshaler for Int

Source§

fn unmarshal_binary(&mut self, data: &[u8]) -> Result<(), MarshallingError>

[unmarshal_binary()] tries to decode a Int from a byte-slice buffer. Returns an Error if the buffer is not exactly [self.marshal_size()] bytes long or if the contents of the buffer represents an out-of-range integer.

Source§

impl Clone for Int

Source§

fn clone(&self) -> Int

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 Int

Source§

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

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

impl Default for Int

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Int

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Int

Source§

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

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

impl LowerHex for Int

Source§

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

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

impl Marshaling for Int

Source§

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>

Encode the contents of this object and write it to an io::Write.
Source§

fn unmarshal_from(&mut self, r: &mut impl Read) -> Result<(), MarshallingError>

Decode the content of this object by reading from a 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.
Source§

fn unmarshal_from_random(&mut self, r: &mut (impl Read + Stream))

Source§

fn marshal_id(&self) -> [u8; 8]

[marshal_id()] returns the type tag used in encoding/decoding
Source§

impl<'a, 'b> Mul<&'a Int> for &'b Int

Source§

type Output = Int

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Int) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a> Mul<&'a Int> for Int

Source§

type Output = Int

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Int) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a> Mul<Int> for &'a Int

Source§

type Output = Int

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Int) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for Int

Source§

type Output = Int

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Int) -> Self::Output

Performs the * operation. Read more
Source§

impl Ord for Int

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Int

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Int

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Scalar for Int

Source§

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

[set_int64()] sets the Int to an arbitrary 64-bit “small integer” value. The modulus must already be initialized.

Source§

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

[sub()] sets the target to a - b mod m. Target receives a’s modulus.

Source§

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

[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

[one()] sets the Int to the value 1. The modulus must already be initialized.

Source§

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.

Source§

fn inv(self, a: &Self) -> Self

[inv()] sets the target to the modular inverse of a with respect to modulus m.

Source§

fn neg(self, a: &Self) -> Self

[neg()] sets the target to -a mod m.

Source§

impl Serialize for Int

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl UpperHex for Int

Source§

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

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

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> 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,