async def test_attest_state_invalid_nonce(contract_factory): """Should fail with invalid nonce""" _, contract = contract_factory state_hash = 1234 nonce = 666 message_hash = pedersen_hash( nonce, pedersen_hash(some_vehicle, pedersen_hash(state_hash, 0))) sig_r, sig_s = sign(msg_hash=message_hash, priv_key=some_signer_secret) with pytest.raises(StarkException): await contract.attest_state( vehicle_id=some_vehicle, nonce=nonce, state_hash=state_hash, ).invoke(signature=[sig_r, sig_s])
async def test_set_signer_invalid_nonce(contract_factory): """Should fail to update the signer with a bad nonce""" _, contract = contract_factory nonce = 666 message_hash = pedersen_hash( nonce, pedersen_hash(some_vehicle, pedersen_hash(some_other_signer, 0))) sig_r, sig_s = sign(msg_hash=message_hash, priv_key=some_owner_secret) with pytest.raises(StarkException): await contract.set_signer( vehicle_id=some_vehicle, nonce=nonce, signer_public_key=some_other_signer, ).invoke(signature=[sig_r, sig_s])
async def test_set_signer_not_owner(contract_factory): """Should fail to update the signer if owner didn't sign the message""" _, contract = contract_factory nonce = await contract.get_nonce(vehicle_id=some_vehicle).call() nonce = nonce.result[0] message_hash = pedersen_hash( nonce, pedersen_hash(some_vehicle, pedersen_hash(some_other_signer, 0))) # Error here: signing with vehicle signer, not owner sig_r, sig_s = sign(msg_hash=message_hash, priv_key=some_signer_secret) with pytest.raises(StarkException): await contract.set_signer( vehicle_id=some_vehicle, nonce=nonce, signer_public_key=some_other_signer, ).invoke(signature=[sig_r, sig_s])
async def test_attest_state(contract_factory): """Should successfully attest to a state hash & increment nonce""" _, contract = contract_factory state_hash = 1234 nonce = 0 message_hash = pedersen_hash( nonce, pedersen_hash(some_vehicle, pedersen_hash(state_hash, 0))) sig_r, sig_s = sign(msg_hash=message_hash, priv_key=some_signer_secret) await contract.attest_state( vehicle_id=some_vehicle, nonce=nonce, state_hash=state_hash, ).invoke(signature=[sig_r, sig_s]) # Check the nonce was incremented new_nonce = await contract.get_nonce(vehicle_id=some_vehicle).call() assert new_nonce.result == (nonce + 1, )
async def test_set_signer(contract_factory): """Should successfully update the signer for the car""" _, contract = contract_factory nonce = await contract.get_nonce(vehicle_id=some_vehicle).call() nonce = nonce.result[0] message_hash = pedersen_hash( nonce, pedersen_hash(some_vehicle, pedersen_hash(some_other_signer, 0))) sig_r, sig_s = sign(msg_hash=message_hash, priv_key=some_owner_secret) await contract.set_signer( vehicle_id=some_vehicle, nonce=nonce, signer_public_key=some_other_signer, ).invoke(signature=[sig_r, sig_s]) # Check that the signer is updated new_signer = await contract.get_signer(vehicle_id=some_vehicle).call() assert new_signer.result == (some_other_signer, ) # ... and the nonce was incremented new_nonce = await contract.get_nonce(vehicle_id=some_vehicle).call() assert new_nonce.result == (nonce + 1, )
def compute_tx_hash(sender_a_id, a_amount, sender_b_id, b_amount): a_hash = pedersen_hash(int(sender_a_id), a_amount) b_hash = pedersen_hash(int(sender_b_id), b_amount) return pedersen_hash(a_hash, b_hash)
DIR = os.path.dirname(__file__) file_name = "../voting_input2.json" file_path = os.path.join(DIR, file_name) from starkware.crypto.signature.signature import pedersen_hash, sign POLL_ID = 10018 input_data = json.load(open('voting_input.json')) input_data['public_keys'][3] = '0x0' input_data['public_keys'][5] = '0x0' input_data['public_keys'][8] = '0x0' # Generate a "yes" vote for voter 6. voter_id = 6 priv_key = 123456 * voter_id + 654321 vote = 1 r, s = sign( msg_hash=pedersen_hash(POLL_ID, vote), priv_key=priv_key) input_data['votes'] = [{ 'voter_id': voter_id, 'vote': vote, 'r': hex(r), 's': hex(s), }] with open(file_name, 'w') as f: json.dump(input_data, f, indent=4) f.write('\n')
# Generate key pairs. priv_keys = [] pub_keys = [] for i in range(10): priv_key = 123456 * i + 654321 # See "Safety note" below. priv_keys.append(priv_key) pub_key = private_to_stark_key(priv_key) pub_keys.append(pub_key) # Generate 3 votes of voters 3, 5, and 8. votes = [] for (voter_id, vote) in [(3, 0), (5, 1), (8, 0)]: r, s = sign(msg_hash=pedersen_hash(POLL_ID, vote), priv_key=priv_keys[voter_id]) votes.append({ 'voter_id': voter_id, 'vote': vote, 'r': hex(r), 's': hex(s), }) # Write the data (public keys and votes) to a JSON file. input_data = { 'public_keys': list(map(hex, pub_keys)), 'votes': votes, } with open(file_path, 'w') as f: