0

I need to test the randomness of a Blum Blum Shub generator I built in Python, using the ENT randomness tests. I have a python array of pseudo random bits of form [0, 1, 1, 1, 0, 0, 1, 0, 0, ...], and I need to write this sequence of bits into a binary file that I can then run through the ENT randomness test platform (ENT website linked here).

Would anyone be able to help me find a way to do this? I tried using the struct package, but I do not think I am using it correctly: if p is the bit array as shown above, I am writing to the file as follows using struct:

f=open("myfile","wb")
myfmt='b'*len(p)
bin=struct.pack(myfmt,*p)
f.write(bin)
f.close()

Could anyone point out how to correctly do this? Further, if instead the array was not of 0, 1 values but instead composed of the pseudo random positive integers, what would be the correct way to write these to a file to test for randomness?

7
  • What's the error you get? We don't have p so we can't check :( I see from docs.python.org/3/library/struct.html#format-characters that the 'b' format string for a byte, so whatever p is an array of (something between -128 and 127), it should dump them into a binary file, one byte per input. For example, the following emits a 3-byte file as expected: with open('myfile', 'wb') as fid: fid.write(struct.pack('3b', *[1,0,1])) Commented Apr 12, 2020 at 1:08
  • If you want random bits I suggest you use in integer and set the bits with bit operators. They are inherently packed, then. Commented Apr 12, 2020 at 1:10
  • @AhmedFasih p looks like the array [0, 1, 1, 0, 1, 1, 0, 1, ...]. It is not that I get an error but that I am not sure if this is correct. Commented Apr 12, 2020 at 1:10
  • @Keith So it would be better to take the "raw" array of random integers and convert those to bits directly? I would happily close an answer that explained how to do this and why this was correct. Commented Apr 12, 2020 at 1:12
  • Ok, then the output file will one one byte per bit. If your generator produces positive integers, you can use H, I, L, or Q format characters for 1, 2, 4, or 8-bytes per element in the output file respectively. I'm not familiar with ENT though, so it's possible this isn't the format it requires (i.e., it might want random bits packed with no padding). Commented Apr 12, 2020 at 1:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.