/// <summary> /// Encrypt a specific `Ballot` in the context of a specific `CiphertextElectionContext` /// /// This method accepts a ballot representation that only includes `True` selections. /// It will fill missing selections for a contest with `False` values, and generate `placeholder` /// selections to represent the number of seats available for a given contest. By adding `placeholder` /// votes /// /// This method also allows for ballots to exclude passing contests for which the voter made no selections. /// It will fill missing contests with `False` selections and generate `placeholder` selections that are marked `True`. /// /// <param name="ballot">the selection in the valid input form</param> /// <param name="internalManifest">the `InternalManifest` which defines this ballot's structure</param> /// <param name="context">all the cryptographic context for the election</param> /// <param name="ballotCodeSeed">Hash from previous ballot or hash from device</param> /// <param name="nonce">an optional value used to seed the `Nonce` generated for this ballot /// if this value is not provided, the secret generating mechanism of the OS provides its own</param> /// <param name="shouldVerifyProofs">specify if the proofs should be verified prior to returning (default True)</param> /// <returns>A `CiphertextBallot`</returns> /// </summary> public static unsafe CiphertextBallot Ballot( PlaintextBallot ballot, InternalManifest internalManifest, CiphertextElectionContext context, ElementModQ ballotCodeSeed, ElementModQ nonce = null, bool shouldVerifyProofs = true) { if (nonce == null) { var status = NativeInterface.Encrypt.Ballot( ballot.Handle, internalManifest.Handle, context.Handle, ballotCodeSeed.Handle, shouldVerifyProofs, out NativeCiphertextBallot ciphertext); if (status != Status.ELECTIONGUARD_STATUS_SUCCESS) { Console.WriteLine($"Encrypt Ballot Error Status: {status}"); } return(new CiphertextBallot(ciphertext)); } else { var status = NativeInterface.Encrypt.Ballot( ballot.Handle, internalManifest.Handle, context.Handle, ballotCodeSeed.Handle, nonce.Handle, shouldVerifyProofs, out NativeCiphertextBallot ciphertext); if (status != Status.ELECTIONGUARD_STATUS_SUCCESS) { Console.WriteLine($"EncryptWithNonce Ballot Error Status: {status}"); } return(new CiphertextBallot(ciphertext)); } }
/// <summary> /// Given an encrypted BallotContest, validates the encryption state against /// a specific encryption seed and public key /// by verifying the accumulated sum of selections match the proof. /// Calling this function expects that the object is in a well-formed encrypted state /// with the `ballot_selections` populated with valid encrypted ballot selections, /// the ElementModQ `description_hash`, the ElementModQ `crypto_hash`, /// and the ConstantChaumPedersenProof all populated. /// Specifically, the seed hash in this context is the hash of the ContestDescription, /// or whatever `ElementModQ` was used to populate the `description_hash` field. /// </summary> public unsafe bool IsValidEncryption( ElementModQ encryptionSeed, ElementModP elGamalPublicKey, ElementModQ cryptoExtendedBaseHash) { return(NativeInterface.CiphertextBallotContest.IsValidEncryption( Handle, encryptionSeed.Handle, elGamalPublicKey.Handle, cryptoExtendedBaseHash.Handle)); }
unsafe internal ElGamalKeyPair(ElementModQ secretKey) { var status = NativeInterface.ElGamalKeyPair.New( secretKey.Handle, out Handle); if (status != Status.ELECTIONGUARD_STATUS_SUCCESS) { Console.WriteLine($"ElGamalKeyPair Error Status: {status}"); } }
/// <Summary> /// Decrypt the ciphertext directly using the provided secret key. /// /// This is a convenience accessor useful for some use cases. /// This method should not be used by consumers operating in live secret ballot elections. /// </Summary> unsafe public ulong?Decrypt(ElementModQ secretKey) { ulong plaintext = 0; var status = NativeInterface.ElGamalCiphertext.DecryptWithSecret( Handle, secretKey.Handle, ref plaintext); if (status != Status.ELECTIONGUARD_STATUS_SUCCESS) { Console.WriteLine($"Decrypt Error Status: {status}"); return(null); } return(plaintext); }
public unsafe CiphertextElectionContext(ulong numberOfGuardians, ulong quorum, ElementModP publicKey, ElementModQ commitmentHash, ElementModQ manifestHash) { var status = NativeInterface.CiphertextElectionContext.Make( numberOfGuardians, quorum, publicKey.Handle, commitmentHash.Handle, manifestHash.Handle, out Handle); if (status != Status.ELECTIONGUARD_STATUS_SUCCESS) { Console.WriteLine($"CiphertextElectionContext Error Status: {status}"); } }
/// <summary> /// Encrypts a message with a given random nonce and an ElGamal public key. /// /// <param name="plaintext"> Message to elgamal_encrypt; must be an integer in [0,Q). </param> /// <param name="nonce"> Randomly chosen nonce in [1,Q). </param> /// <param name="publicKey"> ElGamal public key. </param> /// <returns>A ciphertext tuple.</returns> /// </summary> unsafe public static ElGamalCiphertext Encrypt( ulong plaintext, ElementModQ nonce, ElementModP publicKey) { var status = NativeInterface.ElGamal.Encrypt( plaintext, nonce.Handle, publicKey.Handle, out NativeElGamalCiphertext ciphertext); if (status != Status.ELECTIONGUARD_STATUS_SUCCESS) { Console.WriteLine($"Encrypt Error Status: {status}"); return(null); } return(new ElGamalCiphertext(ciphertext)); }
protected override bool Free() { if (IsClosed) { return(true); } var status = ElementModQ.Free(this); if (status != Status.ELECTIONGUARD_STATUS_SUCCESS) { Console.WriteLine($"ElementModQ Error Free: {status}"); return(false); } return(true); }
/// <summary> /// Encrypt a specific `Ballot` in the context of a specific `CiphertextElectionContext` /// /// This method accepts a ballot representation that only includes `True` selections. /// It will fill missing selections for a contest with `False` values, and generate `placeholder` /// selections to represent the number of seats available for a given contest. By adding `placeholder` /// votes /// /// This method also allows for ballots to exclude passing contests for which the voter made no selections. /// It will fill missing contests with `False` selections and generate `placeholder` selections that are marked `True`. /// /// This version of the encrypt method returns a `compact` version of the ballot that includes a minimal representation /// of the plaintext ballot along with the crypto parameters that are required to expand the ballot /// /// <param name="ballot">the selection in the valid input form</param> /// <param name="internalManifest">the `InternalManifest` which defines this ballot's structure</param> /// <param name="context">all the cryptographic context for the election</param> /// <param name="ballotCodeSeed">Hash from previous ballot or hash from device</param> /// <param name="nonceSeed">an optional value used to seed the `Nonce` generated for this ballot /// if this value is not provided, the secret generating mechanism of the OS provides its own</param> /// <param name="shouldVerifyProofs">specify if the proofs should be verified prior to returning (default True)</param> /// <returns>A `CiphertextBallot`</returns> /// </summary> public static unsafe CompactCiphertextBallot CompactBallot( PlaintextBallot ballot, InternalManifest internalManifest, CiphertextElectionContext context, ElementModQ ballotCodeSeed, bool verifyProofs = true) { var status = NativeInterface.Encrypt.CompactBallot( ballot.Handle, internalManifest.Handle, context.Handle, ballotCodeSeed.Handle, verifyProofs, out NativeCompactCiphertextBallot ciphertext); if (status != Status.ELECTIONGUARD_STATUS_SUCCESS) { Console.WriteLine($"Encrypt CompactBallot Error Status: {status}"); } return(new CompactCiphertextBallot(ciphertext)); }
/// <Summary> /// Make an elgamal keypair from a secret. /// </Summary> unsafe static public ElGamalKeyPair FromSecret(ElementModQ secretKey) { return(new ElGamalKeyPair(secretKey)); }