aboutsummaryrefslogtreecommitdiffstats
path: root/src/math/gfpmath/curve_gfp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/math/gfpmath/curve_gfp.cpp')
-rw-r--r--src/math/gfpmath/curve_gfp.cpp74
1 files changed, 38 insertions, 36 deletions
diff --git a/src/math/gfpmath/curve_gfp.cpp b/src/math/gfpmath/curve_gfp.cpp
index 89fa74c49..c5abc2a5f 100644
--- a/src/math/gfpmath/curve_gfp.cpp
+++ b/src/math/gfpmath/curve_gfp.cpp
@@ -1,39 +1,51 @@
/******************************************************
- * Elliptic curves over GF(p) (source file) *
- * *
- * (C) 2007 Martin Doering *
- * Christoph Ludwig *
- * Falko Strenzke *
- ******************************************************/
+* Elliptic curves over GF(p) *
+* *
+* (C) 2007 Martin Doering *
+* Christoph Ludwig *
+* Falko Strenzke *
+* 2008 Jack Lloyd *
+******************************************************/
#include <botan/curve_gfp.h>
+#include <botan/bigint.h>
+#include <assert.h>
namespace Botan {
-CurveGFp::CurveGFp(GFpElement const& a, GFpElement const& b,
- const BigInt& p) :
- mA(a), mB(b)
+void CurveGFp::set_shrd_mod(SharedPtrConverter<GFpModulus> const mod)
{
- if(p != mA.get_p() || p != mB.get_p())
- throw Invalid_Argument("could not construct curve: moduli of arguments differ");
+ mp_mod = mod.get_shared();
+ mA.turn_off_sp_red_mul();// m.m. is not needed, must be trf. back
+ mB.turn_off_sp_red_mul();// m.m. is not needed, must be trf. back
+ //ok, above we destroy any evantually computated montg. mult. values,
+ // but that won´t influence performance in usual applications
+ mA.set_shrd_mod(mod.get_shared());
+ mB.set_shrd_mod(mod.get_shared());
+ }
+CurveGFp::CurveGFp(const GFpElement& a, const GFpElement& b,
+ const BigInt& 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");
+ }
std::tr1::shared_ptr<GFpModulus> p_mod = std::tr1::shared_ptr<GFpModulus>(new GFpModulus(p));
// the above is the creation of the GFpModuls object which will be shared point-wide
// (in the context of a point of course)
set_shrd_mod(p_mod);
}
-
// copy constructor
-CurveGFp::CurveGFp(CurveGFp const& other)
+CurveGFp::CurveGFp(const CurveGFp& other)
: mA(other.get_a()),
mB(other.get_b())
{
mp_mod = std::tr1::shared_ptr<GFpModulus>(new GFpModulus(*other.mp_mod));
- //assert(mp_mod->p_equal_to(mA.get_p()));
- //assert(mp_mod->p_equal_to(mB.get_p()));
+ assert(mp_mod->p_equal_to(mA.get_p()));
+ assert(mp_mod->p_equal_to(mB.get_p()));
set_shrd_mod(mp_mod);
if(other.mp_mres_a.get())
{
@@ -51,7 +63,7 @@ CurveGFp::CurveGFp(CurveGFp const& other)
}
// assignment operator
-CurveGFp const& CurveGFp::operator=(CurveGFp const& other)
+const CurveGFp& CurveGFp::operator=(const CurveGFp& other)
{
// for exception safety...
GFpElement a_tmp = other.mA;
@@ -90,7 +102,7 @@ GFpElement const CurveGFp::get_b() const
BigInt const CurveGFp::get_p() const
{
- //assert(mp_mod.get() != 0);
+ assert(mp_mod.get() != 0);
return mp_mod->get_p();
}
@@ -104,18 +116,6 @@ void CurveGFp::swap(CurveGFp& other)
std::swap(mp_mres_b, other.mp_mres_b);
std::swap(mp_mres_one, other.mp_mres_one);
}
-
-void CurveGFp::set_shrd_mod(std::tr1::shared_ptr<GFpModulus> const mod)
- {
- mp_mod = mod;
- mA.turn_off_sp_red_mul();// m.m. is not needed, must be trf. back
- mB.turn_off_sp_red_mul();// m.m. is not needed, must be trf. back
- //ok, above we destroy any evantually computated montg. mult. values,
- // but that won't influence performance in usual applications
- mA.set_shrd_mod(mod);
- mB.set_shrd_mod(mod);
- }
-
GFpElement const CurveGFp::get_mres_a() const
{
if(mp_mres_a.get() == 0)
@@ -126,7 +126,6 @@ GFpElement const CurveGFp::get_mres_a() const
}
return GFpElement(*mp_mres_a);
}
-
GFpElement const CurveGFp::get_mres_b() const
{
if(mp_mres_b.get() == 0)
@@ -137,7 +136,6 @@ GFpElement const CurveGFp::get_mres_b() const
}
return GFpElement(*mp_mres_b);
}
-
std::tr1::shared_ptr<GFpElement const> const CurveGFp::get_mres_one() const
{
if(mp_mres_one.get() == 0)
@@ -148,10 +146,14 @@ std::tr1::shared_ptr<GFpElement const> const CurveGFp::get_mres_one() const
}
return mp_mres_one;
}
-
-bool operator==(CurveGFp const& lhs, CurveGFp const& rhs)
+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^2f = x^3 + (" << elem.get_a() << ")x + (" << elem.get_b() << ")";
+ }
+
}