def test_keysigs(self): m = hashlib.sha256() m.update(b"Nobody respects") m.update(b" the spammish repetition") skx, pkx = genkeys(n, p, g) hm = int(m.hexdigest(), 16) sig = sign(skx, hm, p, g, q) self.assertTrue(verify(pkx, sig, hm, p, g, q)) self.assertFalse(verify(pkx, sig, hm + 1, p, g, q))
def test_2(self): print("Test 2: test isValidTx() with some invalid transactions") nPeople = 10 people = [] #new List RSAKeyPair for i in range(nPeople): sk, pk = genkeys(n, p, g) people.append((sk, pk)) # Create a pool and an index into key pairs utxoPool = UTXOPool() utxoToKeyPair = {} keyPairAtIndex = {} nUTXOTx = 10 maxUTXOTxOutput = 5 maxInput = 3 maxValue = 100 for i in range(nUTXOTx): num = random.randint(1, maxUTXOTxOutput) tx = Transaction() # add num randonm outputs to tx for j in range(num): # pick a random public address rIndex = random.randint(0, len(people) - 1) #print("Index",rIndex); print(people[rIndex]) addr = people[rIndex][1] #(sk,pk) value = random.random() * maxValue tx.addOutput(value, addr) keyPairAtIndex[j] = people[rIndex] tx.finalize() # add all num tx outputs to utxo pool for j in range(num): ut = UTXO(tx.hash, j) utxoPool.addUTXO(ut, tx.getOutput(j)) utxoToKeyPair[ut] = keyPairAtIndex[j] utxoSet = utxoPool.getAllUTXO() print("Len of utxoSet", len(utxoSet)) maxValidInput = min(maxInput, len(utxoSet)) nTxPerTest = 11 maxValidInput = 2 maxOutput = 3 passes = True for i in range(nTxPerTest): tx = Transaction() uncorrupted = True utxoAtIndex = {} nInput = random.randint(1, maxValidInput + 1) inputValue = 0.0 for j in range(nInput): utxo = random.sample(utxoSet, 1)[0] tx.addInput(utxo.getTxHash(), utxo.getIndex()) inputValue += utxoPool.getTxOutput(utxo).value utxoAtIndex[j] = utxo nOutput = random.randint(1, maxOutput) outputValue = 0.0 for j in range(nOutput): value = random.random() * (maxValue) if (outputValue + value > inputValue): break rIndex = random.randint(0, len(people) - 1) addr = people[rIndex][1] tx.addOutput(value, addr) outputValue += value pCorrupt = 0.1 for j in range(nInput): m = hashlib.sha256() m.update(str.encode(tx.getRawDataToSign(j))) hm = int(m.hexdigest(), 16) if (random.random() < pCorrupt): hm += 1 uncorrupted = False keyPair = utxoToKeyPair[utxoAtIndex[j]] tx.addSignature(sign(keyPair[0], hm, p, g, q), j) tx.finalize() if (isValidTx(tx, utxoPool) != uncorrupted): passes = False self.assertTrue(passes)
# Test case n, g, p, q = 256, 11189361631195852088154673407566885728548496486362662112597687161142104619469702160215294558351391466982303919803857229515093575816938371433954759500448775, 11220611807188583130302963536190351192186270126479330588604287699892081267588448305835704397593153801135202051719876685351614175538253684346816652027037363, 1218442816993522937915646204915776994404649089503 #m = hashlib.sha256() #m.update(b"Nobody respects") #m.update(b" the spammish repetition") #hm=int(m.hexdigest(),16) ## Genesis Transaction value = 100 idx = 0 h0 = hashlib.sha256() t0 = Transaction() t0.addInput(h0, idx) sk0, pk0 = genkeys(n, p, g) t0.addOutput(value, pk0) m = hashlib.sha256() m.update(t0.getRawDataToSign(idx)) hm = int(m.hexdigest(), 16) sig0 = sign(sk0, hm, p, g, q) t0.addSignature(sig0, idx) t0.finalize() # Convert genesis transaction to UTXO ut = UTXO(t0.hash, idx) # Add to UTXOPool pool = UTXOPool() pool.put(ut, t0.outputs)
def test_1(self): print("Test 1: test isValidTx() with valid transactions") nPeople = 10 people = [] #new List DigSigKeyPair for i in range(nPeople): sk, pk = genkeys(n, p, g) people.append((sk, pk)) # Create a pool and an index into key pairs utxoPool = UTXOPool() utxoToKeyPair = {} keyPairAtIndex = {} nUTXOTx = 10 maxUTXOTxOutput = 5 maxInput = 3 maxValue = 100 for i in range(nUTXOTx): num = random.randint(1, maxUTXOTxOutput) tx = Transaction() # add num randonm outputs to tx for j in range(num): # pick a random public address rIndex = random.randint(0, len(people) - 1) #print("Index",rIndex); print(people[rIndex]) addr = people[rIndex][1] #(sk,pk) value = random.random() * maxValue tx.addOutput(value, addr) keyPairAtIndex[j] = people[rIndex] tx.finalize() # add all num tx outputs to utxo pool for j in range(num): ut = UTXO(tx.getHash(), j) utxoPool.addUTXO(ut, tx.getOutput(j)) utxoToKeyPair[ut] = keyPairAtIndex[j] utxoSet = utxoPool.getAllUTXO() print("Len of utxoSet", len(utxoSet)) maxValidInput = min(maxInput, len(utxoSet)) nTxPerTest = 11 maxValidInput = 2 maxOutput = 3 passes = True for i in range(nTxPerTest): tx = Transaction() utxoAtIndex = {} nInput = random.randint(1, maxValidInput + 1) inputValue = 0.0 for j in range(nInput): utxo = random.sample(utxoSet, 1)[0] tx.addInput(utxo.getTxHash(), utxo.getIndex()) inputValue += utxoPool.getTxOutput(utxo).value utxoAtIndex[j] = utxo nOutput = random.randint(1, maxOutput) outputValue = 0.0 for j in range(nOutput): value = random.random() * (maxValue) if (outputValue + value > inputValue): break rIndex = random.randint(0, len(people) - 1) addr = people[rIndex][1] tx.addOutput(value, addr) outputValue += value for j in range(nInput): tx.addSignature( sign(utxoToKeyPair[utxoAtIndex[j]][0], tx.getRawDataToSign(j), p, g), j) tx.finalize() if (not txHandler.isValidTx(tx)): passes = False self.assertTrue(passes)