123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.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. *)(* *)(*****************************************************************************)moduleSp=Libsecp256k1.InternalmoduletypeSCALAR_SIG=sigtypetincludeS.B58_DATAwithtypet:=tincludeS.ENCODERwithtypet:=tvalzero:tvalone:tvalof_Z:Z.t->tvalto_Z:t->Z.tvalof_int:int->tvaladd:t->t->tvalmul:t->t->tvalnegate:t->tvalsub:t->t->tvalof_bits_exn:string->tvalto_bits:t->stringvalinverse:t->toptionvalpow:t->Z.t->tvalequal:t->t->boolendmoduleGroup:sigvalorder:Z.tmoduleScalar:SCALAR_SIGtypetvalpp:Format.formatter->t->unitincludeCompare.Swithtypet:=tincludeS.RAW_DATAwithtypet:=tincludeS.B58_DATAwithtypet:=tincludeS.ENCODERwithtypet:=tvale:tvalg:tvalh:tvalof_coordinates:x:Z.t->y:Z.t->tvalof_bits_exn:string->tvalto_bits:t->stringvalmul:Scalar.t->t->tval(+):t->t->tval(-):t->t->tend=structletorder=Z.of_string_base16"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"letstring_revs=letlen=String.lengthsinString.initlen(funi->s.[len-1-i])letb32_of_Zz=letcs=Cstruct.create32inletbits=Z.to_bitszinletlength=min32(String.lengthbits)inletbits=String.subbits0lengthinletbits=string_revbitsinCstruct.blit_from_stringbits0cs(32-length)length;csletz_of_b32b=b|>Cstruct.to_string|>string_rev|>Z.of_bitsmoduleScalar:SCALAR_SIGwithtypet=Sp.Scalar.t=structtypet=Sp.Scalar.tletzero=Sp.Scalar.zero()letone=Sp.Scalar.one()letequalxy=Sp.Scalar.equalxyletof_Zz=letz=Z.eremzorderinletr=Sp.Scalar.const()inletcs=b32_of_Zzinlet_=Sp.Scalar.set_b32rcsinrletto_Zs=letcs=Cstruct.create32inSp.Scalar.get_b32css;cs|>z_of_b32letof_inti=i|>Z.of_int|>of_Zletpowtn=Z.powm(to_Zt)norder|>of_Zletaddxy=letr=Sp.Scalar.const()inlet_=Sp.Scalar.addrxyinrletmulxy=letr=Sp.Scalar.const()inSp.Scalar.mulrxy;rletnegatex=letr=Sp.Scalar.const()inSp.Scalar.negaterx;rletsubxy=addx(negatey)letof_bits_exnbits=letr=Sp.Scalar.const()in(* trim to 32 bytes *)letcs=Cstruct.create32inCstruct.blit_from_stringbits0cs0(min(String.lengthbits)32);(* ignore overflow condition, it's always 0 based on the c-code *)let_=Sp.Scalar.set_b32rcsinr(* TODO, check that we are less than the order *)letto_bitsx=letc=Cstruct.create32inSp.Scalar.get_b32cx;Cstruct.to_stringcletinversex=ifx=zerothenNoneelseletr=Sp.Scalar.const()inSp.Scalar.inverserx;SomertypeBase58.data+=Dataoftletb58check_encoding=Base58.register_encoding~prefix:Base58.Prefix.secp256k1_scalar~length:32~to_raw:to_bits~of_raw:(funs->Option.catch(fun()->of_bits_exns))~wrap:(funx->Datax)lettitle="Secp256k1_group.Scalar"letname="A scalar for the secp256k1 group"includeHelpers.MakeB58(structtypenonrect=tletname=nameletb58check_encoding=b58check_encodingend)includeHelpers.MakeEncoder(structtypenonrect=tletname=namelettitle=titleletraw_encoding=Data_encoding.(convto_bitsof_bits_exnstring)letto_b58check=to_b58checkletto_short_b58check=to_short_b58checkletof_b58check=of_b58checkletof_b58check_opt=of_b58check_optletof_b58check_exn=of_b58check_exnend)endtypet=Sp.Group.Jacobian.t(* type ge = Sp.Group.ge *)letfield_of_Zz=letfe=Sp.Field.const()inletcs=b32_of_Zzinlet_=Sp.Field.set_b32fecsinfeletgroup_of_jacobianj=letr=Sp.Group.of_fields()inSp.Group.Jacobian.get_gerj;rletjacobian_of_groupg=letj=Sp.Group.Jacobian.of_fields()inSp.Group.Jacobian.set_gejg;jletof_coordinates~x~y=Sp.Group.of_fields~x:(field_of_Zx)~y:(field_of_Zy)()|>jacobian_of_grouplete=Sp.Group.Jacobian.of_fields~infinity:true()letg=letgx=Z.of_string"55066263022277343669578718895168534326250603453777594175500187360389116729240"andgy=Z.of_string"32670510020758816978083085130507043184471273380659243275938904335757337482424"inof_coordinates~x:gx~y:gy(* To obtain the second generator, take the sha256 hash of the decimal representation of g1_y
python -c "import hashlib;print int(hashlib.sha256('32670510020758816978083085130507043184471273380659243275938904335757337482424').hexdigest(),16)"
*)leth=lethx=Z.of_string"54850469061264194188802857211425616972714231399857248865148107587305936171824"andhy=Z.of_string"6558914719042992724977242403721980463337660510165027616783569279181206179101"inof_coordinates~x:hx~y:hylet(+)xy=letr=Sp.Group.Jacobian.of_fields()inSp.Group.Jacobian.add_varrxy;rlet(-)xy=letneg_y=Sp.Group.Jacobian.of_fields()inSp.Group.Jacobian.negneg_yy;x+neg_yletmulsg=letr=Sp.Group.Jacobian.of_fields()inSp.Group.Jacobian.mulr(group_of_jacobiang)s;rletto_bitsj=letx=group_of_jacobianjandbuf=Cstruct.create33inletcs=Sp.Group.to_pubkey~compress:truebufxinCstruct.to_stringcsletof_bits_exnbits=letbuf=Cstruct.of_stringbitsandx=Sp.Group.of_fields()inSp.Group.from_pubkeyxbuf;x|>jacobian_of_groupmoduleEncoding=structtypeBase58.data+=Dataoftlettitle="Secp256k1_group.Group"letname="An element of secp256k1"letb58check_encoding=Base58.register_encoding~prefix:Base58.Prefix.secp256k1_element~length:33~to_raw:to_bits~of_raw:(funs->Option.catch(fun()->of_bits_exns))~wrap:(funx->Datax)includeHelpers.MakeB58(structtypenonrect=tletname=nameletb58check_encoding=b58check_encodingend)includeHelpers.MakeEncoder(structtypenonrect=tletname=namelettitle=titleletraw_encoding=Data_encoding.(convto_bitsof_bits_exnstring)letto_b58check=to_b58checkletto_short_b58check=to_short_b58checkletof_b58check=of_b58checkletof_b58check_opt=of_b58check_optletof_b58check_exn=of_b58check_exnend)endincludeEncodingletto_bytespk=to_bitspk|>Bytes.of_stringletof_bytes_opts=Option.catch(fun()->Bytes.to_strings|>of_bits_exn)letto_string=to_bitsletof_string_opts=Option.catch(fun()->of_bits_exns)letsize=37includeCompare.Make(structtypenonrect=tletcompareab=String.compare(to_stringa)(to_stringb)end)includeHelpers.MakeRaw(structtypenonrect=tletname=nameletof_bytes_opt=of_bytes_optletof_string_opt=of_string_optletto_string=to_stringend)end