Library MonadVM.Ec.Algebra

File generated by rocq-of-ocaml
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import RocqOfOCaml.Settings.
From Stdlib Require Import Program.Wf.

Require Import MonadVM.Support.UintN.
Require MonadVM.Numeric.
Require MonadVM.Option.
Require MonadVM.String.

Require Import MonadVM.Support.Variants.
Require MonadVM.Support.CryptoBlake2.
Require Import MonadExecutionSpecs.Termination.Natural.
Require Import MonadExecutionSpecs.Termination.IntegerLoops.
Require Import MonadExecutionSpecs.Termination.Ec.
Require Import RocqOfOCaml.Opaque.
Local Ltac _rocq_witness_type typ :=
  let typ := eval hnf in typ in
  lazymatch typ with
  | list ?a ⇒ let a := _rocq_witness_type a in constr:(list a)
  | option ?a ⇒ let a := _rocq_witness_type a in constr:(option a)
  | prod ?a ?b ⇒ let a := _rocq_witness_type a in
      let b := _rocq_witness_type b in constr:(prod a b)
  | sum ?a ?b ⇒ let a := _rocq_witness_type a in
      let b := _rocq_witness_type b in constr:(sum a b)
  | ?a → ?b ⇒ let a := _rocq_witness_type a in
      let b := _rocq_witness_type b in constr:(a → b)
  | _ ⇒ typ
  end.
The module type of fields, given in terms of binary operations +, -, ×, /. Unary multiplicative inversion inv is also provided as part of the signature although it can be derived from /.
Module FIELD.
  Record signature {t : Set} : Type := {
    t := t;
    op_eq : t → t → bool;
    op_ltgt : t → t → bool;
    zero : t;
    one : t;
    
Embed an integer in the field. Note that this can be derived, but all our instances can provide more direct implementations than a naive algorithm.
    op_tildedollar : int → t;
    
Read the input as an unsigned integer and embed it in the field. The input can be arbitrarily long, and is not constrained to fit in a machine integer.
    op_tildeat : MonadVM.String.t → t;
    op_plus : t → t → t;
    op_minus : t → t → t;
    op_star : t → t → t;
    op_div : t → t → t;
    inv : t → t;
  }.
End FIELD.
Definition FIELD := @FIELD.signature.
Arguments FIELD {_}.

The module type of Euclidean domains. Remember that an Euclidean domain is a ring equipped with a degree function degree : t ∖ {0} → ℕ satisfying:
  • Whenever a, b ∈ t and b ≠ 0, there exist q, r ∈ t satisfying a = q × b + r and either r = 0 or
f(r) < f(b). The choice of degree function is unimportant, but its existence guarantees termination of the Euclidean algorithm, which is used to construct quotient fields over the domain.
Module EUCLIDEAN_DOMAIN.
  Record signature {t : Set} : Type := {
    t := t;
    op_eq : t → t → bool;
    op_ltgt : t → t → bool;
    zero : t;
    one : t;
    op_tildeat : MonadVM.String.t → t;
    op_tildedollar : int → t;
    degree : t → int;
    op_plus : t → t → t;
    op_minus : t → t → t;
    op_star : t → t → t;
    div_rem : t → t → t × t;
  }.
End EUCLIDEAN_DOMAIN.
Definition EUCLIDEAN_DOMAIN := @EUCLIDEAN_DOMAIN.signature.
Arguments EUCLIDEAN_DOMAIN {_}.

The functor Quotient_field constructs a field by quotienting the Euclidean domain D by the element Mod.modulus, which is assumed to be irreducible. Field division is implemented via the extended Euclidean algorithm.
Module Quotient_field_Mod_signature.
  Record signature {D_t : Set} {D : EUCLIDEAN_DOMAIN (t := D_t)} : Type := {
    modulus : D.(EUCLIDEAN_DOMAIN.t);
  }.
End Quotient_field_Mod_signature.
Definition Quotient_field_Mod_signature {D_t : Set}
  {D : EUCLIDEAN_DOMAIN (t := D_t)} := @Quotient_field_Mod_signature.signature
  D_t D.

