compile-program
Private Voting Made Easy with Parasol
We want to run a voting system for an issue near and dear to our hearts: whether to adopt a dog as our office pet. We could poll everyone in the office to see if they approve of the idea, but that would reveal how each person voted; no one wants to be known as the person who looked Fido deep into their big, brown eyes and said you can’t be here. Instead, we can use Fully Homomorphic Encryption (FHE) to tally the votes without revealing who would prefer Fido to stay at home.
To implement this secure voting system to keep Fido, we’ll use the Parasol framework to compile and run a simple voting program. This program will accept encrypted votes either that either vote in favor or against Fido, tally them, and return the Fido’s fate without ever revealing individual votes.
#include <parasol.h>
/**
* Performs encrypted voting on a single issue.
*
* Tallies encrypted votes where voters approve or reject an issue. The issue
* passes if there are more approvals then rejections.
*
* @param[in] votes Array of encrypted votes (each vote should be +1 or -1).
* @param[in] num_votes Total number of votes in the array.
* @param[out] didTheIssuePass Encrypted boolean indicating if the issue passed.
*
* @note The [[clang::fhe_program]] attribute marks this as an FHE program.
* @note The [[clang::encrypted]] attribute indicates parameters that are passed
* in as encrypted values.
*/
[[clang::fhe_program]]
void tally_votes([[clang::encrypted]] int8_t *votes,
uint16_t num_votes,
[[clang::encrypted]] bool *didTheIssuePass) {
int16_t sum = 0;
for (uint16_t i = 0; i < num_votes; i++) {
// Use iselect8 to sanitize votes: keep the vote if it is -1 or 1, otherwise
// set the vote to 0. It is equivalent to the ternary operator.
int8_t sanitized_vote =
iselect8(votes[i] == 1 || votes[i] == -1, votes[i], 0);
sum += sanitized_vote;
}
// The issue passes if more people voted in favor of the issue.
*didTheIssuePass = sum > 0;
}
The program is standard C code with two FHE-specific annotations:
[[clang::fhe_program]]marks the function as an FHE program, and[[clang::encrypted]]marks parameters that will be passed in as ciphertexts.
To compile the program, save the code to a file (e.g., voting.c) and use the Parasol compiler to compile the result into an FHE program.
# Compile the voting program with clang, targeting the parasol CPU
# and enabling optimizations.
clang \
-target parasol \
-O2 \
voting.c \
-o voting
Congratulations! You’ve just compiled your first FHE program 🎉.
But how do we use the program to run the auction in practice? Luckily we provide the Sunscreen Secure Processing Framework (SPF) service, which allows developers to upload, run, and decrypt the results of FHE programs easily. This enables developers to focus on building their applications without worrying about the complexities of FHE or managing the underlying compute infrastructure.