Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/default value #66

Merged
merged 1 commit into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions codegen/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,14 @@
return Impl.reencrypt(euint{i}.unwrap(ciphertext), publicKey);
}}

function reencrypt(euint{i} ciphertext, bytes32 publicKey, uint{i} defaultValue) internal view returns (bytes memory reencrypted) {{
if (euint{i}.unwrap(ciphertext) != 0) {{
return Impl.reencrypt(euint{i}.unwrap(ciphertext), publicKey);
}} else {{
return Impl.reencrypt(euint{i}.unwrap(asEuint{i}(defaultValue)), publicKey);
}}
}}

function req(euint{i} ciphertext) internal view {{
Impl.req(euint{i}.unwrap(ciphertext));
}}
Expand Down
6 changes: 1 addition & 5 deletions examples/BlindAuction.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ contract BlindAuction is EIP712WithModifier {
onlySignedPublicKey(publicKey, signature)
returns (bytes memory)
{
if (TFHE.isInitialized(bids[msg.sender])) {
return TFHE.reencrypt(bids[msg.sender], publicKey);
} else {
return TFHE.reencrypt(TFHE.asEuint32(0), publicKey);
}
return TFHE.reencrypt(bids[msg.sender], publicKey, 0);
}

// Returns the user bid
Expand Down
12 changes: 2 additions & 10 deletions examples/EncryptedERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ contract EncryptedERC20 is EIP712WithModifier {
onlySignedPublicKey(publicKey, signature)
returns (bytes memory)
{
if (TFHE.isInitialized(totalSupply)) {
return TFHE.reencrypt(totalSupply, publicKey);
} else {
return TFHE.reencrypt(TFHE.asEuint32(0), publicKey);
}
return TFHE.reencrypt(totalSupply, publicKey, 0);
}

// Returns the balance of the caller encrypted under the provided public key.
Expand All @@ -71,11 +67,7 @@ contract EncryptedERC20 is EIP712WithModifier {
onlySignedPublicKey(publicKey, signature)
returns (bytes memory)
{
if (TFHE.isInitialized(balances[msg.sender])) {
return TFHE.reencrypt(balances[msg.sender], publicKey);
} else {
return TFHE.reencrypt(TFHE.asEuint32(0), publicKey);
}
return TFHE.reencrypt(balances[msg.sender], publicKey, 0);
}

// Sets the `encryptedAmount` as the allowance of `spender` over the caller's tokens.
Expand Down
48 changes: 48 additions & 0 deletions lib/TFHE.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,22 @@ library TFHE {
return Impl.reencrypt(euint8.unwrap(ciphertext), publicKey);
}

function reencrypt(
euint8 ciphertext,
bytes32 publicKey,
uint8 defaultValue
) internal view returns (bytes memory reencrypted) {
if (euint8.unwrap(ciphertext) != 0) {
return Impl.reencrypt(euint8.unwrap(ciphertext), publicKey);
} else {
return
Impl.reencrypt(
euint8.unwrap(asEuint8(defaultValue)),
publicKey
);
}
}

function req(euint8 ciphertext) internal view {
Impl.req(euint8.unwrap(ciphertext));
}
Expand All @@ -2195,6 +2211,22 @@ library TFHE {
return Impl.reencrypt(euint16.unwrap(ciphertext), publicKey);
}

function reencrypt(
euint16 ciphertext,
bytes32 publicKey,
uint16 defaultValue
) internal view returns (bytes memory reencrypted) {
if (euint16.unwrap(ciphertext) != 0) {
return Impl.reencrypt(euint16.unwrap(ciphertext), publicKey);
} else {
return
Impl.reencrypt(
euint16.unwrap(asEuint16(defaultValue)),
publicKey
);
}
}

function req(euint16 ciphertext) internal view {
Impl.req(euint16.unwrap(ciphertext));
}
Expand All @@ -2220,6 +2252,22 @@ library TFHE {
return Impl.reencrypt(euint32.unwrap(ciphertext), publicKey);
}

function reencrypt(
euint32 ciphertext,
bytes32 publicKey,
uint32 defaultValue
) internal view returns (bytes memory reencrypted) {
if (euint32.unwrap(ciphertext) != 0) {
return Impl.reencrypt(euint32.unwrap(ciphertext), publicKey);
} else {
return
Impl.reencrypt(
euint32.unwrap(asEuint32(defaultValue)),
publicKey
);
}
}

function req(euint32 ciphertext) internal view {
Impl.req(euint32.unwrap(ciphertext));
}
Expand Down