123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)moduleStubs=structtypet=Bigstringaf.ttypeelt=Bls12_381.Fr.tletsize=Bls12_381.Fr.size_in_bytesletallocate_elt()=Bls12_381.Fr.(copyone)externalget:elt->t->int->unit="caml_polynomial_get_stubs"[@@noalloc](** [get res p i] writes [i]-th element of a given array [p] in [res]
- requires: [0 <= i < size p]
- ensures: [res = p[i]] *)externalset:t->elt->int->unit="caml_polynomial_set_stubs"[@@noalloc](** [set p fr i] writes [fr] in the [i]-th element of [p]
- requires: [0 <= i < size p]
- ensures: [fr = p[i]] *)endmoduletypeStubs_sig=sigtypet=Bigstringaf.ttypeeltvalallocate_elt:unit->eltvalsize:intvalget:elt->t->int->unitvalset:t->elt->int->unitendmoduletypeCarray_sig=sigtypet=Bigstringaf.ttypeeltvalencoding:tData_encoding.tvalallocate:int->t(** [allocate len] creates a C array of size [len] intialized with zeros of blst_fr *)vallength:t->int(** [length c] returns the length of a C array [c] *)valget:t->int->elt(** [get c i] returns the [i]-th element of a C array [c] *)valset:t->elt->int->unitvalcopy:?offset:int->?len:int->t->t(** [copy c] copies [len] elements from a C array [c] starting from position [offset] *)valblit:t->src_off:int->t->dst_off:int->len:int->unit(** [blit src src_off dst dst_off len] copies [len] elements from [src] to
[dst] starting at the respective offsets. *)valto_array:?offset:int->?len:int->t->eltarray(** [to_array c] converts a C array [c] to an OCaml array *)valof_array:eltarray->t(** [of_array c] converts an OCaml array [c] to a C array *)endmoduleMake(S:Stubs_sig):Carray_sigwithtypeelt=S.elt=structtypet=S.ttypeelt=S.eltletlengtha=Bigstringaf.lengtha/S.sizeletallocaten=ifn<1thenraise@@Invalid_argument"allocate: size should be >= 1";letsize=S.size*ninletres=Bigstringaf.createsizeinresletgetpi=ifi<0||i>=lengthpthenraise@@Invalid_argument"get: index out of bounds";letres=S.allocate_elt()inS.getrespi;resletsetpfri=ifi<0||i>=lengthpthenraise@@Invalid_argument"set: index out of bounds";S.setpfriletto_array?(offset=0)?lenp=letlen=Option.value~default:(lengthp-offset)leniniflen<0||offset<0||lengthp-offset<lenthenraise@@Invalid_argument(Format.sprintf"to_array: invalid len %d or offset %d for size %d"lenoffset(lengthp));Array.initlen(funi->getpi)letof_arraycaml_array=letn=Array.lengthcaml_arrayinletres=allocateninArray.iteri(funig->setresgi)caml_array;resletencoding=Data_encoding.(conv(funbs->Bigstringaf.to_stringbs)(funs->letlen=String.lengthsinBigstringaf.of_string~off:0~lens)string)letblitsrc~src_offdst~dst_off~len=letsrc_off=src_off*S.sizeinletdst_off=dst_off*S.sizeinletlen=len*S.sizeinBigstringaf.blitsrc~src_offdst~dst_off~lenletcopy?(offset=0)?lenp=letlen=Option.value~default:(lengthp-offset)leniniflen<0||offset<0||lengthp-offset<lenthenraise@@Invalid_argument"copy: invalid len or offset";letres=allocateleninblitp~src_off:offsetres~dst_off:0~len;resendincludeMake(Stubs)