clacc/gmp.rs
1//! Module for implementations using [`::gmp`].
2use gmp::mpz::Mpz;
3
4#[cfg_attr(docsrs, doc(cfg(feature = "gmp")))]
5impl crate::BigInt for Mpz {
6
7 /// ```
8 /// use gmp::mpz::Mpz;
9 /// let x: Mpz = <Mpz as clacc::BigInt>::from_i64(256);
10 /// let y: Mpz = 256.into();
11 /// assert_eq!(x, y);
12 /// ```
13 fn from_i64(v: i64) -> Self {
14 return v.into()
15 }
16
17 /// ```
18 /// use gmp::mpz::Mpz;
19 /// let x: Mpz = <Mpz as clacc::BigInt>::from_bytes_be(
20 /// vec![0x01, 0x00].as_slice(),
21 /// );
22 /// let y: Mpz = 256.into();
23 /// assert_eq!(x, y);
24 /// ```
25 fn from_bytes_be(bytes: &[u8]) -> Self {
26 bytes.into()
27 }
28
29 /// ```
30 /// use gmp::mpz::Mpz;
31 /// let x = vec![0x01, 0x00];
32 /// let y: Mpz = 256.into();
33 /// assert_eq!(x, <Mpz as clacc::BigInt>::to_bytes_be(&y));
34 /// ```
35 fn to_bytes_be(&self) -> Vec<u8> {
36 self.into()
37 }
38
39 /// ```
40 /// use gmp::mpz::Mpz;
41 /// let x: Mpz = 240.into();
42 /// let y: Mpz = 46.into();
43 /// let (g, a, b) = <Mpz as clacc::BigInt>::gcdext(&x, &y);
44 /// assert_eq!(g, 2.into());
45 /// assert_eq!(a, (-9).into());
46 /// assert_eq!(b, 47.into());
47 /// ```
48 fn gcdext(&self, y: &Self) -> (Self, Self, Self) {
49 let (g, a, b) = Mpz::gcdext(self, y);
50 (g.into(), a.into(), b.into())
51 }
52
53 /// ```
54 /// use gmp::mpz::Mpz;
55 /// let b: Mpz = 5.into();
56 /// let e: Mpz = 3.into();
57 /// let m: Mpz = 13.into();
58 /// let c = <Mpz as clacc::BigInt>::powm(&b, &e, &m);
59 /// assert_eq!(c, 8.into());
60 /// ```
61 fn powm(&self, e: &Self, m: &Self) -> Self {
62 Mpz::powm(self, e, m)
63 }
64
65 /// ```
66 /// use gmp::mpz::Mpz;
67 /// let a: Mpz = 3.into();
68 /// assert_eq!(<Mpz as clacc::BigInt>::size_in_bits(&a), 2);
69 /// let b: Mpz = 256.into();
70 /// assert_eq!(<Mpz as clacc::BigInt>::size_in_bits(&b), 9);
71 /// ```
72 fn size_in_bits(&self) -> usize {
73 Mpz::size_in_base(self, 2)
74 }
75}