Updated documentation

pull/154/head
Jeffrey Walton 2016-04-05 14:22:45 -04:00
parent 34a34967ac
commit caea6f1c59
3 changed files with 207 additions and 38 deletions

190
algebra.h
View File

@ -1,7 +1,6 @@
// algebra.h - written and placed in the public domain by Wei Dai
//! \file
//! \headerfile algebra.h
//! \file algebra.h
//! \brief Classes for performing mathematics over different fields
#ifndef CRYPTOPP_ALGEBRA_H
@ -15,15 +14,15 @@ NAMESPACE_BEGIN(CryptoPP)
class Integer;
// "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, the following code
// will produce incorrect results:
// abcd = group.Add(group.Add(a,b), group.Add(c,d));
// But this should be fine:
// abcd = group.Add(a, group.Add(b, group.Add(c,d));
//! Abstract Group
//! \brief Abstract group
//! \tparam T element class or type
//! \details <tt>const Element&</tt> returned by member functions are references
//! to internal data members. Since each object may have only
//! one such data member for holding results, the following code
//! will produce incorrect results:
//! <pre> abcd = group.Add(group.Add(a,b), group.Add(c,d));</pre>
//! But this should be fine:
//! <pre> abcd = group.Add(a, group.Add(b, group.Add(c,d));</pre>
template <class T> class CRYPTOPP_NO_VTABLE AbstractGroup
{
public:
@ -31,48 +30,167 @@ public:
virtual ~AbstractGroup() {}
//! \brief Compare two elements for equality
//! \param a first element
//! \param b second element
//! \returns true if the elements are equal, false otherwise
//! \details Equal() tests the elements for equality using <tt>a==b</tt>
virtual bool Equal(const Element &a, const Element &b) const =0;
//! \brief Provides the Identity element
//! \returns the Identity element
virtual const Element& Identity() const =0;
//! \brief Adds elements in the group
//! \param a first element
//! \param b second element
//! \returns the sum of <tt>a</tt> and <tt>b</tt>
virtual const Element& Add(const Element &a, const Element &b) const =0;
//! \brief Inverts the element in the group
//! \param a first element
//! \returns the inverse of the element
virtual const Element& Inverse(const Element &a) const =0;
//! \brief Determine if inversion is fast
//! \returns true if inversion is fast, false otherwise
virtual bool InversionIsFast() const {return false;}
//! \brief Doubles an element in the group
//! \param a the element
//! \returns the element doubled
virtual const Element& Double(const Element &a) const;
//! \brief Subtracts elements in the group
//! \param a first element
//! \param b second element
//! \returns the difference of <tt>a</tt> and <tt>b</tt>. The element <tt>a</tt> must provide a Subtract member function.
virtual const Element& Subtract(const Element &a, const Element &b) const;
//! \brief TODO
//! \param a first element
//! \param b second element
//! \returns TODO
virtual Element& Accumulate(Element &a, const Element &b) const;
//! \brief Reduces an element in the congruence class
//! \param a element to reduce
//! \param b the congruence class
//! \returns the reduced element
virtual Element& Reduce(Element &a, const Element &b) const;
//! \brief Performs a scalar multiplication
//! \param a multiplicand
//! \param e multiplier
//! \returns the product
virtual Element ScalarMultiply(const Element &a, const Integer &e) const;
//! \brief TODO
//! \param x first multiplicand
//! \param e1 the first multiplier
//! \param y second multiplicand
//! \param e2 the second multiplier
//! \returns TODO
virtual Element CascadeScalarMultiply(const Element &x, const Integer &e1, const Element &y, const Integer &e2) const;
//! \brief Multiplies a base to multiple exponents in a group
//! \param results an array of Elements
//! \param base the base to raise to the exponents
//! \param exponents an array of exponents
//! \param exponentsCount the number of exponents in the array
//! \details SimultaneousMultiply() multiplies the base to each exponent in the exponents array and stores the
//! result at the respective position in the results array.
//! \details SimultaneousMultiply() must be implemented in a derived class.
//! \pre <tt>COUNTOF(results) == exponentsCount</tt>
//! \pre <tt>COUNTOF(exponents) == exponentsCount</tt>
virtual void SimultaneousMultiply(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
};
//! Abstract Ring
//! \brief Abstract ring
//! \tparam T element class or type
//! \details <tt>const Element&</tt> returned by member functions are references
//! to internal data members. Since each object may have only
//! one such data member for holding results, the following code
//! will produce incorrect results:
//! <pre> abcd = group.Add(group.Add(a,b), group.Add(c,d));</pre>
//! But this should be fine:
//! <pre> abcd = group.Add(a, group.Add(b, group.Add(c,d));</pre>
template <class T> class CRYPTOPP_NO_VTABLE AbstractRing : public AbstractGroup<T>
{
public:
typedef T Element;
//! \brief Construct an AbstractRing
AbstractRing() {m_mg.m_pRing = this;}
//! \brief Copy construct an AbstractRing
//! \param source other AbstractRing
AbstractRing(const AbstractRing &source)
{CRYPTOPP_UNUSED(source); m_mg.m_pRing = this;}
//! \brief Assign an AbstractRing
//! \param source other AbstractRing
AbstractRing& operator=(const AbstractRing &source)
{CRYPTOPP_UNUSED(source); return *this;}
//! \brief Determines whether an element is a unit in the group
//! \param a the element
//! \returns true if the element is a unit after reduction, false otherwise.
virtual bool IsUnit(const Element &a) const =0;
//! \brief Retrieves the multiplicative identity
//! \returns the multiplicative identity
virtual const Element& MultiplicativeIdentity() const =0;
//! \brief Multiplies elements in the group
//! \param a the multiplicand
//! \param b the multiplier
//! \returns the product of a and b
virtual const Element& Multiply(const Element &a, const Element &b) const =0;
//! \brief Calculate the multiplicative inverse of an element in the group
//! \param a the element
virtual const Element& MultiplicativeInverse(const Element &a) const =0;
//! \brief Square an element in the group
//! \param a the element
//! \returns the element squared
virtual const Element& Square(const Element &a) const;
//! \brief Divides elements in the group
//! \param a the dividend
//! \param b the divisor
//! \returns the quotient
virtual const Element& Divide(const Element &a, const Element &b) const;
//! \brief Raises a base to an exponent in the group
//! \param a the base
//! \param e the exponent
//! \returns the exponentiation
virtual Element Exponentiate(const Element &a, const Integer &e) const;
//! \brief TODO
//! \param x first element
//! \param e1 first exponent
//! \param y second element
//! \param e2 second exponent
//! \returns TODO
virtual Element CascadeExponentiate(const Element &x, const Integer &e1, const Element &y, const Integer &e2) const;
//! \brief Exponentiates a base to multiple exponents in the Ring
//! \param results an array of Elements
//! \param base the base to raise to the exponents
//! \param exponents an array of exponents
//! \param exponentsCount the number of exponents in the array
//! \details SimultaneousExponentiate() raises the base to each exponent in the exponents array and stores the
//! result at the respective position in the results array.
//! \details SimultaneousExponentiate() must be implemented in a derived class.
//! \pre <tt>COUNTOF(results) == exponentsCount</tt>
//! \pre <tt>COUNTOF(exponents) == exponentsCount</tt>
virtual void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
//! \brief Retrieves the multiplicative group
//! \returns the multiplicative group
virtual const AbstractGroup<T>& MultiplicativeGroup() const
{return m_mg;}
@ -124,7 +242,9 @@ private:
// ********************************************************
//! Base and Exponent
//! \brief Base and exponent
//! \tparam T base class or type
//! \tparam T exponent class or type
template <class T, class E = Integer>
struct BaseAndExponent
{
@ -144,15 +264,37 @@ template <class Element, class Iterator>
// ********************************************************
//! Abstract Euclidean Domain
//! \brief Abstract Euclidean domain
//! \tparam T element class or type
//! \details <tt>const Element&</tt> returned by member functions are references
//! to internal data members. Since each object may have only
//! one such data member for holding results, the following code
//! will produce incorrect results:
//! <pre> abcd = group.Add(group.Add(a,b), group.Add(c,d));</pre>
//! But this should be fine:
//! <pre> abcd = group.Add(a, group.Add(b, group.Add(c,d));</pre>
template <class T> class CRYPTOPP_NO_VTABLE AbstractEuclideanDomain : public AbstractRing<T>
{
public:
typedef T Element;
//! \brief Performs the division algorithm on two elements in the ring
//! \param r the remainder
//! \param q the quotient
//! \param a the dividend
//! \param d the divisor
virtual void DivisionAlgorithm(Element &r, Element &q, const Element &a, const Element &d) const =0;
//! \brief Performs a modular reduction in the ring
//! \param a the element
//! \param b the modulus
//! \returns the result of <tt>a%b</tt>.
virtual const Element& Mod(const Element &a, const Element &b) const =0;
//! \brief Calculates the greatest common denominator in the ring
//! \param a the first element
//! \param b the second element
//! \returns the the greatest common denominator of a and b.
virtual const Element& Gcd(const Element &a, const Element &b) const;
protected:
@ -161,7 +303,15 @@ protected:
// ********************************************************
//! EuclideanDomainOf
//! \brief Euclidean domain
//! \tparam T element class or type
//! \details <tt>const Element&</tt> returned by member functions are references
//! to internal data members. Since each object may have only
//! one such data member for holding results, the following code
//! will produce incorrect results:
//! <pre> abcd = group.Add(group.Add(a,b), group.Add(c,d));</pre>
//! But this should be fine:
//! <pre> abcd = group.Add(a, group.Add(b, group.Add(c,d));</pre>
template <class T> class EuclideanDomainOf : public AbstractEuclideanDomain<T>
{
public:
@ -224,7 +374,15 @@ private:
mutable Element result;
};
//! Quotient Ring
//! \brief Quotient ring
//! \tparam T element class or type
//! \details <tt>const Element&</tt> returned by member functions are references
//! to internal data members. Since each object may have only
//! one such data member for holding results, the following code
//! will produce incorrect results:
//! <pre> abcd = group.Add(group.Add(a,b), group.Add(c,d));</pre>
//! But this should be fine:
//! <pre> abcd = group.Add(a, group.Add(b, group.Add(c,d));</pre>
template <class T> class QuotientRing : public AbstractRing<typename T::Element>
{
public:

View File

@ -24,10 +24,13 @@ CRYPTOPP_DLL_TEMPLATE_CLASS AbstractEuclideanDomain<Integer>;
//! \brief Ring of congruence classes modulo n
//! \details This implementation represents each congruence class as the smallest
//! non-negative integer in that class.
//! \details Each instance of the class provides two temporary elements to
//! preserve intermediate calculations for future use. For example,
//! \ref ModularArithmetic::Multiply "Multiply" saves its last result in member
//! variable <tt>m_result1</tt>.
//! \details <tt>const Element&</tt> returned by member functions are references
//! to internal data members. Since each object may have only
//! one such data member for holding results, the following code
//! will produce incorrect results:
//! <pre> abcd = group.Add(group.Add(a,b), group.Add(c,d));</pre>
//! But this should be fine:
//! <pre> abcd = group.Add(a, group.Add(b, group.Add(c,d));</pre>
class CRYPTOPP_DLL ModularArithmetic : public AbstractRing<Integer>
{
public:
@ -119,7 +122,7 @@ public:
const Integer& Identity() const
{return Integer::Zero();}
//! \brief Adds elements in the Ring
//! \brief Adds elements in the ring
//! \param a first element
//! \param b second element
//! \returns the sum of <tt>a</tt> and <tt>b</tt>
@ -131,12 +134,12 @@ public:
//! \returns TODO
Integer& Accumulate(Integer &a, const Integer &b) const;
//! \brief Inverts the element in the Ring
//! \brief Inverts the element in the ring
//! \param a first element
//! \returns the inverse of the element
const Integer& Inverse(const Integer &a) const;
//! \brief Subtracts elements in the Ring
//! \brief Subtracts elements in the ring
//! \param a first element
//! \param b second element
//! \returns the difference of <tt>a</tt> and <tt>b</tt>. The element <tt>a</tt> must provide a Subtract member function.
@ -148,7 +151,7 @@ public:
//! \returns TODO
Integer& Reduce(Integer &a, const Integer &b) const;
//! \brief Doubles an element in the Ring
//! \brief Doubles an element in the ring
//! \param a the element
//! \returns the element doubled
//! \details Double returns <tt>Add(a, a)</tt>. The element <tt>a</tt> must provide an Add member function.
@ -161,38 +164,38 @@ public:
const Integer& MultiplicativeIdentity() const
{return Integer::One();}
//! \brief Multiplies elements in the Ring
//! \param a first element
//! \param b second element
//! \brief Multiplies elements in the ring
//! \param a the multiplicand
//! \param b the multiplier
//! \returns the product of a and b
//! \details Multiply returns <tt>a*b\%n</tt>.
const Integer& Multiply(const Integer &a, const Integer &b) const
{return m_result1 = a*b%m_modulus;}
//! \brief Square an element in the Ring
//! \brief Square an element in the ring
//! \param a the element
//! \returns the element squared
//! \details Square returns <tt>a*a\%n</tt>. The element <tt>a</tt> must provide a Square member function.
const Integer& Square(const Integer &a) const
{return m_result1 = a.Squared()%m_modulus;}
//! \brief Determines whether an element is a unit in the Ring
//! \brief Determines whether an element is a unit in the ring
//! \param a the element
//! \returns true if the element is a unit after reduction, false otherwise.
bool IsUnit(const Integer &a) const
{return Integer::Gcd(a, m_modulus).IsUnit();}
//! \brief Calculate the multiplicative inverse of an element in the Ring
//! \brief Calculate the multiplicative inverse of an element in the ring
//! \param a the element
//! \details MultiplicativeInverse returns <tt>a<sup>-1</sup>\%n</tt>. The element <tt>a</tt> must
//! provide a InverseMod member function.
const Integer& MultiplicativeInverse(const Integer &a) const
{return m_result1 = a.InverseMod(m_modulus);}
//! \brief Divides elements in the Ring
//! \param a first element
//! \param b second element
//! \returns the element squared
//! \brief Divides elements in the ring
//! \param a the dividend
//! \param b the divisor
//! \returns the quotient
//! \details Divide returns <tt>a*b<sup>-1</sup>\%n</tt>.
const Integer& Divide(const Integer &a, const Integer &b) const
{return Multiply(a, MultiplicativeInverse(b));}
@ -205,7 +208,7 @@ public:
//! \returns TODO
Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const;
//! \brief Exponentiates a base to multiple exponents in the Ring
//! \brief Exponentiates a base to multiple exponents in the ring
//! \param results an array of Elements
//! \param base the base to raise to the exponents
//! \param exponents an array of exponents
@ -217,17 +220,17 @@ public:
//! \pre <tt>COUNTOF(exponents) == exponentsCount</tt>
void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
//! \brief Provides the maximum bit size of an element in the Ring
//! \brief Provides the maximum bit size of an element in the ring
//! \returns maximum bit size of an element
unsigned int MaxElementBitLength() const
{return (m_modulus-1).BitCount();}
//! \brief Provides the maximum byte size of an element in the Ring
//! \brief Provides the maximum byte size of an element in the ring
//! \returns maximum byte size of an element
unsigned int MaxElementByteLength() const
{return (m_modulus-1).ByteCount();}
//! \brief Provides a random element in the Ring
//! \brief Provides a random element in the ring
//! \param rng RandomNumberGenerator used to generate material
//! \param ignore_for_now unused
//! \returns a random element that is uniformly distributed
@ -261,6 +264,13 @@ protected:
//! \brief Performs modular arithmetic in Montgomery representation for increased speed
//! \details The Montgomery representation represents each congruence class <tt>[a]</tt> as
//! <tt>a*r\%n</tt>, where <tt>r</tt> is a convenient power of 2.
//! \details <tt>const Element&</tt> returned by member functions are references
//! to internal data members. Since each object may have only
//! one such data member for holding results, the following code
//! will produce incorrect results:
//! <pre> abcd = group.Add(group.Add(a,b), group.Add(c,d));</pre>
//! But this should be fine:
//! <pre> abcd = group.Add(a, group.Add(b, group.Add(c,d));</pre>
class CRYPTOPP_DLL MontgomeryRepresentation : public ModularArithmetic
{
public:

View File

@ -32,6 +32,7 @@ public:
//! \brief Derive key from the password
//! \param derived the byte buffer to receive the derived password
//! \param derivedLen the size of the byte buffer to receive the derived password
//! \param purpose an octet indicating the purpose of the derivation
//! \param password the byte buffer with the password
//! \param passwordLen the size of the password, in bytes
//! \param salt the byte buffer with the salt