Exemplo n.º 1
0
        private bool _TestFailed(OmgNum num)
        {
            m_primeTester.SetTestedNumber(num.Copy());

            for (int i = 0; i < m_testsPerNumber; i++)
            {
                OmgNum testBase = OmgOp.Random(OmgNum.GetConst(2), num);
                bool   isPrime  = m_primeTester.IsPrimeToBase(testBase);
                testBase.Release();

                if (!isPrime)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        private OmgNum _FindFactorsWithPollard(Dictionary <OmgNum, OmgNum> result, OmgNum num)
        {
            num = num.Copy();

            while (!num.IsOne())
            {
                OmgNum factor = m_polard.FindFactor(num, maxIterations: 100000);
                if (factor == null || factor.IsOne() || OmgOp.Equal(factor, num))
                {
                    return(num);
                }

                OmgNum pow = _FindPow(ref num, factor);
                result[factor] = pow;
            }

            return(num);
        }
Exemplo n.º 3
0
        public SignaureKey GenerateSignature(int keyByteLength)
        {
            int componentLength = keyByteLength * 8 / 2;

            OmgNum p = m_primeSource.GeneratePrime(componentLength);
            OmgNum q = m_primeSource.GeneratePrime(componentLength);

            OmgNum n     = OmgOp.Multiply(p, q);
            OmgNum carmN = _Carmichael(p.Dec(), q.Dec());
            OmgNum e     = _SelectRandomExponent(carmN, out OmgNum d);

            p.Release();
            p.Release();

            return(new SignaureKey {
                cert = new SignCertificate {
                    N = n.Copy(), D = d
                },
                secret = new SignSecret()
                {
                    N = n, E = e
                }
            });
        }
Exemplo n.º 4
0
        public Dictionary <OmgNum, OmgNum> Factorize(OmgNum num)
        {
            var initNum = num.Copy();
            var result  = new Dictionary <OmgNum, OmgNum>();

            if (_IsSmallPrime(result, num))
            {
                return(result);
            }

            num = _FindFactorsWithPollard(result, num);
            num = _FermaFactorization(result, num);

            if (!num.IsOne())
            {
                result[num] = 1.ToOmgNum();
            }

            if (result.Count == 1 && result.TryGetValue(initNum, out var power) && power.IsOne())
            {
                return(result);
            }

            var nonPrimeFactors = new Stack <OmgNum>(result.Keys);

            while (nonPrimeFactors.Count > 0)
            {
                var factor = nonPrimeFactors.Pop();
                if (result[factor].IsZero())
                {
                    result.Remove(factor);
                    continue;
                }

                var primeFactors = Factorize(factor);
                if (primeFactors.Count == 1 && primeFactors.Values.First().IsOne())
                {
                    continue;
                }

                var topPower = result[factor];
                result.Remove(factor);

                foreach (var primeFactor in primeFactors)
                {
                    var pow = OmgOp.Multiply(topPower, primeFactor.Value);
                    if (result.ContainsKey(primeFactor.Key))
                    {
                        result[primeFactor.Key] = OmgOp.Add(result[primeFactor.Key], pow);
                    }
                    else
                    {
                        result[primeFactor.Key] = pow;
                    }
                }
            }

            var keys = new List <OmgNum>(result.Keys);

            foreach (var key in keys)
            {
                if (result[key].IsZero())
                {
                    result.Remove(key);
                }
            }

            return(result);
        }