ff_uint/ff/
mod.rs

1//! This crate provides traits for working with finite fields.
2
3// Catch documentation errors caused by code changes.
4#![deny(rustdoc::broken_intra_doc_links)]
5#![allow(unused_imports)]
6
7pub use ff_uint_derive::*;
8
9#[cfg(feature = "rand_support")]
10use rand_core::RngCore;
11
12pub mod traits;
13
14pub use self::arith_impl::*;
15
16mod arith_impl {
17    /// Calculate a - b - borrow, returning the result and modifying
18    /// the borrow value.
19    #[inline(always)]
20    pub fn sbb(a: u64, b: u64, borrow: &mut u64) -> u64 {
21        let tmp = (1u128 << 64) + u128::from(a) - u128::from(b) - u128::from(*borrow);
22
23        *borrow = if tmp >> 64 == 0 { 1 } else { 0 };
24
25        tmp as u64
26    }
27
28    /// Calculate a + b + carry, returning the sum and modifying the
29    /// carry value.
30    #[inline(always)]
31    pub fn adc(a: u64, b: u64, carry: &mut u64) -> u64 {
32        let tmp = u128::from(a) + u128::from(b) + u128::from(*carry);
33
34        *carry = (tmp >> 64) as u64;
35
36        tmp as u64
37    }
38
39    /// Calculate a + (b * c) + carry, returning the least significant digit
40    /// and setting carry to the most significant digit.
41    #[inline(always)]
42    pub fn mac_with_carry(a: u64, b: u64, c: u64, carry: &mut u64) -> u64 {
43        let tmp = (u128::from(a)) + u128::from(b) * u128::from(c) + u128::from(*carry);
44
45        *carry = (tmp >> 64) as u64;
46
47        tmp as u64
48    }
49}