aboutsummaryrefslogtreecommitdiffstats
path: root/src/math
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-02-25 02:25:53 +0000
committerlloyd <[email protected]>2010-02-25 02:25:53 +0000
commit1b19128c46e14a073192e900e35c2506a87102cf (patch)
treed76ad6f0de698de151aebbc308de88d6fc32a4ff /src/math
parent83b575cdbef5cc86f80797d94cda4ea18a3a56cf (diff)
Cleanup EC2OSP, remove should-have-been-internal functions
Diffstat (limited to 'src/math')
-rw-r--r--src/math/gfpmath/point_gfp.cpp113
-rw-r--r--src/math/gfpmath/point_gfp.h7
2 files changed, 35 insertions, 85 deletions
diff --git a/src/math/gfpmath/point_gfp.cpp b/src/math/gfpmath/point_gfp.cpp
index 86bf82f61..7d5f0e17a 100644
--- a/src/math/gfpmath/point_gfp.cpp
+++ b/src/math/gfpmath/point_gfp.cpp
@@ -391,100 +391,55 @@ PointGFp operator*(const PointGFp& point, const BigInt& scalar)
// encoding and decoding
SecureVector<byte> EC2OSP(const PointGFp& point, byte format)
{
- if(format == PointGFp::UNCOMPRESSED)
- return encode_uncompressed(point);
- else if(format == PointGFp::COMPRESSED)
- return encode_compressed(point);
- else if(format == PointGFp::HYBRID)
- return encode_hybrid(point);
- else
- throw Invalid_Argument("illegal point encoding format specification");
- }
-
-SecureVector<byte> encode_compressed(const PointGFp& point)
- {
if(point.is_zero())
- {
- SecureVector<byte> result (1);
- result[0] = 0;
- return result;
- }
+ return SecureVector<byte>(1); // single 0 byte
+
+ const u32bit p_bits = point.get_curve().get_p().bits();
+
+ u32bit p_bytes = point.get_curve().get_p().bytes();
- u32bit l = point.get_curve().get_p().bits();
- int dummy = l & 7;
- if(dummy != 0)
- {
- l += 8 - dummy;
- }
- l /= 8;
- SecureVector<byte> result (l+1);
- result[0] = 2;
BigInt x = point.get_affine_x();
- SecureVector<byte> bX = BigInt::encode_1363(x, l);
- result.copy(1, bX.begin(), bX.size());
BigInt y = point.get_affine_y();
- if(y.get_bit(0))
- {
- result[0] |= 1;
- }
- return result;
- }
-SecureVector<byte> encode_uncompressed(const PointGFp& point)
- {
- if(point.is_zero())
+ SecureVector<byte> bX = BigInt::encode_1363(x, p_bytes);
+ SecureVector<byte> bY = BigInt::encode_1363(y, p_bytes);
+
+ if(format == PointGFp::UNCOMPRESSED)
{
- SecureVector<byte> result (1);
- result[0] = 0;
+ SecureVector<byte> result(2*p_bytes+1);
+ result[0] = 4;
+
+ result.copy(1, bX.begin(), p_bytes);
+ result.copy(p_bytes+1, bY.begin(), p_bytes);
return result;
}
- u32bit l = point.get_curve().get_p().bits();
- int dummy = l & 7;
- if(dummy != 0)
+ else if(format == PointGFp::COMPRESSED)
{
- l += 8 - dummy;
- }
- l /= 8;
- SecureVector<byte> result (2*l+1);
- result[0] = 4;
- BigInt x = point.get_affine_x();
- BigInt y = point.get_affine_y();
- SecureVector<byte> bX = BigInt::encode_1363(x, l);
- SecureVector<byte> bY = BigInt::encode_1363(y, l);
- result.copy(1, bX.begin(), l);
- result.copy(l+1, bY.begin(), l);
- return result;
+ SecureVector<byte> result(p_bytes+1);
+ result[0] = 2;
- }
+ result.copy(1, bX.begin(), bX.size());
+
+ if(y.get_bit(0))
+ result[0] |= 1;
-SecureVector<byte> encode_hybrid(const PointGFp& point)
- {
- if(point.is_zero())
- {
- SecureVector<byte> result (1);
- result[0] = 0;
return result;
}
- u32bit l = point.get_curve().get_p().bits();
- int dummy = l & 7;
- if(dummy != 0)
- {
- l += 8 - dummy;
- }
- l /= 8;
- SecureVector<byte> result (2*l+1);
- result[0] = 6;
- BigInt x = point.get_affine_x();
- BigInt y = point.get_affine_y();
- SecureVector<byte> bX = BigInt::encode_1363(x, l);
- SecureVector<byte> bY = BigInt::encode_1363(y, l);
- result.copy(1, bX.begin(), bX.size());
- result.copy(l+1, bY.begin(), bY.size());
- if(y.get_bit(0))
+ else if(format == PointGFp::HYBRID)
{
- result[0] |= 1;
+ SecureVector<byte> result(2*p_bytes+1);
+ result[0] = 6;
+
+ result.copy(1, bX.begin(), bX.size());
+ result.copy(p_bytes+1, bY.begin(), bY.size());
+
+ if(y.get_bit(0))
+ result[0] |= 1;
+
+ return result;
}
- return result;
+ else
+ throw Invalid_Argument("illegal point encoding format specification");
}
PointGFp OS2ECP(const MemoryRegion<byte>& os, const CurveGFp& curve)
diff --git a/src/math/gfpmath/point_gfp.h b/src/math/gfpmath/point_gfp.h
index e9645d5db..a0623c071 100644
--- a/src/math/gfpmath/point_gfp.h
+++ b/src/math/gfpmath/point_gfp.h
@@ -198,12 +198,7 @@ PointGFp BOTAN_DLL create_random_point(RandomNumberGenerator& rng,
// encoding and decoding
SecureVector<byte> BOTAN_DLL EC2OSP(const PointGFp& point, byte format);
-PointGFp BOTAN_DLL OS2ECP(MemoryRegion<byte> const& os, const CurveGFp& curve);
-
-/* Should these be private? */
-SecureVector<byte> BOTAN_DLL encode_uncompressed(const PointGFp& point);
-SecureVector<byte> BOTAN_DLL encode_hybrid(const PointGFp& point);
-SecureVector<byte> BOTAN_DLL encode_compressed(const PointGFp& point);
+PointGFp BOTAN_DLL OS2ECP(const MemoryRegion<byte>& os, const CurveGFp& curve);
// swaps the states of point1 and point2, does not throw!
// cf. Meyers, Item 25