Sale!

 CS419 Project 4: Proof-of-Work Generator/Validator solved

Original price was: $35.00.Current price is: $30.00. $25.50

Category:

Description

5/5 - (5 votes)

Introduction Proof-of-work was created as a mechanism to demonstrate that the creator of the proof-of-work had to put in a substantial amount of computing effort while making it efficient for a recipient to validate this. In Bitcoin, proof of work serves two functions: 1. It makes creating a block with a valid block hash so difficult that it is extremely unlikely that two bitcoin nodes would propose a block at the same time. 2. It takes so much computation to compute the proof of work to create a valid hash for one block that the amount of computing power an attacker would need to modify a sequence of hash pointers for many blocks to change an old transaction is not feasible. This assignment contains two parts: 1. In part 1, you will implement a program that computes a proof of work for a file. You will specify a difficulty level on the command line and the program prints a set of headers as output. 2. In part 2, you will implement a program that validates the headers produced in part 1. If the proof of work is incorrect or the hash is incorrect then the validation will fail. Environment  4/14/22, 4:31 PM Project 4 https://rutgers.instructure.com/courses/160949/assignments/1888148 2/12 This is an individual project. All work should be your own except for the referenced code for the hash function. You may use Go, Python, Java, C, or C++ in your implementation. You should be able to implement this on any platform but you are responsible to make sure that the program runs on Rutgers iLab systems with no extra software. Background Hashcash was an idea to reduce the amount of email spam. It would do this by requiring the sender to solve a difficult puzzle before sending a message. The sender would then provide proof that this puzzle was solved and messages would be rejected if such a proof was missing or in valid. The proof, called a stamp, was provided as a header in the mail message. Someone using hashcash might spend several seconds creating the stamp. That’s not disruptive for someone who is sending a few messages but is essentially impossible for a spammer who needs to generate stamps for millions of messages. Because the puzzle uses the content of the mail message and the recipient’s address, the spammer would need to spend many years of computing time to send a million messages as the stamp would need to be computed for each recipient and is unique to each message. The solution to the puzzle needs to be verified efficiently by receivers, so we need a puzzle that’s difficult to solve but easy to verify. The idea of hashcash was adopted by Bitcoin and many other cryptocurrencies to enable participating systems provide evidence of Proof of Work when adding a new block to the bitcoin blockchain. The puzzle Hashcash had to create a puzzle that was a function of the message, was sufficiently difficult to compute, but was efficient to verify. This aligns closely with properties of one-way functions. Cryptographic hashes are one-way functions that provide fixed-length output. For example, here’s a SHA-256 (256-bit SHA-2) hash of the text ‘The grass is green’: f3ccca8f3852f5e2932d75db5675d59de30d9fa10530dd9855bd4a6cd0661d8e It takes only a few milliseconds to compute this. The inverse function, finding some text that would produce this hash, requires a brute-force search. You would need to try hashing a huge amount of  4/14/22, 4:31 PM Project 4 https://rutgers.instructure.com/courses/160949/assignments/1888148 3/12 different messages to find one the produces the same hash. For example, Google attacked the SHA-1 hash but only by trying over 9×10 hashes. The attack required 12,000,000 GPU years of computing power. A stronger hash function, such as SHA-2, would take far longer. Clearly, reversing a hash is far too difficult a problem to use as a puzzle. An easier puzzle We can make the puzzle easier. Suppose we come up with some text, W, that we append to the message M. When we hash the concatenated message, hash(M||W), the resulting hash value will have a certain property. For example, this was our SHA-256 hash of ‘The grass is green’: f3ccca8f3852f5e2932d75db5675d59de30d9fa10530dd9855bd4a6cd0661d8e If we look at the first bytes in binary, we see the bits are 1111 0011 1100 … . Suppose we want to add some text to the message so that the resulting SHA-256 hash will be a value whose first 6 bits are 0. There is no way to predict how to create a message that will result in such a hash. The only thing we can do is try different combinations, suffixing the message with different values of W. This particular challenge turns out not to relatively easy and we can solve it in a few milliseconds. For example, if we add the letters ‘ hB ’ to the text, we get this hash: 0243cdcf382f41bd88c65147ecf0328fc0258b0a49417c568891962e631d4c2d The binary value starts with 6 zeros: 0000 0010 0100 0011 … Adaptive complexity We can adjust the difficulty of the puzzle by altering the number of leading 0 bits that we need to find in the resulting hash. This difficulty will be only an average. Sometimes we might get lucky and find a suffix that produces the desired result quickly. Other times it will take longer. Here’s a table of some of the suffixes I came up with for the text ‘The grass is green’ for different difficulty levels, where d is the number of leading 0 bits in the resulting hash when the string W is appended to the text. Difficulty, d Suffix, W Iterations Time (s) 9 T 19 0.000023 18  4/14/22, 4:31 PM Project 4 https://rutgers.instructure.com/courses/160949/assignments/1888148 4/12 Difficulty, d Suffix, W Iterations Time (s) 19 @J9B 597,172 0.2138 23 /8hV 6,759,807 2.4135 24 5#PgD 74,279,789 26.586 27 uwT2G 145,358,750 51.930 32 6j8wCC 2,966,002,502 1,065.6 34 fbI3xN 19,966,156,427 7,220.0 36 l2iP#X 34,835,138,909 12,532 In this example, I found a string suffix that results in a hash with 19 leading 0 bits by testing around 600,000 variations of suffixes in less than a quarter of a second. However, finding a suffix that results in a hash value with 23 leading 0 bits took testing almost almost seven million suffixes and took almost 2.5 seconds. The puzzle gets difficult quickly. Finding text that would produce a hash with 32 zero bits required testing almost three billion different suffixes and took a bit over 17 minutes. To get 34 leading zeros, I had to test close to 20 billion suffixes and that took two hours on my M1 Mac Mini. Producing a hash with 36 leading zeros required testing over 34 billion suffixes and about 3.5 hours of time. Note that I was using nonoptimized code and hash computations can be performed a lot faster within a GPU (graphics processing unit) but the difference in complexity remains the same. Size-invariant difficulty The time to compute a hash is longer for large messages; we need to iterate over more bytes to compute the hash. To make the difficulty not be a function of the message length, we can modify the puzzle to one where we append suffixes to a hash of the message and then hash the resulting message to see what we get. The average difficulty of the problem now is simply the value d, which defines the number of leading 0 bits in the hash result. The actual time it takes to find the right suffix W will depend on: Luck: we might get lucky and test some strings early on that will provide the hash result we want. We may get lucky occasionally but not most of the time. The law of averages will play out. The speed of your computer and the quality of your code. We can speed up hashes greatly by using GPUs or custom processors as people do with bitcoin mining, but we won’t do that in this assignment.  4/14/22, 4:31 PM Project 4 https://rutgers.instructure.com/courses/160949/assignments/1888148 5/12 Mostly, the difficulty depends on the value of d, the number of leading zero bits we want in the resulting hash. Searching for a hash that produces a greater number of Proof of Work The suffix that was found — the string W — that, when appended to the message, produces the desired hash output, is called the Proof of Work. It is a short string that we can provide to prove that we did the work to find this value that produces the desired hash. The proof of work is very efficient to test. For instance, it took my program over 19 billion hashes to find that we can append the string fbI3xN to the text The grass is green so that the first 24 bits in the resulting hash will all be 0. This string is the proof of work. You can verify the proof of work by computing one hash and looking at the resulting value. It will take you only a millisecond. Your assignment: part 1 Your assignment is to write a program called pow-create that creates a proof of work string for a given file: pow-create nbits file The work is the search for a string that, when suffixed to a hash of the given file (file), will result in a SHA-256 hash (256-bit version of the SHA-2 hash) that contains a certain number (nbits) of leading 0s. For example, suppose we have the following text in a file called walrus.txt: The time has come, the Walrus said, To talk of many things: Of shoes — and ships — and sealing-wax — Of cabbages — and kings — And why the sea is boiling hot — And whether pigs have wings. We can use the openssl command on a macOS or Linux system to find its SHA-256 hash: $ openssl sha256 < walrus.txt 66efa274991ef4ab1ed1b89c06c2c8270bb73ffdc28a9002a334ec3023039945 The hash starts with 66ef . The hex digit 6 is 0101 in binary, so we currently have one leading zero bit in this message.  4/14/22, 4:31 PM Project 4 https://rutgers.instructure.com/courses/160949/assignments/1888148 6/12 To create a proof of work string with a difficulty of 20 (at least 20 leading zero bits), we run the command: $ pow-create 20 walrus.txt File: walrus.txt Initial-hash: 66efa274991ef4ab1ed1b89c06c2c8270bb73ffdc28a9002a334ec3023039945 Proof-of-work: rftE Hash: 000005ca35310f45d7ef2b28753a74cf410734b4a5930247d15128d23e419ca0 Leading-zero-bits: 21 Iterations: 1467959 Compute-time: 1.0712 Your pow-create command will: 1. Create a SHA-256 hash of the specified file. 2. Convert it to a printable hex string (matching the string produced by the openssl command). 3. Pick a string that’s a potential proof of work value. 4. Create a hash of the string representation of the hash in (2) concatenated with the potential proof of work string in (3). 5. If the hash doesn’t start with at least nbits zero bits (the number supplied by the first argument of the command line) then go back to step (3) and try a different suffix. For this example, it took almost 1.5 million hashes with different variations of suffixes to find a hash that starts with 20 zero bits (in this particular case, the first hash we found that started with at least 20 leading 0 bits actually contained 21). The string that we came up with is rftE . This is presented as the proof of work in the Proof-of-work header. Output Your program will print results to the standard output (stdout) in a standard header format as used by mail headers or HTTP. This is one name-value item per line with each line containing the header name, a colon, one or more spaces, and the value. A header item shall not span multiple lines and the output will not contain blank lines. The headers your program must produce are: File: The name of the file. Initial-hash: The SHA-256 hash of the file (as a printable hex value) Proof-of-work: The printable string that is your proof of work. Hash: The SHA-256 hash of the original string concatenated with the proof of work.  4/14/22, 4:31 PM Project 4 https://rutgers.instructure.com/courses/160949/assignments/1888148 7/12 Leading-zero-bits: The actual number of leading 0 bits in the hash you computed. This value should be greater than or equal to the number requested. Iterations The number of different proof-of-work values you had to try before you found one that works. Compute time: How long this process took, in seconds (including decimal seconds if appropriate). Testing your proof of work You can easily test your program’s result against the data produced by the openssl command Run your pow-create command: $ ./pow-create 20 walrus.txt File: testfiles/walrus.txt File: testfiles/walrus.txt Initial-hash: 66efa274991ef4ab1ed1b89c06c2c8270bb73ffdc28a9002a334ec3023039945 Proof-of-work: rftE Hash: 000005ca35310f45d7ef2b28753a74cf410734b4a5930247d15128d23e419ca0 Leading-zero-bits: 21 Iterations: 1467959 Compute-time: 1.0712 The only header we care about here is the Proof-of-work value, rftE . We can check our hash with the one openssl produces by running the openssl command with the sha256 argument. This value should match the Initial-hash header: $ openssl sha256 sampleheader ./pow-check sampleheader samplefile As with the previous assignment, if you are using python, you can submit either: A. pow-create and pow-check scripts that run the program or B. (preferably) programs named pow-create and pow-check that that start with a #! line followed by your code. For example: #!/usr/bin/python3 print(‘Hello, world!’) … If you are using java, you will have a makefile that compiles the class files and the pow-create and powcheck programs will be scripts that runs the appropriate java command with the arguments. The file powcreate will contain something like this: #!/bin/bash CLASSPATH=. java PowCreate “$@” Testing Test your scripts on an iLab machine to make sure they work prior to submitting. Test that your programs gracefully handle any invalid input thrown at them. The programs should never crash or produce cryptic-looking stack traces regardless of what input you throw at it. Make sure that they you can validate the sample files, even if your pow-create program generates different proof-ofwork values, as it almost certainly will. Make various modifications to the sample files to ensure that you detect errors correctly. 