-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aaf32d5
commit c10308d
Showing
8 changed files
with
58 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,7 @@ contract TestAsyncDecrypt is SepoliaZamaFHEVMConfig, SepoliaZamaGatewayConfig, G | |
Remember our [**Encrypted Counter**](../../getting_started/first_smart_contract.md) contract from before? Here’s an improved version of it, upgraded to support decryption: | ||
``` | ||
```solidity | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
@@ -84,47 +84,46 @@ import "fhevm/gateway/GatewayCaller.sol"; | |
/// @dev Uses TFHE library for fully homomorphic encryption operations and Gateway for decryption | ||
/// @custom:experimental This contract is experimental and uses FHE technology with decryption capabilities | ||
contract EncryptedCounter3 is SepoliaZamaFHEVMConfig, SepoliaZamaGatewayConfig, GatewayCaller { | ||
/// @dev Decrypted state variable | ||
euint8 internal counter; | ||
uint8 public decryptedCounter; | ||
|
||
constructor() { | ||
Gateway.setGateway(Gateway.defaultGatewayAddress()); | ||
|
||
// Initialize counter with an encrypted zero value | ||
counter = TFHE.asEuint8(0); | ||
TFHE.allowThis(counter); | ||
} | ||
|
||
function incrementBy(einput amount, bytes calldata inputProof) public { | ||
// Convert input to euint8 and add to counter | ||
euint8 incrementAmount = TFHE.asEuint8(amount, inputProof); | ||
counter = TFHE.add(counter, incrementAmount); | ||
TFHE.allowThis(counter); | ||
} | ||
|
||
/// @notice Request decryption of the counter value | ||
function requestDecryptCounter() public { | ||
uint256[] memory cts = new uint256[](1); | ||
cts[0] = Gateway.toUint256(counter); | ||
Gateway.requestDecryption(cts, this.callbackCounter.selector, 0, block.timestamp + 100, false); | ||
} | ||
|
||
/// @notice Callback function for counter decryption | ||
/// @param decryptedInput The decrypted counter value | ||
/// @return The decrypted value | ||
function callbackCounter(uint256, uint8 decryptedInput) public onlyGateway returns (uint8) { | ||
decryptedCounter = decryptedInput; | ||
return decryptedInput; | ||
} | ||
|
||
/// @notice Get the decrypted counter value | ||
/// @return The decrypted counter value | ||
function getDecryptedCounter() public view returns (uint8) { | ||
return decryptedCounter; | ||
} | ||
} | ||
/// @dev Decrypted state variable | ||
euint8 internal counter; | ||
uint8 public decryptedCounter; | ||
constructor() { | ||
Gateway.setGateway(Gateway.defaultGatewayAddress()); | ||
// Initialize counter with an encrypted zero value | ||
counter = TFHE.asEuint8(0); | ||
TFHE.allowThis(counter); | ||
} | ||
function incrementBy(einput amount, bytes calldata inputProof) public { | ||
// Convert input to euint8 and add to counter | ||
euint8 incrementAmount = TFHE.asEuint8(amount, inputProof); | ||
counter = TFHE.add(counter, incrementAmount); | ||
TFHE.allowThis(counter); | ||
} | ||
/// @notice Request decryption of the counter value | ||
function requestDecryptCounter() public { | ||
uint256[] memory cts = new uint256[](1); | ||
cts[0] = Gateway.toUint256(counter); | ||
Gateway.requestDecryption(cts, this.callbackCounter.selector, 0, block.timestamp + 100, false); | ||
} | ||
/// @notice Callback function for counter decryption | ||
/// @param decryptedInput The decrypted counter value | ||
/// @return The decrypted value | ||
function callbackCounter(uint256, uint8 decryptedInput) public onlyGateway returns (uint8) { | ||
decryptedCounter = decryptedInput; | ||
return decryptedInput; | ||
} | ||
/// @notice Get the decrypted counter value | ||
/// @return The decrypted counter value | ||
function getDecryptedCounter() public view returns (uint8) { | ||
return decryptedCounter; | ||
} | ||
} | ||
``` | ||
|
||
### Tests for `EncryptedCounter3` | ||
|
@@ -169,7 +168,7 @@ describe("EncryptedCounter3", function () { | |
// Wait for decryption to complete | ||
await awaitAllDecryptionResults(); | ||
|
||
// Check decrypted value (should be 3: initial 0 + three increments) | ||
// Check decrypted value (should be 5: initial 0 + an increment of 5) | ||
const decryptedValue = await this.counterContract.getDecryptedCounter(); | ||
expect(decryptedValue).to.equal(5); | ||
}); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
bcm-at-zama
Author
Contributor
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hum, now I see 5 in "expect(decryptedValue).to.equal(5);"
has it been changed elsewhere, in another PR?
it's back to being wrong
@poppyseedDev