Instantly share code, notes, and snippets.

# StuartGordonReid/BinaryMatrixRank.py

Created August 25, 2015 15:57
Star You must be signed in to star a gist
Python implementation of the Binary Matrix Rank cryptographic test for randomness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 def matrix_rank(self, bin_data: str, q=32): """ Note that this description is taken from the NIST documentation   http://csrc.nist.gov/publications/nistpubs/800-22-rev1a/SP800-22rev1a.pdf The focus of the test is the rank of disjoint sub-matrices of the entire sequence. The purpose of this test is to check for linear dependence among fixed length sub strings of the original sequence. Note that this test also appears in the DIEHARD battery of tests. :param bin_data: a binary string :return: the p-value from the test """ shape = (q, q) n = len(bin_data) block_size = int(q * q) num_m = math.floor(n / (q * q)) block_start, block_end = 0, block_size # print(q, n, num_m, block_size) if num_m > 0: max_ranks = [0, 0, 0] for im in range(num_m): block_data = bin_data[block_start:block_end] block = numpy.zeros(len(block_data)) for i in range(len(block_data)): if block_data[i] == '1': block[i] = 1.0 m = block.reshape(shape) ranker = BinaryMatrix(m, q, q) rank = ranker.compute_rank() # print(rank) if rank == q: max_ranks += 1 elif rank == (q - 1): max_ranks += 1 else: max_ranks += 1 # Update index trackers block_start += block_size block_end += block_size piks = [1.0, 0.0, 0.0] for x in range(1, 50): piks *= 1 - (1.0 / (2 ** x)) piks = 2 * piks piks = 1 - piks - piks chi = 0.0 for i in range(len(piks)): chi += pow((max_ranks[i] - piks[i] * num_m), 2.0) / (piks[i] * num_m) p_val = math.exp(-chi / 2) return p_val else: return -1.0

### ITFreha commented Mar 28, 2019

What is the BinaryMatrix?

### ITFreha commented Mar 29, 2019

@StuartGordonReid is BinaryMatrix array?

### ITFreha commented Mar 29, 2019

@StuartGordonReid China number one