|
Crypto++ 8.9
Free C++ class library of cryptographic schemes
|
Ring of congruence classes modulo n. More...
#include <modarith.h>
Inheritance diagram for ModularArithmetic:Public Types | |
| typedef int | RandomizationParameter |
| typedef Integer | Element |
Public Types inherited from AbstractRing< Integer > | |
| typedef Integer | Element |
Public Types inherited from AbstractGroup< T > | |
| typedef T | Element |
Public Member Functions | |
| ModularArithmetic (const Integer &modulus=Integer::One()) | |
| Construct a ModularArithmetic. | |
| ModularArithmetic (const ModularArithmetic &ma) | |
| Copy construct a ModularArithmetic. | |
| ModularArithmetic & | operator= (const ModularArithmetic &ma) |
| Assign a ModularArithmetic. | |
| ModularArithmetic (BufferedTransformation &bt) | |
| Construct a ModularArithmetic. | |
| virtual ModularArithmetic * | Clone () const |
| Clone a ModularArithmetic. | |
| void | DEREncode (BufferedTransformation &bt) const |
| Encodes in DER format. | |
| void | DEREncodeElement (BufferedTransformation &out, const Element &a) const |
| Encodes element in DER format. | |
| void | BERDecodeElement (BufferedTransformation &in, Element &a) const |
| Decodes element in DER format. | |
| const Integer & | GetModulus () const |
| Retrieves the modulus. | |
| void | SetModulus (const Integer &newModulus) |
| Sets the modulus. | |
| virtual bool | IsMontgomeryRepresentation () const |
| Retrieves the representation. | |
| virtual Integer | ConvertIn (const Integer &a) const |
| Reduces an element in the congruence class. | |
| virtual Integer | ConvertOut (const Integer &a) const |
| Reduces an element in the congruence class. | |
| const Integer & | Half (const Integer &a) const |
| Divides an element by 2. | |
| bool | Equal (const Integer &a, const Integer &b) const |
| Compare two elements for equality. | |
| const Integer & | Identity () const |
| Provides the Identity element. | |
| const Integer & | Add (const Integer &a, const Integer &b) const |
| Adds elements in the ring. | |
| Integer & | Accumulate (Integer &a, const Integer &b) const |
| TODO. | |
| const Integer & | Inverse (const Integer &a) const |
| Inverts the element in the ring. | |
| const Integer & | Subtract (const Integer &a, const Integer &b) const |
| Subtracts elements in the ring. | |
| Integer & | Reduce (Integer &a, const Integer &b) const |
| TODO. | |
| const Integer & | Double (const Integer &a) const |
| Doubles an element in the ring. | |
| const Integer & | MultiplicativeIdentity () const |
| Retrieves the multiplicative identity. | |
| const Integer & | Multiply (const Integer &a, const Integer &b) const |
| Multiplies elements in the ring. | |
| const Integer & | Square (const Integer &a) const |
| Square an element in the ring. | |
| bool | IsUnit (const Integer &a) const |
| Determines whether an element is a unit in the ring. | |
| const Integer & | MultiplicativeInverse (const Integer &a) const |
| Calculate the multiplicative inverse of an element in the ring. | |
| const Integer & | Divide (const Integer &a, const Integer &b) const |
| Divides elements in the ring. | |
| Integer | CascadeExponentiate (const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const |
| TODO. | |
| void | SimultaneousExponentiate (Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const |
| Exponentiates a base to multiple exponents in the ring. | |
| unsigned int | MaxElementBitLength () const |
| Provides the maximum bit size of an element in the ring. | |
| unsigned int | MaxElementByteLength () const |
| Provides the maximum byte size of an element in the ring. | |
| Element | RandomElement (RandomNumberGenerator &rng, const RandomizationParameter &ignore_for_now=0) const |
| Provides a random element in the ring. | |
| bool | operator== (const ModularArithmetic &rhs) const |
| Compares two ModularArithmetic for equality. | |
Public Member Functions inherited from AbstractRing< Integer > | |
| AbstractRing () | |
| Construct an AbstractRing. | |
| AbstractRing (const AbstractRing &source) | |
| Copy construct an AbstractRing. | |
| AbstractRing & | operator= (const AbstractRing &source) |
| Assign an AbstractRing. | |
| virtual Element | Exponentiate (const Element &a, const Integer &e) const |
| Raises a base to an exponent in the group. | |
| virtual const AbstractGroup< Integer > & | MultiplicativeGroup () const |
| Retrieves the multiplicative group. | |
Public Member Functions inherited from AbstractGroup< T > | |
| virtual bool | Equal (const Element &a, const Element &b) const =0 |
| Compare two elements for equality. | |
| virtual const Element & | Add (const Element &a, const Element &b) const =0 |
| Adds elements in the group. | |
| virtual const Element & | Inverse (const Element &a) const =0 |
| Inverts the element in the group. | |
| virtual bool | InversionIsFast () const |
| Determine if inversion is fast. | |
| virtual const Element & | Double (const Element &a) const |
| Doubles an element in the group. | |
| virtual const Element & | Subtract (const Element &a, const Element &b) const |
| Subtracts elements in the group. | |
| virtual Element & | Accumulate (Element &a, const Element &b) const |
| TODO. | |
| virtual Element & | Reduce (Element &a, const Element &b) const |
| Reduces an element in the congruence class. | |
| virtual Element | ScalarMultiply (const Element &a, const Integer &e) const |
| Performs a scalar multiplication. | |
| virtual Element | CascadeScalarMultiply (const Element &x, const Integer &e1, const Element &y, const Integer &e2) const |
| TODO. | |
| virtual void | SimultaneousMultiply (Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const |
| Multiplies a base to multiple exponents in a group. | |
Static Public Attributes | |
| static const RandomizationParameter | DefaultRandomizationParameter |
Ring of congruence classes modulo n.
This implementation represents each congruence class as the smallest non-negative integer in that class.
const Element& returned by member functions are references to internal data members. Since each object may have only one such data member for holding results, you should use the class like this:
abcd = group.Add(a, group.Add(b, group.Add(c,d));
The following code will produce incorrect results:
abcd = group.Add(group.Add(a,b), group.Add(c,d));
If a ModularArithmetic() is copied or assigned the modulus is copied, but not the internal data members. The internal data members are undefined after copy or assignment.
Definition at line 43 of file modarith.h.
| typedef int ModularArithmetic::RandomizationParameter |
Definition at line 47 of file modarith.h.
| typedef Integer ModularArithmetic::Element |
Definition at line 48 of file modarith.h.
|
inlinevirtual |
Definition at line 50 of file modarith.h.
|
inline |
Construct a ModularArithmetic.
| modulus | congruence class modulus |
Definition at line 54 of file modarith.h.
|
inline |
Copy construct a ModularArithmetic.
| ma | other ModularArithmetic |
Definition at line 59 of file modarith.h.
| ModularArithmetic::ModularArithmetic | ( | BufferedTransformation & | bt | ) |
Construct a ModularArithmetic.
| bt | BER encoded ModularArithmetic |
|
inline |
Assign a ModularArithmetic.
| ma | other ModularArithmetic |
Definition at line 64 of file modarith.h.
|
inlinevirtual |
Clone a ModularArithmetic.
Clone effectively copy constructs a new ModularArithmetic. The caller is responsible for deleting the pointer returned from this method.
Reimplemented in MontgomeryRepresentation.
Definition at line 81 of file modarith.h.
| void ModularArithmetic::DEREncode | ( | BufferedTransformation & | bt | ) | const |
Encodes in DER format.
| bt | BufferedTransformation object |
| void ModularArithmetic::DEREncodeElement | ( | BufferedTransformation & | out, |
| const Element & | a | ||
| ) | const |
Encodes element in DER format.
| out | BufferedTransformation object |
| a | Element to encode |
| void ModularArithmetic::BERDecodeElement | ( | BufferedTransformation & | in, |
| Element & | a | ||
| ) | const |
Decodes element in DER format.
| in | BufferedTransformation object |
| a | Element to decode |
|
inline |
|
inline |
|
inlinevirtual |
Retrieves the representation.
Reimplemented in MontgomeryRepresentation.
Definition at line 108 of file modarith.h.
Reduces an element in the congruence class.
| a | element to convert |
ConvertIn is useful for derived classes, like MontgomeryRepresentation, which must convert between representations.
Reimplemented in MontgomeryRepresentation.
Definition at line 115 of file modarith.h.
Reduces an element in the congruence class.
| a | element to convert |
ConvertOut is useful for derived classes, like MontgomeryRepresentation, which must convert between representations.
Reimplemented in MontgomeryRepresentation.
Definition at line 123 of file modarith.h.
Divides an element by 2.
| a | element to convert |
Compare two elements for equality.
| a | first element |
| b | second element |
Equal() tests the elements for equality using a==b
Definition at line 135 of file modarith.h.
|
inlinevirtual |
Provides the Identity element.
Implements AbstractGroup< T >.
Definition at line 140 of file modarith.h.
Adds elements in the ring.
| a | first element |
| b | second element |
a and b TODO.
| a | first element |
| b | second element |
Inverts the element in the ring.
| a | first element |
Subtracts elements in the ring.
| a | first element |
| b | second element |
a and b. The element a must provide a Subtract member function. TODO.
| a | first element |
| b | second element |
Doubles an element in the ring.
| a | the element |
Double returns Add(a, a). The element a must provide an Add member function.
Definition at line 176 of file modarith.h.
|
inlinevirtual |
Retrieves the multiplicative identity.
the base class implementations returns 1.
Implements AbstractRing< Integer >.
Reimplemented in MontgomeryRepresentation.
Definition at line 182 of file modarith.h.
|
inlinevirtual |
Multiplies elements in the ring.
| a | the multiplicand |
| b | the multiplier |
Multiply returns a*b%n.
Implements AbstractRing< Integer >.
Reimplemented in MontgomeryRepresentation.
Definition at line 190 of file modarith.h.
Square an element in the ring.
| a | the element |
Square returns a*a%n. The element a must provide a Square member function.
Reimplemented from AbstractRing< Integer >.
Reimplemented in MontgomeryRepresentation.
Definition at line 197 of file modarith.h.
|
inlinevirtual |
Determines whether an element is a unit in the ring.
| a | the element |
Implements AbstractRing< Integer >.
Definition at line 203 of file modarith.h.
Calculate the multiplicative inverse of an element in the ring.
| a | the element |
MultiplicativeInverse returns a-1%n. The element a must provide a InverseMod member function.
Implements AbstractRing< Integer >.
Reimplemented in MontgomeryRepresentation.
Definition at line 210 of file modarith.h.
|
inlinevirtual |
Divides elements in the ring.
| a | the dividend |
| b | the divisor |
Divide returns a*b-1%n.
Reimplemented from AbstractRing< Integer >.
Definition at line 218 of file modarith.h.
|
virtual |
TODO.
| x | first element |
| e1 | first exponent |
| y | second element |
| e2 | second exponent |
Reimplemented from AbstractRing< Integer >.
Reimplemented in MontgomeryRepresentation.
|
virtual |
Exponentiates a base to multiple exponents in the ring.
| results | an array of Elements |
| base | the base to raise to the exponents |
| exponents | an array of exponents |
| exponentsCount | the number of exponents in the array |
SimultaneousExponentiate() raises the base to each exponent in the exponents array and stores the result at the respective position in the results array.
SimultaneousExponentiate() must be implemented in a derived class.
COUNTOF(results) == exponentsCount COUNTOF(exponents) == exponentsCount Reimplemented from AbstractRing< Integer >.
Reimplemented in MontgomeryRepresentation.
|
inline |
Provides the maximum bit size of an element in the ring.
Definition at line 243 of file modarith.h.
|
inline |
Provides the maximum byte size of an element in the ring.
Definition at line 248 of file modarith.h.
|
inline |
Provides a random element in the ring.
| rng | RandomNumberGenerator used to generate material |
| ignore_for_now | unused |
RandomElement constructs a new element in the range [0,n-1], inclusive. The element's class must provide a constructor with the signature Element(RandomNumberGenerator rng,
Element min, Element max).
Definition at line 258 of file modarith.h.
|
inline |
Compares two ModularArithmetic for equality.
| rhs | other ModularArithmetic |
The operator tests for equality using this.m_modulus == rhs.m_modulus.
Definition at line 269 of file modarith.h.
|
static |
Definition at line 272 of file modarith.h.