|
| 1 | +# bch_code.py |
| 2 | +# Copyright (2014) Sandia Corporation. Under the terms of Contract |
| 3 | +# DE-AC04-94AL85000, there is a non-exclusive license for use of this |
| 4 | +# work by or on behalf of the U.S. Government. Export of this program |
| 5 | +# may require a license from the United States Government. |
| 6 | +# |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License |
| 18 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | +# |
| 20 | +import os, bitstring, random, math |
| 21 | +import subprocess |
| 22 | + |
| 23 | +from bitstringutils import * |
| 24 | + |
| 25 | +class bch_code(object): |
| 26 | + """An error-correcting class""" |
| 27 | + |
| 28 | + def __init__(self, nb=1024, fileName='bch_code_log.dat'): |
| 29 | + self.nb = nb |
| 30 | + self.bit_flips = None |
| 31 | + self.logFileName = fileName |
| 32 | + self.logFile = open(self.logFileName, 'ab') |
| 33 | + |
| 34 | + def __destroy__(self): |
| 35 | + self.close() |
| 36 | + |
| 37 | + def close(self): |
| 38 | + if (not self.logFile.closed): |
| 39 | + self.logFile.flush() |
| 40 | + self.logFile.close() |
| 41 | + |
| 42 | + def setup(self, first_measurement, MM=13, TT=20, KK=1024, PP=8): |
| 43 | + """A function to enroll the PUF with the error corrector object |
| 44 | + |
| 45 | + Arguments: |
| 46 | + MM <field>: Galois field, GF, for code. Code length is 2^<field>-1. |
| 47 | + The default value is 13 for a code length 8191. If the parameter is |
| 48 | + set to 0, the program will estimate the value based upon the values |
| 49 | + chosen for k and t. |
| 50 | + TT <correct>: Correction power of the code. Default = 4 |
| 51 | + KK <data bits>: Number of data bits to be encoded. Must divide 4. |
| 52 | + The default value is the maximum supported by the code which |
| 53 | + depends upon the field (-m) and the correction (-t) chosen. |
| 54 | + PP <parallel>: Parallelism in encoder. Does not effect results but |
| 55 | + does change the algorithm used to generate them. Default = 8""" |
| 56 | + |
| 57 | + self.m = MM; self.t = TT; self.k = KK; self.p = PP |
| 58 | + p = subprocess.Popen("bch_encoder.exe -m %d -t %d -k %d -p %d" % (self.m, self.t, self.k, self.p), stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 59 | + p.stdin.write(first_measurement.hex) |
| 60 | + output, errors = p.communicate() |
| 61 | + codeword, syndrome = output.split() |
| 62 | + self.syndrome = syndrome |
| 63 | + return self.syndrome |
| 64 | + |
| 65 | + def decode(self, response): |
| 66 | + p = subprocess.Popen("bch_decoder.exe -m %d -t %d -k %d -p %d" % (self.m, self.t, self.k, self.p), stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 67 | + p.stdin.write(response.hex + self.syndrome + "\n") |
| 68 | + p.stdin.flush() |
| 69 | + p.stdin.close() |
| 70 | + output, errors = p.communicate() |
| 71 | + if len (output.strip()) != self.nb / 4: |
| 72 | + raise ValueError ("Invalid signature returned from decoder") |
| 73 | + |
| 74 | + return bitstring.Bits(hex="0x"+output.strip()) |
| 75 | + |
| 76 | +if __name__=="__main__": |
| 77 | + print "Running self-test" |
| 78 | + |
| 79 | + import simulator |
| 80 | + mySim = simulator.Simulator() |
| 81 | + mySim.setup() |
| 82 | + |
| 83 | + firstMeasurement = mySim.next() |
| 84 | + print firstMeasurement.hex |
| 85 | + myCoder = bch_code() |
| 86 | + helper_data = myCoder.setup(firstMeasurement) # setup with defaults |
| 87 | + |
| 88 | + print "Syndrome: " + myCoder.syndrome |
| 89 | + |
| 90 | + newMeasurement = mySim.next() |
| 91 | + print "(Possibly-) Errored Measurement:\n" + newMeasurement.hex |
| 92 | + print "Errors: " + str(hd(firstMeasurement, newMeasurement)) |
| 93 | + print "Recovered:\n" + myCoder.decode(newMeasurement).hex |
| 94 | + print "Reduced errors: " + str(hd(firstMeasurement, myCoder.decode(newMeasurement))) |
| 95 | + |
| 96 | + print "Done!" |
| 97 | + |
0 commit comments