0

My toy program contains the following lines:

(seq
  (def 'hash 0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad)
  (def 'result 0x0aef6cc8d178ff0f7950a7d02a6f9061ede19bc359fb1f777e7f20a5c729bf77)
  (lit 0 "\x19Ethereum Signed Message:\n32")
  (mstore 28 hash)
  (if (eq (sha3 0 60) result) (stop) (invalid)))

hash is output from web3.sha3("hello world")

result is output from web3.sha3(web3.toHex("\x19Ethereum Signed Message:\n32")+"47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad")

Why does this program evaluate to invalid? If I remove the signed message prefix and just compare hashes of hello world then it works.

0

A couple of issues here.

(1) LLL doesn't understand the string syntax. It interprets "\x19" as a literal 4 bytes rather than the single byte 0x19. Same for "\n" which is intended to be 0x0a. I don't know a good way around this other than writing the bytes directly using mstore8.

(2) You need the {encoding: 'hex'} option to web3.sha3 when calculating result.

> web3.sha3(web3.toHex("\x19Ethereum Signed Message:\n32")+"47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad", {encoding: 'hex'})
"0x93100cc9477ba6522a2d7d5e83d0e075b167224ed8aa0c5860cfd47fa9f22797"    

The following is clunky but it seems to work.

(seq
  (def 'hash 0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad)
  (def 'result 0x93100cc9477ba6522a2d7d5e83d0e075b167224ed8aa0c5860cfd47fa9f22797)
  (mstore8 0 0x19)  ; the "\0x19"
  (lit 1 "Ethereum Signed Message:")
  (mstore8 25 0x0a) ; the "\n"
  (lit 26 "32")
  (mstore 28 hash)
  (if (eq (sha3 0 60) result) (stop) (invalid)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.