Skip to content

Commit

Permalink
Merge pull request #66 from zama-ai/feature/default-value
Browse files Browse the repository at this point in the history
Feature/default value
  • Loading branch information
immortal-tofu authored Jun 30, 2023
2 parents 4c9eafd + 83b9e00 commit 67131a0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
8 changes: 8 additions & 0 deletions codegen/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,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 @@ -2482,6 +2482,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 @@ -2507,6 +2523,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 @@ -2532,6 +2564,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

0 comments on commit 67131a0

Please sign in to comment.