123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2019-2020 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. *)(* *)(*****************************************************************************)typetransaction=Sapling.UTXO.transactionlettransaction_encoding=Sapling.UTXO.transaction_encoding(* The two data structures in the state are all ordered by position, a diff
contains the elements starting from an offset position up to the most recent
position. A diff can be applied to a state stored in a context to obtain a
new state.
Diffs are used by the Michelson interpreter during the evaluation of smart
contracts to keep a temporary state that may be discarded.
Diffs are also returned by an RPC to allow a client to synchronize its own
state with the chain.
*)typediff={commitments_and_ciphertexts:(Sapling.Commitment.t*Sapling.Ciphertext.t)list;nullifiers:Sapling.Nullifier.tlist;}letdiff_encoding=letopenData_encodinginconv(fund->(d.commitments_and_ciphertexts,d.nullifiers))(fun(commitments_and_ciphertexts,nullifiers)->(matchcommitments_and_ciphertextswith|[]->()|(_cm_hd,ct_hd)::rest->letmemo_size=Sapling.Ciphertext.get_memo_sizect_hdinList.iter(fun(_cm,ct)->assert(Compare.Int.(Sapling.Ciphertext.get_memo_sizect=memo_size)))rest);{commitments_and_ciphertexts;nullifiers})(obj2(req"commitments_and_ciphertexts"(list(tup2Sapling.Commitment.encodingSapling.Ciphertext.encoding)))(req"nullifiers"(listSapling.Nullifier.encoding)))moduleMemo_size=structtypet=intletencoding=Data_encoding.uint16letequal=Compare.Int.(=)letmax_uint16=0xffffletmax_uint16_z=Z.of_intmax_uint16leterr=Error("a positive 16-bit integer (between 0 and "^string_of_intmax_uint16^")")letparse_zz=ifCompare.Z.(Z.zero<=z)&&Compare.Z.(z<=max_uint16_z)thenOk(Z.to_intz)elseerrletunparse_to_z=Z.of_intendlettransaction_get_memo_size(transaction:Sapling.UTXO.transaction)=matchtransaction.outputswith|[]->None|{ciphertext;_}::_->(* Encoding ensures all ciphertexts have the same memo size. *)Some(Sapling.Ciphertext.get_memo_sizeciphertext)openCache_memory_helpers(* This should be exported by [lib_sapling] rather than implemented here. *)letinput_in_memory_size=(* type input =
* Sapling.UTXO.input = {
* cv : Sapling.CV.t;
* nf : Sapling.Nullifier.t;
* rk : Sapling.UTXO.rk;
* proof_i : Sapling.UTXO.spend_proof;
* signature : Sapling.UTXO.spend_sig;
* } *)letcv_size=string_size_gen32inletnf_size=string_size_gen32inletrk_size=string_size_gen32inletproof_i_size=string_size_gen@@(48+96+48)inletsignature_size=string_size_gen64inheader_size+!(word_size*?5)+!cv_size+!nf_size+!rk_size+!proof_i_size+!signature_sizeletciphertext_size=(* type t = {
* cv : CV.t;
* epk : DH.epk;
* payload_enc : Bytes.t;
* nonce_enc : Crypto_box.nonce;
* payload_out : Bytes.t;
* nonce_out : Crypto_box.nonce;
* } *)letcv_size=string_size_gen32inletepk_size=string_size_gen32inletnonce_enc_size=string_size_gen24(* from lib_hacl_glue/unix/hacl.ml:Nonce.size *)inletpayload_out_size=string_size_gen(32+32+16)(* from lib_sapling/core.ml:Ciphertext.encoding *)inletnonce_out_size=string_size_gen24inletfixed_payload_data_size=11+8+32+16+4(* from lib_sapling/core.ml:Ciphertext.get_memo_size *)infunmemo_size->letpayload_size=string_size_gen(memo_size+fixed_payload_data_size)inheader_size+!(word_size*?6)+!cv_size+!epk_size+!payload_size+!nonce_enc_size+!payload_out_size+!nonce_out_sizeletoutput_in_memory_size=(* type output = {
* cm : Commitment.t;
* proof_o : output_proof;
* ciphertext : Ciphertext.t;
* } *)letcm_size=string_size_gen32inletproof_o_size=string_size_gen@@(48+96+48)inletciphertext_size=ciphertext_sizeinfunmemo_size->header_size+!(word_size*?3)+!cm_size+!proof_o_size+!ciphertext_sizememo_size(** Returns an approximation of the in-memory size of a Sapling transaction. *)lettransaction_in_memory_size(transaction:Sapling.UTXO.transaction)=(* type transaction =
* transaction = {
* inputs : Sapling.UTXO.input list;
* outputs : Sapling.UTXO.output list;
* binding_sig : Sapling.UTXO.binding_sig;
* balance : int64;
* root : Sapling.Hash.t;
* } *)letbinding_sig_size=string_size_gen64inletbalance_size=int64_sizeinletroot_size=string_size_gen32inletinputs=List.lengthtransaction.inputsinletoutputs=List.lengthtransaction.outputsinletmemo_size=Option.value~default:0(transaction_get_memo_sizetransaction)inheader_size+!(word_size*?5)+!(list_cell_sizeinput_in_memory_size*?inputs)+!(list_cell_size(output_in_memory_sizememo_size)*?outputs)+!binding_sig_size+!balance_size+!root_size(** Returns an approximation of the in-memory size of a Sapling diff. *)letdiff_in_memory_size({commitments_and_ciphertexts;nullifiers}:diff)=letcms_and_cts=List.lengthcommitments_and_ciphertextsinletnfs=List.lengthnullifiersinletcm_size=string_size_gen32inletnf_size=string_size_gen32inletmemo_size=(* All memo_size in a diff should be equal (see invariant enforced by
[diff] encoding above) *)matchcommitments_and_ciphertextswith|[]->0|(_,ct)::_->Sapling.Ciphertext.get_memo_sizectinheader_size+!(word_size*?2)+!list_cell_size(boxed_tup2cm_size(ciphertext_sizememo_size))*?cms_and_cts+!(list_cell_sizenf_size*?nfs)