Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rmul method inside FieldElement Class to be added before completing ex 4 chapter 3 #261

Open
enricobottazzi opened this issue Nov 13, 2022 · 1 comment

Comments

@enricobottazzi
Copy link

In order to complete Exercise 4 chapter 3, it is necessary to execute a call the self == other exception in point addition. This will likely return you an error

  File "/Users/enricobottazzi/Documents/GitHub/ec-primer-rust/ec.py", line 47, in __add__
    s = (3 * self.x**2 + self.a) / (2 * self.y)
TypeError: unsupported operand type(s) for *: 'int' and 'FieldElement'

The way to avoid this error you need ti add the __rmul__ method inside the FieldElement Class as implemented here => https://github.com/jimmysong/programmingbitcoin/blob/master/code-ch03/ecc.py#L75

The problem is that this method is never explained nor mentioned inside the book.

@pleblira
Copy link

pleblira commented Nov 23, 2022

Hi Enrico,

I ran into the same issue last night.

Instead of implementing __rmul__ to FieldElement, I updated the __add__ method on class Point to the following.

From

if self == other:
        s = (3 * self.x**2 + self.a) / (2 * self.y)
        x = s**2 - 2 * self.x
        y = s * (self.x - x) - self.y
        return self.__class__(x, y, self.a, self.b)

to

if self == other:
            s = ((self.x**2+self.x**2+self.x**2)+self.a)/(self.y+self.y)
            x = s**2-(self.x+self.x)
            y = s*(self.x-x)-self.y
            return self.__class__(x, y, self.a, self.b)

Basically, instead of multiplying self.x by 3, I simply added it to itself 3 times for which there was a method for, and so on.

__rmul__ seems to be the scalable solution, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants