next up previous
Next: Functions Up: Using Python for computing Previous: Using Python for computing


What?

If you want to experiment with elliptic curves you fairly soon realize that doing computations with integer numbers, (or rational numbers, or modulo $ p$) can be time consuming, not to mention a little bit boring. This is why it is very convenient to have a calculator at hand. A problem with calculators is that if you want (need?) to work with integer numbers of more than $ 10$ or $ 12$ digits, most probably, you don't get exact answers. Thus, we look for some kind of ``calculator'' that would allow us to perform exact computations in all situations as well as, if possible, automatize some repetitive actions. These two goals can be satisfactorily met by using the program called python 1.

Let's get started. Go to your computer, login and get a terminal with the shell prompt (oh yes, this is ``mildly'' Unix oriented... but python is available in most other operating systems and using it is (99%) the same). Type the word python and hit Return (or Enter) afterwards.

$ python
Python 2.2b1 (#2, Nov  5 2001, 15:20:50) 
[GCC 2.95.3 20010315 (release)] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>>

So, python has started and is waiting for your instructions (»> is python's prompt). We will try first some simple arithmetic (remember to press Return at the end of each of your commands):

>>> 3+2
5
>>> 3*2
6
>>> 3**2
9
>>> 1235412515*983475982457895
1214998536930403943555925L
Notice how we compute powers using the ** symbol.

Also, python knows how to operate with parentheses:

>>> 3*(4-2)
6

Divisions?

>>> 3/2
1
Please notice the previous result: 3/2 evaluates to the ``integer division'' instead of the usual 1.5 that you would expect. If you want to get 1.5 you have to tell python that you want to work with ``floating point'' numbers:
>>> 3.0/2
1.5

As you can imagine, there are also variables:

>>> a=127
>>> b="hello people"
>>> c=1.27
>>> 2*a+1
255
Different values can be assigned to variables. Above, a was assigned an integer number, b the string hello people, and c a floating point number. Finally, you can operate with variables as you normally would.

How about arithmetic modulo $ p$? We can ask python to reduce a number modulo $ p$ using the operator % as follows:

>>> 5 % 3
2
>>> 5234524452**18 % 19
1L
The first line computed the remainder of $ 5$ modulo $ 3$. The second, the remainder of $ 5234524452^{18}$ modulo $ 19$, which is $ 1$ (but you don't need python for this one...).

Before we move on to something more interesting, there are two things that you have to know: how to stop the program and how to ask for help (other than screaming...). Quitting the program is very simple (on a Unix console) just press the keys Control and D at the same time, and you are done. To ask for help, start python again (see instructions above). Then, on python's prompt type help(), and follow the instructions to seek help on a specific topic. Remember to type quit when you are done with the help and want to continue using python. For more information about python you should check http://www.python.org.

Now that we know how to do basic arithmetic with python, could we check if $ (1342,41543452354)$ is on the elliptic curve $ y^2 = x^3 + x +
1725858433486651246286$?

>>> x=1342
>>> y=41543452354
>>> x**3+x+1725858433486651246286
1725858433489068141316L
>>> y**2
1725858433489068141316L
As you see, the point is on the curve (because both sides of the equation evaluate to the same thing). Another possibility is to ask python to compare both sides automatically as follows:
>>> y**2==x**3+x+1725858433486651246286
1
Notice how we ask if two things are equal using the symbol == (instead of =). The answer is the number 1, meaning that they are equal. If things are different we get 0:
>>> "banana"=="orange"
0
while
>>> "banana"!="orange"
1
here != means ``not equal''.


next up previous
Next: Functions Up: Using Python for computing Previous: Using Python for computing
Javier Fernandez 2003-06-24