123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)modulePublic_key_hash=structincludeBlake2B.Make(Base58)(structletname="P256.Public_key_hash"lettitle="A P256 public key hash"letb58check_prefix=Base58.Prefix.p256_public_key_hashletsize=Some20end)moduleLogging=structlettag=Tag.def~doc:titlenameppendendlet()=Base58.check_encoded_prefixPublic_key_hash.b58check_encoding"tz3"36openHacl.P256modulePublic_key=structtypet=Hacl.publickeyletname="P256.Public_key"lettitle="A P256 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=pk_of_bytes_without_validationletsize_=pk_sizetypeBase58.data+=Dataoftletb58check_encoding=Base58.register_encoding~prefix:Base58.Prefix.p256_public_key~length:(size())~to_raw:to_string~of_raw:of_string_opt~wrap:(funx->Datax)let()=Base58.check_encoded_prefixb58check_encoding"p2pk"55lethashv=Public_key_hash.hash_bytes[to_bytesv]includeCompare.Make(structtypenonrect=tletcompare=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="P256.Secret_key"lettitle="A P256 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.p256_secret_key~length:size~to_raw:to_string~of_raw:of_string_opt~wrap:(funx->Datax)let()=Base58.check_encoded_prefixb58check_encoding"p2sk"54includeCompare.Make(structtypenonrect=tletcompare=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.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="P256"lettitle="A P256 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.p256_signature~length:size~to_raw:to_string~of_raw:of_string_opt~wrap:(funx->Datax)let()=Base58.check_encoded_prefixb58check_encoding"p2sig"98includeHelpers.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=of_bytes_exn(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"P256.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"P256.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])includeCompare.Make(structtypenonrect=tletcompare=Bytes.compareend)