diff --git a/readme.md b/readme.md index a339b0e..a8ebbac 100644 --- a/readme.md +++ b/readme.md @@ -217,9 +217,9 @@ $$ ​ 我们使用NIST提供的 [Statistical Test Suite](https://csrc.nist.gov/Projects/Random-Bit-Generation/Documentation-and-Software) 对哈希结果的随机性进行检测。 -​ 首先生成哈希序列。在 $[0,2^{31}-1)$ 中从零开始均匀选取 $2^{21}$ 个数字,将数字对应的 $32$ 位整数作为输入进行哈希,得到 $20MB$ 输出。使用 NIST Statistical Test Suite 基于多种度量进行随机性测试。默认参数选择 $n=1,500,000$ 可符合大部分测试的要求,按文档[6] 要求使用其他参数的,在下表最后一列注明。 +​ 首先生成哈希序列。在 $[0,2^{31}-1)$ 中从零开始均匀选取 $2^{21}$ 个数字,将数字对应的 $32$ 位整数作为输入进行哈希,得到 $20MB$ 输出。使用 NIST Statistical Test Suite 基于多种度量进行随机性测试。默认参数选择 $n=1,500,000$ 可符合大部分测试的要求,按文档 [6] 要求使用其他参数的,在下表最后一列注明。 -| 编号 | 测试类型 | 通过率 | p值均匀性 | 非默认参数 | +| 编号 | 测试类型 | 通过率 | p 值均匀性 | 非默认参数 | | ---- | ------------------------------ | ----------- | ------------ | ------------------------------- | | 01 | Frequency | 111/111 | 0.580520 | - | | 02 | Block Frequency | 20758/20971 | 0.273558 | $n=8000,M=80$ | @@ -263,18 +263,12 @@ $$ 定量计算 -1. 改变位数的最小值 +1. 改变位数的最小值 $B_{\min }=\min \left(\left\{B_{i}\right\}_{i=1, \ldots, N}\right)$ -​ $B_{\min }=\min \left(\left\{B_{i}\right\}_{i=1, \ldots, N}\right)$ - -2. 改变位数的最大值 - $B_{\max }=\max \left(\left\{B_{i}\right\}_{i=1, \ldots, N}\right)$ -3. 改变位数的标准差 - $\Delta B=\sqrt{\frac{1}{N-1} \sum_{i=1}^{N}\left(B_{i}-\bar{B}\right)^{2}}$ -4. 改变位数的平均值 - $\bar{B}=\frac{1}{N} \sum_{i=1}^{N} B_{i}$ -5. 每位的改变概率 - $P=\left(\frac{\bar{B}}{80}\right) \times 100 \%$ +2. 改变位数的最大值 $B_{\max }=\max \left(\left\{B_{i}\right\}_{i=1, \ldots, N}\right)$ +3. 改变位数的标准差 $\Delta B=\sqrt{\frac{1}{N-1} \sum_{i=1}^{N}\left(B_{i}-\bar{B}\right)^{2}}$ +4. 改变位数的平均值 $\bar{B}=\frac{1}{N} \sum_{i=1}^{N} B_{i}$ +5. 每位的改变概率 $P=\left(\frac{\bar{B}}{80}\right) \times 100 \%$ | 最小值 | 最大值 | 标准差 | 平均值 | 每位变化概率 | @@ -302,7 +296,7 @@ $$ [5] Penard W, van Werkhoven T. On the secure hash algorithm family[J]. Cryptography in context, 2008: 1-18. -[6] Bassham, L. , Rukhin, A. , Soto, J. , Nechvatal, J. , Smid, M. , Leigh, S. , Levenson, M. , Vangel, M. , Heckert, N. and Banks, D. (2010), A Statistical Test Suite for Random and Pseudorandom Number Generators for Cryptographic Applications, Special Publication (NIST SP), National Institute of Standards and Technology, Gaithersburg, MD. +[6] Smid E B, Leigh S, Levenson M, et al. A statistical test suite for random and pseudorandom number generators for cryptographic applications[J]. Her research interest includes Computer security, secure operating systems, Access control, Distributed systems, Intrusion detection systems, 2010. @@ -329,7 +323,8 @@ bin/SHA-RNN -s # (3) Hash string │   ├── SHA-RNN # 主程序 │   ├── test1 # Basic Usage Test │   ├── test2 # Consistency Test -│   └── test3 # Perf Test +│   ├── test3 # Perf Test +│   └── test4 # Diffusion Test ├── include │   └── define.h ├── main.cpp @@ -345,9 +340,17 @@ bin/SHA-RNN -s # (3) Hash string │   ├── RNN.hpp # RNN 架构 │   ├── RNNHash.hpp # 派生自 SpongeHash,负责调用 RNN 架构的逻辑 │   └── SpongeHash.hpp # 海绵结构 +├── statistics +│   ├── Makefile +│   ├── analyze.ipynb #  扩散数据分析 +│   ├── diffusion.cpp # 扩散数据生成 +│   ├── eval.py # NIST 随机性测试脚本 +│   ├── sample.cpp # 随机性测试采样 +│   └── text.in # 扩散测试样本数据 └── test ├── test1.cpp # Basic Usage Test ├── test2.cpp # Consistency Test - └── test3.cpp # Perf Test + ├── test3.cpp # Perf Test + └── test4.cpp # Diffusion Test ``` diff --git a/readme.pdf b/readme.pdf index f51f30a..90e904c 100644 Binary files a/readme.pdf and b/readme.pdf differ diff --git a/statistics/defines.h b/statistics/defines.h deleted file mode 100644 index 5ab7de8..0000000 --- a/statistics/defines.h +++ /dev/null @@ -1,2 +0,0 @@ -const int block_size {136}; -const int filp_count {10000}; \ No newline at end of file diff --git a/test/test_diffusion.cpp b/test/test_diffusion.cpp deleted file mode 100644 index d534ef4..0000000 --- a/test/test_diffusion.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "RNNHash.hpp" -#include "define.h" - -const int flip_count {10000}; - - -void flip_test() { - std::ifstream s {"text.in", std::ifstream::binary}; - std::ofstream record {"diffusion.out"}; - std::stringstream ssbuf; - ssbuf << s.rdbuf(); - auto stream {ssbuf.str()}; - - auto buf = new byte[stream.size()]; - std::copy(stream.begin(), stream.end(), buf); - std::cerr << buf << std::endl; - - std::random_device dev; - std::mt19937 rng(dev()); - std::uniform_int_distribution byte_dist(0, stream.size() - 1); - std::uniform_int_distribution bit_dist(0, 7); - - RNNHash h; - RNNHash::Out_t original = h(ssbuf); - // record << stream << std::endl; - // record << original.to_binary_string() << std::endl << std::endl; - - - for (int i = 0; i < flip_count; i++) { - auto bit_r {bit_dist(rng)}; - auto byte_r {byte_dist(rng)}; - auto byte_buf = buf + byte_r; - - *byte_buf = ~ ((*byte_buf) | (0xff ^ (1 << bit_r))) | ((*byte_buf) & (0xff ^ (1 << bit_r))); - - std::stringstream input; - std::string new_stream {(char *)buf}; - input.str(new_stream); - - RNNHash hash; - RNNHash::Out_t out = hash(input); - // std::cerr << new_stream << std::endl; - // record << "flip byte: " << byte_r << "; bit: " << bit_r << std::endl; - // record << out.to_binary_string() << std::endl; - out ^= original; - record << out.one_count() << std::endl; - // if (out.one_count() == 0) { - // std::fstream opt {"1.bin"}; - // std::ofstream ori{"2.bin"}; - // opt << new_stream << std::endl; - // ori << stream << std::endl; - // exit(0); - // } - - *byte_buf = ~ ((*byte_buf) | (0xff ^ (1 << bit_r))) | ((*byte_buf) & (0xff ^ (1 << bit_r))); - } - -} - -int main() { - - flip_test(); - - return 0; -} \ No newline at end of file