2
$\begingroup$

I'm trying to apply the square and multiply algorithm and I'm getting strange results even though I'm pretty sure I've done everything right. I'm trying to calculate $5^{25} \mod 193$:

Binary representation of 25: 11001

\begin{equation} \begin{split} 5^2 = 25 \\ 25*5 = 125 \\ 125^2 = 185 \\ 185*5 = 153 \\ 152^2 = 56 \\ 56^2 = 48 \\ 48^2 = 181 \\ 181*5 = 133 \end{split} \end{equation}

But when I check my answer on wolframalpha it tells me the correct solution should be 22. I checked every single step twice. What did I miss?

1 Answers 1

2

Looks to me like you're computing $5^{57}$.

  • start with $5^2$
  • multiplying that by $5$ gives $5^3$
  • squaring that gives $5^6$
  • multiplying that by $5$ gives $5^7$
  • squaring that gives $5^{14}$
  • squaring that gives $5^{28}$
  • squaring that gives $5^{56}$
  • multiplying that by $5$ gives $5^{57}$
  • 0
    You are right, but I'm following the definition of the algorithm. For every 1-coefficient I square and multiply and else I just square. What is wrong there?2017-02-16
  • 0
    @AdHominem You need to start with the value 1, not 5. I.e., you start with $1^2 = 1$, $1\cdot 5 = 5$ etc. Alternatively, you can start with the value 5, but then you skip the first binary digit, i.e., you only use the digits 1001, not 11001.2017-02-16
  • 0
    @Litho Damn you are right. Such a stupid mistake.2017-02-16