Processing math: 100%
Ask Your Question
1

Inverse of a number modulo 2**255 -19

asked 6 years ago

ZacariasSatrustegui gravatar image

updated 6 years ago

slelievre gravatar image

I don't understand this code to solve the inverse of a number:

b = 256;
q = 2**255 - 19

def expmod(b,e,m): 
    if e == 0: return 1
    t = expmod(b,e/2,m)**2 % m
    if e & 1: t = (t*b) % m
    return t

def inv(x):
   return expmod(x,q-2,q)`

Finally, If I want to put: 23 I can to do this: aux=2*inv(3)

What does the variable e mean?

Could you explain me this code, please?

Thank you so much.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 6 years ago

slelievre gravatar image

updated 6 years ago

Since q is a prime here, Z/q is a field, usually denoted by Fq.

All its nonzero elements are invertible, and form a group under multiplication. This group is called the multiplicative group of this field, or its group of units.

This group has order q1 (since it contains all nonzero elements of Fq), so every nonzero element x in Fq satisfies xq1=1, which can be written as xq2x=1, so we get that for any nonzero x in Fq, xq2 is the multiplicative inverse of x.

The function expmod(b, e, m) (for "exponentiation modulo") computes be modulo m. Here the arguments of this function are named to reflect their role (basis, exponent, modulus).

Finally, the function inv just computes xq2 modulo q.

Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 6 years ago

Seen: 2,573 times

Last updated: Nov 04 '18