Skip to content

Commit

Permalink
增加了统一方法签名的监测方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Trisia committed Aug 18, 2021
1 parent 9ee54c6 commit bbae9b4
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 0 deletions.
6 changes: 6 additions & 0 deletions approximate_entropy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"math"
)

// ApproximateEntropy 近似熵检测,m=5
func ApproximateEntropy(data []byte) *TestResult {
p := ApproximateEntropyTestBytes(data, 5)
return &TestResult{Name: "近似熵检测", P: p, Pass: p >= Alpha}
}

// ApproximateEntropyTest 近似熵检测,m=5
func ApproximateEntropyTest(bits []bool) float64 {
return ApproximateEntropyProto(bits, 5)
Expand Down
6 changes: 6 additions & 0 deletions autocorrelation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"math"
)

// Autocorrelation 自相关检测,d=16
func Autocorrelation(data []byte) *TestResult {
p := AutocorrelationTestBytes(data, 16)
return &TestResult{Name: "自相关检测", P: p, Pass: p >= Alpha}
}

// AutocorrelationTest 自相关检测,d=16
func AutocorrelationTest(bits []bool) float64 {
return AutocorrelationProto(bits, 16)
Expand Down
6 changes: 6 additions & 0 deletions binary_derivative.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"math"
)

// BinaryDerivative 二元推导检测, k=7
func BinaryDerivative(data []byte) *TestResult {
p := BinaryDerivativeTestBytes(data, 7)
return &TestResult{Name: "二元推导检测", P: p, Pass: p >= Alpha}
}

// BinaryDerivativeTest 二元推导检测, k=7
func BinaryDerivativeTest(bits []bool) float64 {
return BinaryDerivativeProto(bits, 7)
Expand Down
6 changes: 6 additions & 0 deletions cumulative.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"math"
)

// Cumulative 累加和检测
func Cumulative(data []byte) *TestResult {
p := CumulativeTestBytes(data)
return &TestResult{Name: "累加和检测", P: p, Pass: p >= Alpha}
}

