summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2024-04-26 18:38:04 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2024-04-26 18:38:04 +0700
commit461911b242d4911c2ba922cb265c1670cad00498 (patch)
tree410fd2f2719e378232e0c1af386a3b39da0f083e
parentAdd implementation note from bc-java (diff)
downloadBouncyCastle.NET-ed25519-461911b242d4911c2ba922cb265c1670cad00498.tar.xz
Add new ECCurve methods for encoding lengths
-rw-r--r--crypto/src/asn1/x9/X9IntegerConverter.cs10
-rw-r--r--crypto/src/crypto/agreement/ECDHBasicAgreement.cs5
-rw-r--r--crypto/src/crypto/agreement/ECDHCBasicAgreement.cs5
-rw-r--r--crypto/src/crypto/agreement/ECMqvBasicAgreement.cs5
-rw-r--r--crypto/src/crypto/engines/SM2Engine.cs2
-rw-r--r--crypto/src/math/ec/ECCurve.cs39
6 files changed, 26 insertions, 40 deletions
diff --git a/crypto/src/asn1/x9/X9IntegerConverter.cs b/crypto/src/asn1/x9/X9IntegerConverter.cs
index e8f457114..586b3caef 100644
--- a/crypto/src/asn1/x9/X9IntegerConverter.cs
+++ b/crypto/src/asn1/x9/X9IntegerConverter.cs
@@ -7,15 +7,9 @@ namespace Org.BouncyCastle.Asn1.X9
 {
     public abstract class X9IntegerConverter
     {
-        public static int GetByteLength(ECFieldElement fe)
-        {
-            return (fe.FieldSize + 7) / 8;
-        }
+        public static int GetByteLength(ECFieldElement fe) => fe.GetEncodedLength();
 
-        public static int GetByteLength(ECCurve c)
-        {
-            return (c.FieldSize + 7) / 8;
-        }
+        public static int GetByteLength(ECCurve c) => c.FieldElementEncodingLength;
 
         public static byte[] IntegerToBytes(BigInteger s, int qLength)
         {
diff --git a/crypto/src/crypto/agreement/ECDHBasicAgreement.cs b/crypto/src/crypto/agreement/ECDHBasicAgreement.cs
index 700bb88e6..b226427c0 100644
--- a/crypto/src/crypto/agreement/ECDHBasicAgreement.cs
+++ b/crypto/src/crypto/agreement/ECDHBasicAgreement.cs
@@ -41,10 +41,7 @@ namespace Org.BouncyCastle.Crypto.Agreement
             this.privKey = ecPrivateKeyParameters;
         }
 
-        public virtual int GetFieldSize()
-        {
-            return (privKey.Parameters.Curve.FieldSize + 7) / 8;
-        }
+        public virtual int GetFieldSize() => privKey.Parameters.Curve.FieldElementEncodingLength;
 
         public virtual BigInteger CalculateAgreement(ICipherParameters pubKey)
         {
diff --git a/crypto/src/crypto/agreement/ECDHCBasicAgreement.cs b/crypto/src/crypto/agreement/ECDHCBasicAgreement.cs
index a58974953..35cb44142 100644
--- a/crypto/src/crypto/agreement/ECDHCBasicAgreement.cs
+++ b/crypto/src/crypto/agreement/ECDHCBasicAgreement.cs
@@ -45,10 +45,7 @@ namespace Org.BouncyCastle.Crypto.Agreement
             this.privKey = ecPrivateKeyParameters;
         }
 
-        public virtual int GetFieldSize()
-        {
-            return (privKey.Parameters.Curve.FieldSize + 7) / 8;
-        }
+        public virtual int GetFieldSize() => privKey.Parameters.Curve.FieldElementEncodingLength;
 
         public virtual BigInteger CalculateAgreement(ICipherParameters pubKey)
         {
diff --git a/crypto/src/crypto/agreement/ECMqvBasicAgreement.cs b/crypto/src/crypto/agreement/ECMqvBasicAgreement.cs
index 64f41dd78..bbf988d38 100644
--- a/crypto/src/crypto/agreement/ECMqvBasicAgreement.cs
+++ b/crypto/src/crypto/agreement/ECMqvBasicAgreement.cs
@@ -27,10 +27,7 @@ namespace Org.BouncyCastle.Crypto.Agreement
             this.privParams = mqvPrivateParameters;
         }
 
-        public virtual int GetFieldSize()
-        {
-            return (privParams.StaticPrivateKey.Parameters.Curve.FieldSize + 7) / 8;
-        }
+        public virtual int GetFieldSize() => privParams.StaticPrivateKey.Parameters.Curve.FieldElementEncodingLength;
 
         public virtual BigInteger CalculateAgreement(ICipherParameters pubKey)
         {
diff --git a/crypto/src/crypto/engines/SM2Engine.cs b/crypto/src/crypto/engines/SM2Engine.cs
index 9445f1466..64fb24375 100644
--- a/crypto/src/crypto/engines/SM2Engine.cs
+++ b/crypto/src/crypto/engines/SM2Engine.cs
@@ -78,7 +78,7 @@ namespace Org.BouncyCastle.Crypto.Engines
                 mRandom = null;
             }
 
-            mCurveLength = (mECParams.Curve.FieldSize + 7) / 8;
+            mCurveLength = mECParams.Curve.FieldElementEncodingLength;
         }
 
         public virtual byte[] ProcessBlock(byte[] input, int inOff, int inLen)
diff --git a/crypto/src/math/ec/ECCurve.cs b/crypto/src/math/ec/ECCurve.cs
index ae0d5d69e..c4a4691d5 100644
--- a/crypto/src/math/ec/ECCurve.cs
+++ b/crypto/src/math/ec/ECCurve.cs
@@ -110,6 +110,16 @@ namespace Org.BouncyCastle.Math.EC
             return new Config(this, this.m_coord, this.m_endomorphism, this.m_multiplier);
         }
 
