2

I need to calculate $\frac{a^p-1}{a-1}\mod m$ to compute a repeated application of a linear transform under a modulus.

((a ** p - 1) // (a - 1)) % m == ((pow(a, p, x) - 1) // (a - 1)) % m

How could I figure an x that would work given a, p, and m? Could it just be x = ((a - 1) * m)? They're all integers and m is prime. It's just that a ** p is simply far to large and for to slow to calculate, and it's unnecessary.

I'm trying to compute the composition of linear transforms. Say there are two functions:

A = lambda x: (a * x + b) % m
B = lambda x: (c * x + d) % m

Composing them is simple. But calculating the repeated application of A is what I'm trying to do. Define the function C as A(x) == C(A, 1)(x), A(A(x)) == C(A, 2)(x), A(A(A(x))) == C(A, 3)(x), and so on. B works too.

C(A, n)(x) == (pow(a, n, m) * x + b * ((a ** n - 1) // (a - 1))) % m
C(B, n)(x) == (pow(c, n, m) * x + d * ((c ** n - 1) // (c - 1))) % m

I'm not trying to back-calculate A. I'm trying to forward calculate C.

YoungCoder5
  • 845
  • 1
  • 7
  • 14
  • 2
    I think it is important to specify how large are each integer. At first glance, I would say that it looks like you need to compute some discrete logarithm to find the answer but it is computationally factorizing large numbers if the integers are larges (which is known to be very hard to do efficiently). It is probably wise to post such question on https://mathoverflow.net/ first as this is rather a math problem. – Jérôme Richard Feb 28 '22 at 19:47
  • 2
    Why not just `x = (a - 1) * m`? – Mark Dickinson Feb 28 '22 at 19:47
  • 2
    @JérômeRichard: I don't think there's any need for integer factorization or discrete logarithm here. Computing `(pow(a, p, (a-1)*m) - 1) // (a-1) % m` is enough. – Mark Dickinson Feb 28 '22 at 19:55
  • @MarkDickinson Indeed, it looks like it works in practice. I did not see this analytical solution. Is there a simple proof? – Jérôme Richard Feb 28 '22 at 20:07
  • @JérômeRichard Yes. https://pastebin.com/5wHQ45hK – YoungCoder5 Feb 28 '22 at 20:19
  • 2
    @JérômeRichard: Yes. `pow(a, p, (a-1)*m) - 1` is congruent to `a**p - 1` modulo `(a-1)*m`. Now simply divide through by `a-1` to deduce that `(pow(a, p, (a-1)*m) - 1) // (a - 1)` is congruent to `(a**p - 1) // (a - 1)` modulo `m`. (Basic law of congruences: if `ka ≡ kb (mod km)` then `a ≡ b (mod m)`, for integers `a` and `b` and positive integers `k` and `m`.) – Mark Dickinson Feb 28 '22 at 20:21

0 Answers0