@cofhe/mock-contracts 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@cofhe/mock-contracts",
3
+ "version": "0.1.0",
4
+ "description": "Mock smart contracts for testing CoFHE with FHE primitives locally",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "sideEffects": false,
9
+ "license": "MIT",
10
+ "files": [
11
+ "dist/**",
12
+ "contracts/**/*.sol",
13
+ "foundry/**/*.sol",
14
+ "test/**/*.sol",
15
+ "foundry.toml",
16
+ "remappings.txt"
17
+ ],
18
+ "keywords": [
19
+ "blockchain",
20
+ "ethereum",
21
+ "smart-contracts",
22
+ "solidity",
23
+ "FHE",
24
+ "encryption",
25
+ "privacy",
26
+ "coprocessor",
27
+ "fhenix",
28
+ "cofhe"
29
+ ],
30
+ "dependencies": {
31
+ "@fhenixprotocol/cofhe-contracts": "0.0.13",
32
+ "@openzeppelin/contracts": "^5.0.0",
33
+ "@openzeppelin/contracts-upgradeable": "^5.0.0"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "^20.0.0",
37
+ "forge-std": "github:foundry-rs/forge-std",
38
+ "tsup": "^8.0.2",
39
+ "hardhat": "^2.24.1",
40
+ "@cofhe/tsconfig": "0.1.0"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public"
44
+ },
45
+ "scripts": {
46
+ "build": "forge build && npx tsx scripts/build-artifacts.ts && tsup",
47
+ "test": "forge test -vvv",
48
+ "test:coverage": "forge coverage",
49
+ "anvil": "anvil --code-size-limit 100000",
50
+ "lint": "echo 'No linting configured for Solidity'",
51
+ "type-check": "echo 'No type checking for Solidity'",
52
+ "clean": "rm -rf out && rm -rf cache",
53
+ "dev": "anvil --code-size-limit 100000"
54
+ }
55
+ }
package/remappings.txt ADDED
@@ -0,0 +1,5 @@
1
+ forge-std/=node_modules/forge-std/src/
2
+ hardhat/=node_modules/hardhat/
3
+ @openzeppelin/contracts/=node_modules/@openzeppelin/contracts/
4
+ @fhenixprotocol/cofhe-contracts/=node_modules/@fhenixprotocol/cofhe-contracts/
5
+ @cofhe/mock-contracts/=node_modules/@cofhe/mock-contracts/contracts/
@@ -0,0 +1,60 @@
1
+ // SPDX-License-Identifier: UNLICENSED
2
+ pragma solidity ^0.8.13;
3
+
4
+ import { Test } from 'forge-std/Test.sol';
5
+ import { TestBed } from '../contracts/TestBed.sol';
6
+ import { CoFheTest } from '../contracts/foundry/CoFheTest.sol';
7
+ import { FHE, InEuint32, euint8, euint256 } from '@fhenixprotocol/cofhe-contracts/FHE.sol';
8
+
9
+ contract TestBedTest is Test, CoFheTest {
10
+ TestBed private testbed;
11
+
12
+ address private user = makeAddr('user');
13
+
14
+ function setUp() public {
15
+ // optional ... enable verbose logging from fhe mocks
16
+ // setLog(true);
17
+
18
+ testbed = new TestBed();
19
+ }
20
+
21
+ function testSetNumberFuzz(uint32 n) public {
22
+ InEuint32 memory number = createInEuint32(n, user);
23
+
24
+ //must be the user who sends transaction
25
+ //or else invalid permissions from fhe allow
26
+ vm.prank(user);
27
+ testbed.setNumber(number);
28
+
29
+ assertHashValue(testbed.eNumber(), n);
30
+ }
31
+
32
+ function testOverflow() public {
33
+ euint8 a = FHE.asEuint8(240);
34
+ euint8 b = FHE.asEuint8(240);
35
+ euint8 c = FHE.add(a, b);
36
+
37
+ assertHashValue(euint8.unwrap(c), (240 + 240) % 256);
38
+ }
39
+
40
+ function testDivideByZero() public {
41
+ euint8 a = FHE.asEuint8(240);
42
+ euint8 b = FHE.asEuint8(0);
43
+ euint8 c = FHE.div(a, b);
44
+
45
+ assertHashValue(euint8.unwrap(c), type(uint8).max);
46
+ }
47
+
48
+ function test256BitsNoOverflow() public {
49
+ euint256 a = FHE.asEuint256(type(uint256).max);
50
+ euint256 b = FHE.asEuint256(type(uint256).max);
51
+ euint256 c = FHE.add(a, b);
52
+
53
+ uint256 expected;
54
+ unchecked {
55
+ expected = type(uint256).max + type(uint256).max;
56
+ }
57
+
58
+ assertHashValue(euint256.unwrap(c), expected);
59
+ }
60
+ }