+        public virtual int FieldElementEncodingLength => (FieldSize + 7) / 8;
+
+        public virtual int GetAffinePointEncodingLength(bool compressed)
+        {
+            int fieldLength = FieldElementEncodingLength;
+            return compressed
+                ?  1 + fieldLength
+                :  1 + fieldLength * 2;
+        }
+
         public virtual ECPoint ValidatePoint(BigInteger x, BigInteger y)
         {
             ECPoint p = CreatePoint(x, y);
@@ -362,24 +372,15 @@ namespace Org.BouncyCastle.Math.EC
          */
         public virtual ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
         {
-            int FE_BYTES = (FieldSize + 7) / 8;
+            int FE_BYTES = FieldElementEncodingLength;
             byte[] table = new byte[len * FE_BYTES * 2];
+            int pos = 0;
+            for (int i = 0; i < len; ++i)
             {
-                int pos = 0;
-                for (int i = 0; i < len; ++i)
-                {
-                    ECPoint p = points[off + i];
-                    byte[] px = p.RawXCoord.ToBigInteger().ToByteArray();
-                    byte[] py = p.RawYCoord.ToBigInteger().ToByteArray();
-
-                    int pxStart = px.Length > FE_BYTES ? 1 : 0, pxLen = px.Length - pxStart;
-                    int pyStart = py.Length > FE_BYTES ? 1 : 0, pyLen = py.Length - pyStart;
-
-                    Array.Copy(px, pxStart, table, pos + FE_BYTES - pxLen, pxLen); pos += FE_BYTES;
-                    Array.Copy(py, pyStart, table, pos + FE_BYTES - pyLen, pyLen); pos += FE_BYTES;
-                }
+                ECPoint p = points[off + i];
+                p.RawXCoord.EncodeTo(table, pos);       pos += FE_BYTES;
+                p.RawYCoord.EncodeTo(table, pos);       pos += FE_BYTES;
             }
-
             return new DefaultLookupTable(this, table, len);
         }
 
@@ -465,7 +466,7 @@ namespace Org.BouncyCastle.Math.EC
             return DecodePoint(encoded.AsSpan());
 #else
             ECPoint p;
-            int expectedLength = (FieldSize + 7) / 8;
+            int expectedLength = FieldElementEncodingLength;
 
             byte type = encoded[0];
             switch (type)
@@ -538,7 +539,7 @@ namespace Org.BouncyCastle.Math.EC
         public virtual ECPoint DecodePoint(ReadOnlySpan<byte> encoded)
         {
             ECPoint p;
-            int expectedLength = (FieldSize + 7) / 8;
+            int expectedLength = FieldElementEncodingLength;
 
             byte type = encoded[0];
             switch (type)
@@ -635,7 +636,7 @@ namespace Org.BouncyCastle.Math.EC
 
             public override ECPoint Lookup(int index)
             {
-                int FE_BYTES = (m_outer.FieldSize + 7) / 8;
+                int FE_BYTES = m_outer.FieldElementEncodingLength;
                 byte[] x = new byte[FE_BYTES], y = new byte[FE_BYTES];
                 int pos = 0;
 
@@ -657,7 +658,7 @@ namespace Org.BouncyCastle.Math.EC
 
             public override ECPoint LookupVar(int index)
             {
-                int FE_BYTES = (m_outer.FieldSize + 7) / 8;
+                int FE_BYTES = m_outer.FieldElementEncodingLength;
                 byte[] x = new byte[FE_BYTES], y = new byte[FE_BYTES];
                 int pos = index * FE_BYTES * 2;