-
Notifications
You must be signed in to change notification settings - Fork 4
/
gen_raw_key.py
69 lines (53 loc) · 1.87 KB
/
gen_raw_key.py
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
#!/usr/bin/env python
# vim: set fileencoding=UTF-8 filetype=python :
r"""
Key statistics calculation
AUTHORS:
- Thomas Loruenser (2013): initial version
"""
###############################################################################
# Copyright 2013, Thomas Loruenser <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
from __future__ import print_function
import sys
import os
import numpy as np
from common import rk_fname
from common import ensure_dir
### globals
size_byte = 2**20
fpcoff = 1000
### functions
# generate raw key and store to file
def gen_key_file(error_rate, get_fname):
# gen raw key
rk = np.zeros(size_byte*8, 'u1')
# introduce error
rk += (np.random.randint(0, fpcoff, size_byte*8) < error_rate*fpcoff).view('u1')
print("Generated error rate:", float(rk.sum())/rk.size, rk)
# save to file
frk = open(rk_fname(error_rate), 'wb')
frk.write(np.packbits(rk).tostring())
frk.close()
### main
if __name__ == '__main__':
# ensure keys dir exist
ensure_dir(rk_fname(0.0))
# print some info
print("File size=", size_byte)
# gen key files
for fperror in range(0, 70, 5):
gen_key_file(float(fperror)/fpcoff, rk_fname)