< Summary

Information
Class: Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Multiplier.WNafL2RMultiplier
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\math\ec\multiplier\WNafL2RMultiplier.cs
Line coverage
100%
Covered lines: 44
Uncovered lines: 0
Coverable lines: 44
Total lines: 98
Line coverage: 100%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
MultiplyPositive(...)90%10100%
GetWindowSize(...)100%1100%

File(s)

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

#LineLine coverage
 1using System;
 2
 3namespace Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Multiplier
 4{
 5    /**
 6    * Class implementing the WNAF (Window Non-Adjacent Form) multiplication
 7    * algorithm.
 8    */
 9    internal class WNafL2RMultiplier
 10        : AbstractECMultiplier
 11    {
 12        /**
 13         * Multiplies <code>this</code> by an integer <code>k</code> using the
 14         * Window NAF method.
 15         * @param k The integer by which <code>this</code> is multiplied.
 16         * @return A new <code>ECPoint</code> which equals <code>this</code>
 17         * multiplied by <code>k</code>.
 18         */
 19        protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
 920        {
 21            // Clamp the window width in the range [2, 16]
 922            int width = System.Math.Max(2, System.Math.Min(16, GetWindowSize(k.BitLength)));
 23
 924            WNafPreCompInfo wnafPreCompInfo = WNafUtilities.Precompute(p, width, true);
 925            ECPoint[] preComp = wnafPreCompInfo.PreComp;
 926            ECPoint[] preCompNeg = wnafPreCompInfo.PreCompNeg;
 27
 928            int[] wnaf = WNafUtilities.GenerateCompactWindowNaf(width, k);
 29
 930            ECPoint R = p.Curve.Infinity;
 31
 932            int i = wnaf.Length;
 33
 34            /*
 35             * NOTE: We try to optimize the first window using the precomputed points to substitute an
 36             * addition for 2 or more doublings.
 37             */
 938            if (i > 1)
 939            {
 940                int wi = wnaf[--i];
 1841                int digit = wi >> 16, zeroes = wi & 0xFFFF;
 42
 943                int n = System.Math.Abs(digit);
 944                ECPoint[] table = digit < 0 ? preCompNeg : preComp;
 45
 46                // Optimization can only be used for values in the lower half of the table
 947                if ((n << 2) < (1 << width))
 748                {
 749                    int highest = LongArray.BitLengths[n];
 50
 51                    // TODO Get addition/doubling cost ratio from curve and compare to 'scale' to see if worth substitut
 752                    int scale = width - highest;
 753                    int lowBits = n ^ (1 << (highest - 1));
 54
 755                    int i1 = ((1 << (width - 1)) - 1);
 756                    int i2 = (lowBits << scale) + 1;
 757                    R = table[i1 >> 1].Add(table[i2 >> 1]);
 58
 759                    zeroes -= scale;
 60
 61                    //Console.WriteLine("Optimized: 2^" + scale + " * " + n + " = " + i1 + " + " + i2);
 762                }
 63                else
 264                {
 265                    R = table[n >> 1];
 266                }
 67
 968                R = R.TimesPow2(zeroes);
 969            }
 70
 52171            while (i > 0)
 51272            {
 51273                int wi = wnaf[--i];
 102474                int digit = wi >> 16, zeroes = wi & 0xFFFF;
 75
 51276                int n = System.Math.Abs(digit);
 51277                ECPoint[] table = digit < 0 ? preCompNeg : preComp;
 51278                ECPoint r = table[n >> 1];
 79
 51280                R = R.TwicePlus(r);
 51281                R = R.TimesPow2(zeroes);
 51282            }
 83
 984            return R;
 985        }
 86
 87        /**
 88         * Determine window width to use for a scalar multiplication of the given size.
 89         *
 90         * @param bits the bit-length of the scalar to multiply by
 91         * @return the window size to use
 92         */
 93        protected virtual int GetWindowSize(int bits)
 994        {
 995            return WNafUtilities.GetWindowSize(bits);
 996        }
 97    }
 98}