Module Quotient_field.
  Class FArgs {D_t : Set} := {
    D : EUCLIDEAN_DOMAIN (t := D_t);
    Mod : (Quotient_field_Mod_signature (D := D));
  }.
  Arguments Build_FArgs {_}.

  Module Impl.
    Definition impl `{_fargs : FArgs} : Set := D.(EUCLIDEAN_DOMAIN.t).

    Definition t `{_fargs : FArgs} : Set := impl.

    Definition reduce `{_fargs : FArgs} (x_value : impl)
      : D.(EUCLIDEAN_DOMAIN.t) :=
      let '(_quot, rem) :=
        D.(EUCLIDEAN_DOMAIN.div_rem) x_value
          Mod.(Quotient_field_Mod_signature.modulus) in
      rem.

    Module Impl_signature.
      Record signature `{_fargs : FArgs} : Type := {
        impl := D.(EUCLIDEAN_DOMAIN.t);
        t := impl;
        reduce : impl → t;
      }.
    End Impl_signature.
    Definition Impl_signature `{_fargs : FArgs} := @Impl_signature.signature _
      _.
    Arguments Impl_signature {_ _}.

    Definition module `{_fargs : FArgs} :Impl_signature :=
      {|
        Impl_signature.reduce := reduce
      |}.
  End Impl.
  Definition Impl `{_fargs : FArgs} := Impl.module.

Inclusion of the module Impl
  Definition impl `{_fargs : FArgs} := Impl.(Impl.Impl_signature.impl).

  Definition t `{_fargs : FArgs} := Impl.(Impl.Impl_signature.t).

  Definition reduce `{_fargs : FArgs} : impl → Impl.(Impl.Impl_signature.t) :=
    Impl.(Impl.Impl_signature.reduce).

  Definition op_eq `{_fargs : FArgs}
    (u_value : D.(EUCLIDEAN_DOMAIN.t)) (v_value : D.(EUCLIDEAN_DOMAIN.t))
    : bool := D.(EUCLIDEAN_DOMAIN.op_eq) u_value v_value.

  Definition op_ltgt `{_fargs : FArgs}
    (u_value : D.(EUCLIDEAN_DOMAIN.t)) (v_value : D.(EUCLIDEAN_DOMAIN.t))
    : bool := D.(EUCLIDEAN_DOMAIN.op_ltgt) u_value v_value.

  Definition zero `{_fargs : FArgs} : D.(EUCLIDEAN_DOMAIN.t) :=
    reduce D.(EUCLIDEAN_DOMAIN.zero).

  Definition one `{_fargs : FArgs} : D.(EUCLIDEAN_DOMAIN.t) :=
    reduce D.(EUCLIDEAN_DOMAIN.one).

  Definition op_tildeat `{_fargs : FArgs} (str : MonadVM.String.t)
    : D.(EUCLIDEAN_DOMAIN.t) := reduce (D.(EUCLIDEAN_DOMAIN.op_tildeat) str).

  Definition op_tildedollar `{_fargs : FArgs} (x_value : int)
    : D.(EUCLIDEAN_DOMAIN.t) :=
    reduce (D.(EUCLIDEAN_DOMAIN.op_tildedollar) x_value).

  Definition lift_2 `{_fargs : FArgs}
    (f_value :
      D.(EUCLIDEAN_DOMAIN.t) → D.(EUCLIDEAN_DOMAIN.t) → D.(EUCLIDEAN_DOMAIN.t))
    (u_value : D.(EUCLIDEAN_DOMAIN.t)) (v_value : D.(EUCLIDEAN_DOMAIN.t))
    : D.(EUCLIDEAN_DOMAIN.t) := reduce (f_value u_value v_value).

  Definition op_plus `{_fargs : FArgs}
    : D.(EUCLIDEAN_DOMAIN.t) → D.(EUCLIDEAN_DOMAIN.t) → D.(EUCLIDEAN_DOMAIN.t) :=
    lift_2 D.(EUCLIDEAN_DOMAIN.op_plus).

  Definition op_minus `{_fargs : FArgs}
    : D.(EUCLIDEAN_DOMAIN.t) → D.(EUCLIDEAN_DOMAIN.t) → D.(EUCLIDEAN_DOMAIN.t) :=
    lift_2 D.(EUCLIDEAN_DOMAIN.op_minus).

  Definition op_star `{_fargs : FArgs}
    : D.(EUCLIDEAN_DOMAIN.t) → D.(EUCLIDEAN_DOMAIN.t) → D.(EUCLIDEAN_DOMAIN.t) :=
    lift_2 D.(EUCLIDEAN_DOMAIN.op_star).

  Program Definition bezout_coeffs `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
    (u_value : D.(EUCLIDEAN_DOMAIN.t)) (v_value : D.(EUCLIDEAN_DOMAIN.t))
    : D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t) :=
    let _rocq_measure (_rocq_state :
      D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t) ×
        D.(EUCLIDEAN_DOMAIN.t)) : nat :=
      let '(_, _, divisor, _) := _rocq_state in MonadExecutionSpecs.Termination.Ec.degree_rank (D.(EUCLIDEAN_DOMAIN.degree) divisor)
      in
    let _rocq_body :
      (∀ _rocq_state :
        D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t)
          × D.(EUCLIDEAN_DOMAIN.t),
        (∀ _rocq_next :
          D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t) ×
            D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t), ltof
          (D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t) ×
            D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t)) _rocq_measure
          _rocq_next _rocq_state →
          D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t)) →
        D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t)) :=
      (fun _rocq_state _rocq_recurse ⇒
        let '(r_value, a_value, r', a') := _rocq_state in
        match D.(EUCLIDEAN_DOMAIN.op_eq) r' D.(EUCLIDEAN_DOMAIN.zero) as
          _rocq_guard return
          (D.(EUCLIDEAN_DOMAIN.op_eq) r' D.(EUCLIDEAN_DOMAIN.zero) = _rocq_guard
          → _) with
        | true ⇒ fun _rocq_guard_eq ⇒ (a_value, r_value)
        | false ⇒ fun _rocq_guard_eq ⇒
          let divisor_degree := D.(EUCLIDEAN_DOMAIN.degree) r' in
          let _rocq_match_scrutinee := D.(EUCLIDEAN_DOMAIN.div_rem) r_value r'
            in
          match _rocq_match_scrutinee as _rocq_match_view return
            (_rocq_match_scrutinee = _rocq_match_view → _) with
          | (quotient, remainder) ⇒ fun _rocq_match_eq ⇒
            let _rocq_match_scrutinee := (r', remainder) in
            match _rocq_match_scrutinee as _rocq_match_view return
              (_rocq_match_scrutinee = _rocq_match_view → _) with
            | (r_value, r') ⇒ fun _rocq_match_eq ⇒
              let _rocq_match_scrutinee :=
                (a',
                  (D.(EUCLIDEAN_DOMAIN.op_minus) a_value
                    (D.(EUCLIDEAN_DOMAIN.op_star) quotient a'))) in
              match _rocq_match_scrutinee as _rocq_match_view return
                (_rocq_match_scrutinee = _rocq_match_view → _) with
              | (a_value, a') ⇒ fun _rocq_match_eq ⇒
                match
                  andb (Z.geb divisor_degree 0)
                    (Z.ltb (D.(EUCLIDEAN_DOMAIN.degree) r') divisor_degree) as
                  _rocq_guard return
                  (andb (Z.geb divisor_degree 0)
                    (Z.ltb (D.(EUCLIDEAN_DOMAIN.degree) r') divisor_degree) =
                  _rocq_guard → _) with
                | true ⇒ fun _rocq_guard_eq ⇒
                  _rocq_recurse (r_value, a_value, r', a')
                    ltac:(MonadExecutionSpecs.Termination.Ec.solve_guarded_degree_decrease)
                | false ⇒ fun _rocq_guard_eq ⇒
                  let '_ :=
                    "Quotient_field.bezout_coeffs: non-decreasing remainder degree"
                    in
                  _rocq_fallback_arg_0
                end eq_refl
              end eq_refl
            end eq_refl
          end eq_refl
        end eq_refl) in
    let _rocq_fix := @Fix
      (D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t)
        × D.(EUCLIDEAN_DOMAIN.t)) (ltof
      (D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t)
        × D.(EUCLIDEAN_DOMAIN.t)) _rocq_measure) (well_founded_ltof
      (D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t)
        × D.(EUCLIDEAN_DOMAIN.t)) _rocq_measure) (fun _ ⇒
      D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t)) _rocq_body in
    let loop (r_value : D.(EUCLIDEAN_DOMAIN.t))
      (a_value : D.(EUCLIDEAN_DOMAIN.t)) (r' : D.(EUCLIDEAN_DOMAIN.t))
      (a' : D.(EUCLIDEAN_DOMAIN.t)) :
      D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t) := _rocq_fix (r_value,
      a_value, r', a') in
    loop u_value D.(EUCLIDEAN_DOMAIN.one) v_value D.(EUCLIDEAN_DOMAIN.zero).

  Definition _rocq_fallback_inv_0 `{_fargs : FArgs} : ltac:(let typ := constr:(ltac:(first [unshelve refine ((unit)%type )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
  Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.

  Definition inv `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
    (u_value : D.(EUCLIDEAN_DOMAIN.t)) : D.(EUCLIDEAN_DOMAIN.t) :=
    let '_ :=
      if op_eq u_value zero then
        let '_ := Build_extensible "Division_by_zero" unit tt in
        _rocq_fallback_inv_0
      else
        tt in
    let '(inv_u, gcd) :=
      (bezout_coeffs (_rocq_fallback_arg_0)) u_value
        Mod.(Quotient_field_Mod_signature.modulus) in
    let '(inv_u, rem) := D.(EUCLIDEAN_DOMAIN.div_rem) inv_u gcd in
    let '_ :=
      if D.(EUCLIDEAN_DOMAIN.op_eq) rem D.(EUCLIDEAN_DOMAIN.zero) then tt else
        _rocq_fallback_inv_0 in
    reduce inv_u.

  Definition op_div `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((D.(EUCLIDEAN_DOMAIN.t) × D.(EUCLIDEAN_DOMAIN.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
    (u_value : D.(EUCLIDEAN_DOMAIN.t)) (v_value : D.(EUCLIDEAN_DOMAIN.t))
    : D.(EUCLIDEAN_DOMAIN.t) :=
    let '_ :=
      if op_eq v_value zero then
        let '_ := Build_extensible "Division_by_zero" unit tt in
        _rocq_fallback_inv_0
      else
        tt in
    let '(inv_v, gcd) :=
      (bezout_coeffs (_rocq_fallback_arg_0)) v_value
        Mod.(Quotient_field_Mod_signature.modulus) in
    let '(inv_v, rem) := D.(EUCLIDEAN_DOMAIN.div_rem) inv_v gcd in
    let '_ :=
      if D.(EUCLIDEAN_DOMAIN.op_eq) rem D.(EUCLIDEAN_DOMAIN.zero) then tt else
        _rocq_fallback_inv_0 in
    reduce (D.(EUCLIDEAN_DOMAIN.op_star) u_value inv_v).

  Module Quotient_field_signature.
    Record signature `{_fargs : FArgs} : Type := {
      t := D.(EUCLIDEAN_DOMAIN.t);
      op_eq : t → t → bool;
      op_ltgt : t → t → bool;
      zero : t;
      one : t;
      op_tildedollar : int → t;
      op_tildeat : string → t;
      op_plus : t → t → t;
      op_minus : t → t → t;
      op_star : t → t → t;
      op_div :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((D.(EUCLIDEAN_DOMAIN.t)
              × D.(EUCLIDEAN_DOMAIN.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          t → t → t;
      inv :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((D.(EUCLIDEAN_DOMAIN.t)
              × D.(EUCLIDEAN_DOMAIN.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          t → t;
      reduce : D.(EUCLIDEAN_DOMAIN.t) → t;
    }.
  End Quotient_field_signature.
  Definition Quotient_field_signature `{_fargs : FArgs} :=
    @Quotient_field_signature.signature _ _.
  Arguments Quotient_field_signature {_ _}.

  Definition functor `{_fargs : FArgs} :Quotient_field_signature :=
    {|
      Quotient_field_signature.op_eq := op_eq;
      Quotient_field_signature.op_ltgt := op_ltgt;
      Quotient_field_signature.zero := zero;
      Quotient_field_signature.one := one;
      Quotient_field_signature.op_tildedollar := op_tildedollar;
      Quotient_field_signature.op_tildeat := op_tildeat;
      Quotient_field_signature.op_plus := op_plus;
      Quotient_field_signature.op_minus := op_minus;
      Quotient_field_signature.op_star := op_star;
      Quotient_field_signature.op_div _rocq_witness_0 :=
        ltac:(first [unshelve refine (op_div
        _rocq_witness_0) | unshelve refine (op_div
        ) | let value := uconstr:(op_div) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto]);
      Quotient_field_signature.inv _rocq_witness_0 :=
        ltac:(first [unshelve refine (inv
        _rocq_witness_0) | unshelve refine (inv
        ) | let value := uconstr:(inv) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto]);
      Quotient_field_signature.reduce := reduce
    |}.
End Quotient_field.
Definition Quotient_field {D_t : Set}
  (D : EUCLIDEAN_DOMAIN (t := D_t))
  (Mod : (Quotient_field_Mod_signature (D := D))) :=
  @Quotient_field.functor D_t (Quotient_field.Build_FArgs D Mod).

The functor Prime_field constructs the prime field of integers modulo Mod.modulus. As with the Quotient_field functor, primality of Mod.modulus is not checked. Conditionally, if Mod.modulus mod 4 = 3, an efficient implementation of modular square root is also provided.
Inclusion of the module Mod
  Definition modulus `{_fargs : FArgs} : MonadVM.Numeric.Integer.t :=
    Mod.(Prime_field_Mod_signature.modulus).

  Module Impl.
    Definition impl `{_fargs : FArgs} : Set := MonadVM.Numeric.Integer.t.

    Definition t `{_fargs : FArgs} : Set := impl.

    Definition reduce `{_fargs : FArgs} (x_value : impl)
      : MonadVM.Numeric.Integer.t :=
      MonadVM.Numeric.Integer.modulo x_value
        Mod.(Prime_field_Mod_signature.modulus).

    Definition op_plus `{_fargs : FArgs} (u_value : t) (v_value : t) : t :=
      let s_value := MonadVM.Numeric.Integer.op_plus u_value v_value in
      if
        MonadVM.Numeric.Integer.op_gteq s_value
          Mod.(Prime_field_Mod_signature.modulus)
      then
        MonadVM.Numeric.Integer.op_minus s_value
          Mod.(Prime_field_Mod_signature.modulus)
      else
        s_value.

    Definition op_minus `{_fargs : FArgs} (u_value : t) (v_value : t) : t :=
      let s_value := MonadVM.Numeric.Integer.op_minus u_value v_value in
      if
        MonadVM.Numeric.Integer.op_lt s_value MonadVM.Numeric.Integer.zero
      then
        MonadVM.Numeric.Integer.op_plus s_value
          Mod.(Prime_field_Mod_signature.modulus)
      else
        s_value.

    Module Impl_signature.
      Record signature `{_fargs : FArgs} : Type := {
        impl :=
          MonadVM.Numeric.IntegerBase.(MonadVM.Numeric.Make.Make_result.Impl).(MonadVM.Numeric.Make.Impl.Impl_signature.t);
        t :=
          MonadVM.Numeric.IntegerBase.(MonadVM.Numeric.Make.Make_result.Impl).(MonadVM.Numeric.Make.Impl.Impl_signature.t);
        reduce :
          MonadVM.Numeric.IntegerBase.(MonadVM.Numeric.Make.Make_result.Impl).(MonadVM.Numeric.Make.Impl.Impl_signature.t)
          → t;
        op_plus : t → t → t;
        op_minus : t → t → t;
      }.
    End Impl_signature.
    Definition Impl_signature `{_fargs : FArgs} := @Impl_signature.signature _.
    Arguments Impl_signature {_}.

    Definition module `{_fargs : FArgs} :Impl_signature :=
      {|
        Impl_signature.reduce := reduce;
        Impl_signature.op_plus := op_plus;
        Impl_signature.op_minus := op_minus
      |}.
  End Impl.
  Definition Impl `{_fargs : FArgs} := Impl.module.

Inclusion of the module Impl
  Definition impl `{_fargs : FArgs} := Impl.(Impl.Impl_signature.impl).

  Definition t `{_fargs : FArgs} := Impl.(Impl.Impl_signature.t).

  Definition reduce `{_fargs : FArgs} : impl → t :=
    Impl.(Impl.Impl_signature.reduce).

  Definition op_plus `{_fargs : FArgs} : t → t → t :=
    Impl.(Impl.Impl_signature.op_plus).

  Definition op_minus `{_fargs : FArgs} : t → t → t :=
    Impl.(Impl.Impl_signature.op_minus).

  Definition op_eq `{_fargs : FArgs}
    (u_value : Impl.(Impl.Impl_signature.t))
    (v_value : Impl.(Impl.Impl_signature.t)) : bool :=
    MonadVM.Numeric.Integer.op_eq u_value v_value.

  Definition op_ltgt `{_fargs : FArgs}
    (u_value : Impl.(Impl.Impl_signature.t))
    (v_value : Impl.(Impl.Impl_signature.t)) : bool :=
    MonadVM.Numeric.Integer.op_ltgt u_value v_value.

  Definition zero `{_fargs : FArgs} : Impl.(Impl.Impl_signature.t) :=
    reduce MonadVM.Numeric.Integer.zero.

  Definition one `{_fargs : FArgs} : Impl.(Impl.Impl_signature.t) :=
    reduce MonadVM.Numeric.Integer.one.

  Definition op_tildeat `{_fargs : FArgs} (str : string)
    : Impl.(Impl.Impl_signature.t) :=
    reduce (MonadVM.Numeric.Integer.op_tildeat str).

  Definition op_tildedollar `{_fargs : FArgs} (x_value : int)
    : Impl.(Impl.Impl_signature.t) :=
    reduce (MonadVM.Numeric.Integer.op_tildedollar x_value).

  Definition op_star `{_fargs : FArgs}
    (u_value : Impl.(Impl.Impl_signature.t))
    (v_value : Impl.(Impl.Impl_signature.t)) : Impl.(Impl.Impl_signature.t) :=
    reduce (MonadVM.Numeric.Integer.op_star u_value v_value).

  Definition inv `{_fargs : FArgs} (u_value : Impl.(Impl.Impl_signature.t))
    : Impl.(Impl.Impl_signature.t) :=
    reduce
      (MonadVM.Numeric.Integer.of_z_exn
        (RocqOfOCaml.OCamlZ.invert (MonadVM.Numeric.Integer.to_z u_value)
          (MonadVM.Numeric.Integer.to_z modulus))).

  Definition op_div `{_fargs : FArgs}
    (u_value : Impl.(Impl.Impl_signature.t))
    (v_value : Impl.(Impl.Impl_signature.t)) : Impl.(Impl.Impl_signature.t) :=
    let inv_v :=
      MonadVM.Numeric.Integer.of_z_exn
        (RocqOfOCaml.OCamlZ.invert (MonadVM.Numeric.Integer.to_z v_value)
          (MonadVM.Numeric.Integer.to_z modulus)) in
    reduce (MonadVM.Numeric.Integer.op_star u_value inv_v).

of_uint_opt i returns the input i as an element of this prime field if it is already reduced, or None otherwise. Useful for precompile input validation.
  Definition of_uint_opt `{_fargs : FArgs} (i_value : MonadVM.Support.UintN.t)
    : option Impl.(Impl.Impl_signature.t) :=
    let i_value := MonadVM.Support.UintN.as_signed i_value in
    if
      MonadVM.Numeric.Integer.op_gteq i_value
        Mod.(Prime_field_Mod_signature.modulus)
    then
      None
    else
      Some (reduce i_value).

When Mod.modulus mod 4 = 3, sqrt_opt efficiently computes square roots, when they exist.
  Definition sqrt_opt `{_fargs : FArgs}
    : option
      (Impl.(Impl.Impl_signature.t) → option Impl.(Impl.Impl_signature.t)) :=
    if
      MonadVM.Numeric.Integer.op_eq
        (MonadVM.Numeric.Integer.modulo modulus
          (MonadVM.Numeric.Integer.op_tildedollar 4))
        (MonadVM.Numeric.Integer.op_tildedollar 3)
    then
      let sqrt_exp :=
        MonadVM.Numeric.Integer.op_div
          (MonadVM.Numeric.Integer.op_plus modulus MonadVM.Numeric.Integer.one)
          (MonadVM.Numeric.Integer.op_tildedollar 4) in
      Some
        (fun (x_value : Impl.(Impl.Impl_signature.t)) ⇒
          if op_eq x_value zero then
            Some zero
          else
            if Z.eqb (MonadVM.Numeric.Integer.legendre x_value modulus) 1 then
              Some
                (reduce
                  ((fun x_1 ⇒ MonadVM.Numeric.Integer.exp_mod x_1 x_value)
                    modulus sqrt_exp))
            else
              None)
    else
      None.

  Module Prime_field_result.
    Record signature `{_fargs : FArgs} : Type := {
      modulus : MonadVM.Numeric.Integer.t;
      Impl : Prime_field.Impl.Impl_signature;
      impl :=
        MonadVM.Numeric.IntegerBase.(MonadVM.Numeric.Make.Make_result.Impl).(MonadVM.Numeric.Make.Impl.Impl_signature.t);
      t := impl;
      reduce :
        MonadVM.Numeric.IntegerBase.(MonadVM.Numeric.Make.Make_result.Impl).(MonadVM.Numeric.Make.Impl.Impl_signature.t)
        → impl;
      op_plus : impl → impl → impl;
      op_minus : impl → impl → impl;
      op_eq : impl → impl → bool;
      op_ltgt : impl → impl → bool;
      zero : impl;
      one : impl;
      op_tildeat : string → impl;
      op_tildedollar : int → impl;
      op_star : impl → impl → impl;
      inv : impl → impl;
      op_div : impl → impl → impl;
      of_uint_opt : MonadVM.Support.UintN.t → option impl;
      sqrt_opt : option (impl → option impl);
    }.
  End Prime_field_result.
  Definition Prime_field_result `{_fargs : FArgs} :=
    @Prime_field_result.signature _.
  Arguments Prime_field_result {_}.

  Definition functor `{_fargs : FArgs} :@Prime_field_result _fargs :=
    ((@Prime_field_result.Build_signature _fargs modulus Impl reduce op_plus
      op_minus op_eq op_ltgt zero one op_tildeat op_tildedollar op_star inv
      op_div of_uint_opt sqrt_opt) : @Prime_field_result _fargs).
End Prime_field.
Definition Prime_field (Mod : Prime_field_Mod_signature) :=
  @Prime_field.functor (Prime_field.Build_FArgs Mod).

The functor Polynomial_ring constructs the Euclidean domain of polynomials over an underlying field.
Module Polynomial_ring.
  Class FArgs {F_t : Set} := {
    F : FIELD (t := F_t);
  }.
  Arguments Build_FArgs {_}.

  Module Impl.
    Definition impl `{_fargs : FArgs} : Set :=
      RocqOfOCaml.OCamlIarray.t F.(FIELD.t).

    Definition t `{_fargs : FArgs} : Set := impl.

    Definition _rocq_fallback_of_coeffs_0 `{_fargs : FArgs} (coeffs : impl) : ltac:(let typ := constr:(ltac:(first [unshelve refine ((impl)%type )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
    Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.

    Definition of_coeffs `{_fargs : FArgs}
      (_rocq_fallback_arg_0 :
        ltac:(unshelve refine ((F.(FIELD.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
      (coeffs : impl) : impl :=
      let '(degree, _) :=
        RocqOfOCaml.OCamlIarray.fold_left
          (fun (function_parameter : int × int) ⇒
            let '(degree, i_value) := function_parameter in
            fun (c_value : F.(FIELD.t)) ⇒
              ((if F.(FIELD.op_eq) c_value F.(FIELD.zero) then
                degree
              else
                i_value), (RocqOfOCaml.OCamlInt.add i_value 1))) ((-1), 0)
          coeffs in
      if
        Z.eqb degree
          (RocqOfOCaml.OCamlInt.sub (RocqOfOCaml.OCamlIarray.length coeffs)
            1)
      then
        coeffs
      else
        (RocqOfOCaml.OCamlIarray.init
          (H := RocqOfOCaml.Opaque.legacy_witness (_rocq_fallback_of_coeffs_0 coeffs)))
          (RocqOfOCaml.OCamlInt.add degree 1)
          (fun (i_value : int) ⇒
            (RocqOfOCaml.OCamlIarray.get
              (H := RocqOfOCaml.Opaque.legacy_witness _rocq_fallback_arg_0))
              coeffs i_value).

    Module Impl_signature.
      Record signature `{_fargs : FArgs} : Type := {
        impl := RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
        t := impl;
        of_coeffs :
          ∀
            (_rocq_fallback_arg_0 :
              ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
              )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
            impl → t;
      }.
    End Impl_signature.
    Definition Impl_signature `{_fargs : FArgs} := @Impl_signature.signature _
      _.
    Arguments Impl_signature {_ _}.

    Definition module `{_fargs : FArgs} :Impl_signature :=
      {|
        Impl_signature.of_coeffs _rocq_witness_0 :=
          ltac:(first [unshelve refine (of_coeffs
          _rocq_witness_0) | unshelve refine (of_coeffs
          ) | let value := uconstr:(of_coeffs) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
          _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
          _rocq_witness_0 | eassumption | typeclasses eauto])
      |}.
  End Impl.
  Definition Impl `{_fargs : FArgs} := Impl.module.

Inclusion of the module Impl
  Definition impl `{_fargs : FArgs} := Impl.(Impl.Impl_signature.impl).

  Definition t `{_fargs : FArgs} := Impl.(Impl.Impl_signature.t).

  Definition of_coeffs `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
      )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ))
    : impl → Impl.(Impl.Impl_signature.t) :=
    Impl.(Impl.Impl_signature.of_coeffs) (_rocq_fallback_arg_0).

Number of coefficients in the polynomial p.
Degree of the polynomial p. By convention, zero is a -1-degree polynomial.
  Definition degree `{_fargs : FArgs}
    (p_value : Impl.(Impl.Impl_signature.impl)) : int :=
    RocqOfOCaml.OCamlInt.sub (length p_value) 1.

  Definition _rocq_fallback_init_0 `{_fargs : FArgs} : ltac:(let typ := constr:(ltac:(first [unshelve refine ((impl)%type )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
  Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.

  Definition init `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((F.(FIELD.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
    (length : int) (p_i : int → F.(FIELD.t))
    : Impl.(Impl.Impl_signature.impl) :=
    (of_coeffs (_rocq_fallback_arg_0))
      ((RocqOfOCaml.OCamlIarray.init
        (H := RocqOfOCaml.Opaque.legacy_witness _rocq_fallback_init_0)) length
        p_i).

p.$(i) returns the i-th coefficient of the polynomial p, or zero if i ≥ length p.
  Definition op_pointdollarlparenrparen `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((F.(FIELD.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
    (p_value : Impl.(Impl.Impl_signature.impl)) (i_value : int) : F.(FIELD.t) :=
    if Z.ltb i_value (length p_value) then
      (RocqOfOCaml.OCamlIarray.get
        (H := RocqOfOCaml.Opaque.legacy_witness _rocq_fallback_arg_0)) p_value
        i_value
    else
      F.(FIELD.zero).

  Definition _rocq_fallback_op_ltgt_0 `{_fargs : FArgs} : ltac:(let typ := constr:(ltac:(first [unshelve refine ((bool)%type )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
  Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.

  Definition op_ltgt `{_fargs : FArgs}
    (p_1 : Impl.(Impl.Impl_signature.impl))
    (p_2 : Impl.(Impl.Impl_signature.impl)) : bool :=
    orb (RocqOfOCaml.OCamlInt.not_equal (length p_1) (length p_2))
      ((RocqOfOCaml.OCamlIarray._exists2
        (H := RocqOfOCaml.Opaque.legacy_witness _rocq_fallback_op_ltgt_0))
        F.(FIELD.op_ltgt) p_1 p_2).

  Definition op_eq `{_fargs : FArgs}
    (p_1 : Impl.(Impl.Impl_signature.impl))
    (p_2 : Impl.(Impl.Impl_signature.impl)) : bool := negb (op_ltgt p_1 p_2).

  Definition zero `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((F.(FIELD.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat)) : Impl.(Impl.Impl_signature.impl) :=
    (init (_rocq_fallback_arg_0)) 0
      (fun (function_parameter : int) ⇒
        let '_ := function_parameter in
        F.(FIELD.zero)).

  Definition one `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((F.(FIELD.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat)) : Impl.(Impl.Impl_signature.impl) :=
    (init (_rocq_fallback_arg_0)) 1
      (fun (function_parameter : int) ⇒
        let '_ := function_parameter in
        F.(FIELD.one)).

  Definition op_tildeat `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((F.(FIELD.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
    (i_value : MonadVM.String.t) : Impl.(Impl.Impl_signature.impl) :=
    (init (_rocq_fallback_arg_0)) 1
      (fun (function_parameter : int) ⇒
        let '_ := function_parameter in
        F.(FIELD.op_tildeat) i_value).

  Definition op_tildedollar `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((F.(FIELD.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
    (i_value : int) : Impl.(Impl.Impl_signature.impl) :=
    (init (_rocq_fallback_arg_0)) 1
      (fun (function_parameter : int) ⇒
        let '_ := function_parameter in
        F.(FIELD.op_tildedollar) i_value).

monomial_x represents the degree-1 monomial x.
  Definition monomial_x `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((F.(FIELD.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat)) : Impl.(Impl.Impl_signature.impl) :=
    (init (_rocq_fallback_arg_0)) 2
      (fun (i_value : int) ⇒
        if Z.eqb i_value 1 then
          F.(FIELD.one)
        else
          F.(FIELD.zero)).

  Definition op_plus `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((F.(FIELD.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
    (p_1 : Impl.(Impl.Impl_signature.impl))
    (p_2 : Impl.(Impl.Impl_signature.impl)) : Impl.(Impl.Impl_signature.impl) :=
    (init (_rocq_fallback_arg_0)) (Z.max (length p_1) (length p_2))
      (fun (i_value : int) ⇒
        F.(FIELD.op_plus)
          ((op_pointdollarlparenrparen (_rocq_fallback_arg_0)) p_1 i_value)
          ((op_pointdollarlparenrparen (_rocq_fallback_arg_0)) p_2 i_value)).

  Definition op_minus `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((F.(FIELD.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
    (p_1 : Impl.(Impl.Impl_signature.impl))
    (p_2 : Impl.(Impl.Impl_signature.impl)) : Impl.(Impl.Impl_signature.impl) :=
    (init (_rocq_fallback_arg_0)) (Z.max (length p_1) (length p_2))
      (fun (i_value : int) ⇒
        F.(FIELD.op_minus)
          ((op_pointdollarlparenrparen (_rocq_fallback_arg_0)) p_1 i_value)
          ((op_pointdollarlparenrparen (_rocq_fallback_arg_0)) p_2 i_value)).

  Program Definition op_star `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((F.(FIELD.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
    (p_1 : Impl.(Impl.Impl_signature.impl))
    (p_2 : Impl.(Impl.Impl_signature.impl)) : Impl.(Impl.Impl_signature.impl) :=
    let len_1 := length p_1 in
    let len_2 := length p_2 in
    if orb (Z.eqb len_1 0) (Z.eqb len_2 0) then
      (zero (_rocq_fallback_arg_0))
    else
      (init (_rocq_fallback_arg_0))
        (RocqOfOCaml.OCamlInt.sub (RocqOfOCaml.OCamlInt.add len_1 len_2) 1)
        (fun (i_value : int) ⇒
          let _rocq_measure (_rocq_state : int × F.(FIELD.t)) : nat :=
            let '(j_value, _) := _rocq_state in MonadExecutionSpecs.Termination.IntegerLoops.countdown_rank j_value
            in
          let _rocq_body :
            (∀ _rocq_state : int × F.(FIELD.t),
              (∀ _rocq_next : int × F.(FIELD.t), ltof (int × F.(FIELD.t))
                _rocq_measure _rocq_next _rocq_state → F.(FIELD.t)) →
              F.(FIELD.t)) :=
            (fun _rocq_state _rocq_recurse ⇒
              let '(j_value, acc) := _rocq_state in
              let k_value := RocqOfOCaml.OCamlInt.sub i_value j_value in
              match orb (Z.ltb j_value 0) (Z.geb k_value len_2) as _rocq_guard
                return (orb (Z.ltb j_value 0) (Z.geb k_value len_2) =
                _rocq_guard → _) with
              | true ⇒ fun _rocq_guard_eq ⇒ acc
              | false ⇒ fun _rocq_guard_eq ⇒
                _rocq_recurse
                  ((RocqOfOCaml.OCamlInt.sub j_value 1),
                    (F.(FIELD.op_plus) acc
                      (F.(FIELD.op_star)
                        ((op_pointdollarlparenrparen (_rocq_fallback_arg_0)) p_1
                          j_value)
                        ((op_pointdollarlparenrparen (_rocq_fallback_arg_0)) p_2
                          k_value))))
                  ltac:(MonadExecutionSpecs.Termination.IntegerLoops.solve_countdown_decrease)
              end eq_refl) in
          let _rocq_fix := @Fix (int × F.(FIELD.t)) (ltof (int × F.(FIELD.t))
            _rocq_measure) (well_founded_ltof (int × F.(FIELD.t)) _rocq_measure)
            (fun _ ⇒ F.(FIELD.t)) _rocq_body in
          let loop (j_value : int) (acc : F.(FIELD.t)) : F.(FIELD.t) :=
            _rocq_fix (j_value, acc) in
          loop (Z.min i_value (RocqOfOCaml.OCamlInt.sub len_1 1)) F.(FIELD.zero)).

  Definition const `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((F.(FIELD.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
    (a_value : F.(FIELD.t)) : Impl.(Impl.Impl_signature.impl) :=
    (init (_rocq_fallback_arg_0)) 1
      (fun (function_parameter : int) ⇒
        let '_ := function_parameter in
        a_value).

monomial a n computes the monomial ax^n.
  Definition monomial `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((F.(FIELD.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
    (a_value : F.(FIELD.t)) (n_value : int) : Impl.(Impl.Impl_signature.impl) :=
    (init (_rocq_fallback_arg_0)) (RocqOfOCaml.OCamlInt.add n_value 1)
      (fun (i_value : int) ⇒
        if Z.eqb i_value n_value then
          a_value
        else
          F.(FIELD.zero)).

  Definition _rocq_fallback_div_rem_0 `{_fargs : FArgs} : ltac:(let typ := constr:(ltac:(first [unshelve refine ((impl
    × impl)%type )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
  Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.

  Program Definition div_rem `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine ((F.(FIELD.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
    (u_value : Impl.(Impl.Impl_signature.impl))
    (v_value : Impl.(Impl.Impl_signature.impl))
    : Impl.(Impl.Impl_signature.impl) × Impl.(Impl.Impl_signature.impl) :=
    let n_value := degree v_value in
    let v_n_inv :=
      F.(FIELD.inv)
        ((op_pointdollarlparenrparen (_rocq_fallback_arg_0)) v_value
          (degree v_value)) in
    let _rocq_measure (_rocq_state :
      Impl.(Impl.Impl_signature.impl) × Impl.(Impl.Impl_signature.impl)) : nat
      :=
      let '(dividend, _) := _rocq_state in MonadExecutionSpecs.Termination.Ec.degree_rank (degree dividend)
      in
    let _rocq_body :
      (∀ _rocq_state :
        Impl.(Impl.Impl_signature.impl) × Impl.(Impl.Impl_signature.impl),
        (∀ _rocq_next :
          Impl.(Impl.Impl_signature.impl) × Impl.(Impl.Impl_signature.impl),
          ltof
          (Impl.(Impl.Impl_signature.impl) × Impl.(Impl.Impl_signature.impl))
          _rocq_measure _rocq_next _rocq_state →
          Impl.(Impl.Impl_signature.impl) × Impl.(Impl.Impl_signature.impl)) →
        Impl.(Impl.Impl_signature.impl) × Impl.(Impl.Impl_signature.impl)) :=
      (fun _rocq_state _rocq_recurse ⇒
        let '(u_value, quot_acc) := _rocq_state in
        let m_value := degree u_value in
        match Z.ltb m_value n_value as _rocq_guard return (Z.ltb m_value n_value
          = _rocq_guard → _) with
        | true ⇒ fun _rocq_guard_eq ⇒ (quot_acc, u_value)
        | false ⇒ fun _rocq_guard_eq ⇒
          let u_m :=
            (op_pointdollarlparenrparen (_rocq_fallback_arg_0)) u_value
              (degree u_value) in
          let q_value :=
            (monomial (_rocq_fallback_arg_0)) (F.(FIELD.op_star) u_m v_n_inv)
              (RocqOfOCaml.OCamlInt.sub m_value n_value) in
          let next_u :=
            (op_minus (_rocq_fallback_arg_0)) u_value
              ((op_star (_rocq_fallback_arg_0)) q_value v_value) in
          match andb (Z.geb m_value 0) (Z.ltb (degree next_u) m_value) as
            _rocq_guard return
            (andb (Z.geb m_value 0) (Z.ltb (degree next_u) m_value) =
            _rocq_guard → _) with
          | true ⇒ fun _rocq_guard_eq ⇒
            _rocq_recurse
              (next_u, ((op_plus (_rocq_fallback_arg_0)) quot_acc q_value))
              ltac:(MonadExecutionSpecs.Termination.Ec.solve_guarded_degree_decrease)
          | false ⇒ fun _rocq_guard_eq ⇒
            let '_ := "Polynomial_ring.div_rem: non-decreasing degree" in
            _rocq_fallback_div_rem_0
          end eq_refl
        end eq_refl) in
    let _rocq_fix := @Fix
      (Impl.(Impl.Impl_signature.impl) × Impl.(Impl.Impl_signature.impl)) (ltof
      (Impl.(Impl.Impl_signature.impl) × Impl.(Impl.Impl_signature.impl))
      _rocq_measure) (well_founded_ltof
      (Impl.(Impl.Impl_signature.impl) × Impl.(Impl.Impl_signature.impl))
      _rocq_measure) (fun _ ⇒
      Impl.(Impl.Impl_signature.impl) × Impl.(Impl.Impl_signature.impl))
      _rocq_body in
    let loop (u_value : Impl.(Impl.Impl_signature.impl))
      (quot_acc : Impl.(Impl.Impl_signature.impl)) :
      Impl.(Impl.Impl_signature.impl) × Impl.(Impl.Impl_signature.impl) :=
      _rocq_fix (u_value, quot_acc) in
    loop u_value ((zero (_rocq_fallback_arg_0))).

eval p x evaluates p at point x efficiently via Horner's rule.
  Definition eval `{_fargs : FArgs}
    (p_value : Impl.(Impl.Impl_signature.impl)) (x_value : F.(FIELD.t))
    : F.(FIELD.t) :=
    RocqOfOCaml.OCamlIarray.fold_right
      (fun (coeff : F.(FIELD.t)) ⇒
        fun (acc : F.(FIELD.t)) ⇒
          F.(FIELD.op_plus) coeff (F.(FIELD.op_star) x_value acc)) p_value
      F.(FIELD.zero).

  Module Polynomial_ring_result.
    Record signature `{_fargs : FArgs} : Type := {
      Impl : Polynomial_ring.Impl.Impl_signature;
      impl := RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
      t := impl;
      of_coeffs :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t) →
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
      length : RocqOfOCaml.OCamlIarray.t F.(FIELD.t) → int;
      degree : RocqOfOCaml.OCamlIarray.t F.(FIELD.t) → int;
      init :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          int → (int → F.(FIELD.t)) → RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
      op_pointdollarlparenrparen :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t) → int → F.(FIELD.t);
      op_ltgt :
        RocqOfOCaml.OCamlIarray.t F.(FIELD.t) →
        RocqOfOCaml.OCamlIarray.t F.(FIELD.t) → bool;
      op_eq :
        RocqOfOCaml.OCamlIarray.t F.(FIELD.t) →
        RocqOfOCaml.OCamlIarray.t F.(FIELD.t) → bool;
      zero :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
      one :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
      op_tildeat :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          MonadVM.String.t → RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
      op_tildedollar :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          int → RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
      monomial_x :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
      op_plus :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t) →
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t) →
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
      op_minus :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t) →
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t) →
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
      op_star :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t) →
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t) →
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
      const :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          F.(FIELD.t) → RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
      monomial :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          F.(FIELD.t) → int → RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
      div_rem :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t) →
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t) →
          RocqOfOCaml.OCamlIarray.t F.(FIELD.t) ×
            RocqOfOCaml.OCamlIarray.t F.(FIELD.t);
      eval :
        RocqOfOCaml.OCamlIarray.t F.(FIELD.t) → F.(FIELD.t) → F.(FIELD.t);
    }.
  End Polynomial_ring_result.
  Definition Polynomial_ring_result `{_fargs : FArgs} :=
    @Polynomial_ring_result.signature _ _.
  Arguments Polynomial_ring_result {_ _}.

  Definition functor `{_fargs : FArgs} :@Polynomial_ring_result F_t _fargs :=
    ((@Polynomial_ring_result.Build_signature F_t _fargs Impl
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (of_coeffs
        _rocq_witness_0) | unshelve refine (of_coeffs
        ) | let value := uconstr:(of_coeffs) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto])) length degree
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (init
        _rocq_witness_0) | unshelve refine (init
        ) | let value := uconstr:(init) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto]))
      (fun _rocq_witness_0 ⇒
        ltac:(first [unshelve refine (op_pointdollarlparenrparen
        _rocq_witness_0) | unshelve refine (op_pointdollarlparenrparen
        ) | let value := uconstr:(op_pointdollarlparenrparen) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto])) op_ltgt op_eq
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (zero
        _rocq_witness_0) | unshelve refine (zero
        ) | let value := uconstr:(zero) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto]))
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (one
        _rocq_witness_0) | unshelve refine (one
        ) | let value := uconstr:(one) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto]))
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (op_tildeat
        _rocq_witness_0) | unshelve refine (op_tildeat
        ) | let value := uconstr:(op_tildeat) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto]))
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (op_tildedollar
        _rocq_witness_0) | unshelve refine (op_tildedollar
        ) | let value := uconstr:(op_tildedollar) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto]))
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (monomial_x
        _rocq_witness_0) | unshelve refine (monomial_x
        ) | let value := uconstr:(monomial_x) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto]))
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (op_plus
        _rocq_witness_0) | unshelve refine (op_plus
        ) | let value := uconstr:(op_plus) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto]))
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (op_minus
        _rocq_witness_0) | unshelve refine (op_minus
        ) | let value := uconstr:(op_minus) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto]))
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (op_star
        _rocq_witness_0) | unshelve refine (op_star
        ) | let value := uconstr:(op_star) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto]))
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (const
        _rocq_witness_0) | unshelve refine (const
        ) | let value := uconstr:(const) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto]))
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (monomial
        _rocq_witness_0) | unshelve refine (monomial
        ) | let value := uconstr:(monomial) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto]))
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (div_rem
        _rocq_witness_0) | unshelve refine (div_rem
        ) | let value := uconstr:(div_rem) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto])) eval) :
      @Polynomial_ring_result F_t _fargs).
