Trait clacc::BigInt

source ·
pub trait BigInt: Clone + Sized + Send + Sync + Eq + PartialOrd + Neg + for<'a> Add<&'a Self, Output = Self> + for<'a> Sub<&'a Self, Output = Self> + for<'a> Mul<&'a Self, Output = Self> + for<'a> Div<&'a Self, Output = Self> + for<'a> Rem<&'a Self, Output = Self> {
    // Required methods
    fn from_i64(v: i64) -> Self;
    fn from_bytes_be(bytes: &[u8]) -> Self;
    fn to_bytes_be(&self) -> Vec<u8>;
    fn gcdext<'a>(&self, y: &'a Self) -> (Self, Self, Self);
    fn powm<'a>(&self, e: &'a Self, m: &Self) -> Self;
    fn size_in_bits(&self) -> usize;
}
Expand description

A trait describing an arbitrary precision integer.

Required Methods§

source

fn from_i64(v: i64) -> Self

Constructs a BigInt from an i64.

source

fn from_bytes_be(bytes: &[u8]) -> Self

Constructs a BigInt from a slice of bytes, with the most significant byte first.

source

fn to_bytes_be(&self) -> Vec<u8>

Constructs a byte vector of the BigInt with the most significant byte first.

source

fn gcdext<'a>(&self, y: &'a Self) -> (Self, Self, Self)

Returns (g, a, b) where g is the greatest common divisor of self and y satisfying g = a * self + b * y.

source

fn powm<'a>(&self, e: &'a Self, m: &Self) -> Self

Returns self^e (mod m).

source

fn size_in_bits(&self) -> usize

Returns the size of self in bits.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl BigInt for BigInt

Available on crate feature bigint only.
source§

fn from_i64(v: i64) -> Self

use num_bigint::BigInt;
let x: BigInt = <BigInt as clacc::BigInt>::from_i64(256);
let y: BigInt = 256.into();
assert_eq!(x, y);
source§

fn from_bytes_be(bytes: &[u8]) -> Self

use num_bigint::BigInt;
let x: BigInt = <BigInt as clacc::BigInt>::from_bytes_be(
    vec![0x01, 0x00].as_slice(),
);
let y: BigInt = 256.into();
assert_eq!(x, y);
source§

fn to_bytes_be(&self) -> Vec<u8>

use num_bigint::BigInt;
let x = vec![0x01, 0x00];
let y: BigInt = 256.into();
assert_eq!(x, <BigInt as clacc::BigInt>::to_bytes_be(&y));
source§

fn gcdext(&self, y: &Self) -> (Self, Self, Self)

use num_bigint::BigInt;
let x: BigInt = 240.into();
let y: BigInt = 46.into();
let (g, a, b) = <BigInt as clacc::BigInt>::gcdext(&x, &y);
assert_eq!(g, 2.into());
assert_eq!(a, (-9).into());
assert_eq!(b, 47.into());
source§

fn powm(&self, e: &Self, m: &Self) -> Self

use num_bigint::BigInt;
let b: BigInt = 5.into();
let e: BigInt = 3.into();
let m: BigInt = 13.into();
let c = <BigInt as clacc::BigInt>::powm(&b, &e, &m);
assert_eq!(c, 8.into());
use num_bigint::BigInt;
let b: BigInt = 3.into();
let e: BigInt = (-1).into();
let m: BigInt = 11.into();
let c = <BigInt as clacc::BigInt>::powm(&b, &e, &m);
assert_eq!(c, 4.into());
source§

fn size_in_bits(&self) -> usize

use num_bigint::BigInt;
let a: BigInt = 3.into();
assert_eq!(<BigInt as clacc::BigInt>::size_in_bits(&a), 2);
let b: BigInt = 256.into();
assert_eq!(<BigInt as clacc::BigInt>::size_in_bits(&b), 9);
source§

impl BigInt for Mpz

Available on crate feature gmp only.
source§

fn from_i64(v: i64) -> Self

use gmp::mpz::Mpz;
let x: Mpz = <Mpz as clacc::BigInt>::from_i64(256);
let y: Mpz = 256.into();
assert_eq!(x, y);
source§

fn from_bytes_be(bytes: &[u8]) -> Self

use gmp::mpz::Mpz;
let x: Mpz = <Mpz as clacc::BigInt>::from_bytes_be(
    vec![0x01, 0x00].as_slice(),
);
let y: Mpz = 256.into();
assert_eq!(x, y);
source§

fn to_bytes_be(&self) -> Vec<u8>

use gmp::mpz::Mpz;
let x = vec![0x01, 0x00];
let y: Mpz = 256.into();
assert_eq!(x, <Mpz as clacc::BigInt>::to_bytes_be(&y));
source§

fn gcdext(&self, y: &Self) -> (Self, Self, Self)

use gmp::mpz::Mpz;
let x: Mpz = 240.into();
let y: Mpz = 46.into();
let (g, a, b) = <Mpz as clacc::BigInt>::gcdext(&x, &y);
assert_eq!(g, 2.into());
assert_eq!(a, (-9).into());
assert_eq!(b, 47.into());
source§

fn powm(&self, e: &Self, m: &Self) -> Self

use gmp::mpz::Mpz;
let b: Mpz = 5.into();
let e: Mpz = 3.into();
let m: Mpz = 13.into();
let c = <Mpz as clacc::BigInt>::powm(&b, &e, &m);
assert_eq!(c, 8.into());
source§

fn size_in_bits(&self) -> usize

use gmp::mpz::Mpz;
let a: Mpz = 3.into();
assert_eq!(<Mpz as clacc::BigInt>::size_in_bits(&a), 2);
let b: Mpz = 256.into();
assert_eq!(<Mpz as clacc::BigInt>::size_in_bits(&b), 9);

Implementors§