< Summary

Information
Class: Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Multiplier.WTauNafMultiplier
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\math\ec\multiplier\WTauNafMultiplier.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 63
Coverable lines: 63
Total lines: 138
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.cctor()100%10%
MultiplyPositive(...)0%20%
MultiplyWTnaf(...)0%20%
MultiplyFromWTnaf(...)0%100%
.ctor(...)100%10%
Precompute(...)0%20%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\math\ec\multiplier\WTauNafMultiplier.cs

#LineLine coverage
 1using System;
 2
 3using Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Abc;
 4
 5namespace Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Multiplier
 6{
 7    /**
 8    * Class implementing the WTNAF (Window
 9    * <code>&#964;</code>-adic Non-Adjacent Form) algorithm.
 10    */
 11    internal class WTauNafMultiplier
 12        : AbstractECMultiplier
 13    {
 14        // TODO Create WTauNafUtilities class and move various functionality into it
 015        internal static readonly string PRECOMP_NAME = "bc_wtnaf";
 16
 17        /**
 18        * Multiplies a {@link org.bouncycastle.math.ec.AbstractF2mPoint AbstractF2mPoint}
 19        * by <code>k</code> using the reduced <code>&#964;</code>-adic NAF (RTNAF)
 20        * method.
 21        * @param p The AbstractF2mPoint to multiply.
 22        * @param k The integer by which to multiply <code>k</code>.
 23        * @return <code>p</code> multiplied by <code>k</code>.
 24        */
 25        protected override ECPoint MultiplyPositive(ECPoint point, BigInteger k)
 026        {
 027            if (!(point is AbstractF2mPoint))
 028                throw new ArgumentException("Only AbstractF2mPoint can be used in WTauNafMultiplier");
 29
 030            AbstractF2mPoint p = (AbstractF2mPoint)point;
 031            AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve;
 032            int m = curve.FieldSize;
 033            sbyte a = (sbyte)curve.A.ToBigInteger().IntValue;
 034            sbyte mu = Tnaf.GetMu(a);
 035            BigInteger[] s = curve.GetSi();
 36
 037            ZTauElement rho = Tnaf.PartModReduction(k, m, a, s, mu, (sbyte)10);
 38
 039            return MultiplyWTnaf(p, rho, a, mu);
 040        }
 41
 42        /**
 43        * Multiplies a {@link org.bouncycastle.math.ec.AbstractF2mPoint AbstractF2mPoint}
 44        * by an element <code>&#955;</code> of <code><b>Z</b>[&#964;]</code> using
 45        * the <code>&#964;</code>-adic NAF (TNAF) method.
 46        * @param p The AbstractF2mPoint to multiply.
 47        * @param lambda The element <code>&#955;</code> of
 48        * <code><b>Z</b>[&#964;]</code> of which to compute the
 49        * <code>[&#964;]</code>-adic NAF.
 50        * @return <code>p</code> multiplied by <code>&#955;</code>.
 51        */
 52        private AbstractF2mPoint MultiplyWTnaf(AbstractF2mPoint p, ZTauElement lambda,
 53            sbyte a, sbyte mu)
 054        {
 055            ZTauElement[] alpha = (a == 0) ? Tnaf.Alpha0 : Tnaf.Alpha1;
 56
 057            BigInteger tw = Tnaf.GetTw(mu, Tnaf.Width);
 58
 059            sbyte[]u = Tnaf.TauAdicWNaf(mu, lambda, Tnaf.Width,
 060                BigInteger.ValueOf(Tnaf.Pow2Width), tw, alpha);
 61
 062            return MultiplyFromWTnaf(p, u);
 063        }
 64
 65        /**
 66        * Multiplies a {@link org.bouncycastle.math.ec.AbstractF2mPoint AbstractF2mPoint}
 67        * by an element <code>&#955;</code> of <code><b>Z</b>[&#964;]</code>
 68        * using the window <code>&#964;</code>-adic NAF (TNAF) method, given the
 69        * WTNAF of <code>&#955;</code>.
 70        * @param p The AbstractF2mPoint to multiply.
 71        * @param u The the WTNAF of <code>&#955;</code>..
 72        * @return <code>&#955; * p</code>
 73        */
 74        private static AbstractF2mPoint MultiplyFromWTnaf(AbstractF2mPoint p, sbyte[] u)
 075        {
 076            AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve;
 077            sbyte a = (sbyte)curve.A.ToBigInteger().IntValue;
 78
 079            WTauNafCallback callback = new WTauNafCallback(p, a);
 080            WTauNafPreCompInfo preCompInfo = (WTauNafPreCompInfo)curve.Precompute(p, PRECOMP_NAME, callback);
 081            AbstractF2mPoint[] pu = preCompInfo.PreComp;
 82
 83            // TODO Include negations in precomp (optionally) and use from here
 084            AbstractF2mPoint[] puNeg = new AbstractF2mPoint[pu.Length];
 085            for (int i = 0; i < pu.Length; ++i)
 086            {
 087                puNeg[i] = (AbstractF2mPoint)pu[i].Negate();
 088            }
 89
 90
 91            // q = infinity
 092            AbstractF2mPoint q = (AbstractF2mPoint) p.Curve.Infinity;
 93
 094            int tauCount = 0;
 095            for (int i = u.Length - 1; i >= 0; i--)
 096            {
 097                ++tauCount;
 098                int ui = u[i];
 099                if (ui != 0)
 0100                {
 0101                    q = q.TauPow(tauCount);
 0102                    tauCount = 0;
 103
 0104                    ECPoint x = ui > 0 ? pu[ui >> 1] : puNeg[(-ui) >> 1];
 0105                    q = (AbstractF2mPoint)q.Add(x);
 0106                }
 0107            }
 0108            if (tauCount > 0)
 0109            {
 0110                q = q.TauPow(tauCount);
 0111            }
 0112            return q;
 0113        }
 114
 115        private class WTauNafCallback
 116            : IPreCompCallback
 117        {
 118            private readonly AbstractF2mPoint m_p;
 119            private readonly sbyte m_a;
 120
 0121            internal WTauNafCallback(AbstractF2mPoint p, sbyte a)
 0122            {
 0123                this.m_p = p;
 0124                this.m_a = a;
 0125            }
 126
 127            public PreCompInfo Precompute(PreCompInfo existing)
 0128            {
 0129                if (existing is WTauNafPreCompInfo)
 0130                    return existing;
 131
 0132                WTauNafPreCompInfo result = new WTauNafPreCompInfo();
 0133                result.PreComp = Tnaf.GetPreComp(m_p, m_a);
 0134                return result;
 0135            }
 136        }
 137    }
 138}