CE235编程代写、代做python程序设计
Assignment 2: Blockchain and Mining with Proof-of-work for Bitcoin
CE235 Computer Security
2024-2025
University of Essex
1. Introduction

1.1 Bitcoin Mining
Bitcoin is a cryptocurrency. In the Bitcoin system Bitcoins are mined through proof-of-work mechanism.
Bitcoin miners are given technical puzzles to solve. There is only one puzzle at any time with a given difficulty
level, which is set by the system administrator. New puzzles are created after the current one is solved.
The first miner who solves the puzzle is awarded a specified number of bitcoins. The winner creates and sign a
new block with digital signature technology and broadcast to other Bitcoin users. The signed block is linked to
the previous signed blocks. These blocks form a chain of blocks (called blockchain) as shown in the following
figure. The new signed blocks are verified by others and could become mature after being confirmed by a given
number of miners, which is measured by length of blocks linked to the new blocks.


1.2 Technical puzzle
The puzzle set in the proof-of-work is to find a specific integer number (called nonce), which together with a
few other numbers (such as hash value of the previous block, the transactions to be included to the new block)
are hashed with SHA-256 algorithm and the hashed value satisfies a given condition.
The puzzle can be formulated as follows:

where preHash is the hash value of the previous block, Tx is transaction of bitcoins. levelHard is a given number,
usually controlled by requiring a consecutive number of most significant bits (MSB) being zeros, for example
the first 30 MSBs being zero. The more MSB zeros required on levelHard, the more difficult to solve the puzzle
(finding the nonce satisfying the condition). Below gives a binary number with the 15 MSB being zeros and 5
least significant bits (LSB).
(MSB) 00000000000000011100000101111110011010101100000 (LSB)

1.3 Signing and verifying a new block
The first miner solving the puzzle will create a new block, which includes a block header (storing the digital
signature of this new block, which will include the hash value of the block body) and a block body. The block
body includes the hash value of the previous block, the found nonce and transactions included in this block. The
digital signature is created by encrypting the hash value of this new block with private key. The block is linked
to the last block of the existing blockchain and broadcast. The new block will then be verified by others using
the winning miner’s public key and checking the hash values of this and previous blocks.

