Skip to main content

ark_ff/fields/models/small_fp/
from.rs

1use crate::fields::models::small_fp::small_fp_backend::{SmallFp, SmallFpConfig};
2use crate::{BigInt, PrimeField};
3
4impl<P: SmallFpConfig> From<u128> for SmallFp<P> {
5    fn from(other: u128) -> Self {
6        let reduced = other % P::MODULUS_U128;
7        let val = P::T::try_from(reduced).ok().unwrap();
8        P::new(val)
9    }
10}
11
12impl<P: SmallFpConfig> From<i128> for SmallFp<P> {
13    fn from(other: i128) -> Self {
14        let abs = other.unsigned_abs().into();
15        if other.is_positive() {
16            abs
17        } else {
18            -abs
19        }
20    }
21}
22
23impl<P: SmallFpConfig> From<bool> for SmallFp<P> {
24    fn from(other: bool) -> Self {
25        if other {
26            P::ONE
27        } else {
28            P::ZERO
29        }
30    }
31}
32
33impl<P: SmallFpConfig> From<u64> for SmallFp<P> {
34    fn from(other: u64) -> Self {
35        Self::from(other as u128)
36    }
37}
38
39impl<P: SmallFpConfig> From<i64> for SmallFp<P> {
40    fn from(other: i64) -> Self {
41        let abs = other.unsigned_abs().into();
42        if other.is_positive() {
43            abs
44        } else {
45            -abs
46        }
47    }
48}
49
50impl<P: SmallFpConfig> From<u32> for SmallFp<P> {
51    fn from(other: u32) -> Self {
52        Self::from(other as u128)
53    }
54}
55
56impl<P: SmallFpConfig> From<i32> for SmallFp<P> {
57    fn from(other: i32) -> Self {
58        let abs = other.unsigned_abs().into();
59        if other.is_positive() {
60            abs
61        } else {
62            -abs
63        }
64    }
65}
66
67impl<P: SmallFpConfig> From<u16> for SmallFp<P> {
68    fn from(other: u16) -> Self {
69        Self::from(other as u128)
70    }
71}
72
73impl<P: SmallFpConfig> From<i16> for SmallFp<P> {
74    fn from(other: i16) -> Self {
75        let abs = other.unsigned_abs().into();
76        if other.is_positive() {
77            abs
78        } else {
79            -abs
80        }
81    }
82}
83
84impl<P: SmallFpConfig> From<u8> for SmallFp<P> {
85    fn from(other: u8) -> Self {
86        Self::from(other as u128)
87    }
88}
89
90impl<P: SmallFpConfig> From<i8> for SmallFp<P> {
91    fn from(other: i8) -> Self {
92        let abs = other.unsigned_abs().into();
93        if other.is_positive() {
94            abs
95        } else {
96            -abs
97        }
98    }
99}
100
101impl<P: SmallFpConfig> From<num_bigint::BigUint> for SmallFp<P> {
102    #[inline]
103    fn from(val: num_bigint::BigUint) -> SmallFp<P> {
104        SmallFp::from_le_bytes_mod_order(&val.to_bytes_le())
105    }
106}
107
108impl<P: SmallFpConfig> From<SmallFp<P>> for num_bigint::BigUint {
109    #[inline(always)]
110    fn from(other: SmallFp<P>) -> Self {
111        other.into_bigint().into()
112    }
113}
114
115impl<P: SmallFpConfig> From<SmallFp<P>> for BigInt<1> {
116    fn from(fp: SmallFp<P>) -> Self {
117        fp.into_bigint()
118    }
119}
120
121impl<P: SmallFpConfig> From<BigInt<1>> for SmallFp<P> {
122    fn from(int: BigInt<1>) -> Self {
123        Self::from_bigint(int).unwrap()
124    }
125}