< Summary

Information
Class: Renci.SshNet.Security.Org.BouncyCastle.Math.Raw.Mod
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\math\raw\Mod.cs
Line coverage
66%
Covered lines: 92
Uncovered lines: 46
Coverable lines: 138
Total lines: 185
Line coverage: 66.6%
Branch coverage
75%
Covered branches: 30
Total branches: 40
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.cctor()100%10%
Invert(...)83.33%1886.53%
Random(...)0%20%
Add(...)0%20%
Subtract(...)0%20%
InversionResult(...)100%2100%
InversionStep(...)91.66%1287.5%
GetTrailingZeroes(...)100%2100%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\math\raw\Mod.cs

#LineLine coverage
 1using System;
 2using System.Diagnostics;
 3
 4using Renci.SshNet.Security.Org.BouncyCastle.Crypto.Utilities;
 5using Renci.SshNet.Security.Org.BouncyCastle.Security;
 6
 7namespace Renci.SshNet.Security.Org.BouncyCastle.Math.Raw
 8{
 9    internal abstract class Mod
 10    {
 011        private static readonly SecureRandom RandomSource = new SecureRandom();
 12
 13        public static void Invert(uint[] p, uint[] x, uint[] z)
 3314        {
 3315            int len = p.Length;
 3316            if (Nat.IsZero(len, x))
 017                throw new ArgumentException("cannot be 0", "x");
 3318            if (Nat.IsOne(len, x))
 019            {
 020                Array.Copy(x, 0, z, 0, len);
 021                return;
 22            }
 23
 3324            uint[] u = Nat.Copy(len, x);
 3325            uint[] a = Nat.Create(len);
 3326            a[0] = 1;
 3327            int ac = 0;
 28
 3329            if ((u[0] & 1) == 0)
 1630            {
 1631                InversionStep(p, u, len, a, ref ac);
 1632            }
 3333            if (Nat.IsOne(len, u))
 034            {
 035                InversionResult(p, ac, a, z);
 036                return;
 37            }
 38
 3339            uint[] v = Nat.Copy(len, p);
 3340            uint[] b = Nat.Create(len);
 3341            int bc = 0;
 42
 3343            int uvLen = len;
 44
 45            for (;;)
 894746            {
 932147                while (u[uvLen - 1] == 0 && v[uvLen - 1] == 0)
 37448                {
 37449                    --uvLen;
 37450                }
 51
 894752                if (Nat.Gte(len, u, v))
 439353                {
 439354                    Nat.SubFrom(len, v, u);
 439355                    Debug.Assert((u[0] & 1) == 0);
 439356                    ac += Nat.SubFrom(len, b, a) - bc;
 439357                    InversionStep(p, u, uvLen, a, ref ac);
 439358                    if (Nat.IsOne(len, u))
 2059                    {
 2060                        InversionResult(p, ac, a, z);
 2061                        return;
 62                    }
 437363                }
 64                else
 455465                {
 455466                    Nat.SubFrom(len, u, v);
 455467                    Debug.Assert((v[0] & 1) == 0);
 455468                    bc += Nat.SubFrom(len, a, b) - ac;
 455469                    InversionStep(p, v, uvLen, b, ref bc);
 455470                    if (Nat.IsOne(len, v))
 1371                    {
 1372                        InversionResult(p, bc, b, z);
 1373                        return;
 74                    }
 454175                }
 891476            }
 3377        }
 78
 79        public static uint[] Random(uint[] p)
 080        {
 081            int len = p.Length;
 082            uint[] s = Nat.Create(len);
 83
 084            uint m = p[len - 1];
 085            m |= m >> 1;
 086            m |= m >> 2;
 087            m |= m >> 4;
 088            m |= m >> 8;
 089            m |= m >> 16;
 90
 91            do
 092            {
 093                byte[] bytes = new byte[len << 2];
 094                RandomSource.NextBytes(bytes);
 095                Pack.BE_To_UInt32(bytes, 0, s);
 096                s[len - 1] &= m;
 097            }
 098            while (Nat.Gte(len, s, p));
 99
 0100            return s;
 0101        }
 102
 103        public static void Add(uint[] p, uint[] x, uint[] y, uint[] z)
 0104        {
 0105            int len = p.Length;
 0106            uint c = Nat.Add(len, x, y, z);
 0107            if (c != 0)
 0108            {
 0109                Nat.SubFrom(len, p, z);
 0110            }
 0111        }
 112
 113        public static void Subtract(uint[] p, uint[] x, uint[] y, uint[] z)
 0114        {
 0115            int len = p.Length;
 0116            int c = Nat.Sub(len, x, y, z);
 0117            if (c != 0)
 0118            {
 0119                Nat.AddTo(len, p, z);
 0120            }
 0121        }
 122
 123        private static void InversionResult(uint[] p, int ac, uint[] a, uint[] z)
 33124        {
 33125            if (ac < 0)
 15126            {
 15127                Nat.Add(p.Length, a, p, z);
 15128            }
 129            else
 18130            {
 18131                Array.Copy(a, 0, z, 0, p.Length);
 18132            }
 33133        }
 134
 135        private static void InversionStep(uint[] p, uint[] u, int uLen, uint[] x, ref int xc)
 8963136        {
 8963137            int len = p.Length;
 8963138            int count = 0;
 8963139            while (u[0] == 0)
 0140            {
 0141                Nat.ShiftDownWord(uLen, u, 0);
 0142                count += 32;
 0143            }
 144
 8963145            {
 8963146                int zeroes = GetTrailingZeroes(u[0]);
 8963147                if (zeroes > 0)
 8963148                {
 8963149                    Nat.ShiftDownBits(uLen, u, zeroes, 0);
 8963150                    count += zeroes;
 8963151                }
 8963152            }
 153
 53744154            for (int i = 0; i < count; ++i)
 17909155            {
 17909156                if ((x[0] & 1) != 0)
 5552157                {
 5552158                    if (xc < 0)
 2759159                    {
 2759160                        xc += (int)Nat.AddTo(len, p, x);
 2759161                    }
 162                    else
 2793163                    {
 2793164                        xc += Nat.SubFrom(len, p, x);
 2793165                    }
 5552166                }
 167
 17909168                Debug.Assert(xc == 0 || xc == -1);
 17909169                Nat.ShiftDownBit(len, x, (uint)xc);
 17909170            }
 8963171        }
 172
 173        private static int GetTrailingZeroes(uint x)
 8963174        {
 8963175            Debug.Assert(x != 0);
 8963176            int count = 0;
 26872177            while ((x & 1) == 0)
 17909178            {
 17909179                x >>= 1;
 17909180                ++count;
 17909181            }
 8963182            return count;
 8963183        }
 184    }
 185}