123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212(*****************************************************************************)(* *)(* 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_stdlibmoduleAnemoiPerm=Bls12_381_hash.Permutation.AnemoimoduleMake(L:LIB)=structopenL(* FIXME: should be removed when
https://gitlab.com/nomadic-labs/cryptography/privacy-team/-/merge_requests/387
is merged *)letparameters=AnemoiPerm.Parameters.security_128_state_size_2letnb_rounds=AnemoiPerm.Parameters.get_number_of_roundsparametersletmatrix=AnemoiPerm.Parameters.get_matrixparametersletrc=AnemoiPerm.Parameters.get_round_constantsparametersletrc=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.add_list(to_list[x0;y0;xn;yn])letdouble_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*xn,yn,_=ifnb_roundsmod2=0thenret(xn,yn,0)elseround(xn,yn,n)inNum.add_list(to_list[x0;y0;xn;yn])letcustom_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.custom_round"@@let*res=Anemoi.anemoi_custom~kx1~ky1~kx2~ky2(xi,yi)inletxj,yj=of_pairresinret@@(xj,yj,i+4)letcompress_custom:scalarrepr->scalarrepr->scalarreprt=funx0y0->with_label~label:"Anemoi.compress_custom"@@let*x00,y00=init_state_for_roundsx0y0inlet*xn,yn,n=repeat~n:(nb_rounds/2)custom_round(x00,y00,2)inlet*xn,yn,_=ifnb_roundsmod2=0thenret(xn,yn,0)elseround(xn,yn,n)inNum.add_list(to_list[x0;y0;xn;yn])letcompress_19_5:scalarrepr->scalarrepr->scalarreprt=funx0y0->(* Anemoi of 20 rounds without the first linear layer *)(* let* x1, y1 = init_state_for_rounds x0 y0 in *)with_label~label:"Anemoi.19.5"@@let*xn,yn,_=repeat~n:10custom_round(x0,y0,0)inNum.add_list(to_list[x0;y0;xn;yn])letdigest:?input_length:int->scalarlistrepr->scalarreprt=fun?input_length:_inputs->matchof_listinputswith|[]->Num.constant(AnemoiPerm.jive128_1S.zeroS.zero)|[x]->let*zero=Num.zeroincompress_19_5zerox|x::rest->foldMcompress_19_5xrestendmoduleAnemoi128=structmoduleP:Hash_sig.P_HASH=structtypescalar=S.tletanemoi_instance=(* 19 rounds *)AnemoiPerm.(allocate_ctxtParameters.security_128_state_size_2)letjivexy=letstate=AnemoiPerm.set_stateanemoi_instance[|x;y|];AnemoiPerm.apply_flystelanemoi_instance;AnemoiPerm.apply_permutationanemoi_instance;AnemoiPerm.get_stateanemoi_instanceinS.(state.(0)+state.(1)+x+y)letdirect?input_length:_inputs=matchArray.to_listinputswith|[]->jiveS.zeroS.zero|[x]->jiveS.zerox|x::rest->List.fold_leftjivexresttypectxt=S.tletinit?input_length:_()=S.zeroletdigestctxtinputs=ignorectxt;directinputsletgetctxt=ctxtend(* compute one round and return intermediate values *)letcompute_one_roundx0y0kxky=letbeta=AnemoiPerm.Parameters.betainletgamma=AnemoiPerm.Parameters.gammainletdelta=AnemoiPerm.Parameters.deltainletg=AnemoiPerm.Parameters.ginletalpha_inv=AnemoiPerm.Parameters.alpha_invinletg2_p_1=S.((g*g)+one)in(* -> Sbox *)(* w^5 = x0 - (beta y0^2 + gamma) *)letw_5=S.(subx0((beta*y0*y0)+gamma))in(* Computing w *)letw=S.(poww_5(to_zalpha_inv))in(* v = y0 - w *)letv=S.suby0win(* u = w^5 + beta * v^2 + delta *)letu=S.(w_5+((beta*v*v)+delta))in(* -> Linear layer + rc *)(* x1 = (u + kx) + g * (v + ky) *)letx1=S.(u+kx+(g*(v+ky)))in(* y1 = (g * (u + kx) + (g^2 + 1) * (v + ky) *)lety1=S.((g*(u+kx))+(g2_p_1*(v+ky)))in(w_5,w,v,u,x1,y1)moduleV:Hash_sig.HASH=Makeend