2. Specification
Block header (signature)
Prev Hash
Nonce
Tx1
Tx2
TxN
Block header (signature)
Prev Hash
Nonce
Tx1
Tx2
TxN
Block header (signature)
Prev Hash
Nonce
Tx1
Tx2
TxN
Block n-2 Block n-1 Block n
find nonce, subject to: hash(preHash, nonce, Tx) < levelHard This assignment takes 18% of the marks (18 marks) of this module. The aim of the assignment is to write a
Python program, which will implement a simplified version of Bitcoin mining and digital signature schemes as
shown in the following figure, with additional task of protecting the confidentiality of the signed message (from
by encrypting/decrypting the signed message (such as with an extra RSA key pair for the validator).

2.1 Task1: Create a RSA public/private key pair with 1024 bits key length [1 mark]
o The RSA key pairs will be used in Task3 and Task4 of this assignment.
o The created RSA public {n,e} and private keys {n,d} need to be displayed with the following
format:

2.2 Task2: Find a nonce with hash algorithm SHA-256, which is a hash value satisfying requirement
of the 6 least significant bits (LSB) being zero [4 marks]. Produce a figure (or a table) which shows
the computation time (denoted by T) used to find a valid nonce by your own computer against the
number of required LSB being zero (denoted by N) changing from 1 to 8 [3 marks].
o Hint: you can extend Example 4 in the provided sample program to complete this task. Example 4
generates only one nonce and check if the nonce is valid.
o You should try many random integers as nonce (with a loop) until you successfully find a nonce
that meets the requirement. The only output from this task is the nonce, which needs to be displayed
with the following format (suppose the found nonce is 12345):


o You can use your program to produce the figure/table automatically, or you can record the
computation times and create the figure/table using other software, then present it to the teaching
staff members during your demonstration. Not to submit the figure/table to Faser.

2.3 Task3: Digitally sign the nonce and your student number with the RSA private key [3 marks]
o The message to be signed is a string consisting of the nonce (found with 6 LSB being zero) and
your student number, which are separated by a space. For example, if the found nonce is 12345 and
your student number is 54321, then the message to be signed needs to be a string “12345 54321”
o You need to sign the message with RSA key pair generated in Task 1.
o The outputs of this Task3 include the hashed value of the message and the signature, which need to
be displayed with the following format.


Find a valid
nonce
Sign message
(nonce, student #)
Verify the digital
signature
Generate RSA
keys
Measure
computation time
Required # of
LSB zeros
Valid
nonce
Encrypted message
& signature
Validation
outcome
Table or figure Keys
Keys
Public key:
(n=0x995361030caa5bf308e272fe07f3466c0727b5ac0c41107142fd97dd75ec4a197250c038
8b8711b210b2beb300980321913e9eb21b22f72c3fe8b62adda13491c6efbf3f4e6c6c60738da
c790af2ca0b8067f4550fae82c8ea85d3fc0667f1de7a193f23a1d30e8e7f2894f07ce26b5d94
85df5a29fc265fc217dbbb91065b35, e=0x10001)
Private key:
(n=0x995361030caa5bf308e272fe07f3466c0727b5ac0c41107142fd97dd75ec4a197250c038
8b8711b210b2beb300980321913e9eb21b22f72c3fe8b62adda13491c6efbf3f4e6c6c60738da
c790af2ca0b8067f4550fae82c8ea85d3fc0667f1de7a193f23a1d30e8e7f2894f07ce26b5d94
85df5a29fc265fc217dbbb91065b35,
d=0x24cf1913a7d74042dce7ac6ea30efae19568299bb7c769009ff20ca2ec9c010011eb23f28
f40aa7562bfdebb4f91aef2c091557cf1b9d7b82651a2663115f1ee0c416b1fec516a83657558
068f1eebffae9f11b2801830acf2b0af4367fcd26ffe4672c5c5165afaeb5eeb81e6497a04192
133476e124b4ce2a869a16fc998e1)

Valid Nonce: 12345

2.4 Task4: Verify the signature [3 marks]
o The signature verification is to be achieved by decrypting the digital signature with public key
{n,e} generated in Task 1 to get the hash value from the signature and compare it to the one
obtained from hashing the signed message.
o The process of signature verification needs to output yes or no depending on the verification
outcome.

2.5 Task5: Protect the confidentiality of the signed message from Task 3 by encrypting/decrypting
the signed message [4 marks]
o You should generate another RSA key pair for the validator.
o The signed message should be encrypted with a key of the validator by the user who signs the
message.
o The encrypted signed message should be decrypted with another key of the validator before the
signature validation by the validator.

3. Sample Program
We provide a sample python program miningBitcoin_sample.py, which includes most of the needed
RSA encryption and digital signature functions to complete the above tasks. It can be run from integrated
development environments (IDLE). It can also be run from the command line like this:
python mingingBitcoin_sample.py

You should modify the sample python program to complete the tasks. Your own program should have a name
like cs_bitcoin_registrationnumber.py (replace registrationnumber by your own registration number). For
example, if your registration number is 1234567, your filename will be:
cs_bitcoin_1234567.py
Your program must run from the command line like this:
python cs_bitcoin_1234567.py
The outputs of your program are required to be displayed, following the specified format for marking purposes.

4. How to submit
Submit your python .py file to Faser by the submission deadline Friday, 13/12/2024.

5. Marking Scheme
You will be asked by the Professor He or teaching assistants at NWU to demonstrate your work and answer questions
to ensure it is your own work. Your marks for this assignment will be dependent on the complement and output
results of your program, and your answers to the questions asked by the teachers. If you are asked to but you don’t
demonstrate your work, no mark will be given to your assignment work.
Apart from demonstration of your work to the teaching staff members, it is mandatory for you to submit your program
file to Faser on time. Otherwise, you may not get any mark for your work on the assignment.
Your submitted program may be checked and tested by Professor He. If problems such as plagiarism are found from
the testing, your marks will be reduced.

Plagiarism
You should work individually on this project. Anything you submit is assumed to be entirely your own work.
The usual Essex policy on plagiarism applies: http://www.essex.ac.uk/plagiarism/.
Message: 12345 54321
Hash value of message:
32547436749427615422843012801191259465058592439985110545719463144077305232244
Signature:
0x3ee3934a23e2d55c7377a125e052e5f305d82fbf0643713acb00cf1cb0c968eb65f56de35d0
37aac5d0d3ae7489d4067e9c38ceee2f4f602ebf90d8a070606c27808f22bd537a42e066c86c3
6d5e5efa786be09e0753f82a847a05d0bdcd0418624a3f3c8a203524f97f56528ffca1e633d29
bd8cfa80fb80bc3b4a53e2d51b5 Appendix A. Sample Python Program for Bitcoin Mining
### First first time use, you need to install module pycryptodome
### using the following commands (without #) in terminal:
# pip uninstall crypto
# pip install pycryptodome

# import necessary modules
from Crypto.PublicKey import RSA
from hashlib import sha512, sha256
import random


#The following function generateRSAKeys creates the RSA public/private key pair.
def generateRSAKeys(numBits):
keyPair = RSA.generate(numBits)

return keyPair

### Example 1: generating RSA public/private keys.
numBits = 1024
keyPair = generateRSAKeys(numBits)
print("Public key: n={", hex(keyPair.n), "}, e={", hex(keyPair.e), "})")
print(' ')
print("Private key: n={", hex(keyPair.n), "}, d={", hex(keyPair.d), "})")
print(' ')


#The following function digitalSignRSA digitally sign a message of type "bytes"
# with RSA private key of keyPair
#- input: msg and keyPairRSA
#- output: signature (type: int)

def digitalSignRSA(msg, keyPairRSA):
### RSA sign the message
# msg = bytes('A message for signing', 'utf-8')

# compute the hash value of the message to be signed with hash algorithm SHA-256
hashValue = int.from_bytes(sha256(msg).digest(), byteorder='big')

#sign (encrypt) the hash value of the message with RSA priviate key
# only the person with RSA private key can perform the operation
signature = pow(hashValue, keyPair.d, keyPair.n)

return(hashValue, signature)

### Example 2: calling function digitalSignRSA to sign a given message
msg = bytes('A message for signing', 'utf-8')
(hashValue, signature) = digitalSignRSA(msg, keyPair)
print("Hash value of message:", hashValue)
print("Signature:", hex(signature))
print(' ')


#The following function digitalVerifyRSA verify the signature of a message of type "bytes"
# with RSA public key of keyPair
#- input: msg, keyPairRSA and signature #- output: validity (type: boolean)
def digitalVerifyRSA(msg, keyPairRSA, signature):
### Verify the digital signature

# compute the hash value of the message to be signed with hash algorithm SHA-256
hashValue = int.from_bytes(sha256(msg).digest(), byteorder='big')

# decrypt the signature of the message to obtain the hash value with RSA public key
# everyone with RSA public key can perform the operation
hashFromSignature = pow(signature, keyPair.e, keyPair.n)

validity = (hashValue == hashFromSignature)
return(validity)

### Example 3: verifying a message signature using function digitalVerifyRSA
# Verify the true digital signature with the original message
msg = bytes('A message for signing', 'utf-8')
validity = digitalVerifyRSA(msg, keyPair, signature)
print("Signature validity:", validity)
print(' ')

# Verify the true digital signature with a tampered message
msgTampered = bytes('A message for signing (tampered)', 'utf-8')
validity = digitalVerifyRSA(msgTampered, keyPair, signature)
print("Signature validity:", validity)
print(' ')

#The following function checkOneNonce will check if one nonce can solve the puzzle
#This function can be used to implement the proof-of-work
#- input: numZerosNeeded (type: int), required number of least significant bits to be zero.
# nonce (type: int), a random number to be checked
#- output: validity (type: boolean), true or false on the validity of this nonce.
def checkOneNonce(numZerosNeeded, nonce):

# Note that hash function sha256 accepts input of type byte.
# so we convert random number nonce to a byte array nByte
#convert the random integer to a byte array
nByte = bytes(str(nonce), 'utf-8')

# compute the hash of the nonce
hash = int.from_bytes(sha256(nByte).digest(), byteorder='big')

# convert the hash value to binary number and extract the needed LSBs.
hashBin = bin(hash)
hashLSB = int(hashBin[-numZerosNeeded:])

# check if the LSBs are all zero
if hashLSB == 0:
print('nRand:', nonce, '; hash_lsb:', hashLSB)
validity = (hashLSB == 0)
return (validity)

# Example 4: generating a random number as a nonce, checking only one nonce with function checkOneNonce,
# the nonce may be valid or may be invalid.
numZerosNeeded = 5
# generate a nonce (a random integer in a specified range)
nonce = random.randint(0,1000000)

Example Output after running the sample program:


validity = checkOneNonce(numZerosNeeded, nonce)
print('The validity of this nonce ', nonce, ' is:', validity)
print('================================================================ ')


### Task 1: Create a RSA public/private key pair with 1024 bits key length [1%]

### Task 2 [7%]: Using hash algorithm SHA-256 to find a nonce, and produce a figure or a table showing the
computation time to find valid naunce against the number of required LSB being zero (1 to 8).

### Task3: Digitally sign the nonce and your student number with the RSA private key [3%]

### Task4: Digitally verify the signature with the RSA public key [3%]
# you should produce and display the output of either “yes” or “no” according to the verification result.

### Task5: Protect the confidentiality of the signed message [4%]
# you should produce and display the output of either “yes” or “no” according to the verification result.

热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图