Skip to content

Commit

Permalink
should now work with ocaml 4.06
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsmkl committed Jan 29, 2018
1 parent 86e353a commit 156ddcc
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 47 deletions.
3 changes: 1 addition & 2 deletions interpreter/main/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ let argspec = Arg.align
"-debug-error", Arg.Set Flags.debug_error, " try to find out why the interpreter failed";
"-m", Arg.Set Flags.merkle, " merkle proof mode";
"-micro", Arg.Set Flags.microstep, " merkle proof mode (microsteps)";
"-merkletest", Arg.Int (fun n -> Mbinary.test n; exit 0), " just run a merkle root computation test with a number of leafs";
"-init", Arg.Set Flags.init, " output initial state hash of a test case";
"-init-vm", Arg.Set Flags.init_vm, " output initial vm of a test case";
"-result", Arg.Set Flags.result, " output final state hash of a test case and the number of steps";
Expand Down Expand Up @@ -166,7 +165,7 @@ let () =
let oc = open_out_bin "decoded.bin" in
for i = 0 to Array.length vm.code - 1 do
let inst = vm.code.(i) in
output_bytes oc (Mbinary.microp_word (get_code inst))
output_bytes oc (Bytes.of_string (Mbinary.microp_word (get_code inst)))
done;
close_out oc
| _ -> () )
Expand Down
2 changes: 1 addition & 1 deletion interpreter/merkle/addglobals.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ let int_binary i =
Bytes.set res 1 (Char.chr ((i lsr 8) land 0xff));
Bytes.set res 2 (Char.chr ((i lsr 16) land 0xff));
Bytes.set res 3 (Char.chr ((i lsr 24) land 0xff));
res
Bytes.to_string res