// CumulativeTestBytes 累加和检测
func CumulativeTestBytes(data []byte) float64 {
return CumulativeTest(B2bitArr(data))
Expand Down
6 changes: 6 additions & 0 deletions discrete_fourier_transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import (
"math/cmplx"
)

// DiscreteFourierTransform 离散傅里叶检测
func DiscreteFourierTransform(data []byte) *TestResult {
p := DiscreteFourierTransformTestBytes(data)
return &TestResult{Name: "离散傅里叶检测", P: p, Pass: p >= Alpha}
}

// DiscreteFourierTransformTestBytes 离散傅里叶检测
func DiscreteFourierTransformTestBytes(data []byte) float64 {
return DiscreteFourierTransformTest(B2bitArr(data))
Expand Down
6 changes: 6 additions & 0 deletions frequency_within_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import (
"fmt"
)

// FrequencyWithinBlock 块内频数检测, m = 100
func FrequencyWithinBlock(data []byte) *TestResult {
p := FrequencyWithinBlockTestBytes(data, 100)
return &TestResult{Name: "块内频数检测", P: p, Pass: p >= Alpha}
}

// FrequencyWithinBlockTest 块内频数检测, m = 100
func FrequencyWithinBlockTest(bits []bool) float64 {
return FrequencyWithinBlockProto(bits, 100)
Expand Down
6 changes: 6 additions & 0 deletions linear_complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"math"
)

// LinearComplexity 线型复杂度检测,m=500
func LinearComplexity(data []byte) *TestResult {
p := LinearComplexityTestBytes(data, 500)
return &TestResult{Name: "线型复杂度检测", P: p, Pass: p >= Alpha}
}

// LinearComplexityTest 线型复杂度检测,m=500
func LinearComplexityTest(bits []bool) float64 {
return LinearComplexityProto(bits, 500)
Expand Down
6 changes: 6 additions & 0 deletions longest_run_of_ones_In_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import (

var pi = []float64{0.0882, 0.2092, 0.2483, 0.1933, 0.1208, 0.0675, 0.0727}

// LongestRunOfOnesInABlock 块内最大“1”游程检测,m=10000
func LongestRunOfOnesInABlock(data []byte) *TestResult {
p := LongestRunOfOnesInABlockTestBytes(data, 10000)
return &TestResult{Name: "块内最大“1”游程检测", P: p, Pass: p >= Alpha}
}

// LongestRunOfOnesInABlockTest 块内最大“1”游程检测,m=10000
func LongestRunOfOnesInABlockTest(bits []bool) float64 {
return LongestRunOfOnesInABlockProto(bits, 10000)
Expand Down
6 changes: 6 additions & 0 deletions matrix_rank.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"math"
)

// MatrixRank 矩阵秩检测,M=Q=32
func MatrixRank(data []byte) *TestResult {
p := MatrixRankTestBytes(data, 32, 32)
return &TestResult{Name: "矩阵秩检测", P: p, Pass: p >= Alpha}
}

// MatrixRankTest 矩阵秩检测,M=Q=32
func MatrixRankTest(bits []bool) float64 {
return MatrixRankProto(bits, 32, 32)
Expand Down
6 changes: 6 additions & 0 deletions maurers_universal.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func mutFactorC(L, K int) float64 {
return v
}

// MaurerUniversal Maurer通用统计检测方法
func MaurerUniversal(data []byte) *TestResult {
p := MaurerUniversalTestBytes(data)
return &TestResult{Name: "Maurer通用统计检测方法", P: p, Pass: p >= Alpha}
}

// MaurerUniversalTestBytes Maurer通用统计检测方法
func MaurerUniversalTestBytes(data []byte) float64 {
return MaurerUniversalTest(B2bitArr(data))
Expand Down
6 changes: 6 additions & 0 deletions mono_bit_frequency.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"math"
)

// MonoBitFrequency 单比特频数检测
func MonoBitFrequency(data []byte) *TestResult {
p := MonoBitFrequencyTestBytes(data)
return &TestResult{Name: "单比特频数检测", P: p, Pass: p >= Alpha}
}

// MonoBitFrequencyTestBytes 单比特频数检测
func MonoBitFrequencyTestBytes(data []byte) float64 {
return MonoBitFrequencyTest(B2bitArr(data))
Expand Down
10 changes: 10 additions & 0 deletions overlapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ import (
"math"
)

// OverlappingTemplateMatching 重叠子序列检测方法,m=5
func OverlappingTemplateMatching(data []byte) *TestResult {
p1, p2 := OverlappingTemplateMatchingTestBytes(data, 5)
return &TestResult{
Name: "重叠子序列检测方法",
P: p1, P2: p2,
Pass: math.Min(p1, p2) >= Alpha,
}
}

// OverlappingTemplateMatchingTest 重叠子序列检测方法,m=5
// bits: 检测序列
// return:
Expand Down
6 changes: 6 additions & 0 deletions poker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ package randomness

import "fmt"

// Poker 扑克检测,m=8
func Poker(data []byte) *TestResult {
p := PokerTestBytes(data, 8)
return &TestResult{Name: "扑克检测", P: p, Pass: p >= Alpha}
}

// PokerTest 扑克检测,m=8
func PokerTest(bits []bool) float64 {
return PokerProto(bits, 8)
Expand Down
6 changes: 6 additions & 0 deletions runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"math"
)

// Runs 游程总数检测
func Runs(data []byte) *TestResult {
p := RunsTestBytes(data)
return &TestResult{Name: "游程总数检测", P: p, Pass: p >= Alpha}
}

// RunsTestBytes 游程总数检测
func RunsTestBytes(data []byte) float64 {
return RunsTest(B2bitArr(data))
Expand Down
6 changes: 6 additions & 0 deletions runs_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ package randomness

import "fmt"

// RunsDistribution 游程分布检测
func RunsDistribution(data []byte) *TestResult {
p := RunsDistributionTestBytes(data)
return &TestResult{Name: "游程分布检测", P: p, Pass: p >= Alpha}
}

// RunsDistributionTestBytes 游程分布检测
func RunsDistributionTestBytes(data []byte) float64 {
return RunsDistributionTest(B2bitArr(data))
Expand Down
41 changes: 41 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package randomness

// Alpha 显著性水平α
const Alpha = 0.01

// TestResult 检测结果
type TestResult struct {
Name string // 检测名称
P float64 // 检测结果
P2 float64 // 检测结果P2
Pass bool // 是否大于等于显著水平
}

// TestFunc 测试方法
type TestFunc func([]byte) *TestResult

// TestItem 测试项目
type TestItem struct {
Name string // 检测名称
// 检测方法
Runner TestFunc
}

// TestMethodArr 测试方法序列
var TestMethodArr = []TestItem{
{"单比特频数检测", MonoBitFrequency},
{"块内频数检测", FrequencyWithinBlock},
{"扑克检测", Poker},
{"重叠子序列检测", OverlappingTemplateMatching},
{"游程总数检测", Runs},
{"游程分布检测", RunsDistribution},
{"块内最大“1”游程检测", LongestRunOfOnesInABlock},
{"二元推导检测", BinaryDerivative},
{"自相关检测", Autocorrelation},
{"矩阵秩检测", MatrixRank},
{"累加和检测", Cumulative},
{"近似熵检测", ApproximateEntropy},
{"线型复杂度检测", LinearComplexity},
{"通用统计检测", MaurerUniversal},
{"离散傅里叶检测", DiscreteFourierTransform},
}

0 comments on commit bbae9b4

Please sign in to comment.