-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest-add.js
More file actions
executable file
·122 lines (101 loc) · 3.25 KB
/
test-add.js
File metadata and controls
executable file
·122 lines (101 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict';
import { safeJSONstringify } from '../../../lib/internal.js';
import { inputProof } from '../../inputProof.js';
import { loadFhevmPublicKeyConfig } from '../../pubkeyCache.js';
import { userDecrypt } from '../../userDecrypt.js';
import { logCLI, parseCommonOptions } from '../../utils.js';
import { FHETestAddresses } from './fheTest.js';
import { ethers } from 'ethers';
// npx . test add --type euint32 --value 123 --network testnet
// npx . test add --type euint32 --value 123 --network devnet
export async function testFHETestAddCommand(options) {
const { config, provider, signer, zamaFhevmApiKey } =
parseCommonOptions(options);
const fheTypedValues = [
{
value: BigInt(options.value),
fheType: options.type,
},
];
if (!FHETestAddresses[config.name]) {
logCLI(`❌ FHETest is not deployed on network ${config.name}`, options);
process.exit(1);
}
const contractAddress = FHETestAddresses[config.name];
// Turn 'euint32' into 'Euint32'
const t = 'E' + options.type.substring(1);
const addFuncName = `add${t}`;
const getFuncName = `get${t}`;
const { publicKey, publicParams } = await loadFhevmPublicKeyConfig(
config,
options,
);
const o = await inputProof({
fheTypedValues,
config,
publicKey,
publicParams,
zamaFhevmApiKey,
options,
});
console.log(safeJSONstringify(o, 2));
const contract = new ethers.Contract(
contractAddress,
[
`function ${addFuncName}(bytes32,bytes) external`,
`function ${getFuncName}() view returns (bytes32)`,
],
signer,
);
const handle = await contract[getFuncName]();
logCLI(`🏈 handle: ${handle}`);
const current = await userDecrypt({
handleContractPairs: [{ handle, contractAddress }],
contractAddresses: [contractAddress],
signer,
config,
zamaFhevmApiKey,
options,
});
logCLI(`🏀 handle clear value: ${current[handle]}`);
logCLI(`💥 FHE.add(${handle}, ${o.handles[0]}) ...`);
/** @type {import('ethers').ContractTransactionResponse} */
const tx = await contract[addFuncName](o.handles[0], o.inputProof);
logCLI(`🛺 tx: ${tx.hash} ...`);
/** @type {import('ethers').ContractTransactionReceipt} */
const txReceipt = await tx.wait();
logCLI(`- tx status: ${txReceipt.status}`);
logCLI(`- tx gas used: ${txReceipt.gasUsed}`);
logCLI(`- tx gas price: ${txReceipt.gasPrice}`);
const newHandle = await contract[getFuncName]();
logCLI(`🏈 new handle: ${newHandle}`);
const next = await userDecrypt({
handleContractPairs: [{ handle: newHandle, contractAddress }],
contractAddresses: [contractAddress],
signer,
config,
zamaFhevmApiKey,
options,
});
const max = {
ebool: 1n,
euint8: 0xffn,
euint16: 0xffffn,
euint32: 0xffffffffn,
euint64: 0xffffffffffffffffn,
euint128: 0xffffffffffffffffffffffffffffffffn,
};
if (
BigInt(next[newHandle]) !==
BigInt(current[handle] + fheTypedValues[0].value) % (max[options.type] + 1n)
) {
logCLI(`clear: ${BigInt(next[newHandle])}`);
logCLI(
`expected: ${BigInt(current[handle] + fheTypedValues[0].value) % (max[options.type] + 1n)}`,
);
logCLI(`❌ Test Failed!`);
throw new Error(`Test Failed!`);
} else {
logCLI(`✅ Test Succeeded!`);
}
}