summary refs log tree commit diff
path: root/crypto/src
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2015-03-23 13:56:59 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2015-03-23 13:56:59 +0700
commitadd1f6be39cfdebffc7e3220b99c11c25624ea6a (patch)
treef10a8ea66ff849e87f262fb5b7ab766ea892b1c6 /crypto/src
parentAdd ANSSI curve FRP256v1 and refactor curve registries (diff)
downloadBouncyCastle.NET-ed25519-add1f6be39cfdebffc7e3220b99c11c25624ea6a.tar.xz
Add SquarePow method to ECFieldElement
Diffstat (limited to 'crypto/src')
-rw-r--r--crypto/src/math/ec/ECFieldElement.cs24
1 files changed, 16 insertions, 8 deletions
diff --git a/crypto/src/math/ec/ECFieldElement.cs b/crypto/src/math/ec/ECFieldElement.cs
index e589fc737..27c4ba2d0 100644
--- a/crypto/src/math/ec/ECFieldElement.cs
+++ b/crypto/src/math/ec/ECFieldElement.cs
@@ -55,6 +55,16 @@ namespace Org.BouncyCastle.Math.EC
             return Square().Add(x.Multiply(y));
         }
 
+        public virtual ECFieldElement SquarePow(int pow)
+        {
+            ECFieldElement r = this;
+            for (int i = 0; i < pow; ++i)
+            {
+                r = r.Square();
+            }
+            return r;
+        }
+
         public virtual bool TestBitZero()
         {
             return ToBigInteger().TestBit(0);
@@ -812,6 +822,11 @@ namespace Org.BouncyCastle.Math.EC
             return new F2mFieldElement(m, ks, aa);
         }
 
+        public override ECFieldElement SquarePow(int pow)
+        {
+            return pow < 1 ? this : new F2mFieldElement(m, ks, x.ModSquareN(pow, m, ks));
+        }
+
         public override ECFieldElement Invert()
         {
             return new F2mFieldElement(this.m, this.ks, this.x.ModInverse(m, ks));
@@ -819,14 +834,7 @@ namespace Org.BouncyCastle.Math.EC
 
         public override ECFieldElement Sqrt()
         {
-            LongArray x1 = this.x;
-            if (x1.IsOne() || x1.IsZero())
-            {
-                return this;
-            }
-
-            LongArray x2 = x1.ModSquareN(m - 1, m, ks);
-            return new F2mFieldElement(m, ks, x2);
+            return (x.IsZero() || x.IsOne()) ? this : SquarePow(m - 1);
         }
 
         /**