let generate_data (addr, i) : string segment =
elem {
Expand Down
14 changes: 6 additions & 8 deletions interpreter/merkle/byteutil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ let trace name = if !Flags.trace then print_endline ("-- " ^ name)

let w256_to_string bs =
let res = ref "" in
for i = 0 to Bytes.length bs - 1 do
let code = Char.code bs.[i] in
for i = 0 to String.length bs - 1 do
let code = Char.code (String.get bs i) in
res := !res ^ (if code < 16 then "0" else "") ^ Printf.sprintf "%x" code
done;
!res
Expand All @@ -32,7 +32,7 @@ let eos s = (pos s = len s)
let check n s = if pos s + n > len s then raise EOS
let skip n s = check n s; s.pos := !(s.pos) + n

let read s = if !(s.pos) < len s then Char.code (s.bytes.[!(s.pos)]) else 0
let read s = if !(s.pos) < len s then Char.code (String.get s.bytes (!(s.pos))) else 0
let peek s = if eos s then None else Some (read s)

(* let get s = check 1 s; let b = read s in skip 1 s; b *)
Expand Down Expand Up @@ -60,7 +60,7 @@ let u64 s =
Int64.(add lo (shift_left hi 32))

let mini_memory bytes =
let s = stream (Memory.to_bytes bytes) in
let s = stream (Bytes.to_string (Memory.to_bytes bytes)) in
(* trace ("Get memory: " ^ w256_to_string (Memory.to_bytes bytes)); *)
let a = u64 s and b = u64 s in
(* trace ("A: " ^ Int64.to_string a); *)
Expand All @@ -78,7 +78,7 @@ let word bytes =
!res

let bytes_to_array bytes =
let s = stream bytes in
let s = stream (Bytes.to_string bytes) in
let res = Array.make (Bytes.length bytes) 0L in
for i = 0 to Bytes.length bytes / 8 do
res.(i) <- u64 s
Expand Down Expand Up @@ -107,8 +107,6 @@ let to_bytes s =
Buffer.clear s.buf;
bs



let s = stream ()

let u8 i = put s (Char.chr (i land 0xff))
Expand Down Expand Up @@ -146,7 +144,7 @@ let extend bs n =
Bytes.set nbs (n-1-i) (Bytes.get bs i)
done;
(* Bytes.blit bs 0 nbs (n-len) len; *)
nbs
Bytes.to_string nbs

let value = function
| I32 i -> u32 i
Expand Down
34 changes: 19 additions & 15 deletions interpreter/merkle/mbinary.ml
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,13 @@ let microp_word op =
value op.immed;
extend (to_bytes s) 32

type w256 = bytes
type w256 = string

open Cryptokit

(* keccak two words, clear control byte *)

(*
let ccb w =
let res = Bytes.copy w in
Bytes.set res 31 (Char.chr 0);
Expand All @@ -299,6 +300,7 @@ let ccb2 w =
let res = Bytes.copy w in
Bytes.set res 31 (Char.chr 1);
res
*)

let keccak w1 w2 =
let hash = Hash.keccak 256 in
Expand Down Expand Up @@ -383,7 +385,7 @@ let rec mapMerkle f arr idx level =

let rec depth x = if x <= 1 then 0 else 1 + depth (x/2)

let get_hash arr =
let get_hash (arr: w256 array) =
makeMerkle arr 0 (depth (Array.length arr*2-1))

let map_hash f arr = mapMerkle f arr 0 (depth (Array.length arr*2-1))
Expand Down Expand Up @@ -417,34 +419,34 @@ let hash_stack arr =

let string_to_array str =
(* need one extra for nil terminated strings*)
let res = Array.make (String.length str) (u256 0) in
for i = 0 to String.length str - 1 do
res.(i) <- u256 (Char.code str.[i])
let res = Array.make (Bytes.length str) (u256 0) in
for i = 0 to Bytes.length str - 1 do
res.(i) <- u256 (Char.code (Bytes.get str i))
done;
res

let string_to_root str = get_hash (string_to_array str)

let zeros = String.make 64 (Char.chr 0)
let zeros = Bytes.make 64 (Char.chr 0)

let get_bytes32 str n = String.sub str n 32
let get_bytes16 str n = String.sub str n 16
let get_bytes32 str n = Bytes.sub str n 32
let get_bytes16 str n = Bytes.sub str n 16

let bytes_to_array32 str =
let cells = max ((String.length str + 31) / 32) 2 in
let cells = max ((Bytes.length str + 31) / 32) 2 in
let res = Array.make cells (u256 0) in
let str_e = str ^ zeros in
let str_e = Bytes.cat str zeros in
for i = 0 to cells - 1 do
res.(i) <- get_bytes32 str_e (i*32)
res.(i) <- Bytes.to_string (get_bytes32 str_e (i*32))
done;
res

let bytes_to_array str =
let cells = max ((String.length str + 15) / 16) 2 in
let cells = max ((Bytes.length str + 15) / 16) 2 in
let res = Array.make cells (u256 0) in
let str_e = str ^ zeros in
let str_e = Bytes.cat str zeros in
for i = 0 to cells - 1 do
res.(i) <- get_bytes16 str_e (i*16)
res.(i) <- Bytes.to_string (get_bytes16 str_e (i*16))
done;
res

Expand Down Expand Up @@ -531,7 +533,7 @@ let hash_io vm =

let string_from_bytes bs =
let rec aux n =
if String.length bs = n || Char.code bs.[n] = 0 then "" else String.make 1 bs.[n] ^ aux (n+1) in
if Bytes.length bs = n || Char.code (Bytes.get bs n) = 0 then "" else String.make 1 (Bytes.get bs n) ^ aux (n+1) in
aux 0

let code_hash arr = get_hash (Array.map (fun v -> microp_word (get_code v)) arr)
Expand Down Expand Up @@ -673,11 +675,13 @@ let test2 () =
let _ = test2()
*)

(*
let test n =
let w = Bytes.create 32 in
let arr = Array.make n w in
let lst = make_levels_aux arr in
prerr_endline (string_of_int (List.length lst))
*)

let w256_to_int w =
let res = ref 0 in
Expand Down
12 changes: 6 additions & 6 deletions interpreter/merkle/mproof.ml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ let get_read_location m loc =
| InputDataIn ->
LocationProof2 (loc_proof_data (value_to_int m.m_regs.reg2) (value_to_int m.m_regs.reg1) vm.input.file_data)

