def generateBP(self): """Transform UC to BP. Call UC generation if UC not present.""" if self.uc is None: self.generateUC() logging.info('Starting BP generation') self.bp = BranchingProgram.fromCircuit(self.uc) logging.info('BP generation successful')
def bp_exp(params, inputFilesFormatString): """Transforming UCs into BPs. params should be a tuple containing (inputLength, numberOfGates) """ inputLength, numberOfGates = params logging.warning('Starting BP generation for %d inputs and %d gates' % params) ioGen = IndistinguishabilityObfuscationGenerator(inputLength, numberOfGates, 100) logging.info('Loading uc_%d_%d'%params) with open('results/'+inputFilesFormatString%params, 'rb') as input: ioGen.uc = cPickle.load(input) logging.warning('Loading successful: %s'%ioGen.uc) logging.warning('Estimated BP size: %d'%BranchingProgram.estimateBPSize(ioGen.uc)) hp.setrelheap() with Timer() as t: ioGen.generateBP() execTime = t.secs memoryUsage = hp.heap().size bpSize = ioGen.bp.length logging.info('Memory usage: %d'%memoryUsage) logging.info('BP length: %d'%bpSize) dbInfo = (inputLength, numberOfGates, bpSize, memoryUsage) return (ioGen.bp, dbInfo, execTime)
def setUp(self): self.inputLength = 2 outputLength = 1 numberOfGates = 2 inputs = [Input("x"+str(x)) for x in range(0, self.inputLength)] # (-(x0 & x1) & (-x2 & x3)) & ((x4 & x5) & -(x6 & -x7)) self.circuit = Circuit(NotGate(AndGate(inputs[0], inputs[1]))) uc = UniversalCircuit(self.inputLength, outputLength, numberOfGates) bp = BranchingProgram.fromCircuit(uc) self.fixedBP = fixBP(bp, self.circuit)
def setUp(self): self.inputLength = 2 outputLength = 1 numberOfGates = 2 inputs = [Input("x" + str(x)) for x in range(0, self.inputLength)] # (-(x0 & x1) & (-x2 & x3)) & ((x4 & x5) & -(x6 & -x7)) self.circuit = Circuit(NotGate(AndGate(inputs[0], inputs[1]))) uc = UniversalCircuit(self.inputLength, outputLength, numberOfGates) bp = BranchingProgram.fromCircuit(uc) self.fixedBP = fixBP(bp, self.circuit)
def fixBP(bp, circuit): """Fix a UBP using the binary description of a circuit.""" logging.info('Starting BP fixing') ctrlInput = UniversalCircuit.obtainCtrlInput(circuit) logging.info('circuit control input: %s'%ctrlInput) assert(len(circuit.inputs)+len(ctrlInput) > max(bp.indexList)) newIns0, newIns1, newIndex = fixInstructions(bp.ins0, bp.ins1, bp.indexList, ctrlInput, dot) newBP = BranchingProgram(newIns0, newIns1, newIndex, bp.zeroPerm, bp.onePerm) logging.info('new BP: %s'%newBP) logging.info('BP fixed successfully') return newBP
def uc_exp(params, inputFilesFormatString): """Universal circuit creation. params should be a tuple containing (inputLength, numberOfGates) """ inputLength, numberOfGates = params logging.warning('Starting UC generation for %d inputs and %d gates' % params) ioGen = IndistinguishabilityObfuscationGenerator(inputLength, numberOfGates, 100) hp.setrelheap() with Timer() as t: ioGen.generateUC() execTime = t.secs memoryUsage = hp.heap().size #Determine datapoints numAndGates = ioGen.uc.countGates(1) numAndGatesNoReuse = ioGen.uc.countGates(1, False) logging.info('Number of And Gates: %d (no reuse: %d)'%(numAndGates, numAndGatesNoReuse)) numNotGates = ioGen.uc.countGates(0) numNotGatesNoReuse = ioGen.uc.countGates(0, False) logging.info('Number of Not Gates: %d (no reuse: %d)'%(numNotGates, numNotGatesNoReuse)) logging.info('Number of Gates: %d'%(numAndGates+numNotGates)) depth = ioGen.uc.getDepth() logging.info('Circuit depth: %d'%depth) estBPsize = BranchingProgram.estimateBPSize(ioGen.uc) logging.info('Estimated bp size: %d'%estBPsize) logging.info('Memory usage: %d'%memoryUsage) if estBPsize > 2**63: estBPsize = -1 logging.info('estimated size too big for sqlite integers') dbInfo = (inputLength, numberOfGates, numAndGates+numNotGates, numAndGates, numNotGates, numAndGatesNoReuse, numNotGatesNoReuse, depth, estBPsize, memoryUsage) return (ioGen.uc, dbInfo, execTime)
assert(len(newIndex)==len(newIns0)) return (newIns0, newIns1, newIndex) if __name__ == '__main__': from obfusc8.circuit import Circuit, Input, AndGate, NotGate from itertools import product inputLength = 2 outputLength = 1 numberOfGates = 2 inputs = [Input("x"+str(x)) for x in range(0, inputLength)] # (-(x0 & x1) & (-x2 & x3)) & ((x4 & x5) & -(x6 & -x7)) circuit = Circuit(NotGate(AndGate(inputs[0], inputs[1]))) print circuit uc = UniversalCircuit(inputLength, outputLength, numberOfGates) print uc bp = BranchingProgram.fromCircuit(uc) print bp fixedBP = fixBP(bp, circuit) print 'After fixing: %s'%fixedBP for test in list(product([0,1], repeat=inputLength)): test = list(test) circuitResult = circuit.evaluate(test) bpResult = fixedBP.evaluate(test) print('Input: %s => circuit: %d, fixedBP: %d, equal?: %s'%(test, circuitResult, bpResult, circuitResult==bpResult))
def fromCircuit(cls, circuit, p, rndMatSize=None): """Create a RBP directly with a circuit input""" return cls(BranchingProgram.fromCircuit(circuit), p, rndMatSize)