mirror of
https://github.com/sochix/TLSharp.git
synced 2026-04-04 22:18:59 +00:00
formatting code (ctrl + k + d)
This commit is contained in:
parent
d330f9614b
commit
504b63a6d7
38 changed files with 16805 additions and 16280 deletions
|
|
@ -1,55 +1,72 @@
|
|||
using System;
|
||||
|
||||
namespace TLSharp.Core.MTProto.Crypto {
|
||||
public class FactorizedPair {
|
||||
namespace TLSharp.Core.MTProto.Crypto
|
||||
{
|
||||
public class FactorizedPair
|
||||
{
|
||||
private readonly BigInteger p;
|
||||
private readonly BigInteger q;
|
||||
|
||||
public FactorizedPair(BigInteger p, BigInteger q) {
|
||||
public FactorizedPair(BigInteger p, BigInteger q)
|
||||
{
|
||||
this.p = p;
|
||||
this.q = q;
|
||||
}
|
||||
|
||||
public FactorizedPair(long p, long q) {
|
||||
public FactorizedPair(long p, long q)
|
||||
{
|
||||
this.p = BigInteger.ValueOf(p);
|
||||
this.q = BigInteger.ValueOf(q);
|
||||
}
|
||||
|
||||
public BigInteger Min {
|
||||
get {
|
||||
public BigInteger Min
|
||||
{
|
||||
get
|
||||
{
|
||||
return p.Min(q);
|
||||
}
|
||||
}
|
||||
|
||||
public BigInteger Max {
|
||||
get {
|
||||
public BigInteger Max
|
||||
{
|
||||
get
|
||||
{
|
||||
return p.Max(q);
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString() {
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("P: {0}, Q: {1}", p, q);
|
||||
}
|
||||
}
|
||||
public class Factorizator {
|
||||
public class Factorizator
|
||||
{
|
||||
public static Random random = new Random();
|
||||
public static long findSmallMultiplierLopatin(long what) {
|
||||
public static long findSmallMultiplierLopatin(long what)
|
||||
{
|
||||
long g = 0;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
int q = (random.Next(128) & 15) + 17;
|
||||
long x = random.Next(1000000000) + 1, y = x;
|
||||
int lim = 1 << (i + 18);
|
||||
for (int j = 1; j < lim; j++) {
|
||||
for (int j = 1; j < lim; j++)
|
||||
{
|
||||
long a = x, b = x, c = q;
|
||||
while (b != 0) {
|
||||
if ((b & 1) != 0) {
|
||||
while (b != 0)
|
||||
{
|
||||
if ((b & 1) != 0)
|
||||
{
|
||||
c += a;
|
||||
if (c >= what) {
|
||||
if (c >= what)
|
||||
{
|
||||
c -= what;
|
||||
}
|
||||
}
|
||||
a += a;
|
||||
if (a >= what) {
|
||||
if (a >= what)
|
||||
{
|
||||
a -= what;
|
||||
}
|
||||
b >>= 1;
|
||||
|
|
@ -57,14 +74,17 @@ namespace TLSharp.Core.MTProto.Crypto {
|
|||
x = c;
|
||||
long z = x < y ? y - x : x - y;
|
||||
g = GCD(z, what);
|
||||
if (g != 1) {
|
||||
if (g != 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if ((j & (j - 1)) == 0) {
|
||||
if ((j & (j - 1)) == 0)
|
||||
{
|
||||
y = x;
|
||||
}
|
||||
}
|
||||
if (g > 1) {
|
||||
if (g > 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -73,15 +93,20 @@ namespace TLSharp.Core.MTProto.Crypto {
|
|||
return Math.Min(p, g);
|
||||
}
|
||||
|
||||
public static long GCD(long a, long b) {
|
||||
while (a != 0 && b != 0) {
|
||||
while ((b & 1) == 0) {
|
||||
public static long GCD(long a, long b)
|
||||
{
|
||||
while (a != 0 && b != 0)
|
||||
{
|
||||
while ((b & 1) == 0)
|
||||
{
|
||||
b >>= 1;
|
||||
}
|
||||
while ((a & 1) == 0) {
|
||||
while ((a & 1) == 0)
|
||||
{
|
||||
a >>= 1;
|
||||
}
|
||||
if (a > b) {
|
||||
if (a > b)
|
||||
{
|
||||
a -= b;
|
||||
}
|
||||
else {
|
||||
|
|
@ -91,16 +116,19 @@ namespace TLSharp.Core.MTProto.Crypto {
|
|||
return b == 0 ? a : b;
|
||||
}
|
||||
|
||||
public static FactorizedPair Factorize(BigInteger pq) {
|
||||
if(pq.BitLength < 64) {
|
||||
public static FactorizedPair Factorize(BigInteger pq)
|
||||
{
|
||||
if (pq.BitLength < 64)
|
||||
{
|
||||
long pqlong = pq.LongValue;
|
||||
long divisor = findSmallMultiplierLopatin(pqlong);
|
||||
return new FactorizedPair(BigInteger.ValueOf(divisor), BigInteger.ValueOf(pqlong/divisor));
|
||||
} else {
|
||||
return new FactorizedPair(BigInteger.ValueOf(divisor), BigInteger.ValueOf(pqlong / divisor));
|
||||
}
|
||||
else {
|
||||
// TODO: port pollard factorization
|
||||
throw new InvalidOperationException("pq too long; TODO: port the pollard algo");
|
||||
// logger.error("pq too long; TODO: port the pollard algo");
|
||||
// return null;
|
||||
// logger.error("pq too long; TODO: port the pollard algo");
|
||||
// return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue