Library MonadVM.Support.UintN

An executable N-backed model of Numeric.Uint.
The OCaml module represents arbitrary-width unsigned integers using an abstract, nonnegative subset of Z.t. Using N here makes that invariant intrinsic to the Rocq type. Operations that can raise in OCaml use constructed opaque defaults. Bounds proofs must still exclude those branches; constructing a default does not prove exception freedom.

Require Import RocqOfOCaml.RocqOfOCaml RocqOfOCaml.Opaque.
Require Import MonadVM.Byte_string.
Require Import MonadVM.Map.
Require Import MonadVM.Rlp.

Local Open Scope Z_scope.

Module Impl.
  Definition t : Set := N.
End Impl.

Definition t : Set := Impl.t.

Definition fallback_uint : t. Proof. exact 0%N. Qed.
Definition fallback_int : Z. Proof. exact 0%Z. Qed.
Definition fallback_ascii : ascii. Proof. exact (ascii_of_nat 0). Qed.

#[local] Instance primitive_int : Basics.Unreachable Z :=
  RocqOfOCaml.Opaque.legacy_witness fallback_int.
#[local] Instance primitive_ascii : Basics.Unreachable ascii :=
  RocqOfOCaml.Opaque.legacy_witness fallback_ascii.

Definition signed : bool := false.
Definition max_t : option t := None.
Definition min_t : option t := Some 0%N.

Definition to_z (value : t) : Z := Z.of_N value.

Definition in_range (value : Z) : bool := Z.leb 0 value.

Definition of_z_opt (value : Z) : option t :=
  if in_range value then Some (Z.to_N value) else None.

Definition of_z_truncating :
    option (Z → t) := None.

Definition of_z_exn
    (value : Z) : t :=
  match of_z_opt value with
  | Some result ⇒ result
  | None ⇒ fallback_uint
  end.

Definition zero : t := 0%N.

Definition one : t := 1%N.

Definition testbit (value : t) (index : int) : bool :=
  Z.testbit (to_z value) index.

rocq-of-ocaml represents an included OCaml module both as a module namespace and as a record-valued package. The namespace Impl above supports paths such as Impl.t; this package supplies the corresponding value exposed by include Numeric.Uint.
Record impl_signature : Type := {
  impl_signed : bool;
  impl_max_t : option t;
  impl_min_t : option t;
  impl_in_range : Z → bool;
  impl_of_z_opt : Z → option t;
  impl_of_z_truncating :
    option (Z → t);
  impl_to_z : t → Z;
  impl_testbit : t → int → bool;
}.

Definition Impl : impl_signature :=
  {|
    impl_signed := signed;
    impl_max_t := max_t;
    impl_min_t := min_t;
    impl_in_range := in_range;
    impl_of_z_opt := of_z_opt;
    impl_of_z_truncating := @of_z_truncating;
    impl_to_z := to_z;
    impl_testbit := testbit;
  |}.

Definition byte
    (index_le : int) (value : t) : ascii :=
  if Z.ltb index_le 0 then
    fallback_ascii
  else
    ascii_of_N
      (N.land (N.shiftr value (8 × Z.to_N index_le)%N) 255%N).

Definition significant_bits (value : t) : int :=
  if N.eqb value 0%N then 0 else Z.of_N (N.succ (N.log2 value)).

Definition significant_bytes (value : t) : int :=
  Z.div (significant_bits value + 7) 8.

Definition of_bool
    (value : bool) : t :=
  if value then 1%N else 0%N.

Definition of_byte
    (value : ascii) : t := N_of_ascii value.

Definition of_int
    (value : int) : t := of_z_exn value.

Definition op_tildedollar : int → t :=
  of_int.

Definition to_int
    (value : t) : int := RocqOfOCaml.OCamlZ.to_int (to_z value).

Definition to_int_opt
    (value : t) : option int :=
  if RocqOfOCaml.OCamlZ.fits_int (to_z value) then
    Some (RocqOfOCaml.OCamlZ.to_int (to_z value))
  else None.

Definition of_uint64
    (value : int64) : t :=
  of_z_exn (RocqOfOCaml.OCamlZ.of_int64_unsigned value).

Definition of_int64
    (value : int64) : t := of_z_exn value.

Definition to_uint64
    (value : t) : int64 :=
  RocqOfOCaml.OCamlZ.to_int64_unsigned (to_z value).

Definition to_int64
    (value : t) : int64 := RocqOfOCaml.OCamlZ.to_int64 (to_z value).

Definition of_uint32
    (value : int32) : t :=
  of_z_exn (RocqOfOCaml.OCamlZ.of_int32_unsigned value).

Definition of_int32
    (value : int32) : t := of_z_exn value.

Definition to_uint32
    (value : t) : int32 :=
  RocqOfOCaml.OCamlZ.to_int32_unsigned (to_z value).

Definition to_int32
    (value : t) : int32 := RocqOfOCaml.OCamlZ.to_int32 (to_z value).

Definition hash (value : t) : int := RocqOfOCaml.OCamlZ.hash (to_z value).

Definition compare (left right : t) : int :=
  match N.compare left right with
  | Lt ⇒ -1
  | Eq ⇒ 0
  | Gt ⇒ 1
  end.

Definition ordered_type : MonadVM.Map.OrderedType (t := t) :=
  {| RocqOfOCaml.OCamlMap.OrderedType.compare := compare |}.

Definition _Set := RocqOfOCaml.OCamlSet.Make ordered_type.
Definition Map := MonadVM.Map.Make ordered_type.

Definition comparison_op (int_cmp : int → int → bool) (left right : t) :
    bool := int_cmp (compare left right) 0.

Definition op_lt (left right : t) : bool := N.ltb left right.
Definition op_lteq (left right : t) : bool := N.leb left right.
Definition op_eq (left right : t) : bool := N.eqb left right.
Definition op_gteq (left right : t) : bool := N.leb right left.
Definition op_gt (left right : t) : bool := N.ltb right left.
Definition op_ltgt (left right : t) : bool := negb (N.eqb left right).
Definition equal : t → t → bool := op_eq.

Definition max (left right : t) : t :=
  if N.ltb left right then right else left.

Definition min (left right : t) : t :=
  if N.ltb left right then left else right.

Definition is_zero
    (value : t) : bool := N.eqb value 0%N.

Definition to_string (value : t) : string :=
  RocqOfOCaml.OCamlZ.to_string (to_z value).

Definition to_hex_string (value : t) : string :=
  RocqOfOCaml.OCamlZ.format "%#x" (to_z value).

Definition ascii_code (value : ascii) : N := N_of_ascii value.

Definition digit_value (base : N) (value : ascii) : option N :=
  let code := ascii_code value in
  let digit :=
    if andb (48 <=? code)%N (code <=? 57)%N then Some (code - 48)%N
    else if andb (65 <=? code)%N (code <=? 70)%N then Some (code - 55)%N
    else if andb (97 <=? code)%N (code <=? 102)%N then Some (code - 87)%N
    else None in
  match digit with
  | Some digit ⇒ if (digit <? base)%N then Some digit else None
  | None ⇒ None
  end.

Fixpoint parse_digits (base : N) (digits : string) (accumulator : N)
    (seen_digit : bool) : option N :=
  match digits with
  | EmptyString ⇒ if seen_digit then Some accumulator else None
  | String char rest ⇒
      if Ascii.eqb char "_"%char then
        parse_digits base rest accumulator seen_digit
      else
        match digit_value base char with
        | Some digit ⇒
            parse_digits base rest (base × accumulator + digit)%N true
        | None ⇒ None
        end
  end.

Definition parse_unsigned (value : string) : option N :=
  match value with
  | String "0"%char (String prefix rest) ⇒
      if orb (Ascii.eqb prefix "x"%char) (Ascii.eqb prefix "X"%char) then
        parse_digits 16%N rest 0%N false
      else if orb (Ascii.eqb prefix "o"%char) (Ascii.eqb prefix "O"%char) then
        parse_digits 8%N rest 0%N false
      else if orb (Ascii.eqb prefix "b"%char) (Ascii.eqb prefix "B"%char) then
        parse_digits 2%N rest 0%N false
      else parse_digits 10%N value 0%N false
  | String "+"%char rest ⇒ parse_digits 10%N rest 0%N false
  | _ ⇒ parse_digits 10%N value 0%N false
  end.

Definition of_string
    (value : string) : t :=
  match parse_unsigned value with
  | Some result ⇒ result
  | None ⇒ fallback_uint
  end.

Definition op_tildeat : string → t :=
  of_string.

Definition of_z_after_op : Z → t :=
  of_z_exn.

Definition lift_1
    (operation : Z → Z) (value : t) : t :=
  of_z_after_op (operation (to_z value)).

Definition lift_2
    (operation : Z → Z → Z) (left right : t) : t :=
  of_z_after_op (operation (to_z left) (to_z right)).

Definition logand
    (left right : t) : t := N.land left right.

Definition logor
    (left right : t) : t := N.lor left right.

Definition logxor
    (left right : t) : t := N.lxor left right.

Definition lognot
    (_value : t) : t := fallback_uint.

Definition op_plus
    (left right : t) : t := (left + right)%N.

Definition op_minus
    (left right : t) : t :=
  if N.leb right left then (left - right)%N
  else fallback_uint.

