123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* Copyright (c) 2020 Metastate AG <hello@metastate.dev> *)(* *)(* 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. *)(* *)(*****************************************************************************)openError_monadmodulePublic_key_hash=structincludeBlake2B.Make(Base58)(structletname="Ed25519.Public_key_hash"lettitle="An Ed25519 public key hash"letb58check_prefix=Base58.Prefix.ed25519_public_key_hashletsize=Some20end)moduleLogging=structlettag=Tag.def~doc:titlenameppendendlet()=Base58.check_encoded_prefixPublic_key_hash.b58check_encoding"tz1"36openHacl.Ed25519modulePublic_key=structtypet=Hacl.publicHacl.Ed25519.keyletname="Ed25519.Public_key"lettitle="Ed25519 public key"letto_bytes=to_bytesletto_strings=Bytes.to_string(to_bytess)letof_bytes_opt=pk_of_bytesletof_string_opts=of_bytes_opt(Bytes.of_strings)letof_bytes_without_validation=of_bytes_optletsize_=pk_sizetypeBase58.data+=Dataoftletb58check_encoding=Base58.register_encoding~prefix:Base58.Prefix.ed25519_public_key~length:(size())~to_raw:to_string~of_raw:of_string_opt~wrap:(funx->Datax)let()=Base58.check_encoded_prefixb58check_encoding"edpk"54lethashv=Public_key_hash.hash_bytes[to_bytesv]includeCompare.Make(structtypenonrect=tletcompare=Hacl.Ed25519.compareend)includeHelpers.MakeRaw(structtypenonrect=tletname=nameletof_bytes_opt=of_bytes_optletof_string_opt=of_string_optletto_string=to_stringend)includeHelpers.MakeB58(structtypenonrect=tletname=nameletb58check_encoding=b58check_encodingend)includeHelpers.MakeEncoder(structtypenonrect=tletname=namelettitle=titleletraw_encoding=letopenData_encodinginconvto_bytesof_bytes_exn(Fixed.bytes(size()))letof_b58check=of_b58checkletof_b58check_opt=of_b58check_optletof_b58check_exn=of_b58check_exnletto_b58check=to_b58checkletto_short_b58check=to_short_b58checkend)letppppft=Format.fprintfppf"%s"(to_b58checkt)endmoduleSecret_key=structtypet=Hacl.secretkeyletname="Ed25519.Secret_key"lettitle="An Ed25519 secret key"letsize=sk_sizeletto_bytes=to_bytesletto_strings=Bytes.to_string(to_bytess)letof_bytes_opt=sk_of_bytesletof_string_opts=of_bytes_opt(Bytes.of_strings)letto_public_key=neuterizetypeBase58.data+=Dataoftletb58check_encoding=Base58.register_encoding~prefix:Base58.Prefix.ed25519_seed~length:size~to_raw:to_string~of_raw:of_string_opt~wrap:(funsk->Datask)(* Legacy NaCl secret key encoding. Used to store both sk and pk. *)letsecret_key_encoding=Base58.register_encoding~prefix:Base58.Prefix.ed25519_secret_key~length:(sk_size+pk_size)~to_raw:(funsk->letpk=neuterizeskinletbuf=Bytes.create(sk_size+pk_size)inblit_to_bytesskbuf;blit_to_bytespk~pos:sk_sizebuf;Bytes.to_stringbuf)~of_raw:(funbuf->letsk=Bytes.(sub(of_stringbuf)0sk_size)insk_of_bytessk)~wrap:(funx->Datax)letof_b58check_opts=matchBase58.simple_decodeb58check_encodingswith|Somex->Somex|None->Base58.simple_decodesecret_key_encodingsletof_b58check_exns=matchof_b58check_optswith|Somex->x|None->Format.kasprintfStdlib.failwith"Unexpected data (%s)"nameletof_b58checks=matchof_b58check_optswith|Somex->Okx|None->generic_error"Failed to read a b58check_encoding data (%s): %S"namesletto_b58checks=Base58.simple_encodeb58check_encodingsletto_short_b58checks=String.sub(to_b58checks)0(10+String.length(Base58.prefixb58check_encoding))let()=Base58.check_encoded_prefixb58check_encoding"edsk"54;Base58.check_encoded_prefixsecret_key_encoding"edsk"98includeCompare.Make(structtypenonrect=tletcompare=compareend)includeHelpers.MakeRaw(structtypenonrect=tletname=nameletof_bytes_opt=of_bytes_optletof_string_opt=of_string_optletto_string=to_stringend)includeHelpers.MakeEncoder(structtypenonrect=tletname=namelettitle=titleletraw_encoding=letopenData_encodinginconvto_bytesof_bytes_exn(Fixed.bytessize)letof_b58check=of_b58checkletof_b58check_opt=of_b58check_optletof_b58check_exn=of_b58check_exnletto_b58check=to_b58checkletto_short_b58check=to_short_b58checkend)letppppft=Format.fprintfppf"%s"(to_b58checkt)endtypet=Bytes.ttypewatermark=Bytes.tletname="Ed25519"lettitle="An Ed25519 signature"letsize=sizeletto_bytess=Bytes.copysletto_strings=Bytes.to_string(to_bytess)letof_bytes_opts=ifBytes.lengths=sizethenSomeselseNoneletof_string_opts=of_bytes_opt(Bytes.of_strings)typeBase58.data+=Dataoftletb58check_encoding=Base58.register_encoding~prefix:Base58.Prefix.ed25519_signature~length:size~to_raw:to_string~of_raw:of_string_opt~wrap:(funx->Datax)let()=Base58.check_encoded_prefixb58check_encoding"edsig"99includeHelpers.MakeRaw(structtypenonrect=tletname=nameletof_bytes_opt=of_bytes_optletof_string_opt=of_string_optletto_string=to_stringend)includeHelpers.MakeB58(structtypenonrect=tletname=nameletb58check_encoding=b58check_encodingend)includeHelpers.MakeEncoder(structtypenonrect=tletname=namelettitle=titleletraw_encoding=letopenData_encodinginconvto_bytesof_bytes_exn(Fixed.bytessize)letof_b58check=of_b58checkletof_b58check_opt=of_b58check_optletof_b58check_exn=of_b58check_exnletto_b58check=to_b58checkletto_short_b58check=to_short_b58checkend)letppppft=Format.fprintfppf"%s"(to_b58checkt)letzero=Bytes.makesize'\000'letsign?watermarkskmsg=letmsg=Blake2B.to_bytes@@Blake2B.hash_bytes@@matchwatermarkwithNone->[msg]|Someprefix->[prefix;msg]insign~sk~msgletcheck?watermarkpksignaturemsg=letmsg=Blake2B.to_bytes@@Blake2B.hash_bytes@@matchwatermarkwithNone->[msg]|Someprefix->[prefix;msg]inverify~pk~msg~signatureletgenerate_key?seed()=matchseedwith|None->let(pk,sk)=keypair()in(Public_key.hashpk,pk,sk)|Someseed->(letseedlen=Bytes.lengthseedinifseedlen<Secret_key.sizetheninvalid_arg(Printf.sprintf"Ed25519.generate_key: seed must be at least %d bytes long (got \
%d)"Secret_key.sizeseedlen)elsematchsk_of_bytes(Bytes.subseed0Secret_key.size)with|None->invalid_arg"Ed25519.generate_key: invalid seed"|Somesk->letpk=neuterizeskin(Public_key.hashpk,pk,sk))letdeterministic_nonceskmsg=letkey=Secret_key.to_bytesskinHacl.Hash.SHA256.HMAC.digest~key~msgletdeterministic_nonce_hashskmsg=Blake2B.to_bytes(Blake2B.hash_bytes[deterministic_nonceskmsg])include(Compare.Bytes:Compare.Swithtypet:=t)