Skip to content

Commit b65093a

Browse files
committed
Implemented EVM.ABI.encode_int*_parameter_as_uint256.
1 parent 35afd62 commit b65093a

File tree

12 files changed

+75
-10
lines changed

12 files changed

+75
-10
lines changed

lib/Clar2EVM/compile.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ and compile_expression env = function
240240
| FunctionCall ("get", [Identifier _; Identifier _]) -> (* TODO *)
241241
[
242242
EVM.from_int 0x80; (* 128 bits *)
243-
EVM.from_int 2; EVM.EXP; EVM.MUL; (* equivalent to EVM.SHL *)
243+
EVM.two; EVM.EXP; EVM.MUL; (* equivalent to EVM.SHL *)
244244
EVM.from_int 0x80; (* 128 bits *)
245-
EVM.from_int 2; EVM.EXP; EVM.SWAP 1; EVM.DIV; (* equivalent to EVM.SWAP 1; EVM.SHR *)
245+
EVM.two; EVM.EXP; EVM.SWAP 1; EVM.DIV; (* equivalent to EVM.SWAP 1; EVM.SHR *)
246246
]
247247

248248
| FunctionCall ("hash160", [value]) ->
@@ -376,7 +376,7 @@ and compile_tuple_literal = function
376376

377377
and compile_packed_word hi lo =
378378
(* [EVM.from_big_int hi; EVM.from_int 0x80; EVM.SHL; EVM.from_big_int lo; EVM.OR] *)
379-
[EVM.from_big_int hi; EVM.from_int 0x80; EVM.from_int 2; EVM.EXP; EVM.MUL; EVM.from_big_int lo; EVM.OR]
379+
[EVM.from_big_int hi; EVM.from_int 0x80; EVM.two; EVM.EXP; EVM.MUL; EVM.from_big_int lo; EVM.OR]
380380

381381
and compile_param (_, type') =
382382
compile_type type'

lib/Clar2EVM/utility.ml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@
22

33
let unreachable () = failwith "unreachable"
44

5-
let unimplemented what = failwith (Printf.sprintf "%s not implemented yet" what)
5+
let unimplemented what =
6+
let message =
7+
if what = "" then "not implemented yet"
8+
else Printf.sprintf "%s not implemented yet" what
9+
in
10+
failwith message
611

7-
let unsupported what = failwith (Printf.sprintf "%s not supported" what)
12+
let unsupported what =
13+
let message =
14+
if what = "" then "not supported"
15+
else Printf.sprintf "%s not supported" what
16+
in
17+
failwith message
818

919
let rec last = function
1020
| [] -> None

lib/EVM/EVM.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(* This is free and unencumbered software released into the public domain. *)
22

3+
#include "utility.ml"
34
#include "opcodes.ml"
45
#include "abi.ml"
56
#include "construct.ml"

lib/EVM/EVM.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(* This is free and unencumbered software released into the public domain. *)
22

3+
#include "utility.mli"
34
#include "opcodes.ml"
45
#include "abi.mli"
56
#include "construct.mli"

lib/EVM/abi.ml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,23 @@ module ABI = struct
1010

1111
and encode_function_signature signature =
1212
String.sub (keccak256 signature) 0 4
13+
14+
and encode_int_parameter_as_uint256 z =
15+
encode_int64_parameter_as_uint256 (Int64.of_int z)
16+
17+
and encode_int32_parameter_as_uint256 z =
18+
encode_int64_parameter_as_uint256 (Int64.of_int32 z)
19+
20+
and encode_int64_parameter_as_uint256 z =
21+
let buffer = Buffer.create 32 in
22+
Buffer.add_int64_be buffer 0L;
23+
Buffer.add_int64_be buffer 0L;
24+
Buffer.add_int64_be buffer 0L;
25+
Buffer.add_int64_be buffer z;
26+
Buffer.contents buffer
27+
28+
and encode_bigint_parameter_as_uint256 z =
29+
match Big_int.int64_of_big_int_opt z with
30+
| Some z -> encode_int64_parameter_as_uint256 z
31+
| None -> unimplemented "" (* TODO *)
1332
end

lib/EVM/abi.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
module ABI : sig
44
val encode_function : string -> string list -> string
55
val encode_function_signature : string -> string
6+
val encode_int_parameter_as_uint256 : int -> string
7+
val encode_int32_parameter_as_uint256 : int32 -> string
8+
val encode_int64_parameter_as_uint256 : int64 -> string
9+
val encode_bigint_parameter_as_uint256 : Big_int.big_int -> string
610
end

lib/EVM/construct.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ let from_string s =
66
let rec from_big_int z =
77
match Big_int.int_of_big_int_opt z with
88
| Some z -> from_int z
9-
| None -> failwith "not implemented yet" (* TODO *)
9+
| None -> PUSH (32, ABI.encode_bigint_parameter_as_uint256 z)
1010

1111
and from_int z =
12-
if z < 0 then failwith "not implemented yet" (* TODO *)
12+
if z < 0 then unimplemented "encoding of negative integers" (* FIXME *)
1313
else if z <= 0xFF then PUSH (1, Char.chr z |> String.make 1)
14-
else failwith "not implemented yet" (* TODO *)
14+
else PUSH (32, ABI.encode_int_parameter_as_uint256 z)
1515

1616
let zero = from_int 0
1717

1818
let one = from_int 1
19+
20+
let two = from_int 2

lib/EVM/construct.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ val zero : opcode
44

55
val one : opcode
66

7+
val two : opcode
8+
79
val from_big_int : Big_int.big_int -> opcode
810

911
val from_int : int -> opcode

lib/EVM/dune

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616
metrics.mli
1717
opcodes.ml
1818
print.ml
19-
print.mli)
19+
print.mli
20+
utility.ml
21+
utility.mli)
2022
(libraries cryptokit num))

lib/EVM/encode.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ and encode_opcode = function
100100
| MSIZE -> 0x59
101101
| GAS -> 0x5A
102102
| JUMPDEST -> 0x5B
103-
| PUSH (0, _) -> failwith "unreachable"
103+
| PUSH (0, _) -> unreachable ()
104104
| PUSH (n, _) -> 0x60 + n - 1
105105
| DUP n -> 0x80 + n - 1
106106
| SWAP n -> 0x90 + n - 1

0 commit comments

Comments
 (0)