static void Main(string[] args) { var a = 234234.ToOmgNum(); var b = "85430534954938530495935803495482304983".ToOmgNum(); var c = "100000000000000000000000000000000".ToOmgNum(); var d = "-100000000000000000".ToOmgNum(); var a1 = OmgOp.Add(a, c); var a2 = OmgOp.Subtract(a, c); var a3 = OmgOp.Multiply(a, b); var a4 = OmgOp.DivMod(b, c); var a6 = OmgOp.Pow(b, c, a); var a7 = OmgOp.Sqrt(b); var sys1 = new OmgEqSys(); sys1.AddEq(1287.ToOmgNum(), 447.ToOmgNum(), 516.ToOmgNum()); bool success1 = sys1.TrySolve(out List <(OmgNum root, OmgNum mod)> solution1); var sys2 = new OmgEqSys(); bool success2 = sys2.TrySolve(out var solution2); }
private OmgNum _FermaFactorization(Dictionary <OmgNum, OmgNum> result, OmgNum num) { OmgNum factor = null; if (OmgOp.DivMod(num, 2.ToOmgNum()).mod.IsZero()) { factor = 2.ToOmgNum(); } if (factor == null) { OmgNum a = OmgOp.Sqrt(num); OmgNum aSq = OmgOp.Multiply(a, a); bool isSquare = OmgOp.Equal(num, aSq); if (isSquare) { factor = a; } a.Inc(); aSq = OmgOp.Multiply(a, a); var s = OmgOp.Subtract(aSq, num); while (factor == null) { var testSqrt = OmgOp.Sqrt(s); if (OmgOp.Equal(s, OmgOp.Multiply(testSqrt, testSqrt))) { factor = (OmgOp.Equal(testSqrt, a)) ? OmgOp.Add(a, a) : OmgOp.Subtract(a, testSqrt); break; } a.Inc(); aSq = OmgOp.Multiply(a, a); s = OmgOp.Subtract(aSq, num); } } if (factor == null || factor.IsOne()) { return(num); } OmgNum pow = _FindPow(ref num, factor); if (result.ContainsKey(factor)) { result[factor] = OmgOp.Add(result[factor], pow); } else { result[factor] = pow; } return(num); }
private void _MakeGeronIteration() { OmgNum rootAprox = m_rootAprox.OmgWrapper(); var res = OmgOp.DivMod(m_targetSquareNum, rootAprox); res.mod.Release(); OmgNum sum = OmgOp.Add(rootAprox, res.div); res.div.Release(); sum.Raw.DivByTwo(); OmgPool.ReleaseNumber(m_buffer); m_buffer = m_rootAprox; m_rootAprox = sum.Raw; }
public static IEnumerable <OmgNum> Decode(IEnumerable <OmgNum> code, PrivKey key) { foreach (var num in code) { OmgNum m1 = OmgOp.Pow(num, key.dP, key.P); OmgNum m2 = OmgOp.Pow(num, key.dQ, key.Q); OmgNum mdif = OmgOp.Subtract(m1, m2); OmgNum h = OmgOp.Multiply(key.qInv, mdif, key.P); OmgNum hq = OmgOp.Multiply(h, key.Q); OmgNum m = OmgOp.Add(m2, hq); m1.Release(); m2.Release(); mdif.Release(); h.Release(); hq.Release(); yield return(m); } }
internal RawNum MultiplyKaratsuba(RawNum left, RawNum right) { if (left.IsZero() || right.IsZero()) { return(OmgPool.GetRawZero()); } int n = Math.Min(left.Size, right.Size); (RawNum head, RawNum tail)leftSep = _SeparateHead(left, n); (RawNum head, RawNum tail)rightSep = _SeparateHead(right, n); RawNum headsProd = Multiply(leftSep.head, rightSep.head); RawNum tailsProd = Multiply(leftSep.tail, rightSep.tail); RawNum headTail = Multiply(leftSep.head, rightSep.tail); RawNum tailHead = Multiply(leftSep.tail, rightSep.head); OmgNum midSum = OmgOp.Add(new OmgNum(headTail), new OmgNum(tailHead)); m_multRaw = OmgPool.GetRawZero(); m_multRaw.CopyFrom(tailsProd); midSum.Raw.Digits.InsertRange(0, Enumerable.Repeat <uint>(0, n)); headsProd.Digits.InsertRange(0, Enumerable.Repeat <uint>(0, 2 * n)); OmgNum res = OmgOp.Add(new OmgNum(tailsProd), midSum); OmgNum finalRes = OmgOp.Add(new OmgNum(headsProd), res); midSum.Release(); res.Release(); OmgPool.ReleaseNumber(headTail); OmgPool.ReleaseNumber(tailHead); return(finalRes.Raw); }
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); }