+test.ha (1618B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use bytes; 5 use crypto::random; 6 use encoding::hex; 7 8 @test fn sample() void = { 9 let seed: [32]u8 = [0xff...]; 10 let priv: key = [0...]; 11 priv[31] = 0xbf; 12 13 newkey(&priv, &seed); 14 15 let pub: key = [0...]; 16 pubkey(&pub, &priv); 17 18 const pexpected: key = [ 19 0x84, 0x7c, 0x0d, 0x2c, 0x37, 0x52, 0x34, 0xf3, 0x65, 0xe6, 20 0x60, 0x95, 0x51, 0x87, 0xa3, 0x73, 0x5a, 0x0f, 0x76, 0x13, 21 0xd1, 0x60, 0x9d, 0x3a, 0x6a, 0x4d, 0x8c, 0x53, 0xae, 0xaa, 22 0x5a, 0x22, 23 ]; 24 25 assert(bytes::equal(pexpected, pub)); 26 27 const otherpub: key = [ 28 0x28, 0x18, 0x84, 0xe0, 0x0f, 0xae, 0x8a, 0x33, 0x75, 0x05, 29 0xbf, 0x38, 0x15, 0x2a, 0x97, 0xc0, 0x20, 0x4a, 0x8c, 0x1d, 30 0x4c, 0xfa, 0x2d, 0x2b, 0x12, 0x99, 0x80, 0xed, 0xe7, 0x32, 31 0xaf, 0x0d, 32 ]; 33 34 const expected: key = [ 35 0x07, 0x4a, 0xaf, 0x3c, 0xa3, 0x87, 0xd5, 0xa3, 0x71, 0x25, 36 0x9f, 0x50, 0xb3, 0xf0, 0xa1, 0xe9, 0x63, 0x6b, 0x18, 0x1d, 37 0x5e, 0x4e, 0x6e, 0xb3, 0x1a, 0xe9, 0xda, 0x01, 0x05, 0x4a, 38 0x8c, 0x3b, 39 ]; 40 41 let shared: key = [0...]; 42 43 derive(&shared, &priv, &otherpub); 44 assert(bytes::equal(expected, shared)); 45 }; 46 47 @test fn random() void = { 48 let seed: [32]u8 = [0...]; 49 let priv1: key = [0...]; 50 let priv2: key = [0...]; 51 let pub1: key = [0...]; 52 let pub2: key = [0...]; 53 let shared1: key = [0...]; 54 let shared2: key = [0...]; 55 56 random::buffer(seed); 57 newkey(&priv1, &seed); 58 59 random::buffer(seed); 60 newkey(&priv2, &seed); 61 62 pubkey(&pub1, &priv1); 63 pubkey(&pub2, &priv2); 64 65 derive(&shared1, &priv1, &pub2); 66 derive(&shared2, &priv2, &pub1); 67 68 assert(bytes::equal(&shared1, &shared2)); 69 };