From f3a051f1b4b78051510550fc668aac54d141623f Mon Sep 17 00:00:00 2001 From: AmarnathCJD Date: Wed, 7 Feb 2024 15:19:25 +0530 Subject: [PATCH] fix bug, assiging to nil MAP in album Updates chan, add pollarid rho Algorithm in SplitPQ (15x faster) (Authkey Gen) --- internal/math/math.go | 41 +++++++++++++++++++++++++++++++++++++++++ telegram/updates.go | 3 +++ 2 files changed, 44 insertions(+) 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/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