diff --git a/internal/math/math.go b/internal/math/math.go index a80c99af..1a551c25 100755 --- a/internal/math/math.go +++ b/internal/math/math.go @@ -38,6 +38,8 @@ func DoRSAencrypt(block []byte, key *rsa.PublicKey) []byte { // SplitPQ splits a number into two primes, while p1 < p2 // Part of diffie hellman's algorithm, how it works - no idea func SplitPQ(pq *big.Int) (p1, p2 *big.Int) { + // Benchmark: Fac 15x faster than SplitPQ + return Fac(pq) // TODO: test extensively for fail cases rndmax := big.NewInt(0).SetBit(big.NewInt(0), 64, 1) what := big.NewInt(0).Set(pq) @@ -127,3 +129,42 @@ func Xor(dst, src []byte) { dst[i] ^= src[i] } } + +// Fac splits a number into two primes, while p < q +// Part of diffie hellman's algorithm +// Uses Pollard's rho algorithm +func Fac(pq *big.Int) (p, q *big.Int) { + p = big.NewInt(0).Set(pq) + q = big.NewInt(1) + + x := big.NewInt(2) + y := big.NewInt(2) + d := big.NewInt(1) + + for d.Cmp(big.NewInt(1)) == 0 { + x = f(x, pq) + y = f(f(y, pq), pq) + + temp := big.NewInt(0).Set(x) + temp.Sub(temp, y) + temp.Abs(temp) + d.GCD(nil, nil, temp, pq) + } + + p.Set(d) + q.Div(pq, d) + + if p.Cmp(q) == 1 { + p, q = q, p + } + + return p, q +} + +func f(x, n *big.Int) *big.Int { + result := big.NewInt(0).Set(x) + result.Mul(result, result) + result.Add(result, big.NewInt(1)) + result.Mod(result, n) + return result +} diff --git a/telegram/cache.go b/telegram/cache.go index 3d42e006..fe56e917 100644 --- a/telegram/cache.go +++ b/telegram/cache.go @@ -68,8 +68,8 @@ func NewCache(logLevel string) *CACHE { // --------- Cache file Functions --------- func (c *CACHE) WriteFile() { - c.Lock() - defer c.Unlock() + //c.Lock() + //defer c.Unlock() // necessary? if c.file == nil { var err error diff --git a/telegram/helpers.go b/telegram/helpers.go index 6397babb..671829aa 100644 --- a/telegram/helpers.go +++ b/telegram/helpers.go @@ -448,7 +448,7 @@ mediaTypeSwitch: break } if a, ok := at.(*DocumentAttributeVideo); ok { - var duration = int64(getValue(a.Duration, int32(GetVideoDuration(fileName))).(int32)) + var duration = int64(getValue(int32(a.Duration), int32(GetVideoDuration(fileName))).(int32)) if a.W == 0 || a.H == 0 { w, h := GetVideoDimensions(fileName) if w > 0 && h > 0 { diff --git a/telegram/updates.go b/telegram/updates.go index e528d5e9..5a64b17f 100644 --- a/telegram/updates.go +++ b/telegram/updates.go @@ -190,6 +190,9 @@ func (c *Client) handleAlbum(message MessageObj) { messages: []*NewMessage{packMessage(c, &message)}, groupedId: message.GroupedID, } + if c.dispatcher.activeAlbums == nil { + c.dispatcher.activeAlbums = make(map[int64]*albumBox) + } c.dispatcher.activeAlbums[message.GroupedID] = abox go func() { <-abox.waitExit