Skip to content

Discussion: use CTR encryption for mock FHE ops#92

Open
architect-dev wants to merge 1 commit intomainfrom
test/mock-fhe-ops-with-ctr-encryption
Open

Discussion: use CTR encryption for mock FHE ops#92
architect-dev wants to merge 1 commit intomainfrom
test/mock-fhe-ops-with-ctr-encryption

Conversation

@architect-dev
Copy link
Contributor

Adds a symmetric encryption to MockFheOps.sol. This allows the mock ops to use encrypted values through the pipeline, and allows better testing of things like sealing, and makes the euint___ variables opaque instead of cleartext. Its a step towards making the hardhat mocks as similar as possible to the real chain. With the new fhenixsdk which handles the PermitV2 stuff, this is very easy to update in the clients too.

    // https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)
    // ctrSymmetricEncrypt(ctrSymmetricEncrypt(5, key), key) = 5
    function ctrSymmetricEncrypt(
        bytes memory data,
        bytes memory _key
    ) internal pure returns (bytes memory result) {
        // Store data length on stack for later use
        uint256 length = data.length;

        assembly {
            // Set result to free memory pointer
            result := mload(0x40)
            // Increase free memory pointer by length + 32
            mstore(0x40, add(add(result, length), 32))
            // Set result length
            mstore(result, length)
        }

        // Iterate over the data stepping by 32 bytes
        for (uint256 i = 0; i < length; i += 32) {

        // Generate hash of the key and offset
        bytes memory packed = abi.encodePacked(_key, i);
        bytes32 hash = keccak256(packed);

        bytes32 chunk;
        assembly {
            // Read 32-bytes data chunk
            chunk := mload(add(data, add(i, 32)))
        }
        // XOR the chunk with hash
        chunk ^= hash;
        assembly {
            // Write 32-byte encrypted chunk
            mstore(add(result, add(i, 32)), chunk)
        }
        }
    }
    ```
    
    Do you guys think this is worthwhile to explore?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant