Skip to content

Commit 3addec7

Browse files
committed
Better printing and sliceget fixes
1 parent 0688e81 commit 3addec7

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

src/ecCircuits.ml

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -518,14 +518,17 @@ module TestBack : CBackend = struct
518518
| 0 -> true
519519
| 1 ->
520520
let blocks = block_deps_of_deps w_out d in
521+
(* Format.eprintf "Checking block width...@."; *)
521522
Array.for_all (fun (_, d) ->
522523
if Map.is_empty d then true
523524
else
524525
let _, bits = Map.any d in
525526
Set.is_empty bits ||
526527
let base = Set.at_rank_exn 0 bits in
528+
(* Format.eprintf "Base for current block: %d@." base; *)
527529
Set.for_all (fun bit ->
528530
let dist = bit - base in
531+
(* Format.eprintf "Current bit: %d | Current dist: %d | Limit: %d@." bit dist w_in; *)
529532
0 <= dist && dist < w_in
530533
) bits
531534
) blocks
@@ -595,13 +598,14 @@ module MakeCircuitInterfaceFromCBackend(Backend: CBackend) : CircuitInterface =
595598

596599
let pp_circ (fmt : Format.formatter) (c: circ) : unit =
597600
match c with
598-
| `CArray _ -> Format.eprintf "Circ(Array)"
599-
| `CBitstring _ -> Format.eprintf "Circ(Bitstring)"
600-
| `CTuple _ -> Format.eprintf "Circ(Tuple)"
601+
| `CArray (r, w) -> Format.eprintf "Circ(Array[%d@%d])" (Backend.size_of_reg r) w
602+
| `CBitstring r -> Format.eprintf "Circ(Bitstring[%d])" (Backend.size_of_reg r)
603+
| `CTuple (_, ws) -> Format.eprintf "Circ(Tuple(%a))" (fun fmt szs ->
604+
Format.fprintf fmt "%d" (List.hd szs); List.iter (Format.fprintf fmt ", %d") (List.tl szs)) ws
601605
| `CBool _ -> Format.eprintf "Circ(Bool)"
602606

603607
let pp_circuit (fmt: Format.formatter) ((c, inps) : circuit) : unit =
604-
Format.eprintf "@[<hov 2>%a: @\n%a@]@\n"
608+
Format.eprintf "@[<hov 2>Circuit:@\nOut type %a@\nInputs: %a@]"
605609
pp_circ c
606610
(fun fmt inps -> List.iter (fun inp -> Format.fprintf fmt "%a@\n" pp_cinp inp) inps) inps
607611

@@ -620,6 +624,13 @@ module MakeCircuitInterfaceFromCBackend(Backend: CBackend) : CircuitInterface =
620624
`List cs
621625
let arg_of_init f =
622626
`Init f
627+
let pp_arg fmt arg : unit =
628+
match arg with
629+
| `Circuit c -> Format.fprintf fmt "%a" pp_circuit c
630+
| `Constant i -> Format.fprintf fmt "Constant: %s" (BI.to_string i)
631+
| `Init f -> Format.fprintf fmt "Init: Type of f(0): %a" pp_circuit (f 0)
632+
| `List cs -> Format.fprintf fmt "@[<hov 2> Circuit list: @\n%a@]"
633+
(fun fmt cs -> List.iter (Format.fprintf fmt "%a@\n" pp_circuit) cs) cs
623634
end
624635
open CArgs
625636

@@ -931,19 +942,25 @@ module MakeCircuitInterfaceFromCBackend(Backend: CBackend) : CircuitInterface =
931942
in op
932943
in
933944
match op with
934-
| { kind = `ASliceGet ((w, n), m) } ->
945+
| { kind = `ASliceGet ((n, w), m) } ->
935946
begin match args with
936947
| [ `Circuit (`CArray (r, w'), cinps) ; `Constant i ] when w = w' ->
937-
(`CBitstring (Backend.slice r (BI.to_int i) m), cinps)
938-
| _ -> assert false
948+
(`CBitstring (Backend.slice r (BI.to_int i) m), cinps)
949+
| args ->
950+
Format.eprintf "Bad arguments for array slice get: w = %d@.%a@." w
951+
(fun fmt args -> List.iter (Format.fprintf fmt "%a@\n" pp_arg) args) args;
952+
assert false
953+
end
954+
| { kind = `ASliceSet ((n, w), m) } ->
955+
begin match args with
956+
| [ `Circuit (`CArray (arr, w'), arrinps) ; `Constant i ; `Circuit (`CBitstring bs, bsinps) ] when w = w' ->
957+
let i = BI.to_int i in
958+
(`CArray (Backend.insert arr i bs, w), merge_inputs arrinps bsinps)
959+
| args ->
960+
Format.eprintf "Bad arguments for array slice set:@.w=%d@.%a@." w
961+
(fun fmt args -> List.iter (Format.fprintf fmt "%a@\n" pp_arg) args) args;
962+
assert false
939963
end
940-
| { kind = `ASliceSet ((w, n), m) } ->
941-
begin match args with
942-
| [ `Circuit (`CArray (arr, w'), arrinps) ; `Circuit (`CBitstring bs, bsinps) ; `Constant i ] when w = w' ->
943-
let i = BI.to_int i in
944-
(`CArray (Backend.insert arr i bs, w), merge_inputs arrinps bsinps)
945-
| _ -> assert false
946-
end
947964
(* FIXME: what do we want for out of bounds extract? Decide later *)
948965
| { kind = `Extract (w_in, w_out) } ->
949966
begin match args with
@@ -1470,7 +1487,7 @@ module MakeCircuitInterfaceFromCBackend(Backend: CBackend) : CircuitInterface =
14701487
let decompose (in_w: width) (out_w: width) ((`CBitstring r, inps) as c: cbitstring cfun) : cbitstring cfun list =
14711488
if not (is_decomposable in_w out_w c) then
14721489
let deps = Backend.Deps.block_deps_of_reg out_w r in
1473-
Format.eprintf "Failed to decompose. Deps:@.%a" (Backend.Deps.pp_block_deps) deps;
1490+
Format.eprintf "Failed to decompose. in_w=%d out_w=%d Deps:@.%a" in_w out_w (Backend.Deps.pp_block_deps) deps;
14741491
assert false
14751492
else
14761493
let n = (Backend.size_of_reg r) / out_w in

0 commit comments

Comments
 (0)