cupcake/integer_arith/
mod.rs

1// Copyright (c) Facebook, Inc. and its affiliates.
2//
3// This source code is licensed under the MIT license found in the
4// LICENSE file in the root directory of this source tree.
5pub mod scalar;
6pub mod butterfly;
7pub mod util;
8
9#[cfg(feature = "bigint")]
10pub mod bigint;
11
12/// The trait for utility functions related to scalar-like types.
13pub trait ArithUtils<T> {
14
15    /// Construct a new "modulus", which is a u64 plus information needed for fast modular reduction.
16    fn new_modulus(a: u64) -> T;
17
18    fn modulus(a: &T, q: &T) -> T;
19
20    fn double(a: &T) -> T;
21
22    // sample a value in [0, bound-1]
23    fn sample_blw(bound: &T) -> T;
24
25    fn sample_below_from_rng(bound: &T, rng: &mut dyn Rng) -> T;
26
27    fn one() -> T {
28        Self::from_u32_raw(1u32)
29    }
30
31    fn zero() -> T {
32        Self::from_u32_raw(0u32)
33    }
34
35    fn add_mod(a: &T, b: &T, q: &T) -> T;
36    fn sub_mod(a: &T, b: &T, q: &T) -> T;
37    fn mul_mod(a: &T, b: &T, q: &T) -> T;
38    fn inv_mod(a: &T, q: &T) -> T;
39
40    fn from_u32(a: u32, q: &T) -> T;
41
42    fn pow_mod(a: &T, b: &T, c: &T) -> T;
43
44    fn add(a: &T, b: &T) -> T;
45
46    fn sub(a: &T, b: &T) -> T;
47
48    fn div(a: &T, b: &T) -> T;
49
50    fn mul(a: &T, b: &T) -> T;
51
52    // conversion
53    fn from_u32_raw(a: u32) -> T;
54    fn from_u64_raw(a: u64) -> T;
55    fn to_u64(a: &T) -> u64;
56}
57
58pub trait ArithOperators{
59    fn add_u64(&mut self, a: u64);
60
61    fn sub_u64(&mut self, a: u64);
62
63    fn rep(&self) -> u64;
64}
65
66pub trait SuperTrait<T>: ArithOperators + ArithUtils<T> + Clone + From<u64> + From<u32> + PartialEq{}
67
68pub trait Rng: rand::CryptoRng + rand::RngCore {}