Definition op_star
    (left right : t) : t := (left × right)%N.

Definition op_div
    (left right : t) : t :=
  if N.eqb right 0%N then fallback_uint
  else N.div left right.

Definition modulo
    (left right : t) : t :=
  if N.eqb right 0%N then fallback_uint
  else N.modulo left right.

Definition rem : t → t → t :=
  modulo.

Definition addmod
    (left right modulus : t) : t := modulo (left + right)%N modulus.

Definition mulmod
    (left right modulus : t) : t := modulo (left × right)%N modulus.

Definition shift_left
    (value : t) (shift : int) : t :=
  if Z.ltb shift 0 then fallback_uint
  else Z.to_N (Z.shiftl (to_z value) shift).

Definition shift_right
    (value : t) (shift : int) : t :=
  if Z.ltb shift 0 then fallback_uint
  else Z.to_N (Z.shiftr (to_z value) shift).

Definition op_starstar
    (base : t) (exponent : int) : t :=
  if Z.ltb exponent 0 then fallback_uint
  else N.pow base (Z.to_N exponent).

Definition exp_mod
    (modulo_value x_value y_value : t) : t :=
  if N.eqb modulo_value 0%N then fallback_uint
  else
    Z.to_N
      (RocqOfOCaml.OCamlZ.powm (to_z x_value) (to_z y_value)
        (to_z modulo_value)).

Definition minus_1_64th
    (value : t) : t := (value - N.div value 64)%N.

Definition bytes_to_whole_words
    (value : t) : t := N.div (value + 31)%N 32%N.

Definition as_signed
    (value : t) : Z := to_z value.

Fixpoint reverse_append (value suffix : string) : string :=
  match value with
  | EmptyString ⇒ suffix
  | String byte rest ⇒ reverse_append rest (String byte suffix)
  end.

Definition reverse (value : string) : string :=
  reverse_append value EmptyString.

Definition of_bytes_be
    (value : MonadVM.Byte_string.Bytes.t) : t :=
  Z.to_N (RocqOfOCaml.OCamlZ.of_bits (reverse value)).

Definition to_bytes_be
    (value : t) : MonadVM.Byte_string.Bytes.t :=
  
  reverse (RocqOfOCaml.OCamlZ.to_bits_minimal (to_z value)).

Definition of_rlp
    (value : MonadVM.Rlp.t) : option t :=
  match value with
  | MonadVM.Rlp.Bytes bytes ⇒
      let result := of_bytes_be bytes in
      
      if Z.eqb (significant_bytes result)
          (RocqOfOCaml.OCamlString.length bytes)
      then Some result else None
  | MonadVM.Rlp.List _ ⇒ None
  end.

Definition to_rlp
    (value : t) : MonadVM.Rlp.t := MonadVM.Rlp.Bytes (to_bytes_be value).

Definition ceil_div
    (left right : t) : t :=
  op_div (op_minus (op_plus left right) 1%N) right.

Definition div_rem
    (left right : t) : t × t :=
  if N.eqb right 0%N then
    (fallback_uint, fallback_uint)
  else (N.div left right, N.modulo left right).

The representation is a faithful refinement of nonnegative Z.
Lemma to_z_nonnegative (value : t) : (0 ≤ to_z value)%Z.
Proof.
  apply N2Z.is_nonneg.
Qed.

Lemma of_z_opt_nonnegative (value : Z) :
  (0 ≤ value)%Z → of_z_opt value = Some (Z.to_N value).
Proof.
  intros Hvalue.
  unfold of_z_opt, in_range.
  assert (Hleb : Z.leb 0 value = true).
  { apply Z.leb_le. exact Hvalue. }
  rewrite Hleb.
  reflexivity.
Qed.

Lemma to_z_of_z_nonnegative (value : Z) :
  (0 ≤ value)%Z → to_z (Z.to_N value) = value.
Proof.
  apply Z2N.id.
Qed.

Lemma to_z_plus (left right : t) :
  to_z (op_plus left right) = (to_z left + to_z right)%Z.
Proof.
  apply N2Z.inj_add.
Qed.

Lemma to_z_mul (left right : t) :
  to_z (op_star left right) = (to_z left × to_z right)%Z.
Proof.
  apply N2Z.inj_mul.
Qed.

Lemma to_z_minus (left right : t) :
  (right ≤ left)%N →
  to_z (op_minus left right) = (to_z left - to_z right)%Z.
Proof.
  intros Hle.
  unfold op_minus.
  assert (Hleb : N.leb right left = true).
  { apply N.leb_le. exact Hle. }
  rewrite Hleb.
  apply N2Z.inj_sub.
  exact Hle.
Qed.