ZhePang / Python_Specification_for_Schnorr_Adaptor

5 stars 4 forks source link

Improve readability of test_vectors.csv #9

Closed siv2r closed 3 months ago

siv2r commented 7 months ago

The CSV file's test type and result columns are ambiguous, especially when we test the adapt and extract_secadaptor APIs. For instance,

Side Note 1: All the BIP340 test vectors were verified with only the schnorr_verify function. Therefore, the result column was the output of this function and didn’t need a test-type column. In our case, we require three different verification functions:

Side Note 2: The API return types are inconsistent. extract_adaptor API (return type Optional[Point]) can return [1] Point, [2] None, or [3] False. However, extract_secadaptor & adapt will never return False. This inconsistency affects the value of the result column in the CSV table.

siv2r commented 7 months ago

I can think of the following solutions:

We can rename the “test type” column to “target api” which will contain the APIs that the test vectors are used for

I am leaning toward soln 2.

@jonasnick @ZhePang What do you guys suggest?

jonasnick commented 7 months ago

Side Note 2: The API return types are inconsistent.

Yeah, there are multiple places where types are wrong:

❯ mypy test-vector.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]
test-vector.py:154: error: Argument 1 to "compress_point" has incompatible type "tuple[int, int] | None"; expected "tuple[int, int]"  [arg-type]
Found 18 errors in 2 files (checked 1 source file)
jonasnick commented 7 months ago

I agree that extract_t, adapt and extract_adaptor should not raise Errors except for the length of the inputs.

Side note: We should rename the functions and variables to be more understandable, similar what we've done in the C implementation.

We could also have separate CSV files for testing different API functions with sensible columns for the specific API tested.

siv2r commented 7 months ago

We could also have separate CSV files for testing different API functions with sensible columns for the specific API tested.

I like this idea. This would simplify things.

How does four CSV files (one for each API) sound?

Presign

seckey pubkey message aux_rand adaptor pre-signature result comment

The result column is AND of the following boolean values:


Extract Adaptor

pubkey message pre-signature adaptor result comment

The result column is: _adaptor == extractadaptor(pre-signature, msg, pubkey)


Adapt

pre-signature secadaptor bip340 signature result comment

The result column is: _schnorrverify(adapt(pre-signature, secadaptor))


Extract Secadaptor

pre-signature bip340 signature secadaptor result comment

The result column is: _secadaptor == extractsecadaptor(pre-signature, bip340 signature)

siv2r commented 7 months ago

How does four CSV files (one for each API) sound?

We can also combine them into two CSV files instead: [1] Presign & Extract Adaptor [2] Adapt & Extract Secadaptor, as their columns are similar.

siv2r commented 7 months ago

Did this issue close automatically after #8 merged? If so, we need to reopen it.

Initially, I had planned to fix this issue in the #8 pull request, but I decided against it to keep the PR brief. I will create a separate pull request for this.

ZhePang commented 7 months ago

Yeah I think it automatically closed it. I'll reopen it.