123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)(** Reference specification is version 2020.1.2
https://github.com/zcash/zips/blob/master/protocol/sapling.pdf
*)(** Each instance of the Sapling protocol should be identified by a unique string
identifier which is used as anti-replay. It should typically contain a chain
identifier and the identifier of a smart-contract.
**)moduletypeT_bytes=sigtypetvalto_bytes:t->bytesvalof_bytes_exn:bytes->tendmoduletypeT_encoding=sigtypetvalencoding:tData_encoding.tendmoduletypeT_encoding_bytes=sigincludeT_bytesincludeT_encodingwithtypet:=tendmoduletypeT_encoding_compare=sigincludeT_encodingvalcompare:t->t->intend(** Spending keys allow to spend and derive viewing keys.
It contains key material and information to derive deterministically more
spending keys using the zip-32 standard (equivalent of bip-32 for ZCash).
See spec section 3.1 **)moduletypeSpending_key=sigincludeT_encoding(** Outgoing viewing keys allow to keep track of spendings from a key.
This feature is optional, a payer can decide to not keep track of its
outgoing transactions. **)typeovkvalof_bytes:bytes->toptionvalto_bytes:t->bytes(** Generate a key from a 32 byte uniformly random seed.
The source of randomness should be of cryptographic quality. **)valof_seed:bytes->t(** [derive_key k n] derives the nth key from [k] **)valderive_key:t->int32->t(** Returns the next available index from a key **)valchild_index:t->int32valb58check_encoding:tTezos_crypto.Base58.encodingend(** Viewing keys allow to see incoming and outgoing transactions without
giving the ability to spend. Stored in the zip-32 format. **)moduletypeViewing_key=sigtypespending_keyincludeT_encodingvalof_bytes:bytes->toptionvalto_bytes:t->bytesvalof_sk:spending_key->t(** Indexes to derive addresses. Starting from [default_index],
the following addresses can be derived with [index_succ] **)typeindexvalcompare_index:index->index->intvaldefault_index:indexvalindex_succ:index->indexvalindex_to_int64:index->int64valindex_of_int64:int64->indexvalindex_encoding:indexData_encoding.t(** To be given to the payer. Does not appear on the blockchain.
Different addresses can be derived from one key which is necessary to
prevent a loss of anonymity because of several payers colluding.
e.g. two payers can conclude that they are paying the same person
if they send money to the same address. **)typeaddressvaladdress_encoding:addressData_encoding.tvaladdress_b58check_encoding:addressTezos_crypto.Base58.encoding(** Returns a new address and a new index to be used for the next address
generation. **)valnew_address:t->index->index*address(** A dummy address can be generated to create dummy inputs or outputs of
value 0. Note that tokens sent to dummy addresses are lost! **)valdummy_address:unit->address(** Incoming viewing keys can be used to see transactions received to any
address generated from this key.
This feature is not enforced by the protocol, a payer could spend tokens
on-chain and transmit to the recipient invalid information. The recipient
should always check the received transactions with its ivk.
**)typeivkvalto_ivk:t->ivktypeovkvalovk_of_xfvk:t->ovkendmoduletypeWallet=sigmoduleSpending_key:Spending_keymoduleViewing_key:Viewing_keywithtypespending_key:=Spending_key.tendmoduletypeHash=sig(** Pedersen's Commitment of a transaction output (i.e. address and value).
A merkle tree is formed with the existing commitment and filled with
a default uncommitted value **)typecommitmentincludeT_encoding_compareincludeT_byteswithtypet:=tvaluncommitted:height:int->t(** Hash function to compute the merkle tree (Pedersen's hash on JubJub curve).
Height is the height we are hashing at in the merkle tree. **)valmerkle_hash:height:int->t->t->t(** Hashes and commitments are the same object but are given different types
to avoid confusing nodes and leaves. **)valof_commitment:commitment->tvalto_commitment:t->commitmentendmoduletypeUTXO=sig(** Ciphertexts are encrypted information used to retrieve payments and can
be decrypted with full keys or just ovk.
They can be stored on-chain or transmitted off-chain from payer to
recipient. **)typeciphertext(** A commitment is the equivalent of a transaction output, with the important
difference of not leaking any information. Must be stored on chain. **)typecommitment(** Commitment value. The value of a transaction output is committed
(Pedersen's commitment) separately. **)typecvtypehash(** Nullifiers are used to invalidated a commitment, that is marking it as
spent. However they can't be linked to the commitment that they invalidate.
Nullifiers are derived from a commitment and a secret key using a pseudo
random function. **)typenullifier(** Randomised signature keys.
All inputs are signed with a randomised version of a secret key. **)typerk(** Zero knowledge proofs needed to spend a transaction outputs.
See spec section 4.15 **)typespend_proof(** Signature needed to spend tokens. Computed with a randomised version of
the secret key and verifies under a randomised version of the public key.
Signs a hash of an input concatenated with an optional anti-replay string.
**)typespend_sig(** Zero-knowledge proof needed to create money.
See spec section 4.15 **)typeoutput_proof(** Contains the necessary information to spend tokens
(except the root which we include in the transaction). **)typeinput={cv:cv;nf:nullifier;rk:rk;proof_i:spend_proof;signature:spend_sig;}valinput_encoding:inputData_encoding.t(** Contains the necessary information to create tokens. **)typeoutput={cm:commitment;proof_o:output_proof;ciphertext:ciphertext;}valoutput_encoding:outputData_encoding.t(** Ties a transaction to a balance (difference between the tokens created
and spent).
Proves with the commitment values that sum of values of inputs minus
sums of values of output equals balance. **)typebinding_sigvalbinding_sig_encoding:binding_sigData_encoding.t(** Transaction that is sent to a verifier.
The root corresponds to a merkle tree where the inputs are present.
Even if this root can in principle be very old, a verifier may
keep only the last n known roots considering anything older as invalid.
[bound_data] is arbitrary data that gets signed by the Sapling keys and
can typically be used to connect the Sapling protocol to another one.
For example it can contain the recipient address of an unshield operation.
The memo_size field is checked at encoding and encoding to be the real
memo size of all outputs.
A transaction leaks the balance between inputs and outputs and the number
of inputs and outputs. Note that the number of inputs is limited to 5208
and number of outputs to 2019, by a check in the encoding.
This is important to avoid invalidating a
proof over the balance as described in section 4.12 of the spec. *)typetransaction={inputs:inputlist;outputs:outputlist;binding_sig:binding_sig;balance:int64;root:hash;bound_data:string;}(** The encoding enforces the limits on number of inputs and outputs. *)valtransaction_encoding:transactionData_encoding.t(** Maximum amount of shielded tokens. This value is imposed by
librustzcash. **)valmax_amount:int64valvalid_amount:int64->boolmoduleLegacy:sigtypetransaction_new=transaction(* This type is for backward compatibility with a previous definition of
[transaction] which didn't have any [bound_data]. *)typetransaction={inputs:inputlist;outputs:outputlist;binding_sig:binding_sig;balance:int64;root:hash;}valtransaction_encoding:transactionData_encoding.tvalcast:transaction->transaction_newendend(** Regroups what needs to be exposed to a Validator **)moduletypeValidator=sig(** Loads the ZCash parameters for Groth16, searching them in:
- [/usr/share/zcash-params]
- [${OPAM_SWITCH_PREFIX}/share/zcash-params]
- [${HOME}/.zcash-params]
Only Sapling's parameters are loaded, not Sprout's.
This function must be called before any of the proving and verification
functions requiring a context.
Usually you should not need to call this function directly as it is done
by the fist call to `with_\{proving,verification\}_ctx`.
However you can call this function in order to:
- pay its cost upfront and have more predictable latency later
- make sure that the parameters are present in the system and avoid a
failure later. *)valinit_params:unit->unitmoduleCiphertext:sigincludeT_encodingvalget_memo_size:t->intendmoduleCommitment:sigincludeT_encoding_bytesvalvalid_position:int64->boolendmoduleCV:T_encodingmoduleHash:Hashwithtypecommitment:=Commitment.tmoduleNullifier:T_encoding_comparemoduleUTXO:UTXOwithtypeciphertext:=Ciphertext.tandtypecommitment:=Commitment.tandtypecv:=CV.tandtypehash:=Hash.tandtypenullifier:=Nullifier.tmoduleVerification:sig(** A verification context.
Stores information involving the commitment values of inputs and
outputs and it is used to verify the binding_sig.
A context should created with [init], passed to [check_spend] for all
inputs and [check_output] for all outputs, passed to [final_check] and
finally freed with [free]. **)typetvalwith_verification_ctx:(t->'a)->'a(** Checks the zero-knowledge proof for spending money, the spend_sig,
and stores info in the context. String is the anti-replay. **)valcheck_spend:t->UTXO.input->Hash.t->string->bool(** Checks the zero-knowledge proof for creating money
and stores information in the context **)valcheck_output:t->UTXO.output->bool(** Utilise the context to check the balance. String is the anti-replay
string. **)valfinal_check:t->UTXO.transaction->string->boolendend(** A Diffie-Hellman key exchange is done between the payer and the recipient
to encrypt the ciphertext. This can be done off or on chain. **)moduletypeDH_esk=sigtypeesk(** Generate a random scalar to be used by the sender for DH. **)valesk_random:unit->eskendmoduletypeCommitment=sig(** Randomness used for the commitment. **)typercmtypeviewing_key_addressincludeT_encoding_bytesvalcompute:viewing_key_address->amount:int64->rcm->tvalvalid_position:int64->boolendmoduletypeCV=sigincludeT_encodingvalof_bytes:bytes->toptionend(** See spec section 3.8 **)moduletypeNullifier=sigtypercmtypeviewing_key_addresstypeviewing_keyincludeT_encoding_comparevalcompute:viewing_key_address->viewing_key->amount:int64->rcm->position:int64->tendmoduletypeRcm=sigincludeT_encodingvalrandom:unit->tvalassert_valid:t->unitend(** Signatures for Client/Validator/Storage + some functions used in tests *)moduletypeClient=sigincludeWalletmoduleRcm:RcmmoduleNullifier:Nullifierwithtypercm:=Rcm.tandtypeviewing_key_address:=Viewing_key.addressandtypeviewing_key:=Viewing_key.tmoduleCommitment:Commitmentwithtypercm:=Rcm.tandtypeviewing_key_address:=Viewing_key.addressmoduleCV:CVincludeValidatorwithmoduleCommitment:=CommitmentandmoduleCV:=CVandmoduleNullifier:=NullifiermoduleDH:DH_eskmoduleProving:sig(** A proving context.
Stores information about the commitment values and is used
for the binding_sig.
A context should created with [init], passed to [spend_proof] for all
inputs and [output_proof] for all outputs, passed to [make_binding_sig]
and finally freed with [free]. **)typet(** Randomness to randomise the signature key. **)typearvalwith_proving_ctx:(t->'a)->'avalar_random:unit->ar(** String is the anti-replay. **)valspend_sig:Spending_key.t->ar->CV.t->Nullifier.t->UTXO.rk->UTXO.spend_proof->string->UTXO.spend_sigvalspend_proof:t->Viewing_key.t->Spending_key.t->Viewing_key.address->Rcm.t->ar->amount:int64->root:Hash.t->witness:Bytes.t->CV.t*UTXO.rk*UTXO.spend_proofvaloutput_proof:t->DH.esk->Viewing_key.address->Rcm.t->amount:int64->CV.t*UTXO.output_proof(** See spec section 4.12 **)valmake_binding_sig:t->UTXO.inputlist->UTXO.outputlist->balance:int64->bound_data:string->string->UTXO.binding_sigendmoduleForge:sigmoduleInput:sig(** Necessary infos to create spend some money belonging to our key. **)typet={rcm:Rcm.t;pos:int64;amount:int64;address:Viewing_key.address;}includeT_encodingwithtypet:=tvalcompare:t->t->int(** Decrypt a ciphertext to create a input. The decryption can fail if the
description key is incorrect or the ciphertext is incorrect.
The returned bytes are a memo added by the sender **)valof_ciphertext:pos:int64->Ciphertext.t->Viewing_key.t->(Bytes.t*t)option(** Same as of_ciphertext but requires only the ovk. **)valof_ciphertext_out:pos:int64->Ciphertext.t->Spending_key.ovk->Commitment.t->(Bytes.t*t)option(** Check that a commitment corresponds to an input **)valcheck_cm:t->Commitment.t->boolend(** Necessary information to create tokens. **)moduleOutput:sigtypet={address:Viewing_key.address;amount:int64;memo:Bytes.t}(** Creates a ciphertext that the recipient can use to spend his tokens,
and our ovk can see as spent tokens. **)valto_ciphertext:t->CV.t->Viewing_key.t->Rcm.t->DH.esk->Ciphertext.t*Commitment.t(** Same as to_ciphertext but does not allow the ovk to decrypt (ie.
[of_ciphertext_out] will return None). **)valto_ciphertext_without_ovk:t->Rcm.t->DH.esk->CV.t->Ciphertext.t*Commitment.tendendend