aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/crypto/internal/subtle/comparison_test.v
blob: b339746bf1a120344821bb400c4b95aa7b0bb54f (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module subtle

fn test_constant_time_byte_eq() {
	assert constant_time_byte_eq(0, 0) == 1
	assert constant_time_byte_eq(1, 1) == 1
	assert constant_time_byte_eq(255, 255) == 1
	assert constant_time_byte_eq(255, 1) == 0
	assert constant_time_byte_eq(1, 255) == 0
	assert constant_time_byte_eq(2, 1) == 0
}

fn test_constant_time_eq() {
	assert constant_time_eq(0, 0) == 1
	assert constant_time_eq(255, 255) == 1
	assert constant_time_eq(65536, 65536) == 1
	assert constant_time_eq(-1, -1) == 1
	assert constant_time_eq(-256, -256) == 1
	assert constant_time_eq(0, 1) == 0
}

fn test_constant_time_select() {
	assert constant_time_select(1, 1, 0) == 1
	assert constant_time_select(1, 1, 255) == 1
	assert constant_time_select(1, 1, 255 * 255) == 1
	assert constant_time_select(1, 2, 0) == 2
	assert constant_time_select(1, 2, 255) == 2
	assert constant_time_select(1, 2, 255 * 255) == 2
	//
	assert constant_time_select(0, 1, 0) == 0
	assert constant_time_select(0, 1, 255) == 255
	assert constant_time_select(0, 1, 255 * 255) == 255 * 255
	assert constant_time_select(0, 2, 0) == 0
	assert constant_time_select(0, 2, 255) == 255
	assert constant_time_select(0, 2, 255 * 255) == 255 * 255
}

fn test_constant_time_compare() {
	assert constant_time_compare([byte(1), 2, 3], [byte(1), 2, 3]) == 1
	assert constant_time_compare([byte(1), 2, 3], [byte(1), 2, 9]) == 0
	assert constant_time_compare([byte(1), 2, 3], [byte(1), 2, 3, 4]) == 0
	assert constant_time_compare([byte(1), 2, 3], [byte(1), 2]) == 0
}

fn test_constant_time_copy() {
	y := [byte(3), 4, 5]
	mut x := [byte(0), 0, 0]
	constant_time_copy(0, mut x, y)
	assert x == [byte(0), 0, 0]
	constant_time_copy(1, mut x, y)
	assert x == y
	assert x == [byte(3), 4, 5]
}

fn test_constant_time_less_or_eq() {
	assert constant_time_less_or_eq(1, 1) == 1
	assert constant_time_less_or_eq(1, 2) == 1
	assert constant_time_less_or_eq(1, 3) == 1
	assert constant_time_less_or_eq(255, 255) == 1
	assert constant_time_less_or_eq(255, 256) == 1
	assert constant_time_less_or_eq(255, 257) == 1
	assert constant_time_less_or_eq(1, 0) == 0
	assert constant_time_less_or_eq(2, 1) == 0
	assert constant_time_less_or_eq(3, 2) == 0
	assert constant_time_less_or_eq(255, 3) == 0
}