aboutsummaryrefslogtreecommitdiffstats
path: root/src/math/gfpmath
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-02-25 03:58:35 +0000
committerlloyd <[email protected]>2010-02-25 03:58:35 +0000
commit8ba3a81c5f1cbe488269df5e009de3d165eb0654 (patch)
tree87daff36a9e415d0d118d708c2e51b23dd700d3b /src/math/gfpmath
parentc13f576834a52b03b88366cb243da49fc784b284 (diff)
CurveGFp: Inline, deleting source file. Store only a,b,p as
BigInts. Also reorder constructor args to p, a, b which seems more sensible to me.
Diffstat (limited to 'src/math/gfpmath')
-rw-r--r--src/math/gfpmath/curve_gfp.cpp45
-rw-r--r--src/math/gfpmath/curve_gfp.h50
-rw-r--r--src/math/gfpmath/info.txt1
-rw-r--r--src/math/gfpmath/point_gfp.cpp26
-rw-r--r--src/math/gfpmath/point_gfp.h1
5 files changed, 39 insertions, 84 deletions
diff --git a/src/math/gfpmath/curve_gfp.cpp b/src/math/gfpmath/curve_gfp.cpp
deleted file mode 100644
index b3be7d228..000000000
--- a/src/math/gfpmath/curve_gfp.cpp
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
-* Elliptic curves over GF(p)
-*
-* (C) 2007 Martin Doering, Christoph Ludwig, Falko Strenzke
-* 2008-2010 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#include <botan/curve_gfp.h>
-#include <botan/bigint.h>
-#include <assert.h>
-#include <ostream>
-
-namespace Botan {
-
-CurveGFp::CurveGFp(const GFpElement& a, const GFpElement& b,
- const BigInt& p) :
- modulus(p), mA(a), mB(b)
- {
- if(p != mA.get_p() || p != mB.get_p())
- throw Invalid_Argument("could not construct curve: moduli of arguments differ");
- }
-
-// swaps the states of *this and other, does not throw
-void CurveGFp::swap(CurveGFp& other)
- {
- std::swap(mA, other.mA);
- std::swap(mB, other.mB);
- std::swap(modulus, other.modulus);
- }
-
-bool operator==(const CurveGFp& lhs, const CurveGFp& rhs)
- {
- return (lhs.get_p() == rhs.get_p() &&
- lhs.get_a() == rhs.get_a() &&
- lhs.get_b() == rhs.get_b());
- }
-
-std::ostream& operator<<(std::ostream& output, const CurveGFp& elem)
- {
- return output << "y^2 = x^3 + (" << elem.get_a() << ")x + (" << elem.get_b() << ")";
- }
-
-}
diff --git a/src/math/gfpmath/curve_gfp.h b/src/math/gfpmath/curve_gfp.h
index e4ee7c8f5..cc1d42290 100644
--- a/src/math/gfpmath/curve_gfp.h
+++ b/src/math/gfpmath/curve_gfp.h
@@ -10,8 +10,7 @@
#ifndef BOTAN_GFP_CURVE_H__
#define BOTAN_GFP_CURVE_H__
-#include <botan/gfp_element.h>
-#include <iosfwd>
+#include <botan/bigint.h>
namespace Botan {
@@ -24,12 +23,12 @@ class BOTAN_DLL CurveGFp
/**
* Construct the elliptic curve E: y^2 = x^3 + ax + b over GF(p)
+ * @param p prime number of the field
* @param a first coefficient
* @param b second coefficient
- * @param p prime number of the field
*/
- CurveGFp(const GFpElement& a, const GFpElement& b,
- const BigInt& p);
+ CurveGFp(const BigInt& p, const BigInt& a, const BigInt& b) :
+ p(p), a(a), b(b) {}
// CurveGFp(const CurveGFp& other) = default;
// CurveGFp& operator=(const CurveGFp& other) = default;
@@ -38,58 +37,49 @@ class BOTAN_DLL CurveGFp
* Get coefficient a
* @result coefficient a
*/
- const GFpElement& get_a() const { return mA; }
+ const BigInt& get_a() const { return a; }
/**
* Get coefficient b
* @result coefficient b
*/
- const GFpElement& get_b() const { return mB; }
+ const BigInt& get_b() const { return b; }
/**
* Get prime modulus of the field of the curve
* @result prime modulus of the field of the curve
*/
- const BigInt& get_p() const { return modulus; }
+ const BigInt& get_p() const { return p; }
/**
* swaps the states of *this and other, does not throw
* @param other The curve to swap values with
*/
- void swap(CurveGFp& other);
+ void swap(CurveGFp& other)
+ {
+ std::swap(a, other.a);
+ std::swap(b, other.b);
+ std::swap(p, other.p);
+ }
+
+ bool operator==(const CurveGFp& other) const
+ {
+ return (p == other.p && a == other.a && b == other.b);
+ }
private:
- BigInt modulus;
- GFpElement mA;
- GFpElement mB;
+ BigInt p, a, b;
};
-// relational operators
-BOTAN_DLL bool operator==(const CurveGFp& lhs, const CurveGFp& rhs);
-
inline bool operator!=(const CurveGFp& lhs, const CurveGFp& rhs)
{
return !(lhs == rhs);
}
-// io operators
-BOTAN_DLL std::ostream& operator<<(std::ostream& output, const CurveGFp& elem);
-
-// swaps the states of curve1 and curve2, does not throw!
-// cf. Meyers, Item 25
-inline
-void swap(CurveGFp& curve1, CurveGFp& curve2)
- {
- curve1.swap(curve2);
- }
-
-} // namespace Botan
-
+}
namespace std {
-// swaps the states of curve1 and curve2, does not throw!
-// cf. Meyers, Item 25
template<> inline
void swap<Botan::CurveGFp>(Botan::CurveGFp& curve1,
Botan::CurveGFp& curve2)
diff --git a/src/math/gfpmath/info.txt b/src/math/gfpmath/info.txt
index 8c80e9e9d..68cff026d 100644
--- a/src/math/gfpmath/info.txt
+++ b/src/math/gfpmath/info.txt
@@ -7,7 +7,6 @@ point_gfp.h
</header:public>
<source>
-curve_gfp.cpp
gfp_element.cpp
point_gfp.cpp
</source>
diff --git a/src/math/gfpmath/point_gfp.cpp b/src/math/gfpmath/point_gfp.cpp
index f9aaf5c3c..b8fe3351b 100644
--- a/src/math/gfpmath/point_gfp.cpp
+++ b/src/math/gfpmath/point_gfp.cpp
@@ -20,9 +20,9 @@ BigInt decompress_point(bool yMod2,
{
BigInt xpow3 = x * x * x;
- BigInt g = curve.get_a().get_value() * x;
+ BigInt g = curve.get_a() * x;
g += xpow3;
- g += curve.get_b().get_value();
+ g += curve.get_b();
g = g % curve.get_p();
BigInt z = ressol(g, curve.get_p());
@@ -209,7 +209,7 @@ PointGFp& PointGFp::mult2_in_place()
S = x + x;
- GFpElement a_z4 = curve.get_a();
+ GFpElement a_z4(curve.get_p(), curve.get_a());
GFpElement z2 = point_z() * point_z();
a_z4 *= z2;
@@ -327,16 +327,20 @@ void PointGFp::check_invariants() const
if(coord_z == BigInt(1))
{
- GFpElement ax = curve.get_a() * point_x();
- if(y2 != (x3 + ax + curve.get_b()))
+ GFpElement ax(curve.get_p(), curve.get_a());
+ ax *= point_x();
+
+ GFpElement b(curve.get_p(), curve.get_b());
+
+ if(y2 != (x3 + ax + b))
throw Illegal_Point();
}
GFpElement Zpow2 = point_z() * point_z();
GFpElement Zpow3 = Zpow2 * point_z();
- GFpElement AZpow4 = Zpow3 * point_z() * curve.get_a();
+ GFpElement AZpow4 = Zpow3 * point_z() * GFpElement(curve.get_p(), curve.get_a());
const GFpElement aXZ4 = AZpow4 * point_x();
- const GFpElement bZ6 = curve.get_b() * Zpow3 * Zpow3;
+ const GFpElement bZ6 = GFpElement(curve.get_p(), curve.get_b()) * Zpow3 * Zpow3;
if(y2 != (x3 + aXZ4 + bZ6))
throw Illegal_Point();
@@ -501,7 +505,13 @@ PointGFp create_random_point(RandomNumberGenerator& rng,
GFpElement x = GFpElement(p, r);
GFpElement x3 = x * x * x;
- GFpElement y = (curve.get_a() * x) + (x3 * curve.get_b());
+ GFpElement ax(curve.get_p(), curve.get_a());
+ ax *= x;
+
+ GFpElement bx3(curve.get_p(), curve.get_b());
+ bx3 *= x3;
+
+ GFpElement y = ax + bx3;
if(ressol(y.get_value(), p) > 0)
return PointGFp(curve, x.get_value(), y.get_value());
diff --git a/src/math/gfpmath/point_gfp.h b/src/math/gfpmath/point_gfp.h
index 2da4b0506..6613da2e2 100644
--- a/src/math/gfpmath/point_gfp.h
+++ b/src/math/gfpmath/point_gfp.h
@@ -11,6 +11,7 @@
#define BOTAN_POINT_GFP_H__
#include <botan/curve_gfp.h>
+#include <botan/gfp_element.h>
#include <vector>
namespace Botan {