Score:1

How does the Mongomery Algorithm work?

so flag

can someone please explain to me what's the role of montgomery reduction algorithm and how to implement it in python. I wrote the code below to calculate a*b mod m but it doesn't seem to work well.

def montgomery(a,b,m):

bin_m=format(m,'b')
R=pow(2,len(bin_m))

a_prim= a *(R % m)
b_prim= b *(R % m)

reverse_R=euc(R,m)

if inverse_R<0:
    inverse_R+=m

c_prim=(a_prim*b_prim)*reverse_R
result=c_prim*reverse_R

return print(result)
Myria avatar
in flag
If you're using Python, there's no reason to do this yourself, because Python has arbitrary-precision integers built into the language. Just do `pow(b, e, m)` to compute $b^e \pmod m$.
Rohit Gupta avatar
pg flag
You could ask this on Super User
fgrieu avatar
ng flag
Have a look at [this](https://crypto.stackexchange.com/a/84332/555). Note that unless you break the representation of integers into chunks, you can't use Montgomery arithmetic, only simulate it. Also notice that the Montgomery method for Elliptic Curve scalar multiplication is not directly related to Montgomery arithmetic. If you have more questions on the math, [edit](https://crypto.stackexchange.com/posts/104005/edit) the question, toning down the python aspects of it, and removing the _cryptocurrency_ tag.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.