let find_file vm name =
let find_file vm (name:string) =
let res = ref SimpleProof in
for i = 0 to Array.length vm.input.file_data - 1 do
if string_from_bytes vm.input.file_name.(i) = name then res := LocationProof (loc_proof i bytes_to_root vm.input.file_data)
Expand All @@ -119,7 +119,7 @@ let find_file vm name =
let find_files vm =
let res = ref [] in
for i = 0 to Array.length vm.input.file_data - 1 do
if String.length vm.input.file_name.(i) > 0 && vm.input.file_name.(i).[0] <> '\000' then begin
if Bytes.length vm.input.file_name.(i) > 0 && (Bytes.get vm.input.file_name.(i) 0) <> '\000' then begin
res := (map_location_proof bytes_to_root vm.input.file_data i, map_location_proof string_to_root vm.input.file_name i, i, Mbinary.string_from_bytes vm.input.file_name.(i) ^ ".out") :: !res
end
done;
Expand Down Expand Up @@ -306,7 +306,7 @@ let read_from_proof regs vm proof = function
( match proof with
| LocationProof2 (_, loc2, (_, lst2)) ->
let leaf = get_leaf (loc2/32) lst2 in
let byte = Bytes.get leaf (loc2 mod 32) in
let byte = String.get leaf (loc2 mod 32) in
get_value (i (Char.code byte))
| _ -> raise EmptyArray )
| _ -> value_from_proof proof
Expand Down Expand Up @@ -482,7 +482,7 @@ let check_update_call_ptr state1 state2 (m,vm) =
m.bin_vm = hash_vm_bin vm &&
state2 = hash_machine_bin m2

let check_update_memsize (state1:bytes) (state2:bytes) (m,vm) =
let check_update_memsize (state1:w256) (state2:w256) (m,vm) =
let vm2 = {vm with bin_memsize=(if m.bin_microp.mem_ch then value_to_int m.bin_regs.reg1 else 0) + vm.bin_memsize} in
state1 = hash_machine_bin m &&
m.bin_vm = hash_vm_bin vm &&
Expand Down Expand Up @@ -544,9 +544,9 @@ let build_root v = make_zero (Int64.to_int (Decode.word v))

let modify_data i nv str =
let nv = Int64.to_int (Decode.word nv) in
let res = Bytes.copy str in
let res = Bytes.of_string str in
Bytes.set res i (Char.chr nv);
res
Bytes.to_string res

let write_register_bin proof vm regs v = function
| NoOut -> vm
Expand Down
29 changes: 15 additions & 14 deletions interpreter/merkle/mrun.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ exception VmTrap
exception VmError

type input = {
file_name : string array;
file_data : string array;
file_name : bytes array;
file_data : bytes array;
file_size : int array;
}

Expand All @@ -33,8 +33,8 @@ let inc_pc vm = vm.pc <- vm.pc+1
let custom_command = Hashtbl.create 7

let empty_input sz = {
file_name = Array.make sz "";
file_data = Array.make sz "";
file_name = Array.make sz Bytes.empty;
file_data = Array.make sz Bytes.empty;
file_size = Array.make sz 0;
}

Expand Down Expand Up @@ -192,9 +192,9 @@ let read_register vm reg = function
| InputNameIn ->
let str = vm.input.file_name.(value_to_int reg.reg2) in
let s1 = value_to_int reg.reg1 in
let chr = if s1 < String.length str then Char.code str.[s1] else 0 in
let chr = if s1 < Bytes.length str then Char.code (Bytes.get str s1) else 0 in
i chr
| InputDataIn -> i (Char.code vm.input.file_data.(value_to_int reg.reg2).[value_to_int reg.reg1])
| InputDataIn -> i (Char.code (Bytes.get vm.input.file_data.(value_to_int reg.reg2) (value_to_int reg.reg1)))

let get_register regs = function
| Reg1 -> regs.reg1
Expand All @@ -209,7 +209,7 @@ let memop mem v addr = function

