Security Scol plugin
Public Types | Public Member Functions | Static Public Attributes | Protected Attributes | List of all members
ModularArithmetic Class Reference

Ring of congruence classes modulo n. More...

#include <modarith.h>

Inheritance diagram for ModularArithmetic:
AbstractRing< Integer > AbstractGroup< T > MontgomeryRepresentation

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.
 
ModularArithmeticoperator= (const ModularArithmetic &ma)
 Assign a ModularArithmetic.
 
 ModularArithmetic (BufferedTransformation &bt)
 Construct a ModularArithmetic.
 
virtual ModularArithmeticClone () 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 IntegerGetModulus () 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 IntegerHalf (const Integer &a) const
 Divides an element by 2.
 
bool Equal (const Integer &a, const Integer &b) const
 Compare two elements for equality.
 
const IntegerIdentity () const
 Provides the Identity element.
 
const IntegerAdd (const Integer &a, const Integer &b) const
 Adds elements in the ring.
 
IntegerAccumulate (Integer &a, const Integer &b) const
 TODO.
 
const IntegerInverse (const Integer &a) const
 Inverts the element in the ring.
 
const IntegerSubtract (const Integer &a, const Integer &b) const
 Subtracts elements in the ring.
 
IntegerReduce (Integer &a, const Integer &b) const
 TODO.
 
const IntegerDouble (const Integer &a) const
 Doubles an element in the ring.
 
const IntegerMultiplicativeIdentity () const
 Retrieves the multiplicative identity.
 
const IntegerMultiply (const Integer &a, const Integer &b) const
 Multiplies elements in the ring.
 
