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