Skip to content

Instantly share code, notes, and snippets.

@samtay
Created September 19, 2023 18:08
Show Gist options
  • Save samtay/ba8be637329c24e3cfd50ab8dc86c31a to your computer and use it in GitHub Desktop.
Save samtay/ba8be637329c24e3cfd50ab8dc86c31a to your computer and use it in GitHub Desktop.
Parasol - Single Key - Private Tx Example
import "sunscreen/src/FHE.sol";
contract PrivateTxExample {
// Encrypted balances
mapping(bytes => bytes) balances;
constructor() {
// Instantiate our FHE subcontract
fhe = new FHE();
}
// Register a new user with their public key
// Under single-key model, the user must submit an encryption of zero
// for their initial balance. Typically this would be accompanied by a ZKP,
// proving that the amount is indeed zero.
function register(bytes memory publicKey, bytes memory encryptedZero) public {
// Don't overwrite someone's balance
require(balances[publicKey].length == 0);
// Register the new user with a balance of zero
balances[publicKey] = encryptedZero;
}
// Execute a private transfer
// Note: would typically require a ZKP proving amounts are equal and valid and amount <= sender's balance
function transfer(
bytes memory senderPk,
bytes memory receiverPk,
bytes memory amountSender,
bytes memory amountReceiver
) public {
balances[senderPk] = fhe.subtractUint256EncEnc(senderPk, balances[senderPk], amountSender);
balances[receiverPk] = fhe.addUint256EncEnc(receiverPk, balances[receiverPk], amountReceiver);
}
/// Retrieve user's encrypted balance
function balance(bytes memory publicKey) public view returns (bytes memory) {
return balances[publicKey];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment