diff options
author | lloyd <[email protected]> | 2010-03-15 15:30:56 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-03-15 15:30:56 +0000 |
commit | 1935eebb275a9770ba017e519b0e5b9182523159 (patch) | |
tree | de3c502e1a8c4c9d11284cb0192f530fa7ab6d33 /src/math | |
parent | de6ce8809c9f7c87981d7588255f65acf8d3b45c (diff) |
Add PointGFp::monty_sqr
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/numbertheory/point_gfp.cpp | 46 | ||||
-rw-r--r-- | src/math/numbertheory/point_gfp.h | 8 |
2 files changed, 44 insertions, 10 deletions
diff --git a/src/math/numbertheory/point_gfp.cpp b/src/math/numbertheory/point_gfp.cpp index 7df12a53c..d4c68d1dc 100644 --- a/src/math/numbertheory/point_gfp.cpp +++ b/src/math/numbertheory/point_gfp.cpp @@ -58,6 +58,33 @@ BigInt PointGFp::monty_mult(const BigInt& a, const BigInt& b, return result; } +BigInt PointGFp::monty_sqr(const BigInt& x, + MemoryRegion<word>& workspace) + { + //return monty_mult(x, x, workspace); + + if(x.is_zero()) + return 0; + + const BigInt& p = curve.get_p(); + const u32bit p_size = (workspace.size() - 1) / 2; + + const word p_dash = curve.get_p_dash(); + + workspace.clear(); + + bigint_simple_sqr(workspace, x.data(), x.sig_words()); + + bigint_monty_redc(workspace, workspace.size(), + p.data(), p_size, p_dash); + + BigInt result; + result.grow_to(p_size); + copy_mem(result.get_reg().begin(), &workspace[p_size], p_size); + + return result; + } + void PointGFp::add(const PointGFp& rhs, Workspace& workspace) { @@ -91,11 +118,11 @@ void PointGFp::add(const PointGFp& rhs, BigInt& y = ws_bn[9]; BigInt& z = ws_bn[10]; - rhs_z2 = monty_mult(rhs.coord_z, rhs.coord_z, ws); + rhs_z2 = monty_sqr(rhs.coord_z, ws); U1 = monty_mult(coord_x, rhs_z2, ws); S1 = monty_mult(coord_y, monty_mult(rhs.coord_z, rhs_z2, ws), ws); - lhs_z2 = monty_mult(coord_z, coord_z, ws); + lhs_z2 = monty_sqr(coord_z, ws); U2 = monty_mult(rhs.coord_x, lhs_z2, ws); S2 = monty_mult(rhs.coord_y, monty_mult(coord_z, lhs_z2, ws), ws); @@ -115,13 +142,13 @@ void PointGFp::add(const PointGFp& rhs, return; } - U2 = monty_mult(H, H, ws); + U2 = monty_sqr(H, ws); S2 = monty_mult(U2, H, ws); U2 = monty_mult(U1, U2, ws); - x = mod_p.reduce(monty_mult(r, r, ws) - S2 - U2*2); + x = mod_p.reduce(monty_sqr(r, ws) - S2 - U2*2); U2 -= x; if(U2.is_negative()) @@ -246,20 +273,19 @@ void PointGFp::mult2(Workspace& workspace) BigInt& y = ws_bn[7]; BigInt& z = ws_bn[8]; - y_2 = monty_mult(coord_y, coord_y, ws); + y_2 = monty_sqr(coord_y, ws); S = mod_p.reduce(4 * monty_mult(coord_x, y_2, ws)); - z4 = monty_mult(coord_z, coord_z, ws); - z4 = monty_mult(z4, z4, ws); + z4 = monty_sqr(monty_sqr(coord_z, ws), ws); a_z4 = monty_mult(curve.get_a_r(), z4, ws); - M = mod_p.reduce(a_z4 + 3 * monty_mult(coord_x, coord_x, ws)); + M = mod_p.reduce(a_z4 + 3 * monty_sqr(coord_x, ws)); - x = mod_p.reduce(monty_mult(M, M, ws) - 2*S); + x = mod_p.reduce(monty_sqr(M, ws) - 2*S); - U = mod_p.reduce(monty_mult(y_2, y_2, ws) << 3); + U = mod_p.reduce(monty_sqr(y_2, ws) << 3); S -= x; while(S.is_negative()) diff --git a/src/math/numbertheory/point_gfp.h b/src/math/numbertheory/point_gfp.h index 4ed8df160..e4cf6f7d5 100644 --- a/src/math/numbertheory/point_gfp.h +++ b/src/math/numbertheory/point_gfp.h @@ -161,6 +161,14 @@ class BOTAN_DLL PointGFp MemoryRegion<word>& workspace); /** + * Montgomery squaring/reduction + * @param x multiplicand + * @param workspace temp space + */ + BigInt monty_sqr(const BigInt& x, + MemoryRegion<word>& workspace); + + /** * Point addition */ void add(const PointGFp& other, Workspace& workspace); |