aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-03-16 21:41:40 +0000
committerlloyd <[email protected]>2010-03-16 21:41:40 +0000
commitd42ca2fade9ef3e07bae3b8ca89bdb7efe450145 (patch)
tree179b5cdd97b19572f57ff01a32a344ba8e94f72c /src
parent6e344d5a1eafe1c428d0baa62188b7a91d7ebe11 (diff)
Shuffle functions for easier reading
Diffstat (limited to 'src')
-rw-r--r--src/math/numbertheory/point_gfp.cpp146
-rw-r--r--src/math/numbertheory/point_gfp.h3
2 files changed, 75 insertions, 74 deletions
diff --git a/src/math/numbertheory/point_gfp.cpp b/src/math/numbertheory/point_gfp.cpp
index 5319b3809..d7ad72552 100644
--- a/src/math/numbertheory/point_gfp.cpp
+++ b/src/math/numbertheory/point_gfp.cpp
@@ -31,6 +31,7 @@ PointGFp::PointGFp(const CurveGFp& curve, const BigInt& x, const BigInt& y) :
coord_z = mod_p.reduce(curve.get_r());
}
+// Montgomery multiplication
void PointGFp::monty_mult(BigInt& z,
const BigInt& x, const BigInt& y,
MemoryRegion<word>& workspace) const
@@ -58,6 +59,7 @@ void PointGFp::monty_mult(BigInt& z,
copy_mem(z.get_reg().begin(), &workspace[p_size], p_size);
}
+// Montgomery squaring
void PointGFp::monty_sqr(BigInt& z, const BigInt& x,
MemoryRegion<word>& workspace) const
{
@@ -83,8 +85,8 @@ void PointGFp::monty_sqr(BigInt& z, const BigInt& x,
copy_mem(z.get_reg().begin(), &workspace[p_size], p_size);
}
-void PointGFp::add(const PointGFp& rhs,
- Workspace& workspace)
+// Point addition
+void PointGFp::add(const PointGFp& rhs, Workspace& workspace)
{
if(is_zero())
{
@@ -174,6 +176,76 @@ void PointGFp::add(const PointGFp& rhs,
coord_z = z;
}
+// *this *= 2
+void PointGFp::mult2(Workspace& workspace)
+ {
+ if(is_zero())
+ return;
+ else if(coord_y.is_zero())
+ {
+ *this = PointGFp(curve); // setting myself to zero
+ return;
+ }
+
+ const BigInt& p = curve.get_p();
+
+ MemoryRegion<word>& ws = workspace.ws_monty;
+ std::vector<BigInt>& ws_bn = workspace.ws_bn;
+
+ BigInt& y_2 = ws_bn[0];
+ BigInt& S = ws_bn[1];
+ BigInt& z4 = ws_bn[2];
+ BigInt& a_z4 = ws_bn[3];
+ BigInt& M = ws_bn[4];
+ BigInt& U = ws_bn[5];
+ BigInt& x = ws_bn[6];
+ BigInt& y = ws_bn[7];
+ BigInt& z = ws_bn[8];
+
+ monty_sqr(y_2, coord_y, ws);
+
+ monty_mult(S, coord_x, y_2, ws);
+ S <<= 2; // * 4
+ while(S >= p)
+ S -= p;
+
+ monty_sqr(z4, monty_sqr(coord_z, ws), ws);
+ monty_mult(a_z4, curve.get_a_r(), z4, ws);
+
+ M = 3 * monty_sqr(coord_x, ws);
+ M += a_z4;
+ while(M >= p)
+ M -= p;
+
+ monty_sqr(x, M, ws);
+ x -= (S << 1);
+ while(x.is_negative())
+ x += p;
+
+ monty_sqr(U, y_2, ws);
+ U <<= 3;
+ while(U >= p)
+ U -= p;
+
+ S -= x;
+ while(S.is_negative())
+ S += p;
+
+ monty_mult(y, M, S, ws);
+ y -= U;
+ if(y.is_negative())
+ y += p;
+
+ monty_mult(z, coord_y, coord_z, ws);
+ z <<= 1;
+ if(z >= p)
+ z -= p;
+
+ coord_x = x;
+ coord_y = y;
+ coord_z = z;
+ }
+
// arithmetic operators
PointGFp& PointGFp::operator+=(const PointGFp& rhs)
{
@@ -270,76 +342,6 @@ PointGFp& PointGFp::operator*=(const BigInt& scalar)
return *this;
}
-// *this *= 2
-void PointGFp::mult2(Workspace& workspace)
- {
- if(is_zero())
- return;
- else if(coord_y.is_zero())
- {
- *this = PointGFp(curve); // setting myself to zero
- return;
- }
-
- const BigInt& p = curve.get_p();
-
- MemoryRegion<word>& ws = workspace.ws_monty;
- std::vector<BigInt>& ws_bn = workspace.ws_bn;
-
- BigInt& y_2 = ws_bn[0];
- BigInt& S = ws_bn[1];
- BigInt& z4 = ws_bn[2];
- BigInt& a_z4 = ws_bn[3];
- BigInt& M = ws_bn[4];
- BigInt& U = ws_bn[5];
- BigInt& x = ws_bn[6];
- BigInt& y = ws_bn[7];
- BigInt& z = ws_bn[8];
-
- monty_sqr(y_2, coord_y, ws);
-
- monty_mult(S, coord_x, y_2, ws);
- S <<= 2; // * 4
- while(S >= p)
- S -= p;
-
- monty_sqr(z4, monty_sqr(coord_z, ws), ws);
- monty_mult(a_z4, curve.get_a_r(), z4, ws);
-
- M = 3 * monty_sqr(coord_x, ws);
- M += a_z4;
- while(M >= p)
- M -= p;
-
- monty_sqr(x, M, ws);
- x -= (S << 1);
- while(x.is_negative())
- x += p;
-
- monty_sqr(U, y_2, ws);
- U <<= 3;
- while(U >= p)
- U -= p;
-
- S -= x;
- while(S.is_negative())
- S += p;
-
- monty_mult(y, M, S, ws);
- y -= U;
- if(y.is_negative())
- y += p;
-
- monty_mult(z, coord_y, coord_z, ws);
- z <<= 1;
- if(z >= p)
- z -= p;
-
- coord_x = x;
- coord_y = y;
- coord_z = z;
- }
-
BigInt PointGFp::get_affine_x() const
{
if(is_zero())
diff --git a/src/math/numbertheory/point_gfp.h b/src/math/numbertheory/point_gfp.h
index f597990ff..f5e409ca7 100644
--- a/src/math/numbertheory/point_gfp.h
+++ b/src/math/numbertheory/point_gfp.h
@@ -245,8 +245,7 @@ inline PointGFp operator*(const BigInt& scalar, const PointGFp& point)
inline PointGFp operator*(const PointGFp& point, const BigInt& scalar)
{
- PointGFp result(point);
- return result *= scalar;
+ return scalar * point;
}
// encoding and decoding