diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-09-25 15:07:52 +0400 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-09-25 15:07:52 +0400 |
commit | 15761ce5e261b9d2241213a86a9956d618abfdd7 (patch) | |
tree | c03a314ae1559e5015d44c596b6f53fbc1e26647 /core/src/main/java/org/bouncycastle/math | |
parent | e7e45cbcbbddc29976e2c7a9d71a37de6fb1ff1e (diff) |
Add NafR2LMultiplier
Diffstat (limited to 'core/src/main/java/org/bouncycastle/math')
3 files changed, 56 insertions, 1 deletions
diff --git a/core/src/main/java/org/bouncycastle/math/ec/MixedNafR2LMultiplier.java b/core/src/main/java/org/bouncycastle/math/ec/MixedNafR2LMultiplier.java index 2b700fbc..294e6b0b 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/MixedNafR2LMultiplier.java +++ b/core/src/main/java/org/bouncycastle/math/ec/MixedNafR2LMultiplier.java @@ -2,10 +2,18 @@ package org.bouncycastle.math.ec; import java.math.BigInteger; +/** + * Class implementing the NAF (Non-Adjacent Form) multiplication algorithm (right-to-left) using + * mixed coordinates. + */ public class MixedNafR2LMultiplier implements ECMultiplier { protected int additionCoord, doublingCoord; + /** + * By default, addition will be done in Jacobian coordinates, and doubling will be done in + * Modified Jacobian coordinates (independent of the original coordinate system of each point). + */ public MixedNafR2LMultiplier() { this(ECCurve.COORD_JACOBIAN, ECCurve.COORD_JACOBIAN_MODIFIED); diff --git a/core/src/main/java/org/bouncycastle/math/ec/NafL2RMultiplier.java b/core/src/main/java/org/bouncycastle/math/ec/NafL2RMultiplier.java index d94d25dc..09c15d7e 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/NafL2RMultiplier.java +++ b/core/src/main/java/org/bouncycastle/math/ec/NafL2RMultiplier.java @@ -3,7 +3,7 @@ package org.bouncycastle.math.ec; import java.math.BigInteger; /** - * Class implementing the NAF (Non-Adjacent Form) multiplication algorithm. + * Class implementing the NAF (Non-Adjacent Form) multiplication algorithm (left-to-right). */ public class NafL2RMultiplier implements ECMultiplier { diff --git a/core/src/main/java/org/bouncycastle/math/ec/NafR2LMultiplier.java b/core/src/main/java/org/bouncycastle/math/ec/NafR2LMultiplier.java new file mode 100644 index 00000000..9d652418 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/math/ec/NafR2LMultiplier.java @@ -0,0 +1,47 @@ +package org.bouncycastle.math.ec; + +import java.math.BigInteger; + +/** + * Class implementing the NAF (Non-Adjacent Form) multiplication algorithm (right-to-left). + */ +public class NafR2LMultiplier implements ECMultiplier +{ + public ECPoint multiply(ECPoint p, BigInteger k, PreCompInfo preCompInfo) + { + if (k.signum() < 0) + { + throw new IllegalArgumentException("'k' cannot be negative"); + } + if (k.signum() == 0) + { + return p.getCurve().getInfinity(); + } + + p = p.normalize(); + + ECPoint R0 = p.getCurve().getInfinity(), R1 = p; + + int[] naf = WNafUtil.generateCompactNaf(k); + int zeroes = 0; + + for (int i = 0; i < naf.length; ++i) + { + int ni = naf[i]; + int digit = ni >> 16; + zeroes += ni & 0xFFFF; + + while (--zeroes >= 0) + { + R1 = R1.twice(); + } + + ECPoint r = digit < 0 ? R1.negate() : R1; + R0 = R0.add(r); + + zeroes = 1; + } + + return R0; + } +} |