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

Checking for powers of zero and divide by zero corner cases in FieldElement class in ecc.py #287

Open
salmonberry7 opened this issue Oct 30, 2024 · 0 comments

Comments

@salmonberry7
Copy link

salmonberry7 commented Oct 30, 2024

In method __pow__ of class FieldElement an incorrect result of 1 is returned when 0 to the power k(p - 1) is calculated, where k is a non-zero integer (in the case of k = 0 the correct result of 1 is returned, since in field Fp, as in maths generally, zero to the power zero is normally taken to be 1 (eg. see Zero to the power of zero), and Python's built-in function pow computes pow(0, 0) as 1). This problem arises since n will come out as zero in this case and then num will be set to 1. The self.num == 0 case could be handled separately at the start of the function, with a ZeroDivisionError exception raised if exponent is any negative integer :

if self.num == 0 :
	if exponent < 0 :
		# original code returns 0 or 1 in this case
		raise ZeroDivisionError
	elif exponent == 0 :
		# original code works in this case
		return self.__class__(1, self.prime)
	else :
		# exponent > 0
		# original code returns 0 or 1 in this case
		return self.__class__(0, self.prime)

The code then works correctly as before for the case self.num != 0.

This bug does not affect the square root of zero in sqrt method of class S256Field when raising to power (p + 1)/4, since 0 < (p + 1)/4 < p - 1 for any odd prime p, and it does not affect taking the inverse in Fp by raising to power p - 2 in __truediv__. However __truediv__ should raise a ZeroDivisionError exception if the denominator is zero - currently it returns self.__class__(0, self.prime) in this case, without complaining.

@salmonberry7 salmonberry7 changed the title Checking For 0 to Power 0 and Divide By Zero Corner Cases In Class FieldElement in ecc.py Checking For Powers of Zero and Divide By Zero Corner Cases In Class FieldElement in ecc.py Oct 31, 2024
@salmonberry7 salmonberry7 changed the title Checking For Powers of Zero and Divide By Zero Corner Cases In Class FieldElement in ecc.py Checking for powers of zero and divide by zero corner cases in FieldElement class in ecc.py Oct 31, 2024
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

1 participant