End Polynomial_ring.
Definition Polynomial_ring {F_t : Set} (F : FIELD (t := F_t)) :=
  @Polynomial_ring.functor F_t (Polynomial_ring.Build_FArgs F).

For a field of scalars F, the functor Polynomial_extension(F)(Mod) constructs the field of polynomials over F by quotienting Polynomial_ring(F) over the element Mod.modulus, which is assumed to be irreducible.
Modular reduction is done via the Euclidean method implemented in Quotient_field. This could be specialized to perform reduction via iterated multiplication and subtraction, but it's hard to do functionally. Avoid until it proves a bottleneck.
Module Polynomial_extension_Mod_signature.
  Record signature {F_t : Set} {F : FIELD (t := F_t)} : Type := {
    modulus : (Polynomial_ring F).(Polynomial_ring.Polynomial_ring_result.t);
  }.
End Polynomial_extension_Mod_signature.
Definition Polynomial_extension_Mod_signature {F_t : Set} {F : FIELD (t := F_t)}
  := @Polynomial_extension_Mod_signature.signature F_t F.

Module Polynomial_extension.
  Class FArgs {F_t : Set} := {
    F : FIELD (t := F_t);
    Mod : (Polynomial_extension_Mod_signature (F := F));
    _rocq_projection_assumption_0 :
      ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
      )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ);
  }.
  Arguments Build_FArgs {_} _ _ {_}.

  Definition _rocq_projection_assumption_0_instance `{_fargs : FArgs} :=
    _rocq_projection_assumption_0.



Inclusion of the module Mod
  Definition modulus `{_fargs : FArgs} :
    (Polynomial_ring F).(Polynomial_ring.Polynomial_ring_result.t) :=
    Mod.(Polynomial_extension_Mod_signature.modulus).

  Definition Underlying_ring_fargs `{_fargs : FArgs} :=
    ltac:(first [unshelve refine (Polynomial_ring.Build_FArgs F
    )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
    _rocq_projection_assumption_0 | eassumption | typeclasses eauto]).

  Definition Underlying_ring `{_fargs : FArgs} :
    Polynomial_ring.Polynomial_ring_result (_fargs := Underlying_ring_fargs) :=
    ltac:(first [unshelve refine ((Polynomial_ring.functor
    (_fargs := (Underlying_ring_fargs )))
    )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
    _rocq_projection_assumption_0 | eassumption | typeclasses eauto]).

  Definition Quotient_field_include `{_fargs : FArgs} :=
    ltac:(first [unshelve refine (Quotient_field ltac:(refine (_ :
      EUCLIDEAN_DOMAIN
        (t :=
          (Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.t))); constructor; [refine ((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.op_eq)) | refine ((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.op_ltgt)) | refine (((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.zero)
        (_rocq_projection_assumption_0))) | refine (((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.one)
        (_rocq_projection_assumption_0))) | refine (((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.op_tildeat)
        (_rocq_projection_assumption_0))) | refine (((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.op_tildedollar)
        (_rocq_projection_assumption_0))) | refine ((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.degree)) | refine (((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.op_plus)
        (_rocq_projection_assumption_0))) | refine (((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.op_minus)
        (_rocq_projection_assumption_0))) | refine (((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.op_star)
        (_rocq_projection_assumption_0))) | refine (((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.div_rem)
        (_rocq_projection_assumption_0)))]; shelve) ltac:(refine (_ :
      Quotient_field_Mod_signature); constructor; [refine (Mod.(Polynomial_extension_Mod_signature.modulus))]; shelve)
    )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
    _rocq_projection_assumption_0 | eassumption | typeclasses eauto]).

Inclusion of the module Quotient_field_include
  Definition t `{_fargs : FArgs} := RocqOfOCaml.OCamlIarray.t F.(FIELD.t).

  Definition op_eq `{_fargs : FArgs} : t → t → bool :=
    Quotient_field_include.(Quotient_field.Quotient_field_signature.op_eq).

  Definition op_ltgt `{_fargs : FArgs} : t → t → bool :=
    Quotient_field_include.(Quotient_field.Quotient_field_signature.op_ltgt).

  Definition zero `{_fargs : FArgs} : t :=
    Quotient_field_include.(Quotient_field.Quotient_field_signature.zero).

  Definition one `{_fargs : FArgs} : t :=
    Quotient_field_include.(Quotient_field.Quotient_field_signature.one).

  Definition op_tildedollar `{_fargs : FArgs} : int → t :=
    Quotient_field_include.(Quotient_field.Quotient_field_signature.op_tildedollar).

  Definition op_tildeat `{_fargs : FArgs} : MonadVM.String.t → t :=
    Quotient_field_include.(Quotient_field.Quotient_field_signature.op_tildeat).

  Definition op_plus `{_fargs : FArgs} : t → t → t :=
    Quotient_field_include.(Quotient_field.Quotient_field_signature.op_plus).

  Definition op_minus `{_fargs : FArgs} : t → t → t :=
    Quotient_field_include.(Quotient_field.Quotient_field_signature.op_minus).

  Definition op_star `{_fargs : FArgs} : t → t → t :=
    Quotient_field_include.(Quotient_field.Quotient_field_signature.op_star).

  Definition op_div `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(let typ := constr:(ltac:(first [unshelve refine ((t × t)%type
      )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ))
    : t → t → t :=
    Quotient_field_include.(Quotient_field.Quotient_field_signature.op_div)
      (_rocq_fallback_arg_0).

  Definition inv `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(let typ := constr:(ltac:(first [unshelve refine ((t × t)%type
      )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ))
    : t → t :=
    Quotient_field_include.(Quotient_field.Quotient_field_signature.inv)
      (_rocq_fallback_arg_0).

  Definition reduce `{_fargs : FArgs} :
    Underlying_ring.(Polynomial_ring.Polynomial_ring_result.t) → t :=
    Quotient_field_include.(Quotient_field.Quotient_field_signature.reduce).

  Definition monomial_x `{_fargs : FArgs} : t :=
    reduce
      (((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.monomial_x)
        (_rocq_projection_assumption_0))).

  Definition const `{_fargs : FArgs} (p_value : F.(FIELD.t)) : t :=
    reduce
      (((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.const)
        (_rocq_projection_assumption_0)) p_value).

  Definition op_pointdollarlparenrparen `{_fargs : FArgs}
    (p_value : t) (i_value : int) : F.(FIELD.t) :=
    ((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.op_pointdollarlparenrparen)
      (_rocq_projection_assumption_0)) p_value i_value.

Degree of this extension over F. Elements have at most this many coefficients.
Given a function f : t → t which is assumed to be an automorphism, automorphism f constructs a potentially more efficient implementation by precomputing the table f(1), f(x), f(x²), ... ahead of time. For a polynomial aₙxⁿ + ... + a₀x⁰, automorphism f computes aₙf(xⁿ) + ... + a₀f(x⁰) from the precomputed terms f(xᵏ).
  Definition _rocq_fallback_automorphism_0 `{_fargs : FArgs} :
    ltac:(let typ := constr:(ltac:(first [unshelve refine ((RocqOfOCaml.OCamlSeq.t
      t)%type
    )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
  Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.

  Definition _rocq_fallback_automorphism_1 `{_fargs : FArgs} :
    ltac:(let typ := constr:(ltac:(first [unshelve refine ((RocqOfOCaml.OCamlSeq.t
      t → RocqOfOCaml.OCamlSeq.t t)%type
    )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
    _rocq_fallback_automorphism_0 | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
  Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.

  Definition automorphism `{_fargs : FArgs}
    (_rocq_fallback_arg_0 :
      ltac:(unshelve refine (((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.t))%type); RocqOfOCaml.Opaque.opaque_inhabit 64%nat))
    (f_value : t → t) : t → t :=
    let f_x := f_value monomial_x in
    let powers :=
      RocqOfOCaml.OCamlIarray.of_seq
        ((RocqOfOCaml.OCamlSeq.iterate_take
          (H := RocqOfOCaml.Opaque.legacy_witness _rocq_fallback_automorphism_0))
          extension_degree (fun (f_x_i : t) ⇒ op_star f_x f_x_i) (one)) in
    fun (p_value : t) ⇒
      reduce
        (RocqOfOCaml.OCamlIarray.fold_left
          (((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.op_plus)
            (_rocq_projection_assumption_0)))
          (((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.zero)
            (_rocq_projection_assumption_0)))
          (RocqOfOCaml.OCamlIarray.mapi
            (fun (i_value : int) ⇒
              fun (p_i : F.(FIELD.t)) ⇒
                ((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.op_star)
                  (_rocq_projection_assumption_0))
                  ((RocqOfOCaml.OCamlIarray.get
                    (H := RocqOfOCaml.Opaque.legacy_witness _rocq_fallback_arg_0))
                    powers i_value)
                  (((Underlying_ring (_fargs := _fargs)).(Polynomial_ring.Polynomial_ring_result.const)
                    (_rocq_projection_assumption_0)) p_i)) p_value)).

Power function, used for computing Frobenius automorphisms.
  Program Definition op_starstar `{_fargs : FArgs}
    (p_value : t) (n_value : MonadVM.Support.UintN.t) : t :=
    let _rocq_measure (_rocq_state : MonadVM.Support.UintN.Impl.t × t × t) : nat
      :=
      let '(n_value, _, _) := _rocq_state in MonadExecutionSpecs.Termination.Natural.n_rank n_value
      in
    let _rocq_body :
      (∀ _rocq_state : MonadVM.Support.UintN.Impl.t × t × t,
        (∀ _rocq_next : MonadVM.Support.UintN.Impl.t × t × t, ltof
          (MonadVM.Support.UintN.Impl.t × t × t) _rocq_measure _rocq_next
          _rocq_state → t) → t) :=
      (fun _rocq_state _rocq_recurse ⇒
        let '(n_value, p_value, acc) := _rocq_state in
        match MonadVM.Support.UintN.op_eq n_value MonadVM.Support.UintN.zero as
          _rocq_guard return
          (MonadVM.Support.UintN.op_eq n_value MonadVM.Support.UintN.zero =
          _rocq_guard → _) with
        | true ⇒ fun _rocq_guard_eq ⇒ acc
        | false ⇒ fun _rocq_guard_eq ⇒
          let _rocq_match_scrutinee :=
            MonadVM.Support.UintN.div_rem n_value
              (MonadVM.Support.UintN.op_tildedollar 2) in
          match _rocq_match_scrutinee as _rocq_match_view return
            (_rocq_match_scrutinee = _rocq_match_view → _) with
          | (n_value, r_value) ⇒ fun _rocq_match_eq ⇒
            let acc :=
              match
                MonadVM.Support.UintN.op_eq r_value MonadVM.Support.UintN.one as
                _rocq_guard return
                (MonadVM.Support.UintN.op_eq r_value MonadVM.Support.UintN.one =
                _rocq_guard → _) with
              | true ⇒ fun _rocq_guard_eq ⇒ op_star acc p_value
              | false ⇒ fun _rocq_guard_eq ⇒ acc
              end eq_refl in
            _rocq_recurse (n_value, (op_star p_value p_value), acc)
              ltac:(MonadExecutionSpecs.Termination.Natural.solve_uint_halving_decrease)
          end eq_refl
        end eq_refl) in
    let _rocq_fix := @Fix (MonadVM.Support.UintN.Impl.t × t × t) (ltof
      (MonadVM.Support.UintN.Impl.t × t × t) _rocq_measure) (well_founded_ltof
      (MonadVM.Support.UintN.Impl.t × t × t) _rocq_measure) (fun _ ⇒ t)
      _rocq_body in
    let loop (n_value : MonadVM.Support.UintN.Impl.t) (p_value : t) (acc : t) :
      t := _rocq_fix (n_value, p_value, acc) in
    loop n_value p_value (one).

  Module Polynomial_extension_result.
    Record signature `{_fargs : FArgs} : Type := {
      modulus : (Polynomial_ring F).(Polynomial_ring.Polynomial_ring_result.t);
      Underlying_ring :
        Polynomial_ring.Polynomial_ring_result (_fargs := Underlying_ring_fargs);
      t := t;
      op_eq : t → t → bool;
      op_ltgt : t → t → bool;
      zero : t;
      one : t;
      op_tildedollar : int → t;
      op_tildeat : MonadVM.String.t → t;
      op_plus : t → t → t;
      op_minus : t → t → t;
      op_star : t → t → t;
      op_div :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((t × t)%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          t → t → t;
      inv :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((t × t)%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          t → t;
      reduce : Underlying_ring.(Polynomial_ring.Polynomial_ring_result.t) → t;
      monomial_x : t;
      const : F.(FIELD.t) → t;
      op_pointdollarlparenrparen : t → int → F.(FIELD.t);
      extension_degree : int;
      automorphism :
        ∀
          (_rocq_fallback_arg_0 :
            ltac:(let typ := constr:(ltac:(first [unshelve refine ((Underlying_ring.(Polynomial_ring.Polynomial_ring_result.t))%type
            )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)),
          (t → t) → t → t;
      op_starstar : t → MonadVM.Support.UintN.t → t;
    }.
  End Polynomial_extension_result.
  Definition Polynomial_extension_result `{_fargs : FArgs} :=
    @Polynomial_extension_result.signature _ _.
  Arguments Polynomial_extension_result {_ _}.

  Definition functor `{_fargs : FArgs} :@Polynomial_extension_result F_t _fargs
    :=
    ((@Polynomial_extension_result.Build_signature F_t _fargs modulus
      Underlying_ring op_eq op_ltgt zero one op_tildedollar op_tildeat op_plus
      op_minus op_star
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (op_div
        _rocq_witness_0) | unshelve refine (op_div
        ) | let value := uconstr:(op_div) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto]))
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (inv
        _rocq_witness_0) | unshelve refine (inv
        ) | let value := uconstr:(inv) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto])) reduce monomial_x
      const op_pointdollarlparenrparen extension_degree
      (fun _rocq_witness_0 ⇒ ltac:(first [unshelve refine (automorphism
        _rocq_witness_0) | unshelve refine (automorphism
        ) | let value := uconstr:(automorphism) in RocqOfOCaml.Opaque.supply_witnesses value ltac:(first [exact
        _rocq_witness_0 | eassumption | typeclasses eauto])]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
        _rocq_witness_0 | eassumption | typeclasses eauto])) op_starstar) :
      @Polynomial_extension_result F_t _fargs).
End Polynomial_extension.
Definition Polynomial_extension {F_t : Set}
  (F : FIELD (t := F_t)) (Mod : (Polynomial_extension_Mod_signature (F := F)))
  {_rocq_projection_assumption_0 :
    ltac:(let typ := constr:(ltac:(first [unshelve refine ((F.(FIELD.t))%type
    )]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ)}
  :=
  @Polynomial_extension.functor F_t
    (Polynomial_extension.Build_FArgs F Mod (_rocq_projection_assumption_0 :=
      _rocq_projection_assumption_0)).

The functor Complex_extension defines the complex extension of a field F. This is mathematically equivalent to forming a polynomial extension over the polynomial x² + 1, but more efficient.
Module Complex_extension.
  Class FArgs {F_t : Set} := {
    F : FIELD (t := F_t);
  }.
  Arguments Build_FArgs {_}.

  Module t.
    Record record `{_fargs : FArgs} : Set := Build {
      re : F.(FIELD.t);
      im : F.(FIELD.t);
    }.
    Definition with_re `{_fargs : FArgs} re (r : record) :=
      Build _ _ re r.(im).
    Definition with_im `{_fargs : FArgs} im (r : record) :=
      Build _ _ r.(re) im.
  End t.
  Definition t `{_fargs : FArgs} := t.record.

  Definition op_eq `{_fargs : FArgs} (x_value : t) (y_value : t) : bool :=
    andb
      (F.(FIELD.op_eq) x_value.(MonadVM.Ec.Algebra.Complex_extension.t.re)
        y_value.(MonadVM.Ec.Algebra.Complex_extension.t.re))
      (F.(FIELD.op_eq) x_value.(MonadVM.Ec.Algebra.Complex_extension.t.im)
        y_value.(MonadVM.Ec.Algebra.Complex_extension.t.im)).

  Definition op_ltgt `{_fargs : FArgs} (x_value : t) (y_value : t) : bool :=
    orb
      (F.(FIELD.op_ltgt) x_value.(MonadVM.Ec.Algebra.Complex_extension.t.re)
        y_value.(MonadVM.Ec.Algebra.Complex_extension.t.re))
      (F.(FIELD.op_ltgt) x_value.(MonadVM.Ec.Algebra.Complex_extension.t.im)
        y_value.(MonadVM.Ec.Algebra.Complex_extension.t.im)).

  Definition zero `{_fargs : FArgs} : t :=
    {| MonadVM.Ec.Algebra.Complex_extension.t.re := F.(FIELD.zero);
      MonadVM.Ec.Algebra.Complex_extension.t.im := F.(FIELD.zero); |}.

  Definition one `{_fargs : FArgs} : t :=
    {| MonadVM.Ec.Algebra.Complex_extension.t.re := F.(FIELD.one);
      MonadVM.Ec.Algebra.Complex_extension.t.im := F.(FIELD.zero); |}.

  Definition i_value `{_fargs : FArgs} : t :=
    {| MonadVM.Ec.Algebra.Complex_extension.t.re := F.(FIELD.zero);
      MonadVM.Ec.Algebra.Complex_extension.t.im := F.(FIELD.one); |}.

  Definition op_tildeat `{_fargs : FArgs} (str : MonadVM.String.t) : t :=
    {| MonadVM.Ec.Algebra.Complex_extension.t.re := F.(FIELD.op_tildeat) str;
      MonadVM.Ec.Algebra.Complex_extension.t.im := F.(FIELD.zero); |}.

  Definition op_tildedollar `{_fargs : FArgs} (x_value : int) : t :=
    {|
      MonadVM.Ec.Algebra.Complex_extension.t.re :=
        F.(FIELD.op_tildedollar) x_value;
      MonadVM.Ec.Algebra.Complex_extension.t.im := F.(FIELD.zero); |}.

  Definition real `{_fargs : FArgs} (x_value : F.(FIELD.t)) : t :=
    {| MonadVM.Ec.Algebra.Complex_extension.t.re := x_value;
      MonadVM.Ec.Algebra.Complex_extension.t.im := F.(FIELD.zero); |}.

  Definition imaginary `{_fargs : FArgs} (x_value : F.(FIELD.t)) : t :=
    {| MonadVM.Ec.Algebra.Complex_extension.t.re := F.(FIELD.zero);
      MonadVM.Ec.Algebra.Complex_extension.t.im := x_value; |}.

  Definition conjugate `{_fargs : FArgs} (function_parameter : t) : t :=
    let '{|
      MonadVM.Ec.Algebra.Complex_extension.t.re := re;
        MonadVM.Ec.Algebra.Complex_extension.t.im := im
        |} := function_parameter in
    {| MonadVM.Ec.Algebra.Complex_extension.t.re := re;
      MonadVM.Ec.Algebra.Complex_extension.t.im :=
        F.(FIELD.op_minus) F.(FIELD.zero) im; |}.

  Definition norm `{_fargs : FArgs} (function_parameter : t) : F.(FIELD.t) :=
    let '{|
      MonadVM.Ec.Algebra.Complex_extension.t.re := re;
        MonadVM.Ec.Algebra.Complex_extension.t.im := im
        |} := function_parameter in
    F.(FIELD.op_plus) (F.(FIELD.op_star) re re) (F.(FIELD.op_star) im im).

  Definition op_plus `{_fargs : FArgs} (x_value : t) (y_value : t) : t :=
    {|
      MonadVM.Ec.Algebra.Complex_extension.t.re :=
        F.(FIELD.op_plus) x_value.(MonadVM.Ec.Algebra.Complex_extension.t.re)
          y_value.(MonadVM.Ec.Algebra.Complex_extension.t.re);
      MonadVM.Ec.Algebra.Complex_extension.t.im :=
        F.(FIELD.op_plus) x_value.(MonadVM.Ec.Algebra.Complex_extension.t.im)
          y_value.(MonadVM.Ec.Algebra.Complex_extension.t.im); |}.

  Definition op_minus `{_fargs : FArgs} (x_value : t) (y_value : t) : t :=
    {|
      MonadVM.Ec.Algebra.Complex_extension.t.re :=
        F.(FIELD.op_minus) x_value.(MonadVM.Ec.Algebra.Complex_extension.t.re)
          y_value.(MonadVM.Ec.Algebra.Complex_extension.t.re);
      MonadVM.Ec.Algebra.Complex_extension.t.im :=
        F.(FIELD.op_minus) x_value.(MonadVM.Ec.Algebra.Complex_extension.t.im)
          y_value.(MonadVM.Ec.Algebra.Complex_extension.t.im); |}.

  Definition op_star `{_fargs : FArgs} (x_value : t) (y_value : t) : t :=
    {|
      MonadVM.Ec.Algebra.Complex_extension.t.re :=
        F.(FIELD.op_minus)
          (F.(FIELD.op_star) x_value.(MonadVM.Ec.Algebra.Complex_extension.t.re)
            y_value.(MonadVM.Ec.Algebra.Complex_extension.t.re))
          (F.(FIELD.op_star) x_value.(MonadVM.Ec.Algebra.Complex_extension.t.im)
            y_value.(MonadVM.Ec.Algebra.Complex_extension.t.im));
      MonadVM.Ec.Algebra.Complex_extension.t.im :=
        F.(FIELD.op_plus)
          (F.(FIELD.op_star) x_value.(MonadVM.Ec.Algebra.Complex_extension.t.re)
            y_value.(MonadVM.Ec.Algebra.Complex_extension.t.im))
          (F.(FIELD.op_star) x_value.(MonadVM.Ec.Algebra.Complex_extension.t.im)
            y_value.(MonadVM.Ec.Algebra.Complex_extension.t.re)); |}.

  Definition inv `{_fargs : FArgs} (x_value : t) : t :=
    let x' := conjugate x_value in
    let nx_inv := F.(FIELD.inv) (norm x_value) in
    {|
      MonadVM.Ec.Algebra.Complex_extension.t.re :=
        F.(FIELD.op_star) x'.(MonadVM.Ec.Algebra.Complex_extension.t.re) nx_inv;
      MonadVM.Ec.Algebra.Complex_extension.t.im :=
        F.(FIELD.op_star) x'.(MonadVM.Ec.Algebra.Complex_extension.t.im) nx_inv;
      |}.

  Definition op_div `{_fargs : FArgs} (x_value : t) (y_value : t) : t :=
    op_star x_value (inv y_value).

When the underlying field provides a square root function, sqrt_opt computes square roots in the complex extension. Note that unlike in the real case, the complex extension of a prime field does not in general have all the square roots.
  Definition sqrt_opt `{_fargs : FArgs}
    (sqrt_opt : F.(FIELD.t) → option F.(FIELD.t)) : t → option t :=
    let two := F.(FIELD.op_plus) F.(FIELD.one) F.(FIELD.one) in
    fun (v_value : t) ⇒
      let '{|
        MonadVM.Ec.Algebra.Complex_extension.t.re := a_value;
          MonadVM.Ec.Algebra.Complex_extension.t.im := b_value
          |} := v_value in
      if F.(FIELD.op_eq) b_value F.(FIELD.zero) then
        match sqrt_opt a_value with
        | Some y_value ⇒ MonadVM.Option._return (real y_value)
        | None ⇒
          MonadVM.Option.op_letdollar
            (sqrt_opt (F.(FIELD.op_minus) F.(FIELD.zero) a_value))
            (fun w_value ⇒ MonadVM.Option._return (imaginary w_value))
        end
      else
        MonadVM.Option.op_letdollar
          (sqrt_opt
            (F.(FIELD.op_plus) (F.(FIELD.op_star) a_value a_value)
              (F.(FIELD.op_star) b_value b_value)))
          (fun r_value ⇒
            MonadVM.Option.op_letdollar
              match
                sqrt_opt
                  (F.(FIELD.op_div) (F.(FIELD.op_plus) a_value r_value) two)
                with
              | Some c_value ⇒ MonadVM.Option._return c_value
              | None ⇒
                sqrt_opt
                  (F.(FIELD.op_div) (F.(FIELD.op_minus) a_value r_value) two)
              end
              (fun c_value ⇒
                let d_value :=
                  F.(FIELD.op_div) b_value (F.(FIELD.op_star) two c_value) in
                MonadVM.Option._return
                  {| MonadVM.Ec.Algebra.Complex_extension.t.re := c_value;
                    MonadVM.Ec.Algebra.Complex_extension.t.im := d_value; |})).

  Module Complex_extension_result.
    Record signature `{_fargs : FArgs} {t : Set} : Type := {
      t := t;
      t__rocq_record_make : F.(FIELD.t) → F.(FIELD.t) → t;
      t__rocq_record_get_re : t → F.(FIELD.t);
      t__rocq_record_get_im : t → F.(FIELD.t);
      op_eq : t → t → bool;
      op_ltgt : t → t → bool;
      zero : t;
      one : t;
      i_value : t;
      op_tildeat : MonadVM.String.t → t;
      op_tildedollar : int → t;
      real : F.(FIELD.t) → t;
      imaginary : F.(FIELD.t) → t;
      conjugate : t → t;
      norm : t → F.(FIELD.t);
      op_plus : t → t → t;
      op_minus : t → t → t;
      op_star : t → t → t;
      inv : t → t;
      op_div : t → t → t;
      sqrt_opt : (F.(FIELD.t) → option F.(FIELD.t)) → t → option t;
    }.
  End Complex_extension_result.
  Definition Complex_extension_result `{_fargs : FArgs} :=
    @Complex_extension_result.signature _ _.
  Arguments Complex_extension_result {_ _ _}.

  Definition functor `{_fargs : FArgs} :Complex_extension_result (t := _) :=
    {|
      Complex_extension_result.t__rocq_record_make :=
        fun _rocq_record_field_re _rocq_record_field_im ⇒
          {| MonadVM.Ec.Algebra.Complex_extension.t.re := _rocq_record_field_re;
            MonadVM.Ec.Algebra.Complex_extension.t.im := _rocq_record_field_im;
            |};
      Complex_extension_result.t__rocq_record_get_re :=
        fun _rocq_record_value ⇒
          _rocq_record_value.(MonadVM.Ec.Algebra.Complex_extension.t.re);
      Complex_extension_result.t__rocq_record_get_im :=
        fun _rocq_record_value ⇒
          _rocq_record_value.(MonadVM.Ec.Algebra.Complex_extension.t.im);
      Complex_extension_result.op_eq := op_eq;
      Complex_extension_result.op_ltgt := op_ltgt;
      Complex_extension_result.zero := zero;
      Complex_extension_result.one := one;
      Complex_extension_result.i_value := i_value;
      Complex_extension_result.op_tildeat := op_tildeat;
      Complex_extension_result.op_tildedollar := op_tildedollar;
      Complex_extension_result.real := real;
      Complex_extension_result.imaginary := imaginary;
      Complex_extension_result.conjugate := conjugate;
      Complex_extension_result.norm := norm;
      Complex_extension_result.op_plus := op_plus;
      Complex_extension_result.op_minus := op_minus;
      Complex_extension_result.op_star := op_star;
      Complex_extension_result.inv := inv;
      Complex_extension_result.op_div := op_div;
      Complex_extension_result.sqrt_opt := sqrt_opt
    |}.
End Complex_extension.
Definition Complex_extension {F_t : Set} (F : FIELD (t := F_t)) :=
  @Complex_extension.functor F_t (Complex_extension.Build_FArgs F).