Class CswProof


  • public class CswProof
    extends Object
    • Constructor Detail

      • CswProof

        public CswProof()
    • Method Detail

      • setup

        public static boolean setup​(ProvingSystemType psType,
                                    int rangeSize,
                                    int numCustomFields,
                                    boolean isConstantPresent,
                                    Optional<Integer> segmentSize,
                                    String provingKeyPath,
                                    String verificationKeyPath,
                                    boolean zk,
                                    int maxProofPlusVkSize,
                                    boolean compressPk,
                                    boolean compressVk)
        Generate (provingKey, verificationKey) pair for this circuit.
        Parameters:
        psType - - proving system to be used
        rangeSize - - number of blocks between `mcbScTxsComStart` and `mcbScTxsComEnd`
        numCustomFields - - exact number of custom fields the circuit must support
        isConstantPresent - - whether the circuit must support the presence of a constant
        segmentSize - - the segment size to be used to generate (pk, vk). Must be smaller equal than the segment size passed to the ProvingSystem.generateDLogKeys() method. If not specified, it will default to the same size as the one passed to ProvingSystem.generateDLogKeys() method.
        provingKeyPath - - file path to which saving the proving key
        verificationKeyPath - - file path to which saving the verification key
        maxProofPlusVkSize - - maximum allowed size for proof + vk
        compressPk - - if the proving key must be saved to provingKeyPath in compressed form
        compressVk - - if the verification key must be saved to verificationKeyPath in compressed form
        Returns:
        true if (pk, vk) generation and saving to file was successfull, false otherwise
      • setup

        public static boolean setup​(ProvingSystemType psType,
                                    int rangeSize,
                                    int numCustomFields,
                                    boolean isConstantPresent,
                                    Optional<Integer> segmentSize,
                                    String provingKeyPath,
                                    String verificationKeyPath,
                                    boolean zk,
                                    int maxProofPlusVkSize)
        Generate (provingKey, verificationKey) pair for this circuit.
        Parameters:
        psType - - proving system to be used
        rangeSize - - number of blocks between `mcbScTxsComStart` and `mcbScTxsComEnd`
        numCustomFields - - exact number of custom fields the circuit must support
        isConstantPresent - - whether the circuit must support the presence of a constant
        segmentSize - - the segment size to be used to generate (pk, vk). Must be smaller equal than the segment size passed to the ProvingSystem.generateDLogKeys() method. If not specified, it will default to the same size as the one passed to ProvingSystem.generateDLogKeys() method.
        provingKeyPath - - file path to which saving the proving key. Proving key will be saved in compressed form.
        verificationKeyPath - - file path to which saving the verification key. Verification key will be saved in compressed form.
        zk - - used to estimate the proof and vk size, tells if the proof will be created using zk or not
        maxProofPlusVkSize - - maximum allowed size for proof + vk
        Returns:
        true if (pk, vk) generation and saving to file was successfull, false otherwise
      • setup

        public static boolean setup​(ProvingSystemType psType,
                                    int rangeSize,
                                    int numCustomFields,
                                    boolean isConstantPresent,
                                    Optional<Integer> segmentSize,
                                    String provingKeyPath,
                                    String verificationKeyPath,
                                    int maxProofPlusVkSize)
        Generate (provingKey, verificationKey) pair for this circuit.
        Parameters:
        psType - - proving system to be used
        rangeSize - - number of blocks between `mcbScTxsComStart` and `mcbScTxsComEnd`
        numCustomFields - - exact number of custom fields the circuit must support
        isConstantPresent - - whether the circuit must support the presence of a constant
        segmentSize - - the segment size to be used to generate (pk, vk). Must be smaller equal than the segment size passed to the ProvingSystem.generateDLogKeys() method. If not specified, it will default to the same size as the one passed to ProvingSystem.generateDLogKeys() method.
        provingKeyPath - - file path to which saving the proving key. Proving key will be saved in compressed form.
        verificationKeyPath - - file path to which saving the verification key. Verification key will be saved in compressed form.
        maxProofPlusVkSize - - maximum allowed size for proof + vk, estimated assuming not to use zk property
        Returns:
        true if (pk, vk) generation and saving to file was successfull, false otherwise.
      • setup

        public static boolean setup​(ProvingSystemType psType,
                                    int rangeSize,
                                    int numCustomFields,
                                    boolean isConstantPresent,
                                    String provingKeyPath,
                                    String verificationKeyPath,
                                    int maxProofPlusVkSize)
        Generate (provingKey, verificationKey) pair for this circuit.
        Parameters:
        psType - - proving system to be used
        rangeSize - - number of blocks between `mcbScTxsComStart` and `mcbScTxsComEnd`
        numCustomFields - - exact number of custom fields the circuit must support
        isConstantPresent - - whether the circuit must support the presence of a constant
        provingKeyPath - - file path to which saving the proving key. Proving key will be saved in compressed form.
        verificationKeyPath - - file path to which saving the verification key. Verification key will be saved in compressed form.
        maxProofPlusVkSize - - maximum allowed size for proof + vk, estimated assuming not to use zk property
        Returns:
        true if (pk, vk) generation and saving to file was successfull, false otherwise.
      • debugCircuit

        public static Optional<String> debugCircuit​(int rangeSize,
                                                    int numCustomFields,
                                                    CswSysData sysData,
                                                    FieldElement scId,
                                                    Optional<WithdrawalCertificate> lastWcert,
                                                    Optional<CswUtxoProverData> utxoData,
                                                    Optional<CswFtProverData> ftData)
                                             throws IllegalArgumentException
        Checks if possible to create a valid proof with the supplied data. Useful to understand the reason for which proof creation fails (usually some inconsistency with input data).
        Parameters:
        rangeSize - - number of blocks between `mcbScTxsComStart` and `mcbScTxsComEnd`
        numCustomFields - - exact number of custom fields the circuit must support
        sysData - - certificate sys data.
        scId - - the id of the corresponding sidechain
        lastWcert - - the last confirmed wcert in the MC. Can be empty if SC has ceased before we have at least certs for 2 epochs (in this case we can only withdraw FT)
        utxoData - - data required to prove withdraw of a SC utxo. Must be empty if the prover wants to prove withdraw of a FT instead. If this field is present, then lastWCert and sysData.scLastWCertHash must be present too.
        ftData - - data required to prove withdraw of a FT. Must be empty if the prover wants to prove withdraw of a SC utxo instead. If present, then sysData.mcbScTxsComEnd must be present too.
        Returns:
        an Optional containing the name of the first failing constraint if the supplied data don't satisfy all the circuit's constraints, and nothing if all constraints are satisfied.
        Throws:
        IllegalArgumentException - - if inputs are not consistent
      • createProof

        public static byte[] createProof​(int rangeSize,
                                         int numCustomFields,
                                         CswSysData sysData,
                                         FieldElement scId,
                                         Optional<WithdrawalCertificate> lastWcert,
                                         Optional<CswUtxoProverData> utxoData,
                                         Optional<CswFtProverData> ftData,
                                         Optional<Integer> segmentSize,
                                         String provingKeyPath,
                                         boolean checkProvingKey,
                                         boolean zk,
                                         boolean compressed_pk,
                                         boolean compress_proof)
                                  throws IllegalArgumentException
        Compute proof for given parameters
        Parameters:
        rangeSize - - number of blocks between `mcbScTxsComStart` and `mcbScTxsComEnd`
        numCustomFields - - exact number of custom fields the circuit must support
        sysData - - certificate sys data.
        scId - - the id of the corresponding sidechain
        lastWcert - - the last confirmed wcert in the MC. Can be empty if SC has ceased before we have at least certs for 2 epochs (in this case we can only withdraw FT)
        utxoData - - data required to prove withdraw of a SC utxo. Must be empty if the prover wants to prove withdraw of a FT instead. If this field is present, then lastWCert and sysData.scLastWCertHash must be present too.
        ftData - - data required to prove withdraw of a FT. Must be empty if the prover wants to prove withdraw of a SC utxo instead. If present, then sysData.mcbScTxsComEnd must be present too.
        segmentSize - - the segment size to be used to create the proof. Must be equal to the one passed to the setup() method. If not specified, it will default to the same size as the one passed to ProvingSystem.generateDLogKeys() method.
        provingKeyPath - - file path from which reading the proving key
        checkProvingKey - - enable semantic checks on the proving key (WARNING: very expensive)
        zk - - if proof must be created using zk property or not
        compressed_pk - - if the pk read from provingKeyPath is in compressed form or not
        compress_proof - - whether to return the proof bytes in compressed form or not
        Returns:
        the proof bytes
        Throws:
        IllegalArgumentException - if utxoData is present but lastWcert is empty, or if utxoData and ftData are both present
      • createProof

        public static byte[] createProof​(int rangeSize,
                                         int numCustomFields,
                                         CswSysData sysData,
                                         FieldElement scId,
                                         Optional<WithdrawalCertificate> lastWcert,
                                         Optional<CswUtxoProverData> utxoData,
                                         Optional<CswFtProverData> ftData,
                                         Optional<Integer> segmentSize,
                                         String provingKeyPath,
                                         boolean checkProvingKey,
                                         boolean zk)
                                  throws IllegalArgumentException
        Compute proof for given parameters
        Parameters:
        rangeSize - - number of blocks between `mcbScTxsComStart` and `mcbScTxsComEnd`
        numCustomFields - - exact number of custom fields the circuit must support
        sysData - - certificate sys data.
        scId - - the id of the corresponding sidechain
        lastWcert - - the last confirmed wcert in the MC. Can be empty if SC has ceased before we have at least certs for 2 epochs (in this case we can only withdraw FT)
        utxoData - - data required to prove withdraw of a SC utxo. Must be empty if the prover wants to prove withdraw of a FT instead. If this field is present, then lastWCert and sysData.scLastWCertHash must be present too.
        ftData - - data required to prove withdraw of a FT. Must be empty if the prover wants to prove withdraw of a SC utxo instead. If present, then sysData.mcbScTxsComEnd must be present too.
        segmentSize - - the segment size to be used to create the proof. Must be equal to the one passed to the setup() method. If not specified, it will default to the same size as the one passed to ProvingSystem.generateDLogKeys() method.
        provingKeyPath - - file path from which reading the proving key, expected to be in compressed form
        checkProvingKey - - enable semantic checks on the proving key (WARNING: very expensive)
        zk - - if proof must be created using zk property or not
        Returns:
        the proof bytes
        Throws:
        IllegalArgumentException - if utxoData is present but lastWcert is empty, or if utxoData and ftData are both present
      • createProof

        public static byte[] createProof​(int rangeSize,
                                         int numCustomFields,
                                         CswSysData sysData,
                                         FieldElement scId,
                                         Optional<WithdrawalCertificate> lastWcert,
                                         Optional<CswUtxoProverData> utxoData,
                                         Optional<CswFtProverData> ftData,
                                         Optional<Integer> segmentSize,
                                         String provingKeyPath,
                                         boolean zk)
                                  throws IllegalArgumentException
        Compute proof for given parameters
        Parameters:
        rangeSize - - number of blocks between `mcbScTxsComStart` and `mcbScTxsComEnd`
        numCustomFields - - exact number of custom fields the circuit must support
        scId - - the id of the corresponding sidechain
        sysData - - certificate sys data.
        lastWcert - - the last confirmed wcert in the MC. Can be empty if SC has ceased before we have at least certs for 2 epochs (in this case we can only withdraw FT)
        utxoData - - data required to prove withdraw of a SC utxo. Must be empty if the prover wants to prove withdraw of a FT instead. If this field is present, then lastWCert and sysData.scLastWCertHash must be present too.
        ftData - - data required to prove withdraw of a FT. Must be empty if the prover wants to prove withdraw of a SC utxo instead. If present, then sysData.mcbScTxsComEnd must be present too.
        segmentSize - - the segment size to be used to create the proof. Must be equal to the one passed to the setup() method. If not specified, it will default to the same size as the one passed to ProvingSystem.generateDLogKeys() method.
        provingKeyPath - - file path from which reading the proving key, expected to be in compressed form
        zk - - if proof must be created using zk property or not
        Returns:
        the proof bytes
        Throws:
        IllegalArgumentException - if utxoData is present but lastWcert is empty, or if utxoData and ftData are both present
      • createProof

        public static byte[] createProof​(int rangeSize,
                                         int numCustomFields,
                                         CswSysData sysData,
                                         FieldElement scId,
                                         Optional<WithdrawalCertificate> lastWcert,
                                         Optional<CswUtxoProverData> utxoData,
                                         Optional<CswFtProverData> ftData,
                                         Optional<Integer> segmentSize,
                                         String provingKeyPath)
                                  throws IllegalArgumentException
        Compute proof for given parameters. Zero knowledge will be used.
        Parameters:
        rangeSize - - number of blocks between `mcbScTxsComStart` and `mcbScTxsComEnd`
        numCustomFields - - exact number of custom fields the circuit must support
        sysData - - certificate sys data.
        scId - - the id of the corresponding sidechain
        lastWcert - - the last confirmed wcert in the MC. Can be empty if SC has ceased before we have at least certs for 2 epochs (in this case we can only withdraw FT)
        utxoData - - data required to prove withdraw of a SC utxo. Must be empty if the prover wants to prove withdraw of a FT instead. If this field is present, then lastWCert and sysData.scLastWCertHash must be present too.
        ftData - - data required to prove withdraw of a FT. Must be empty if the prover wants to prove withdraw of a SC utxo instead. If present, then sysData.mcbScTxsComEnd must be present too.
        segmentSize - - the segment size to be used to create the proof. Must be equal to the one passed to the setup() method. If not specified, it will default to the same size as the one passed to ProvingSystem.generateDLogKeys() method.
        provingKeyPath - - file path from which reading the proving key, expected to be in compressed form
        Returns:
        the proof bytes
        Throws:
        IllegalArgumentException - if utxoData is present but lastWcert is empty, or if utxoData and ftData are both present
      • createProof

        public static byte[] createProof​(int rangeSize,
                                         int numCustomFields,
                                         CswSysData sysData,
                                         FieldElement scId,
                                         Optional<WithdrawalCertificate> lastWcert,
                                         Optional<CswUtxoProverData> utxoData,
                                         Optional<CswFtProverData> ftData,
                                         String provingKeyPath)
                                  throws IllegalArgumentException
        Compute proof for given parameters. Zero knowledge will be used.
        Parameters:
        rangeSize - - number of blocks between `mcbScTxsComStart` and `mcbScTxsComEnd`
        numCustomFields - - exact number of custom fields the circuit must support
        sysData - - certificate sys data.
        scId - - the id of the corresponding sidechain
        lastWcert - - the last confirmed wcert in the MC. Can be empty if SC has ceased before we have at least certs for 2 epochs (in this case we can only withdraw FT)
        utxoData - - data required to prove withdraw of a SC utxo. Must be empty if the prover wants to prove withdraw of a FT instead. If this field is present, then lastWCert and sysData.scLastWCertHash must be present too.
        ftData - - data required to prove withdraw of a FT. Must be empty if the prover wants to prove withdraw of a SC utxo instead. If present, then sysData.mcbScTxsComEnd must be present too.
        provingKeyPath - - file path from which reading the proving key, expected to be in compressed form
        Returns:
        the proof bytes
        Throws:
        IllegalArgumentException - if utxoData is present but lastWcert is empty, or if utxoData and ftData are both present
      • verifyProof

        public static boolean verifyProof​(CswSysData sysData,
                                          FieldElement scId,
                                          byte[] proof,
                                          boolean checkProof,
                                          String verificationKeyPath,
                                          boolean checkVerificationKey)
        Verify proof using the supplied parameters
        Parameters:
        sysData - - certificate sys data.
        scId - - the id of the corresponding sidechain
        proof - - the bytes of the proof to be verified, expected to be in compressed form
        checkProof - - enable semantic checks on the proof
        verificationKeyPath - - file path from which loading the verification key, expected to be in compressed form
        checkVerificationKey - - enable semantic checks on the verification key
        Returns:
        true, if proof verification was successfull, false if proof verification failed or if some errors occured during verification
      • verifyProof

        public static boolean verifyProof​(CswSysData sysData,
                                          FieldElement scId,
                                          byte[] proof,
                                          String verificationKeyPath)
        Verify proof using the supplied parameters
        Parameters:
        sysData - - certificate sys data.
        scId - - the id of the corresponding sidechain
        proof - - the bytes of the proof to be verified, expected to be in compressed form
        verificationKeyPath - - file path from which loading the verification key, expected to be in compressed form
        Returns:
        true, if proof verification was successfull, false if proof verification failed or if some errors occured during verification