let set_input_name vm s2 s1 v =
let str = vm.input.file_name.(s2) in
let str = if String.length str < 256 then String.make 256 (Char.chr 0) else str in
let str = if Bytes.length str < 256 then Bytes.make 256 (Char.chr 0) else str in
Bytes.set str s1 (Char.chr (value_to_int v));
vm.input.file_name.(s2) <- str

Expand Down Expand Up @@ -307,11 +307,12 @@ let init_memory m instance =
let open Source in
trace ("Segments: " ^ string_of_int (List.length m.data));
let res = ref [] in
let init (dta:bytes Ast.segment) =
let init (dta:string Ast.segment) =
let bts = Bytes.of_string dta.it.init in
let offset = value_to_int (Eval.eval_const instance dta.it.offset) in
let sz = Bytes.length dta.it.init in
let sz = Bytes.length bts in
for i = 0 to sz-1 do
let v = I32 (Int32.of_int (Char.code (Bytes.get dta.it.init i))) in
let v = I32 (Int32.of_int (Char.code (Bytes.get bts i))) in
res :=
[STORE {ty=I32Type; align=0; offset=0l; sz=Some Memory.Mem8};
PUSH v; PUSH (I32 (Int32.of_int (offset+i)))] @ !res
Expand Down Expand Up @@ -737,14 +738,14 @@ let vm_step vm = match vm.code.(vm.pc) with
let s1 = value_to_int vm.stack.(vm.stack_ptr-1) in
let s2 = value_to_int vm.stack.(vm.stack_ptr-2) in
let str = vm.input.file_name.(s2) in
let chr = if s1 < String.length str then Char.code str.[s1] else 0 in
let chr = if s1 < Bytes.length str then Char.code (Bytes.get str s1) else 0 in
vm.stack.(vm.stack_ptr-2) <- i (chr);
vm.stack_ptr <- vm.stack_ptr - 1
| INPUTDATA ->
inc_pc vm;
let s1 = value_to_int vm.stack.(vm.stack_ptr-1) in
let s2 = value_to_int vm.stack.(vm.stack_ptr-2) in
vm.stack.(vm.stack_ptr-2) <- i (Char.code vm.input.file_data.(s2).[s1]);
vm.stack.(vm.stack_ptr-2) <- i (Char.code (Bytes.get vm.input.file_data.(s2) s1));
vm.stack_ptr <- vm.stack_ptr - 1
| OUTPUTSIZE ->
inc_pc vm;
Expand All @@ -760,7 +761,7 @@ let vm_step vm = match vm.code.(vm.pc) with
let s3 = value_to_int vm.stack.(vm.stack_ptr-3) in
vm.stack_ptr <- vm.stack_ptr - 3;
let str = vm.input.file_name.(s3) in
let str = if String.length str = 1 then String.make 256 (Char.chr 0) else str in
let str = if Bytes.length str = 1 then Bytes.make 256 (Char.chr 0) else str in
Bytes.set str s2 (Char.chr s1);
vm.input.file_name.(s3) <- str
| OUTPUTDATA ->
Expand Down Expand Up @@ -913,7 +914,7 @@ let trace_step vm = match vm.code.(vm.pc) with
| INPUTDATA ->
let s1 = value_to_int vm.stack.(vm.stack_ptr-1) in
let s2 = value_to_int vm.stack.(vm.stack_ptr-2) in
let str = String.make 1 vm.input.file_data.(s2).[s1] in
let str = String.make 1 (Bytes.get vm.input.file_data.(s2) s1) in
"INPUTDATA " ^ str
| OUTPUTSIZE -> "OUTPUTSIZE"
| OUTPUTNAME -> "OUTPUTNAME"
Expand Down
2 changes: 1 addition & 1 deletion interpreter/script/run.ml
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ let values_from_arr arr start len =
let task_number = ref 0

let terminate str =
let res = String.make 256 (Char.chr 0) in
let res = Bytes.make 256 (Char.chr 0) in
for i = 0 to String.length str-1 do
Bytes.set res i str.[i]
done;
Expand Down

0 comments on commit 156ddcc

Please sign in to comment.