1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
    Copyright Michael Lodder. All Rights Reserved.
    SPDX-License-Identifier: Apache-2.0
*/
use crate::BigNumber;

use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

/// Represents a cyclic group where all operations are reduced by a modulus.
/// Purely a convenience struct to avoid having to call mod{ops}
pub struct Group {
    /// The upper limit that all values in the group are to be reduced
    pub modulus: BigNumber,
}

macro_rules! binops_group {
    ($ops:ident, $func:ident, $opr:ident) => {
        impl<'a, 'b, 'c> $ops<(&'a BigNumber, &'b BigNumber)> for &'c Group {
            type Output = BigNumber;

            fn $func(self, pair: (&'a BigNumber, &'b BigNumber)) -> Self::Output {
                pair.0.$opr(pair.1, &self.modulus)
            }
        }

        impl<'a, 'c> $ops<(&'a BigNumber, BigNumber)> for &'c Group {
            type Output = BigNumber;

            fn $func(self, pair: (&'a BigNumber, BigNumber)) -> Self::Output {
                self + (pair.0, &pair.1)
            }
        }

        impl<'b, 'c> $ops<(BigNumber, &'b BigNumber)> for &'c Group {
            type Output = BigNumber;

            fn $func(self, pair: (BigNumber, &'b BigNumber)) -> Self::Output {
                self + (&pair.0, pair.1)
            }
        }

        impl<'c> $ops<(BigNumber, BigNumber)> for &'c Group {
            type Output = BigNumber;

            fn $func(self, pair: (BigNumber, BigNumber)) -> Self::Output {
                self + (&pair.0, &pair.1)
            }
        }

        impl<'a, 'b> $ops<(&'a BigNumber, &'b BigNumber)> for Group {
            type Output = BigNumber;

            fn $func(self, pair: (&'a BigNumber, &'b BigNumber)) -> Self::Output {
                &self + pair
            }
        }

        impl<'a> $ops<(&'a BigNumber, BigNumber)> for Group {
            type Output = BigNumber;

            fn $func(self, pair: (&'a BigNumber, BigNumber)) -> Self::Output {
                &self + (pair.0, &pair.1)
            }
        }

        impl<'b> $ops<(BigNumber, &'b BigNumber)> for Group {
            type Output = BigNumber;

            fn $func(self, pair: (BigNumber, &'b BigNumber)) -> Self::Output {
                &self + (&pair.0, pair.1)
            }
        }

        impl $ops<(BigNumber, BigNumber)> for Group {
            type Output = BigNumber;

            fn $func(self, pair: (BigNumber, BigNumber)) -> Self::Output {
                &self + (&pair.0, &pair.1)
            }
        }
    };
}
macro_rules! binops_group_assign {
    ($ops:ident, $func:ident, $opr:ident) => {
        impl<'a, 'b, 'c> $ops<(&'a mut BigNumber, &'b BigNumber)> for &'c Group {
            fn $func(&mut self, pair: (&'a mut BigNumber, &'b BigNumber)) {
                *pair.0 = pair.0.$opr(pair.1, &self.modulus);
            }
        }

        impl<'a, 'c> $ops<(&'a mut BigNumber, BigNumber)> for &'c Group {
            fn $func(&mut self, pair: (&'a mut BigNumber, BigNumber)) {
                *self += (pair.0, &pair.1)
            }
        }

        impl<'a, 'b> $ops<(&'a mut BigNumber, &'b BigNumber)> for Group {
            fn $func(&mut self, pair: (&'a mut BigNumber, &'b BigNumber)) {
                *pair.0 = pair.0.$opr(pair.1, &self.modulus);
            }
        }

        impl<'a> $ops<(&'a mut BigNumber, BigNumber)> for Group {
            fn $func(&mut self, pair: (&'a mut BigNumber, BigNumber)) {
                *self += (pair.0, &pair.1)
            }
        }
    };
}

binops_group!(Add, add, modadd);
binops_group!(Sub, sub, modsub);
binops_group!(Mul, mul, modmul);
binops_group!(Div, div, moddiv);
binops_group_assign!(AddAssign, add_assign, modadd);
binops_group_assign!(SubAssign, sub_assign, modsub);
binops_group_assign!(MulAssign, mul_assign, modmul);
binops_group_assign!(DivAssign, div_assign, moddiv);

impl Group {
    /// Compute -rhs mod self
    pub fn neg(&self, rhs: &BigNumber) -> BigNumber {
        rhs.modneg(&self.modulus)
    }

    /// Compute the sum of the the bignumbers in the group
    pub fn sum<I>(&self, nums: I) -> BigNumber
    where
        I: AsRef<[BigNumber]>,
    {
        let mut r = BigNumber::zero();
        for a in nums.as_ref() {
            r = r.modadd(a, &self.modulus);
        }
        r
    }

    /// Compute the product of the the bignumbers in the group
    pub fn product<I>(&self, nums: I) -> BigNumber
    where
        I: AsRef<[BigNumber]>,
    {
        let mut r = BigNumber::zero();
        for a in nums.as_ref() {
            r = r.modmul(a, &self.modulus);
        }
        r
    }
}