From caea6f1c593ec93abe38339c3757e637f546c616 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Tue, 5 Apr 2016 14:22:45 -0400 Subject: [PATCH] Updated documentation --- algebra.h | 190 ++++++++++++++++++++++++++++++++++++++++++++++++----- modarith.h | 54 ++++++++------- pwdbased.h | 1 + 3 files changed, 207 insertions(+), 38 deletions(-) diff --git a/algebra.h b/algebra.h index 647a4d61..1559cb47 100644 --- a/algebra.h +++ b/algebra.h @@ -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 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));
template 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 a==b 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 a and b 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 a and b. The element a 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 COUNTOF(results) == exponentsCount + //! \pre COUNTOF(exponents) == exponentsCount 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 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));
template class CRYPTOPP_NO_VTABLE AbstractRing : public AbstractGroup { 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 COUNTOF(results) == exponentsCount + //! \pre COUNTOF(exponents) == exponentsCount 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& 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 struct BaseAndExponent { @@ -144,15 +264,37 @@ template // ******************************************************** -//! Abstract Euclidean Domain +//! \brief Abstract Euclidean domain +//! \tparam T element class or type +//! \details 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));
template class CRYPTOPP_NO_VTABLE AbstractEuclideanDomain : public AbstractRing { 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 a%b. 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 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));
template class EuclideanDomainOf : public AbstractEuclideanDomain { public: @@ -224,7 +374,15 @@ private: mutable Element result; }; -//! Quotient Ring +//! \brief Quotient ring +//! \tparam T element class or type +//! \details 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));
template class QuotientRing : public AbstractRing { public: diff --git a/modarith.h b/modarith.h index aa943373..fa430b61 100644 --- a/modarith.h +++ b/modarith.h @@ -24,10 +24,13 @@ CRYPTOPP_DLL_TEMPLATE_CLASS AbstractEuclideanDomain; //! \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 m_result1. +//! \details 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));
class CRYPTOPP_DLL ModularArithmetic : public AbstractRing { 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 a and b @@ -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 a and b. The element a 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 Add(a, a). The element a 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 a*b\%n. 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 a*a\%n. The element a 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 a-1\%n. The element a 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 a*b-1\%n. 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 COUNTOF(exponents) == exponentsCount 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 [a] as //! a*r\%n, where r is a convenient power of 2. +//! \details 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));
class CRYPTOPP_DLL MontgomeryRepresentation : public ModularArithmetic { public: diff --git a/pwdbased.h b/pwdbased.h index 44a19dfe..055c5bd5 100644 --- a/pwdbased.h +++ b/pwdbased.h @@ -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