Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file gadget_anemoi.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)openLang_coreopenLang_stdlibmoduleMake(L:LIB)=structopenLletnb_rounds,_state_size,matrix,rc=Bls12_381_hash.Anemoi.Parameters.state_size_1letrc=Array.init(Array.lengthrc+2)(funi->(* We pad with two zeroes *)ifi/2=nb_roundsthenBls12_381.Fr.zeroelseifimod2=0thenrc.(i/2)elserc.(nb_rounds+(i/2)))letrecrepeat:n:int->('a->'at)->'a->'at=fun~nfe->ifn<=0thenreteelselet*x=feinrepeat~n:(n-1)fxletround:scalarrepr*scalarrepr*int->(scalarrepr*scalarrepr*int)t=fun(xi,yi,i)->letkx,ky=(rc.(i),rc.(i+1))inwith_label~label:"Anemoi.round"@@let*res=Anemoi.anemoi_round~kx~ky(xi,yi)inletxj,yj=of_pairresinret@@(xj,yj,i+2)letinit_state_for_roundsx0y0=let*x00=Num.add~ql:matrix.(0).(0)~qr:matrix.(0).(1)~qc:S.((matrix.(0).(0)*rc.(0))+(matrix.(0).(1)*rc.(1)))x0y0inlet*y00=Num.add~ql:matrix.(1).(0)~qr:matrix.(1).(1)~qc:S.((matrix.(1).(0)*rc.(0))+(matrix.(1).(1)*rc.(1)))x0y0inret(x00,y00)letcompress:scalarrepr->scalarrepr->scalarreprt=funx0y0->with_label~label:"Anemoi.compress"@@let*x00,y00=init_state_for_roundsx0y0inlet*xn,yn,_i=repeat~n:nb_roundsround(x00,y00,2)inNum.add5~k5:S.zeroxnynx0y0y0letdouble_round:scalarrepr*scalarrepr*int->(scalarrepr*scalarrepr*int)t=fun(xi,yi,i)->letkx1,ky1=(rc.(i),rc.(i+1))inletkx2,ky2=(rc.(i+2),rc.(i+3))inwith_label~label:"Anemoi.double_round"@@let*res=Anemoi.anemoi_double_round~kx1~ky1~kx2~ky2(xi,yi)inletxj,yj=of_pairresinret@@(xj,yj,i+4)letcompress_two:scalarrepr->scalarrepr->scalarreprt=funx0y0->with_label~label:"Anemoi.compress_two"@@let*x00,y00=init_state_for_roundsx0y0inlet*xn,yn,n=repeat~n:(nb_rounds/2)double_round(x00,y00,2)inlet*xnn,ynn,_=ifnb_roundsmod2=0thenret(xn,yn,0)elseround(xn,yn,n)inNum.add5~k5:S.zeroxnnynnx0y0y0letdigest:?input_length:int->scalarlistrepr->scalarreprt=fun?input_length:_inputs->matchof_listinputswith|[]->constant_scalar(Bls12_381_hash.Anemoi.jive128_1_compressS.zeroS.zero)|[x]->let*zero=constant_scalarS.zeroincompress_twozerox|x::rest->foldMcompress_twoxrestendmoduleAnemoi128=structmoduleP:Hash_sig.P_HASH=structtypescalar=S.tletdirect?input_length:_inputs=matchArray.to_listinputswith|[]->Bls12_381_hash.Anemoi.jive128_1_compressS.zeroS.zero|[x]->Bls12_381_hash.Anemoi.jive128_1_compressS.zerox|x::rest->List.fold_leftBls12_381_hash.Anemoi.jive128_1_compressxresttypectxt=S.tletinit?input_length:_()=S.zeroletdigestctxtinputs=ignorectxt;directinputsletgetctxt=ctxtendmoduleV:Hash_sig.HASH=Makeend