-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.py
79 lines (58 loc) · 3.07 KB
/
tests.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Python ED25519 Bindings
#
# Copyright 2018-2020 Stichting Polkascan (Polkascan Foundation).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import bip39
import ed25519_zebra
class MyTestCase(unittest.TestCase):
def test_sign_and_verify_message(self):
message = b"test"
# Get private and public key from seed
seed = bip39.bip39_to_mini_secret('daughter song common combine misery cotton audit morning stuff weasel flee field','')
private_key, public_key = ed25519_zebra.ed_from_seed(bytes(seed))
# Generate signature
signature = ed25519_zebra.ed_sign(private_key, message)
# Verify message with signature
self.assertTrue(ed25519_zebra.ed_verify(signature, message, public_key))
def test_verify_signature(self):
public_key = bytes.fromhex('fd8c9f401e9b5db50685be590b4f8e5a10d4abfef9a532ee903f125fd11f129c')
signature = bytes.fromhex('33185bf9e8f6b354751ef7c7a7c692b3d51d5096e52c4c08e8b307f804d025f3'
'f14a060b8c1ad11f92fe0be4cae2c620d86d6012f90d071fb8dbadac2774df02')
# Verify message with signature
self.assertTrue(ed25519_zebra.ed_verify(signature, b"test", public_key))
def test_verify_invalid_public_key(self):
public_key = bytes(1)
signature = bytes.fromhex('33185bf9e8f6b354751ef7c7a7c692b3d51d5096e52c4c08e8b307f804d025f3'
'f14a060b8c1ad11f92fe0be4cae2c620d86d6012f90d071fb8dbadac2774df02')
# Verify message with signature
self.assertFalse(ed25519_zebra.ed_verify(signature, b"test", public_key))
def test_verify_invalid_signature(self):
# Get private and public key from seed
seed = bip39.bip39_to_mini_secret(
'daughter song common combine misery cotton audit morning stuff weasel flee field', '')
private_key, public_key = ed25519_zebra.ed_from_seed(bytes(seed))
# Verify message with signature
self.assertFalse(ed25519_zebra.ed_verify(bytes(32), b"test", public_key))
def test_invalid_seed_length(self):
self.assertRaises(ValueError, ed25519_zebra.ed_from_seed, bytes(1))
def test_sign_invalid_private_key_length(self):
with self.assertRaises(ValueError):
ed25519_zebra.ed_sign(bytes(1), b"test")
def test_public_from_private(self):
private_key = bytes(32)
public_key = ed25519_zebra.ed_public_from_secret(private_key)
self.assertEqual(bytes.fromhex("3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29"), public_key)
if __name__ == '__main__':
unittest.main()