Skip to content

Commit

Permalink
sighash and sig_hasher removal
Browse files Browse the repository at this point in the history
  • Loading branch information
blurpesec committed May 9, 2018
1 parent 64c4a82 commit f6e4eb9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
8 changes: 4 additions & 4 deletions VALIDATOR_GUIDE.md
Expand Up @@ -84,8 +84,8 @@ Next, RLP encode all these elements. To compute your signature, compute the `sha
This is [implemented in Pyethereum](https://github.com/karlfloersch/pyethereum/blob/a66ab671e0bb19327bb8cd11d69664146451c250/ethereum/hybrid_casper/casper_utils.py#L71-L75) as follows:
```
def mk_vote(validator_index, target_hash, target_epoch, source_epoch, key):
sighash = utils.sha3(rlp.encode([validator_index, target_hash, target_epoch, source_epoch]))
v, r, s = utils.ecdsa_raw_sign(sighash, key)
msg_hash = utils.sha3(rlp.encode([validator_index, target_hash, target_epoch, source_epoch]))
v, r, s = utils.ecdsa_raw_sign(msg_hash, key)
sig = utils.encode_int32(v) + utils.encode_int32(r) + utils.encode_int32(s)
return rlp.encode([validator_index, target_hash, target_epoch, source_epoch, sig])
```
Expand All @@ -96,8 +96,8 @@ Like the Casper vote messages, a logout message is an RLP encoded list where the
This is [implemented in Pyethereum](https://github.com/karlfloersch/pyethereum/blob/a66ab671e0bb19327bb8cd11d69664146451c250/ethereum/hybrid_casper/casper_utils.py#L77-L81) as follows:
```
def mk_logout(validator_index, epoch, key):
sighash = utils.sha3(rlp.encode([validator_index, epoch]))
v, r, s = utils.ecdsa_raw_sign(sighash, key)
msg_hash = utils.sha3(rlp.encode([validator_index, epoch]))
v, r, s = utils.ecdsa_raw_sign(msg_hash, key)
sig = utils.encode_int32(v) + utils.encode_int32(r) + utils.encode_int32(s)
return rlp.encode([validator_index, epoch, sig])
```
Expand Down
File renamed without changes.
22 changes: 11 additions & 11 deletions casper/contracts/simple_casper.v.py
Expand Up @@ -354,8 +354,8 @@ def delete_validator(validator_index: int128):
# cannot be labeled @constant because of external call
# even though the call is to a pure contract call
@private
def validate_signature(sighash: bytes32, sig: bytes <= 1024, validator_index: int128) -> bool:
return extract32(raw_call(self.validators[validator_index].addr, concat(sighash, sig), gas=self.VALIDATION_GAS_LIMIT, outsize=32), 0) == convert(1, 'bytes32')
def validate_signature(msg_hash: bytes32, sig: bytes <= 1024, validator_index: int128) -> bool:
return extract32(raw_call(self.validators[validator_index].addr, concat(msg_hash, sig), gas=self.VALIDATION_GAS_LIMIT, outsize=32), 0) == convert(1, 'bytes32')


# ***** Public *****
Expand Down Expand Up @@ -427,14 +427,14 @@ def logout(logout_msg: bytes <= 1024):

# Get hash for signature, and implicitly assert that it is an RLP list
# consisting solely of RLP elements
sighash: bytes32 = extract32(raw_call(self.MSG_HASHER, logout_msg, gas=self.MSG_HASHER_GAS_LIMIT, outsize=32), 0)
msg_hash: bytes32 = extract32(raw_call(self.MSG_HASHER, logout_msg, gas=self.MSG_HASHER_GAS_LIMIT, outsize=32), 0)
values = RLPList(logout_msg, [int128, int128, bytes])
validator_index: int128 = values[0]
epoch: int128 = values[1]
sig: bytes <= 1024 = values[2]

assert self.current_epoch >= epoch
assert self.validate_signature(sighash, sig, validator_index)
assert self.validate_signature(msg_hash, sig, validator_index)

# Check that we haven't already withdrawn
end_dynasty: int128 = self.dynasty + self.DYNASTY_LOGOUT_DELAY
Expand Down Expand Up @@ -469,7 +469,7 @@ def withdraw(validator_index: int128):
def vote(vote_msg: bytes <= 1024):
# Get hash for signature, and implicitly assert that it is an RLP list
# consisting solely of RLP elements
sighash: bytes32 = extract32(raw_call(self.MSG_HASHER, vote_msg, gas=self.MSG_HASHER_GAS_LIMIT, outsize=32), 0)
msg_hash: bytes32 = extract32(raw_call(self.MSG_HASHER, vote_msg, gas=self.MSG_HASHER_GAS_LIMIT, outsize=32), 0)
# Extract parameters
values = RLPList(vote_msg, [int128, bytes32, int128, int128, bytes])
validator_index: int128 = values[0]
Expand All @@ -478,7 +478,7 @@ def vote(vote_msg: bytes <= 1024):
source_epoch: int128 = values[3]
sig: bytes <= 1024 = values[4]

assert self.validate_signature(sighash, sig, validator_index)
assert self.validate_signature(msg_hash, sig, validator_index)
# Check that this vote has not yet been made
assert not bitwise_and(self.checkpoints[target_epoch].vote_bitmap[floor(validator_index / 256)],
shift(convert(1, 'uint256'), validator_index % 256))
Expand Down Expand Up @@ -546,29 +546,29 @@ def vote(vote_msg: bytes <= 1024):
@public
def slash(vote_msg_1: bytes <= 1024, vote_msg_2: bytes <= 1024):
# Message 1: Extract parameters
sighash_1: bytes32 = extract32(raw_call(self.MSG_HASHER, vote_msg_1, gas=self.MSG_HASHER_GAS_LIMIT, outsize=32), 0)
msg_hash_1: bytes32 = extract32(raw_call(self.MSG_HASHER, vote_msg_1, gas=self.MSG_HASHER_GAS_LIMIT, outsize=32), 0)
values_1 = RLPList(vote_msg_1, [int128, bytes32, int128, int128, bytes])
validator_index_1: int128 = values_1[0]
target_epoch_1: int128 = values_1[2]
source_epoch_1: int128 = values_1[3]
sig_1: bytes <= 1024 = values_1[4]

assert self.validate_signature(sighash_1, sig_1, validator_index_1)
assert self.validate_signature(msg_hash_1, sig_1, validator_index_1)

# Message 2: Extract parameters
sighash_2: bytes32 = extract32(raw_call(self.MSG_HASHER, vote_msg_2, gas=self.MSG_HASHER_GAS_LIMIT, outsize=32), 0)
msg_hash_2: bytes32 = extract32(raw_call(self.MSG_HASHER, vote_msg_2, gas=self.MSG_HASHER_GAS_LIMIT, outsize=32), 0)
values_2 = RLPList(vote_msg_2, [int128, bytes32, int128, int128, bytes])
validator_index_2: int128 = values_2[0]
target_epoch_2: int128 = values_2[2]
source_epoch_2: int128 = values_2[3]
sig_2: bytes <= 1024 = values_2[4]

assert self.validate_signature(sighash_2, sig_2, validator_index_2)
assert self.validate_signature(msg_hash_2, sig_2, validator_index_2)

# Check the messages are from the same validator
assert validator_index_1 == validator_index_2
# Check the messages are not the same
assert sighash_1 != sighash_2
assert msg_hash_1 != msg_hash_2

# Detect slashing
slashing_condition_detected: bool = False
Expand Down
12 changes: 6 additions & 6 deletions misc/validation_codes/fixed_address_creator.py
Expand Up @@ -5,7 +5,7 @@
from ethereum import tester
from ethereum import transactions

sighash = serpent.compile('sighash.se.py')
msg_hash = serpent.compile('msg_hash.se.py')

tests = [
[b"\x01"],
Expand All @@ -21,20 +21,20 @@
]

s = tester.state()
c = s.evm(sighash, sender=tester.k0, endowment=0)
c = s.evm(msg_hash, sender=tester.k0, endowment=0)

for test in tests:
z = s.send(tester.k0, c, 0, rlp.encode(test))
z = s.send(tester.k0, c, 0, rlp.encode(test))
assert z == utils.sha3(rlp.encode(test[:-1]))
print("Passed test, gas consumed: ", s.state.receipts[-1].gas_used - s.state.receipts[-2].gas_used - s.last_tx.intrinsic_gas_used)

# Create transaction
t = transactions.Transaction(0, 30 * 10**9, 2999999, '', 0, sighash)
t.startgas = t.intrinsic_gas_used + 50000 + 200 * len(sighash)
t = transactions.Transaction(0, 30 * 10**9, 2999999, '', 0, msg_hash)
t.startgas = t.intrinsic_gas_used + 50000 + 200 * len(msg_hash)
t.v = 27
t.r = 45
t.s = 79
print("Sighash")
print("Message Hash")
print('Send %d wei to %s' % (t.startgas * t.gasprice,
'0x'+utils.encode_hex(t.sender)))

Expand Down
8 changes: 4 additions & 4 deletions tests/conftest.py
Expand Up @@ -324,10 +324,10 @@ def mk_validation_code(address, valcode_type):
@pytest.fixture
def mk_vote():
def mk_vote(validator_index, target_hash, target_epoch, source_epoch, privkey):
sighash = utils.sha3(
msg_hash = utils.sha3(
rlp.encode([validator_index, target_hash, target_epoch, source_epoch])
)
v, r, s = utils.ecdsa_raw_sign(sighash, privkey)
v, r, s = utils.ecdsa_raw_sign(msg_hash, privkey)
sig = utils.encode_int32(v) + utils.encode_int32(r) + utils.encode_int32(s)
return rlp.encode([validator_index, target_hash, target_epoch, source_epoch, sig])
return mk_vote
Expand Down Expand Up @@ -367,8 +367,8 @@ def mk_slash_votes(validator_index, privkey):
@pytest.fixture
def mk_logout():
def mk_logout(validator_index, epoch, key):
sighash = utils.sha3(rlp.encode([validator_index, epoch]))
v, r, s = utils.ecdsa_raw_sign(sighash, key)
msg_hash = utils.sha3(rlp.encode([validator_index, epoch]))
v, r, s = utils.ecdsa_raw_sign(msg_hash, key)
sig = utils.encode_int32(v) + utils.encode_int32(r) + utils.encode_int32(s)
return rlp.encode([validator_index, epoch, sig])
return mk_logout
Expand Down
6 changes: 3 additions & 3 deletions tests/test_chain_initialization.py
Expand Up @@ -21,18 +21,18 @@ def test_rlp_decoding_is_pure(
assert utils.big_endian_to_int(purity_return_val) == 1


def test_sig_hasher_is_pure(
def test_msg_hasher_is_pure(
casper_chain,
base_sender_privkey,
sig_hasher_address,
msg_hasher_address,
purity_checker_address,
purity_checker_ct
):
purity_return_val = casper_chain.tx(
base_sender_privkey,
purity_checker_address,
0,
purity_checker_ct.encode('submit', [sig_hasher_address])
purity_checker_ct.encode('submit', [msg_hasher_address])
)
assert utils.big_endian_to_int(purity_return_val) == 1

Expand Down

0 comments on commit f6e4eb9

Please sign in to comment.