aboutsummaryrefslogtreecommitdiffstats
path: root/src/math/gfpmath/gfp_modulus.h
blob: fcdd13ee185ce8d72509cbcb6a91831d3b69ca9e (plain)
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
/*
* Modulus and related data for a specific implementation of GF(p)
*
* (C) 2008 Martin Doering, Christoph Ludwig, Falko Strenzke
*
* Distributed under the terms of the Botan license
*/

#ifndef BOTAN_GFP_MODULUS_H__
#define BOTAN_GFP_MODULUS_H__

#include <botan/bigint.h>

namespace Botan {

class GFpElement;

/**
* This class represents a GFpElement modulus including the modulus
* related values necessary for the montgomery multiplication.
*/
class BOTAN_DLL GFpModulus
   {
   public:

      /**
      * Construct a GF(P)-Modulus from a BigInt
      */
      GFpModulus(const BigInt& p)
         : m_p(p),
           m_p_dash(),
           m_r(),
           m_r_inv()
         {}

      // GFpModulus(const GFpModulus& other) = default;
      // GFpModulus& operator=(const GFpModulus& other) = default;

      /**
      * Tells whether the precomputations necessary for the use of the
      * montgomery multiplication have yet been established.
      * @result true if the precomputated value are already available.
      */
      bool has_precomputations() const
         {
         return(!m_p_dash.is_zero() && !m_r.is_zero() && !m_r_inv.is_zero());
         }

      /**
      * Swaps this with another GFpModulus, does not throw.
      * @param other the GFpModulus to swap *this with.
      */
      void swap(GFpModulus& other)
         {
         std::swap(m_p, other.m_p);
         std::swap(m_p_dash, other.m_p_dash);
         std::swap(m_r, other.m_r);
         std::swap(m_r_inv, other.m_r_inv);
         }

      /**
      * Tells whether the modulus of *this is equal to the argument.
      * @param mod the modulus to compare this with
      * @result true if the modulus of *this and the argument are equal.
      */
      bool p_equal_to(const BigInt& mod) const
         {
         return (m_p == mod);
         }

      /**
      * Return the modulus of this GFpModulus.
      * @result the modulus of *this.
      */
      const BigInt& get_p() const
         {
         return m_p;
         }

      /**
      * returns the montgomery multiplication related value r.
      * Warning: will be zero if precomputations have not yet been
      * performed!
      * @result r
      */
      const BigInt& get_r() const
         {
         return m_r;
         }

      /**
      * returns the montgomery multiplication related value r^{-1}.
      * Warning: will be zero if precomputations have not yet been
      * performed!
      * @result r^{-1}
      */
      const BigInt& get_r_inv() const
         {
         return m_r_inv;
         }

      /**
      * returns the montgomery multiplication related value p'.
      * Warning: will be zero if precomputations have not yet been
      * performed!
      * @result p'
      */
      const BigInt& get_p_dash() const
         {
         return m_p_dash;
         }

      void reset_values(const BigInt& new_p_dash,
                        const BigInt& new_r,
                        const BigInt& new_r_inv)
         {
         m_p_dash = new_p_dash;
         m_r = new_r;
         m_r_inv = new_r_inv;
         }

   private:
      BigInt m_p; // the modulus itself
      BigInt m_p_dash;
      BigInt m_r;
      BigInt m_r_inv;
   };

}

#endif