programmingbitcoin icon indicating copy to clipboard operation
programmingbitcoin copied to clipboard

Programming Bitcoin , Jimmy Song. ecc.py functions aren't working

Open MaltoonYezi opened this issue 3 years ago • 0 comments

Programming Bitcoin book, Jimmy Song. Chapter 1 ecc.py functions aren't recognized even though the ecc.py (the source file of the functions) is in the same folder as the file in which the Functions are being called

Picture 1: https://i.stack.imgur.com/njYMU.jpg Picture 2: https://i.stack.imgur.com/ZTg8R.jpg Picture 3: https://i.stack.imgur.com/nNuTL.jpg Picture 4: https://i.stack.imgur.com/eje4Q.jpg

Code Exercise 1:

`# Exercise 1
from ecc import FieldElement
from ecc import FieldElementTest

__ne__(FieldElement(7, 13),FieldElement(6, 13),FieldElement(8, 14))

reload(ecc)
run(ecc.FieldElementTest("test_ne"))`

Code Exercise 3:

# Exercise 3
from ecc import FieldElement
from ecc import FieldElementTest

print(test_add())
print(test_sub())

reload(ecc)
run(ecc.FieldElementTest("test_sub"))

The functions are written:

from unittest import TestCase

class FieldElement:

   def __init__(self, num, prime):
       if num >= prime or num < 0:  # <1>
           error = 'Num {} not in field range 0 to {}'.format(
               num, prime - 1)
           raise ValueError(error)
       self.num = num  # <2>
       self.prime = prime

   def __repr__(self):
       return 'FieldElement_{}({})'.format(self.prime, self.num)

   def __eq__(self, other):
       if other is None:
           return False
       return self.num == other.num and self.prime == other.prime  # <3>
   # end::source1[]

   def __ne__(self, other):
       # this should be the inverse of the == operator
       return not (self == other)

   def __add__(self, other):
       if self.prime != other.prime:  # <1>
           raise TypeError('Cannot add two numbers in different Fields')
       num = (self.num + other.num) % self.prime  # <2>
       return self.__class__(num, self.prime)  # <3>
   # end::source2[]

   def __sub__(self, other):
       if self.prime != other.prime:
           raise TypeError('Cannot subtract two numbers in different Fields')
       # self.num and other.num are the actual values
       num = (self.num - other.num) % self.prime
       # self.prime is what we need to mod against
       # We return an element of the same class
       return self.__class__(num, self.prime)
       #raise NotImplementedError

class FieldElementTest(TestCase):

   def test_ne(self):
       a = FieldElement(2, 31)
       b = FieldElement(2, 31)
       c = FieldElement(15, 31)
       self.assertEqual(a, b)
       self.assertTrue(a != c)
       self.assertFalse(a != b)

   def test_add(self):
       a = FieldElement(2, 31)
       b = FieldElement(15, 31)
       self.assertEqual(a + b, FieldElement(17, 31))
       a = FieldElement(17, 31)
       b = FieldElement(21, 31)
       self.assertEqual(a + b, FieldElement(7, 31))

   def test_sub(self):
       a = FieldElement(29, 31)
       b = FieldElement(4, 31)
       self.assertEqual(a - b, FieldElement(25, 31))
       a = FieldElement(15, 31)
       b = FieldElement(30, 31)
       self.assertEqual(a - b, FieldElement(16, 31))

Picture 5: https://i.stack.imgur.com/hLQPM.jpg Picture 6: https://i.stack.imgur.com/jdEcZ.jpg Picture 7: https://i.stack.imgur.com/sMnoC.jpg

I start the Jupyter notebook every time by typing these commands in the command prompt Picture 8: https://i.stack.imgur.com/pSAZa.jpg

@echo on
cd C:\programmingbitcoin\programmingbitcoin

virtualenv -p C:\programmingbitcoin\programmingbitcoin\.venv\Scripts\python.exe .venv

.venv\Scripts\activate.bat

jupyter notebook
REM DO NOT CLOSE THE COMMAND PROMPT! THAT WOULD SHUTDOWN THE JUPYTER!
pause

and keep the command prompt running

Picture 9: https://i.stack.imgur.com/3VE7D.jpg

as I said, These files are in the same folder Picture 10: https://i.stack.imgur.com/QX8Ia.jpg

The installation guide to Jupyter Notebook: https://www.oreilly.com/library/view/programming-bitcoin/9781492031482/preface01.html#setting_up

The steps to installation and starting the Jupyter Notebook I got from that guide

The question is: What seems to be the problem here?

Why is this question being asked? There's the possibility that the problem may be systematic across the whole repository. To prevent further errors, it would be good to solve everything in advance.

The odd thing is that for the 1st time these functions were working, but after I've restarted the Jupyter notebook, they stopped doing so

The question is also posted on StackOverflow: https://stackoverflow.com/questions/70701871/programming-bitcoin-jimmy-song-ecc-py-functions-arent-working and on Bitcointalk: https://bitcointalk.org/index.php?topic=5381272.0

MaltoonYezi avatar Jan 13 '22 20:01 MaltoonYezi