ZhePang / Python_Specification_for_Schnorr_Adaptor

5 stars 4 forks source link

Fix Type Errors #11

Closed jonasnick closed 7 months ago

jonasnick commented 7 months ago

Running the Mypy typechecker reveals some errors in reference.py:

reference.py:143: error: Incompatible types in assignment (expression has type "tuple[int, int]", variable has type "bytes")  [assignment]
reference.py:144: error: Argument 2 to "point_add" has incompatible type "bytes"; expected "tuple[int, int] | None"  [arg-type]
reference.py:145: error: Argument 1 to "has_even_y" has incompatible type "tuple[int, int] | None"; expected "tuple[int, int]"  [arg-type]
reference.py:148: error: Argument 1 to "bytes_from_point" has incompatible type "tuple[int, int] | None"; expected "tuple[int, int]"  [arg-type]
reference.py:149: error: Argument 1 to "parity_from_point" has incompatible type "tuple[int, int] | None"; expected "tuple[int, int]"  [arg-type]
reference.py:149: error: Argument 1 to "bytes_from_point" has incompatible type "tuple[int, int] | None"; expected "tuple[int, int]"  [arg-type]
reference.py:151: error: Argument 2 to "schnorr_pre_verify" has incompatible type "bytes"; expected "tuple[int, int]"  [arg-type]
reference.py:175: error: Incompatible return value type (got "bool", expected "tuple[int, int] | None")  [return-value]
reference.py:179: error: Incompatible return value type (got "bool", expected "tuple[int, int] | None")  [return-value]
reference.py:184: error: Incompatible return value type (got "bool", expected "tuple[int, int] | None")  [return-value]
reference.py:194: error: Incompatible return value type (got "bool", expected "tuple[int, int] | None")  [return-value]
reference.py:295: error: Incompatible types in assignment (expression has type "tuple[int, int]", variable has type "bytes")  [assignment]
reference.py:296: error: Argument 2 to "schnorr_pre_verify" has incompatible type "bytes"; expected "tuple[int, int]"  [arg-type]
reference.py:368: error: Argument 1 to "compress_point" has incompatible type "tuple[int, int] | None"; expected "tuple[int, int]"  [arg-type]
reference.py:403: error: Missing return statement  [return]
reference.py:413: error: Argument 1 to "compress_point" has incompatible type "tuple[int, int] | None"; expected "tuple[int, int]"  [arg-type]
reference.py:427: error: Argument 1 to "compress_point" has incompatible type "tuple[int, int] | None"; expected "tuple[int, int]"  [arg-type]
Found 17 errors in 1 file (checked 1 source file)

Besides fixing these issues, we should also return a different type of

def schnorr_adaptor_extract_t(msg: bytes, pubkey: bytes, sig: bytes) -> Optional[Point]:

as Optional[Point] is also used the denote the type of "affine" Point or Point at infinity. I'd suggest to use Union[bool, Point] (documentation)

ZhePang commented 7 months ago

For the first couple errors regarding "tuple[int, int] | None" for variable R0, since we checked is_infinity(R0) which is checking whether R0 is None, R0 cannot be None in these lines. So I just gave mypy a type hint to ignore the type of R0 since it would always be tuple[int, int]

ZhePang commented 7 months ago

Fixed other issues with type assertion and adopted the suggestion for changing the return type to Union