From 1e5b98a08b5a2d45d77c6efb168da88bbdf0dc53 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 2 Oct 2013 11:13:58 +0700 Subject: Some minor optimizations to modInverse --- .../src/main/java/org/bouncycastle/math/ec/IntArray.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'core/src/main/java/org/bouncycastle/math') diff --git a/core/src/main/java/org/bouncycastle/math/ec/IntArray.java b/core/src/main/java/org/bouncycastle/math/ec/IntArray.java index 89e21a4b..5e61b799 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/IntArray.java +++ b/core/src/main/java/org/bouncycastle/math/ec/IntArray.java @@ -577,6 +577,12 @@ class IntArray // Input: A nonzero polynomial a(z) of degree at most m-1 // Output: a(z)^(-1) mod f(z) + int uzDegree = degree(); + if (uzDegree == 1) + { + return this; + } + // u(z) := a(z) IntArray uz = (IntArray)clone(); @@ -598,10 +604,10 @@ class IntArray g1z.setBit(0); IntArray g2z = new IntArray(t); - while (!uz.isZero()) + while (uzDegree != 0) { // j := deg(u(z)) - deg(v(z)) - int j = uz.degree() - vz.degree(); + int j = uzDegree - vz.degree(); // If j < 0 then: u(z) <-> v(z), g1(z) <-> g2(z), j := -j if (j < 0) @@ -624,10 +630,14 @@ class IntArray // = deg(u(z)) // uz = uz.xor(vz.shiftLeft(j)); uz.addShiftedByBits(vz, j); + uzDegree = uz.degree(); // g1(z) := g1(z) + z^j * g2(z) // g1z = g1z.xor(g2z.shiftLeft(j)); - g1z.addShiftedByBits(g2z, j); + if (uzDegree != 0) + { + g1z.addShiftedByBits(g2z, j); + } } return g2z; } -- cgit v1.2.3