-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlinear_complexity.go
102 lines (89 loc) · 2.42 KB
/
linear_complexity.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) 2021 Quan guanyu
// randomness is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
package randomness
import (
"math"
)
// LinearComplexity 线型复杂度检测,m=500
func LinearComplexity(data []byte) *TestResult {
p, q := LinearComplexityTestBytes(data, 500)
return &TestResult{Name: "线型复杂度检测(m=500)", P: p, Q: q, Pass: p >= Alpha}
}
// LinearComplexityTest 线型复杂度检测,m=500
func LinearComplexityTest(bits []bool) (float64, float64) {
return LinearComplexityProto(bits, 500)
}
// LinearComplexityTestBytes 线型复杂度检测
// data: 待检测序列
// m: m长度
func LinearComplexityTestBytes(data []byte, m int) (float64, float64) {
return LinearComplexityProto(B2bitArr(data), m)
}
// LinearComplexityProto 线型复杂度检测
// bits: 待检测序列
// m: m长度
func LinearComplexityProto(bits []bool, m int) (float64, float64) {
n := len(bits)
N := n / m
if N == 0 {
panic("please provide valid test bits")
}
var v = []float64{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}
var pi = []float64{0.010417, 0.03125, 0.12500, 0.5000, 0.25000, 0.06250, 0.020833}
var V float64 = 0.0
var P float64 = 0
var arr []bool
var complexity int
var T float64
arr = make([]bool, m)
// Step 3, miu
var _1_m float64
if m%2 == 0 {
_1_m = 1.0
} else {
_1_m = -1.0
}
miu := float64(m)/2.0 + (9.0+_1_m)/36.0 - (float64(m)/3.0+2.0/9.0)/math.Pow(2.0, float64(m))
// Step 2, 4, 5
for i := 0; i < N; i++ {
for j := 0; j < m; j++ {
arr[j], bits = bits[0], bits[1:]
}
complexity = linearComplexity(arr, m)
if m%2 == 0 {
_1_m = 1.0
} else {
_1_m = -1.0
}
T = _1_m*(float64(complexity)-miu) + 2.0/9.0
if T <= -2.5 {
v[0]++
} else if T <= -1.5 {
v[1]++
} else if T <= -0.5 {
v[2]++
} else if T <= 0.5 {
v[3]++
} else if T <= 1.5 {
v[4]++
} else if T <= 2.5 {
v[5]++
} else {
v[6]++
}
}
// Step 6
for i := 0; i < 7; i++ {
V += math.Pow(v[i]-float64(N)*pi[i], 2.0) / (float64(N) * pi[i])
}
// Step 7
P = igamc(3.0, V/2.0)
return P, P
}