summaryrefslogtreecommitdiff
path: root/tests/test_bip340.py
blob: cc9e416a7ab68017552a3c12c1906a9f9823d702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import csv

from pathlib import Path
from typing import Any

from nostr.bip340 import pubkey_gen, schnorr_sign, schnorr_verify

TEST_VECTORS = Path(__file__).parent / "test-vectors.csv"


def test_vectors():
    with open(TEST_VECTORS, newline="") as csvfile:
        reader = csv.reader(csvfile)
        next(reader)  # skip column titles

        for row in reader:
            (
                index,
                seckey_hex,
                pubkey_hex,
                aux_rand_hex,
                msg_hex,
                sig_hex,
                result_str,
                comment,
            ) = row

            pubkey = bytes.fromhex(pubkey_hex)
            msg = bytes.fromhex(msg_hex)
            sig = bytes.fromhex(sig_hex)
            result = result_str == "TRUE"

            if seckey_hex != "":
                seckey = bytes.fromhex(seckey_hex)
                pubkey_actual = pubkey_gen(seckey)
                assert pubkey == pubkey_actual
                aux_rand = bytes.fromhex(aux_rand_hex)
                sig_actual = schnorr_sign(msg, seckey, aux_rand)
                assert sig == sig_actual

            result_actual = schnorr_verify(msg, pubkey, sig)
            assert result == result_actual