|
| 1 | +# BITE2 Sample Test |
| 2 | + |
| 3 | +This document describes the **BITE2** test case implemented in `test.js` (specifically the `runSampleBITE2` function). This test demonstrates the capabilities of the BITE2 protocol in handling encrypted data within a smart contract context. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The BITE2 test validates an end-to-end workflow where: |
| 8 | +1. Data is encrypted on the client side. |
| 9 | +2. Encrypted data is submitted to a smart contract on the blockchain. |
| 10 | +3. The blockchain (via a trusted execution environment) decrypts the data securely. |
| 11 | +4. The decrypted data is used in on-chain logic to update the contract state. |
| 12 | + |
| 13 | +## The Smart Contract (`Game.sol`) |
| 14 | + |
| 15 | +The test relies on a Solidity contract named `Game`. This contract implements a simple game mechanic to prove that decryption occurred correctly: |
| 16 | + |
| 17 | +* **State Variables**: |
| 18 | + * `encrypted`: Stores the encrypted bytes submitted by the user. |
| 19 | + * `plaintext`: Stores plaintext bytes submitted by the user for comparison/game logic. |
| 20 | + * `sumDecryptedTotal`: Stores the sum of the decrypted values (used for verification). |
| 21 | + * `userWon`: A boolean flag indicating the game result. |
| 22 | + |
| 23 | +* **Key Functions**: |
| 24 | + * `submitEncrypted(bytes)`: Adds encrypted data to the state. |
| 25 | + * `submitPlaintext(bytes)`: Adds plaintext data to the state. |
| 26 | + * `decryptAndExecute()`: Packages the encrypted and plaintext data and performs a `staticcall` to the precompiled `submitCTX` stored at address `0x14`. The `submitCTX` precompile creates a BITE2 Transaction (Confidential Transaction - CTX), which is then added to the next block. |
| 27 | + * `onDecrypt(bytes[], bytes[])`: Called as part of the execution flow to process the now-decrypted data. It calculates the sums of the values and determines if the user "won" (difference between sums < 101). Every CTX created via the `submitCTX` precompile is sent to the `onDecrypt(bytes[], bytes[])` function of the same address that initiated the call to `submitCTX`. |
| 28 | + |
| 29 | +## Test Steps |
| 30 | + |
| 31 | +The `runSampleBITE2` function performs the following steps: |
| 32 | + |
| 33 | +1. **Setup & Deployment**: |
| 34 | + * Initializes the `BITE` client and Ethers.js provider. |
| 35 | + * Deploys the `Game` smart contract using pre-compiled bytecode. |
| 36 | + |
| 37 | +2. **Encrypted Data Submission**: |
| 38 | + * Generates 5 random numbers (range 50-249). |
| 39 | + * Encrypts each number using `bite.encryptMessage(hexValue)`. |
| 40 | + * Calls `submitEncrypted` on the contract to store these values. |
| 41 | + |
| 42 | +3. **Plaintext Data Submission**: |
| 43 | + * Generates 5 random numbers. |
| 44 | + * Calls `submitPlaintext` on the contract. |
| 45 | + |
| 46 | +4. **Trigger Decryption**: |
| 47 | + * Calls `contract.decryptAndExecute()`. This initiates the secure decryption process on-chain. |
| 48 | + |
| 49 | +5. **Verification**: |
| 50 | + * **Block Wait**: Waits for the `decryptAndExecute` transaction to be mined and for the subsequent block where the `onDecrypt` transaction is executed. |
| 51 | + * **Decryption Check**: Calls `getSumDecrypted()` to retrieve the sum calculated by the contract. It compares this against the sum of the original numbers generated in Step 2. |
| 52 | + * **Logic Check**: Calls `didUserWin()` to see the game result. It compares this against a locally calculated result to ensure the on-chain logic (subtraction and comparison of decrypted values) executed correctly. |
| 53 | + |
| 54 | +## Running the Test |
| 55 | + |
| 56 | +The test is executed as part of the `tests/test.js` script: |
| 57 | + |
| 58 | +```bash |
| 59 | +# From the project root |
| 60 | +node tests/test.js <providerUrl> <chainID> <privateKey> |
| 61 | +``` |
| 62 | + |
| 63 | +* `providerUrl`: RPC endpoint of the SKALE chain. |
| 64 | +* `chainID`: Chain ID of the SKALE chain. |
| 65 | +* `privateKey`: Private key for the account sending transactions. |
| 66 | + |
| 67 | +## Success Criteria |
| 68 | + |
| 69 | +The test passes if: |
| 70 | +1. The transaction to `decryptAndExecute` succeeds. |
| 71 | +2. The `sumDecryptedTotal` field in the contract matches the sum of the validation numbers. |
| 72 | +3. The `userWon` status in the contract matches the expected result based on the random numbers generated. |
0 commit comments