const IntegerSquare (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 IntegerMultiplicativeInverse (const Integer &a) const
 Calculate the multiplicative inverse of an element in the ring.
 
const IntegerDivide (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.
 
AbstractRingoperator= (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 Element CascadeExponentiate (const Element &x, const Integer &e1, const Element &y, const Integer &e2) const
 TODO.
 
virtual void SimultaneousExponentiate (Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
 Exponentiates a base to multiple exponents in the Ring.
 
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
 

Protected Attributes

Integer m_modulus
 
Integer m_result
 
Integer m_result1
 

Detailed Description

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.

See also
Integer on the Crypto++ wiki.

Definition at line 43 of file modarith.h.

Member Typedef Documentation

◆ Element

Definition at line 48 of file modarith.h.

◆ RandomizationParameter

typedef int ModularArithmetic::RandomizationParameter

Definition at line 47 of file modarith.h.

Constructor & Destructor Documentation

◆ ~ModularArithmetic()

virtual ModularArithmetic::~ModularArithmetic ( )
inlinevirtual

Definition at line 50 of file modarith.h.

◆ ModularArithmetic() [1/3]

ModularArithmetic::ModularArithmetic ( const Integer modulus = Integer::One())
inline

Construct a ModularArithmetic.

Parameters
moduluscongruence class modulus

Definition at line 54 of file modarith.h.

◆ ModularArithmetic() [2/3]

ModularArithmetic::ModularArithmetic ( const ModularArithmetic ma)
inline

Copy construct a ModularArithmetic.

Parameters
maother ModularArithmetic

Definition at line 59 of file modarith.h.

◆ ModularArithmetic() [3/3]

ModularArithmetic::ModularArithmetic ( BufferedTransformation bt)

Construct a ModularArithmetic.

Parameters
btBER encoded ModularArithmetic

Definition at line 4541 of file integer.cpp.

Member Function Documentation

◆ Accumulate()

Integer & ModularArithmetic::Accumulate ( Integer a,
const Integer b 
) const

TODO.

Parameters
afirst element
bsecond element
Returns
TODO

Definition at line 4601 of file integer.cpp.

◆ Add()

const Integer & ModularArithmetic::Add ( const Integer a,
const Integer b 
) const

Adds elements in the ring.

Parameters
afirst element
bsecond element
Returns
the sum of a and b

Definition at line 4581 of file integer.cpp.

◆ BERDecodeElement()

void ModularArithmetic::BERDecodeElement ( BufferedTransformation in,
Element a 
) const

Decodes element in DER format.

Parameters
inBufferedTransformation object
aElement to decode

Definition at line 4565 of file integer.cpp.

◆ CascadeExponentiate()

Integer ModularArithmetic::CascadeExponentiate ( const Integer x,
const Integer e1,
const Integer y,
const Integer e2 
) const

TODO.

Parameters
xfirst element
e1first exponent
ysecond element
e2second exponent
Returns
TODO

Definition at line 4667 of file integer.cpp.

◆ Clone()

virtual ModularArithmetic * ModularArithmetic::Clone ( ) const
inlinevirtual

Clone a ModularArithmetic.

Returns
pointer to a new 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.

◆ ConvertIn()

virtual Integer ModularArithmetic::ConvertIn ( const Integer a) const
inlinevirtual

Reduces an element in the congruence class.

Parameters
aelement to convert
Returns
the reduced element

ConvertIn is useful for derived classes, like MontgomeryRepresentation, which must convert between representations.

Reimplemented in MontgomeryRepresentation.

Definition at line 115 of file modarith.h.

◆ ConvertOut()

virtual Integer ModularArithmetic::ConvertOut ( const Integer a) const
inlinevirtual

Reduces an element in the congruence class.

Parameters
aelement to convert
Returns
the reduced element

ConvertOut is useful for derived classes, like MontgomeryRepresentation, which must convert between representations.

Reimplemented in MontgomeryRepresentation.

Definition at line 123 of file modarith.h.

◆ DEREncode()

void ModularArithmetic::DEREncode ( BufferedTransformation bt) const

Encodes in DER format.

Parameters
btBufferedTransformation object

Definition at line 4552 of file integer.cpp.

◆ DEREncodeElement()

void ModularArithmetic::DEREncodeElement ( BufferedTransformation out,
const Element a 
) const

Encodes element in DER format.

Parameters
outBufferedTransformation object
aElement to encode

Definition at line 4560 of file integer.cpp.

◆ Divide()

const Integer & ModularArithmetic::Divide ( const Integer a,
const Integer b 
) const
inlinevirtual

Divides elements in the ring.

Parameters
athe dividend
bthe divisor
Returns
the quotient

Divide returns a*b-1%n.

Reimplemented from AbstractRing< Integer >.

Definition at line 218 of file modarith.h.

◆ Double()

const Integer & ModularArithmetic::Double ( const Integer a) const
inline

Doubles an element in the ring.

Parameters
athe element
Returns
the element doubled

Double returns Add(a, a). The element a must provide an Add member function.

Definition at line 176 of file modarith.h.

◆ Equal()

bool ModularArithmetic::Equal ( const Integer a,
const Integer b 
) const
inline

Compare two elements for equality.

Parameters
afirst element
bsecond element
Returns
true if the elements are equal, false otherwise

Equal() tests the elements for equality using a==b

Definition at line 135 of file modarith.h.

◆ GetModulus()

const Integer & ModularArithmetic::GetModulus ( ) const
inline

Retrieves the modulus.

Returns
the modulus

Definition at line 99 of file modarith.h.

◆ Half()

const Integer & ModularArithmetic::Half ( const Integer a) const

Divides an element by 2.

Parameters
aelement to convert

Definition at line 4570 of file integer.cpp.

◆ Identity()

const Integer & ModularArithmetic::Identity ( ) const
inlinevirtual

Provides the Identity element.

Returns
the Identity element

Implements AbstractGroup< T >.

Definition at line 140 of file modarith.h.

◆ Inverse()

const Integer & ModularArithmetic::Inverse ( const Integer a) const

Inverts the element in the ring.

Parameters
afirst element
Returns
the inverse of the element

Definition at line 4655 of file integer.cpp.

◆ IsMontgomeryRepresentation()

virtual bool ModularArithmetic::IsMontgomeryRepresentation ( ) const
inlinevirtual

Retrieves the representation.

Returns
true if the if the modulus is in Montgomery form for multiplication, false otherwise

Reimplemented in MontgomeryRepresentation.

Definition at line 108 of file modarith.h.

◆ IsUnit()

bool ModularArithmetic::IsUnit ( const Integer a) const
inlinevirtual

Determines whether an element is a unit in the ring.

Parameters
athe element
Returns
true if the element is a unit after reduction, false otherwise.

Implements AbstractRing< Integer >.

Definition at line 203 of file modarith.h.

◆ MaxElementBitLength()

unsigned int ModularArithmetic::MaxElementBitLength ( ) const
inline

Provides the maximum bit size of an element in the ring.

Returns
maximum bit size of an element

Definition at line 243 of file modarith.h.

◆ MaxElementByteLength()

unsigned int ModularArithmetic::MaxElementByteLength ( ) const
inline

Provides the maximum byte size of an element in the ring.

Returns
maximum byte size of an element

Definition at line 248 of file modarith.h.

◆ MultiplicativeIdentity()

const Integer & ModularArithmetic::MultiplicativeIdentity ( ) const
inlinevirtual

Retrieves the multiplicative identity.

Returns
the multiplicative identity

the base class implementations returns 1.

Implements AbstractRing< Integer >.

Reimplemented in MontgomeryRepresentation.

Definition at line 182 of file modarith.h.

◆ MultiplicativeInverse()

const Integer & ModularArithmetic::MultiplicativeInverse ( const Integer a) const
inlinevirtual

Calculate the multiplicative inverse of an element in the ring.

Parameters
athe 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.

◆ Multiply()

const Integer & ModularArithmetic::Multiply ( const Integer a,
const Integer b 
) const
inlinevirtual

Multiplies elements in the ring.

Parameters
athe multiplicand
bthe multiplier
Returns
the product of a and b

Multiply returns a*b%n.

Implements AbstractRing< Integer >.

Reimplemented in MontgomeryRepresentation.

Definition at line 190 of file modarith.h.

◆ operator=()

ModularArithmetic & ModularArithmetic::operator= ( const ModularArithmetic ma)
inline

Assign a ModularArithmetic.

Parameters
maother ModularArithmetic

Definition at line 64 of file modarith.h.

◆ operator==()

bool ModularArithmetic::operator== ( const ModularArithmetic rhs) const
inline

Compares two ModularArithmetic for equality.

Parameters
rhsother ModularArithmetic
Returns
true if this is equal to the other, false otherwise

The operator tests for equality using this.m_modulus == rhs.m_modulus.

Definition at line 269 of file modarith.h.

◆ RandomElement()

Element ModularArithmetic::RandomElement ( RandomNumberGenerator rng,
const RandomizationParameter &  ignore_for_now = 0 
) const
inline

Provides a random element in the ring.

Parameters
rngRandomNumberGenerator used to generate material
ignore_for_nowunused
Returns
a random element that is uniformly distributed

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.

◆ Reduce()

Integer & ModularArithmetic::Reduce ( Integer a,
const Integer b 
) const

TODO.

Parameters
afirst element
bsecond element
Returns
TODO

Definition at line 4638 of file integer.cpp.

◆ SetModulus()

void ModularArithmetic::SetModulus ( const Integer newModulus)
inline

Sets the modulus.

Parameters
newModulusthe new modulus

Definition at line 103 of file modarith.h.

◆ SimultaneousExponentiate()

void ModularArithmetic::SimultaneousExponentiate ( Element results,
const Element base,
const Integer exponents,
unsigned int  exponentsCount 
) const

Exponentiates a base to multiple exponents in the ring.

Parameters
resultsan array of Elements
basethe base to raise to the exponents
exponentsan array of exponents
exponentsCountthe 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.

Precondition
COUNTOF(results) == exponentsCount
COUNTOF(exponents) == exponentsCount

Definition at line 4678 of file integer.cpp.

◆ Square()

const Integer & ModularArithmetic::Square ( const Integer a) const
inlinevirtual

Square an element in the ring.

Parameters
athe element
Returns
the element squared

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.

◆ Subtract()

const Integer & ModularArithmetic::Subtract ( const Integer a,
const Integer b 
) const

Subtracts elements in the ring.

Parameters
afirst element
bsecond element
Returns
the difference of a and b. The element a must provide a Subtract member function.

Definition at line 4621 of file integer.cpp.

Member Data Documentation

◆ DefaultRandomizationParameter

const RandomizationParameter ModularArithmetic::DefaultRandomizationParameter
static

Definition at line 272 of file modarith.h.

◆ m_modulus

Integer ModularArithmetic::m_modulus
protected

Definition at line 280 of file modarith.h.

◆ m_result

Integer ModularArithmetic::m_result
mutableprotected

Definition at line 281 of file modarith.h.

◆ m_result1

Integer ModularArithmetic::m_result1
protected

Definition at line 281 of file modarith.h.


The documentation for this class was generated from the following files: