def gen_random_txblock(q, p, g, TxCnt, filename): empty_str='' for i in range (TxCnt): string = "*** Bitcoin transaction ***\n" # Key Generation phase (alpha1, beta1) = DS.KeyGen(q, p, g) # Keys of payer (alpha2, beta2) = DS.KeyGen(q, p, g) # Keys of payee serial = random.randint(0, 2**128 - 1) string += "Serial number: " + str(serial) + "\n" string += "Payer Public Key (beta): " + str(beta1) + "\n" string += 'Payee Public Key (beta): ' + str(beta2) + "\n" amount = random.randint(1, 1000000) string += "Amount: " + str(amount) + " Satoshi" + "\n" #assigning s and r keys (s, r) = DS.SignGen(string.encode('UTF-8'), q, p, g, alpha1) string += "Signature (s): " + str(s) + "\n" string += "Signature (r): " + str(r) + "\n" empty_str=empty_str + string #print(empty_str) filename='transactions.txt' f = open(filename,"w") f.write(empty_str) f.close()
def gen_random_tx(q, p, g): # Generate Serial serial = random.randrange(pow(2, 127), pow(2, 128)) a1, pk1 = DS.KeyGen(q, p, g) a2, pk2 = DS.KeyGen(q, p, g) amount = random.randrange(1, 1000001) s, h = DS.SignGen(amount, q, p, g, a1) trans = "**** Bitcoin transaction ****\n" + "Serial number: " + str(serial) + "\nPayer public key (beta): " + \ str(pk1) + "\nPayee public key (beta): " + str(pk2) + "\nAmount: " + str(amount) + "\nSignature (s): " + \ str(s) + "\nSignature (h): " + str(h) + "\n" return trans
def gen_random_tx(q, p, g): # Generate Serial serial = random.randrange(pow(2, 127), pow(2, 128)) a1, pk1 = DS.KeyGen(q, p, g) a2, pk2 = DS.KeyGen( q, p, g ) # This isn't actually the key of the payee, the payer would know the wallet id in a real transaction amount = random.randrange(1, 1000001) s, h = DS.SignGen(amount, q, p, g, a1) trans = "**** Bitcoin transaction ****\n" + "Serial number: " + str(serial) + "\nPayer public key (beta): " + \ str(pk1) + "\nPayee public key (beta): " + str(pk2) + "\nAmount: " + str(amount) + "\nSignature (s): " + \ str(s) + "\nSignature (h): " + str(h) + "\n" return trans
def gen_random_tx(q, p, g): serialNumber = randrange(0, (2**128) - 1) payerPrivateKey, payerPublicKey = DS.KeyGen(q, p, g) payeePrivateKey, payeePublicKey = DS.KeyGen(q, p, g) amount = randrange(1, 1000000) s, r = DS.SignGen(serialNumber, q, p, g, payerPrivateKey) tx = "*** Bitcoin transaction ***" + '\n' tx = tx + "Serial number:" + str(serialNumber) + '\n' tx = tx + "Payer public key (beta):" + str(payerPublicKey) + '\n' tx = tx + "Payee public key (beta):" + str(payeePublicKey) + '\n' tx = tx + "Amount:" + str(amount) + '\n' tx = tx + "Signature (s):" + str(s) + '\n' tx = tx + "Signature (r):" + str(r) + '\n' return tx
def gen_random_tx(q, p, g): transaction = "*** Bitcoin transaction ***\n" serial_number = random.getrandbits(128) transaction += "Serial number: " + str(serial_number) + "\n" payerAlpha, payerBeta = DS.KeyGen(q, p, g) payeeAlpha, payeeBeta = DS.KeyGen(q, p, g) transaction += "Payer Public Key (beta): " + str(payerBeta) + "\n" transaction += "Payee Public Key (beta): " + str(payeeBeta) + "\n" amount = random.randint(1, 1000001) transaction += "Amount: " + str(amount) (s, r) = DS.SignGen(transaction.encode('UTF-8'), q, p, g, payerAlpha) transaction += "Signature (s): " + str(s) + "\n" transaction += "Signature (r): " + str(r) + "\n" return transaction
def gen_random_tx(q, p, g): string = "*** Bitcoin transaction ***\n" # Key Generation phase (alpha1, beta1) = DS.KeyGen(q, p, g) # Keys of payer (alpha2, beta2) = DS.KeyGen(q, p, g) # Keys of payee serial = random.randint(0, 2**128 - 1) string += "Serial number: " + str(serial) + "\n" string += "Payer Public Key (beta): " + str(beta1) + "\n" string += 'Payee Public Key (beta): ' + str(beta2) + "\n" amount = random.randint(1, 1000000) string += "Amount: " + str(amount) + " Satoshi" + "\n" #assigning s and r keys (s, r) = DS.SignGen(string.encode('UTF-8'), q, p, g, alpha1) string += "Signature (s): " + str(s) + "\n" string += "Signature (r): " + str(r) + "\n" #print(string) Bitcoin transaction can be seen here return string
def GenerateTestSignatures(q, p, g): f = open("TestSet.txt", "w") f.write(str(q) + "\n") f.write(str(p) + "\n") f.write(str(g) + "\n") (alpha, beta) = DS.KeyGen(q, p, g) f.write(str(beta) + "\n") for i in range(0, 10): message = random_string(random.randint(32, 512)) (s, h) = DS.SignGen(message.encode('UTF-8'), q, p, g, alpha) f.write(message + "\n") f.write(str(s) + "\n") f.write(str(h) + "\n") f.close() return 0
# Testing part # Test public parameters ReturnCode = checkDSparams(q, p, g) if ReturnCode == 0: print("Public parameters: Passed!") elif ReturnCode == -1: print("q is not prime") elif ReturnCode == -2: print("p is not prime") elif ReturnCode == -3: print("q does not divide p") elif ReturnCode == -4: print("g is not a generator") (alpha, beta) = DS.KeyGen(q, p, g) # generate key pair ReturnCode = CheckKeys(q, p, g, alpha, beta) if ReturnCode == 0: print("Public/private key pair: Passed!") else: print("Public/private key pair: Failed!") ReturnCode = CheckSignature(q, p, g, alpha, beta) if ReturnCode == 0: print("Signature generation: Passed!") else: print("Signature generation: Failed!") if (CheckTestSignatures() == 0): print("Sample signature test: Passed!")