v2 / vlib / crypto / ed25519 / internal / ed25519_test.v
56 lines · 46 sloc · 2.03 KB · 0b76a0ca195a79c7f4fadb5d4dc45015efa53d2e
Raw
1module main
2
3// Note: this should be in vlib/crypto/ed25519/ed25519_test.v
4// but is currently one folder below, because of a V parser/symbol registration bug.
5// TODO: move this test back to vlib/crypto/ed25519/ed25519_test.v
6import crypto.ed25519
7
8fn test_sign_verify() {
9 // mut zero := ZeroReader{}
10 public, private := ed25519.generate_key()!
11
12 message := 'test message'.bytes()
13 sig := ed25519.sign(private, message)!
14 res := ed25519.verify(public, message, sig) or { false }
15 assert res == true
16
17 wrongmessage := 'wrong message'.bytes()
18 res2 := ed25519.verify(public, wrongmessage, sig)!
19 assert res2 == false
20}
21
22fn test_equal() {
23 public, private := ed25519.generate_key()!
24
25 assert public.equal(public) == true
26
27 // This is not AVAILABLE
28 /*
29 if !public.Equal(crypto.Signer(private).Public()) {
30 t.Errorf("private.Public() is not Equal to public: %q", public)
31 }*/
32 assert private.equal(private) == true
33
34 otherpub, otherpriv := ed25519.generate_key()!
35 assert public.equal(otherpub) == false
36
37 assert private.equal(otherpriv) == false
38}
39
40fn test_malleability() {
41 // https://tools.ietf.org/html/rfc8032#section-5.1.7 adds an additional test
42 // that s be in [0, order). This prevents someone from adding a multiple of
43 // order to s and obtaining a second valid signature for the same message.
44 msg := [u8(0x54), 0x65, 0x73, 0x74]
45 sig := [u8(0x7c), 0x38, 0xe0, 0x26, 0xf2, 0x9e, 0x14, 0xaa, 0xbd, 0x05, 0x9a, 0x0f, 0x2d, 0xb8,
46 0xb0, 0xcd, 0x78, 0x30, 0x40, 0x60, 0x9a, 0x8b, 0xe6, 0x84, 0xdb, 0x12, 0xf8, 0x2a, 0x27,
47 0x77, 0x4a, 0xb0, 0x67, 0x65, 0x4b, 0xce, 0x38, 0x32, 0xc2, 0xd7, 0x6f, 0x8f, 0x6f, 0x5d,
48 0xaf, 0xc0, 0x8d, 0x93, 0x39, 0xd4, 0xee, 0xf6, 0x76, 0x57, 0x33, 0x36, 0xa5, 0xc5, 0x1e,
49 0xb6, 0xf9, 0x46, 0xb3, 0x1d]
50 publickey := [u8(0x7d), 0x4d, 0x0e, 0x7f, 0x61, 0x53, 0xa6, 0x9b, 0x62, 0x42, 0xb5, 0x22, 0xab,
51 0xbe, 0xe6, 0x85, 0xfd, 0xa4, 0x42, 0x0f, 0x88, 0x34, 0xb1, 0x08, 0xc3, 0xbd, 0xae, 0x36,
52 0x9e, 0xf5, 0x49, 0xfa]
53 // verify should fail on provided bytes
54 res := ed25519.verify(publickey, msg, sig) or { false }
55 assert res == false
56}
57