Posted: August 6th, 2022

CWID: 50284838

Name: Pradhumna Gautam

import hashlib

import time

max_nonce = 2 ** 32 # 4 billion

def proof_of_work(header, difficulty_bits):

# calculate the difficulty target

target = 2 ** (256-difficulty_bits)

for nonce in range(max_nonce):

# define hash_result based on Problem1.a and Problem1.b

# hash_result

# check if this is a valid result, below the target

if int(hash_result, 16) < target:
print("Success with nonce %d" % nonce)
print("Hash is %s" % hash_result)
return (hash_result,nonce)
print("Failed after %d (max_nonce) tries" % nonce)
return nonce
if __name__ == '__main__':
nonce = 0
hash_result = ''
# difficulty from 0 to 31 bits
for difficulty_bits in range(32):
difficulty = 2 ** difficulty_bits
print("Difficulty: %ld (%d bits)" % (difficulty, difficulty_bits))
print("Starting search...")
# checkpoint the current time
start_time = time.time()
# make a new block which includes the hash from the previous block
# we fake a block of transactions - just a string
new_block = 'test block with transactions' + hash_result
# find a valid nonce for the new block
(hash_result, nonce) = proof_of_work(new_block, difficulty_bits)
# checkpoint how long it took to find a result
end_time = time.time()
elapsed_time = end_time - start_time
print("Elapsed Time: %.4f seconds" % elapsed_time)
if elapsed_time > 0:

# estimate the hashes per second

hash_power = float(int(nonce)/elapsed_time)

print(“Hashing Power: %ld hashes per second” % hash_power)

print(“\n====================\n”)

CSCI 497 Assignment 03

30 points

Instruction:

• Show your work (at least 50% penalty otherwise)

• Submit a single Word ( or x) document containing all your answers to the

designated folder (“Assignment #3”) under Assignments (at least 10% penalty otherwise).

Neither PDF documents nor documents in other formats would be accepted.

• Make sure if you submitted the intended one. It is recommended that you download

what has been uploaded and double-check if the correct document has been submitted.

• You can submit as many times as you want, but the last submission will only be graded.

If the last submission is made after the deadline, there should be a late submission

penalty.

• No plagiarism: Do not copy and paste any from textbooks and other resources to answer

questions (Zero will be given otherwise).

• No extension/resubmission request will be accepted.

Note: The instructor and TA will welcome your questions to clarify homework problems but do not

ask if what you answered is correct. We may not reply from Friday evening to Monday morning.

Problem 1 Proof-of-Work Algorithm and Hash Puzzle

As we have discussed in our lectures, Bitcoin achieves proof‐of‐work using hash puzzles. In

order to create a block, the node that proposes hat block is required to find a number, or

nonce , such that when you concatenate the nonce, the previous hash, and the list of

transactions that comprise that block and take the hash of this whole string, then that hash

output should be a number that falls into a target space that is quite small in relation to the

much larger output space of that hash function.

In this assignment, we will concentrate on a simple implementation of proof-of-work

algorithm and its improvement by addressing following questions.

a. Hash a string containing your CWID and full name using SHA-256 and show its hash

value containing only hexadecimal digits using the hexdigest() method included in the

module “hashlib”. Provide the source code, hash value, and the captured screenshot in

your submitted document.

Hashlib module: https://docs.python.org/3/library/hashlib.html

b. Add a nonce (number) to the end of the string created in 1(a) and calculate the hash of

the input (string+nonce) by iterating the nonce (number) from 0 to 30. Provide the

source code and captured screenshot of program outputs in your submitted document.

c. Based on the implementation of a simplified proof-of-work algorithm is included in the

file” pow.py”, set the string created in 1(a) in your new block and provide the

statement and captured screenshot in your submitted document.

d. Based on the changes made in 1(c), test the program. Provide the source code and

program output in your submitted document

e. Set the maximum difficulty (in bits) to 15 bits and maximum nonce to 2^{16}. Test the

program. Provide the source code and program output in your submitted document.

Place an order in 3 easy steps. Takes less than 5 mins.