123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)(** Formats used for Srs:
- the disk format uses compressed affine coordinates to save space
- the memory format uses uncompressed affine coordiantes, because it's
required by the pippenger API
- the interface and memory representation of an Srs is as carray of affine
elements (type G.affine) because they are however the interface that is
exposed uses jacobian format (the type G.t). *)(* The Elt signature is extended with pippenger. *)moduletypeGElt_sig=sigmoduleG:Bls12_381.CURVEincludeCarray.Elt_sigwithtypet=G.affine(** [uncompress res bs n] reads [n] bytes from [bs] and uncompresses
them into an affine point in [res]. Returns 0 if successful. *)valuncompress:G.affine->bytes->int(* This function is already bound in Bls12-381 but not exposed. *)(** [pippenger res srs poly offset len] writes in [res] the
multiexponentiation of [srs] with the polynomial [poly] starting at
[offset] and for [len] elements. Returns 0 if successful. *)valpippenger:G.t->Bigstringaf.t->Polynomial.t->int->int->intendmoduletypeS=sigtypepolynomialtypet[@@derivingrepr]typeeltvalempty:t(** [get srs i] returns the [i]-th element of [srs] *)valget:t->int->elt(** Returns the pippenger ctxt size, i.e. the number of elements the context
is supposed to be called with *)valsize:t->intvalof_bigstring:?len:int->Bigstringaf.t->(t,[>`End_of_fileofstring|`Invalid_pointofint])result(** [of_bigstring ~len bs] reads [len] points of G in affine
compressed format from [bs] and returns a Srs. If [len] is omitted the
whole bs is read.
The bigstring can be loaded from a file with:
{[
let bigstring_of_file filename =
let fd = Unix.openfile filename [ Unix.O_RDONLY ] 0o440 in
Bigarray.array1_of_genarray
@@ Unix.map_file fd Bigarray.char Bigarray.c_layout false
[| (* [-1] means read the whole file *) -1 |]
in
Srs_g1.of_bigstring
(bigstring_of_file ("srs_zcash_g1_21"))
(1 lsl 5)
]}
or with Lwt:
{[
let bigstring_of_file filename =
let fd = Unix.openfile filename [ Unix.O_RDONLY ] 0o440 in
Lwt_bytes.map_file ~fd ~shared:false ()
in
]} *)valgenerate_insecure:int->Bls12_381.Fr.t->t(** [pippenger ctxt poly] computes the multiscalar exponentiation using the
SRS saved in the context and the coefficients of the given polynomial *)valpippenger:?offset:int->?len:int->t->polynomial->elt(* TODO just for pack *)valto_array:?len:int->t->eltarrayvalof_array:eltarray->tendmoduleMake(Elt:GElt_sig)=structmoduleG=Elt.GmoduleCarray=Carray.Make(Elt)typepolynomial=Polynomial.Polynomial_unsafe.ttypet=Carray.t[@@derivingrepr]typeelt=G.tletsize=Carray.lengthletgetpi=Carray.getpi|>G.jacobian_of_affineletto_array?lendata=Carray.to_array?lendata|>Array.mapG.jacobian_of_affineletof_arraycaml_array=Carray.of_array(Array.mapG.affine_of_jacobiancaml_array)letempty=Carray.emptyletgenerate_insecuredx=letxi=refG.oneinArray.initd(fun_->letres=!xiinxi:=G.mul!xix;res)|>of_arrayletof_bigstring?lenbs=letsize_compressed=Elt.size/2inletn=Option.value~default:(Bigstringaf.lengthbs/size_compressed)leninletdata=Carray.allocateninletbuf=Bytes.createsize_compressedinletpoint=Elt.allocate()inlet(let*)=Stdlib.Result.bindinletrecloopi=ifi=nthenOk()elselet*()=tryOk(Bigstringaf.blit_to_bytesbs~src_off:(i*size_compressed)buf~dst_off:0~len:size_compressed)with_->Error(`End_of_file(Format.sprintf"found only %d elements of the required %d"in))inletcode=Elt.uncompresspointbufinlet*()=ifcode<>0thenError(`Invalid_pointi)elseOk()inCarray.setdatapointi;loop(i+1)inlet*()=loop0inOkdataletpippenger?(offset=0)?lensrspoly=ifPolynomial.is_zeropolythenG.zeroelseletpoly_length=Polynomial.degreepoly+1inletlen=Option.value~default:(poly_length-offset)leniniflen<=0||offset<0||poly_length-offset<len||sizesrs-offset<lenthenraise@@Invalid_argument(Format.sprintf"pippenger: invalid len %d or offset %d for size %d"lenoffsetpoly_length);letres=G.(copyzero)inletreturn_code=Elt.pippengerres(Carray.to_bigstringsrs)polyoffsetleninassert(return_code=0);resendmoduleElt_g1=structmoduleG=Bls12_381.G1typet=G.affineletsize=G.size_in_bytesletallocate()=G.(affine_of_jacobianzero)letzero=G.(affine_of_jacobianzero)leteqab=G.eq(G.jacobian_of_affinea)(G.jacobian_of_affineb)externaluncompress:t->bytes->int="caml_blst_p1_uncompress_stubs"[@@noalloc]externalpippenger:G.t->Bigstringaf.t->Polynomial.t->int->int->int="caml_bls12_381_polynomial_srs_g1_pippenger_stubs"[@@noalloc]endmoduleElt_g2=structmoduleG=Bls12_381.G2typet=G.affineletsize=G.size_in_bytesletallocate()=G.(affine_of_jacobianzero)letzero=G.(affine_of_jacobianzero)leteqab=G.eq(G.jacobian_of_affinea)(G.jacobian_of_affineb)externaluncompress:G.affine->bytes->int="caml_blst_p2_uncompress_stubs"[@@noalloc]externalpippenger:G.t->Bigstringaf.t->Polynomial.t->int->int->int="caml_bls12_381_polynomial_srs_g2_pippenger_stubs"[@@noalloc]endmoduleSrs_g1:Swithtypeelt=Bls12_381.G1.tandtypepolynomial=Polynomial.t=Make(Elt_g1)moduleSrs_g2:Swithtypeelt=Bls12_381.G2.tandtypepolynomial=Polynomial.t=Make(Elt_g2)moduleChecks=structopenBls12_381letequalityg1sg2s=letg1=Srs_g1.getg1s0inletg2=Srs_g2.getg2s0inletsize=min(Srs_g1.sizeg1s)(Srs_g2.sizeg2s)infori=0tosize-1doletg1i=Srs_g1.getg1siinletg2i=Srs_g2.getg2siinletgt1=Pairing.pairingg1ig2inletgt2=Pairing.pairingg1g2iinassert(GT.eqgt1gt2)doneletincrementation_g1g1sg2s=letg2=Srs_g2.getg2s0inletg2x=Srs_g2.getg2s1infori=0toSrs_g1.sizeg1s-2doletg1i=Srs_g1.getg1siinletg1iplus=Srs_g1.getg1s(i+1)inletgt1=Pairing.pairingg1ig2xinletgt2=Pairing.pairingg1iplusg2inassert(GT.eqgt1gt2)doneletincrementation_g2g1sg2s=letg1=Srs_g1.getg1s0inletg1x=Srs_g1.getg1s1infori=0toSrs_g2.sizeg2s-2doletg2i=Srs_g2.getg2siinletg2iplus=Srs_g2.getg2s(i+1)inletgt1=Pairing.pairingg1xg2iinletgt2=Pairing.pairingg1g2iplusinassert(GT.eqgt1gt2)doneletpairings(g1s,g2s)=equalityg1sg2s;incrementation_g1g1sg2s;incrementation_g2g1sg2sendmoduleSrs=structmoduleSrs_g1=Srs_g1moduleSrs_g2=Srs_g2typet=Srs_g1.t*Srs_g2.tletgenerate_insecurelog_g1log_g2=letx=Bls12_381.Fr.random()in(Srs_g1.generate_insecure(1lsllog_g1)x,Srs_g2.generate_insecure(1lsllog_g2)x)letcheck=Checks.pairingsend