123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167(*****************************************************************************)(* *)(* MIT License *)(* Copyright (c) 2022 Nomadic Labs <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)moduleFr=Bls12_381.FrmoduleStubs=structtypefr=Fr.ttypefr_array=Fr_carray.t(** [compute_domain res n g] computes [[one; g; ..; g^{n-1}]] for a given
blst_fr element [g]
requires:
- [1 < n <= size res]
ensures:
- [res[i] = g^i] for [i = 0..(n-1)] *)externalcompute_domain:fr_array->int->fr->unit="caml_bls12_381_polynomial_polynomial_compute_domain_stubs"[@@noalloc](** [rescale res a size_res size_a] writes the result of rescaling the evaluation
representation of a polynomial [a] from [domain_a] of size [size_a] to
[domain_res] of size [size_res] in [res]
requires:
- [size res = size_res]
- [size a = size_a]
- [size_res <= size_a]
- [res] and [a] are disjoint
- [size_res mod size_a = 0] *)externalrescale:fr_array->fr_array->int->int->unit="caml_bls12_381_polynomial_polynomial_evaluations_rescale_stubs"[@@noalloc]endmoduleDomain_impl=structtypescalar=Bls12_381.Fr.ttypet=Fr_carray.t[@@derivingrepr]letof_carrayp=pletto_carrayp=pletof_array=Fr_carray.of_arrayletto_arrayd=Fr_carray.to_arraydletlength=Fr_carray.lengthletget=Fr_carray.getletcreatenroot_of_unity=ifn<=1thenraise@@Invalid_argument"create: requires n > 1";letdomain=Fr_carray.allocateninStubs.compute_domaindomainnroot_of_unity;domainletprimitive_root_of_unity=Fr_carray.primitive_root_of_unityletbuild?primitive_rootn=letprimitive_root=matchprimitive_rootwith|None->Fr_carray.primitive_root_of_unityn|Someroot->rootincreatenprimitive_rootletbuild_power_of_two?primitive_rootlog=build?primitive_root(1lsllog)letsubgroup~logd=letl=lengthdinletn=1lslloginifn>l||log<=0thenraise@@Invalid_argument"subgroup: wrong order"elseletdom=Fr_carray.allocateninStubs.rescaledomdnl;domletinversed=letn=lengthdinArray.initn(funi->ifi=0thenFr.(copyone)elseFr_carray.getd(n-i))endmoduletypeDomain_sig=sigtypescalartypet[@@derivingrepr](** [length p] returns the length of a given array [p] *)vallength:t->int(** [get p i] returns the [i]-th element of a given array [p] *)valget:t->int->scalar(** [primitive_root_of_unity n] returns a primitive [n]-th root of unity,
provided it exists *)valprimitive_root_of_unity:int->scalar(** [build n] computes [[one; g; ..; g^{n-1}]] where [g]
is a primitive [n]-th root of unity *)valbuild:?primitive_root:scalar->int->t(** [build_power_of_two log] computes [[one; g; ..; g^{n-1}]] where [g]
is a primitive [n]-th root of unity and [n = 2^log] *)valbuild_power_of_two:?primitive_root:scalar->int->t(** [subgroup log d] returns a subgroup of [d] of order [2^log] *)valsubgroup:log:int->t->t(** [inverse d] returns for a domain [wⁱᵢ] its inverse domain [w⁻ⁱᵢ] *)valinverse:t->scalararray(** [to_array d] converts a C array [d] to an OCaml array *)valto_array:t->scalararray(** [of_array d] converts an OCaml array [d] to a C array *)valof_array:scalararray->tendmoduletypeDomain_unsafe_sig=sigincludeDomain_sig(** [to_carray d] converts [d] from type {!type:t} to type {!type:Fr_carray.t}
Note: [to_carray d] doesn't create a copy of [d] *)valto_carray:t->Fr_carray.t(** [of_carray d] converts [d] from type {!type:Fr_carray.t} to type {!type:t}
Note: [of_carray d] doesn't create a copy of [d] *)valof_carray:Fr_carray.t->tendmoduleDomain_unsafe:Domain_unsafe_sigwithtypescalar=Bls12_381.Fr.t=Domain_implinclude(Domain_unsafe:Domain_sigwithtypet=Domain_unsafe.tandtypescalar=Domain_unsafe.scalar)