feat(coprocessor): add FHE.sum native opcode (op 28) end-to-end#2274
feat(coprocessor): add FHE.sum native opcode (op 28) end-to-end#2274
FHE.sum native opcode (op 28) end-to-end#2274Conversation
Coprocessor Changed Lines CoverageCoverage of added/modified lines in Per-file breakdownDiff CoverageDiff: origin/main...HEAD, staged and unstaged changes
Summary
coprocessor/fhevm-engine/fhevm-engine-common/src/tfhe_ops.rscoprocessor/fhevm-engine/host-listener/src/database/tfhe_event_propagate.rs |
f7bffd5 to
08912b5
Compare
9cbb51a to
80a4f8d
Compare
FHE.sum native opcode (op 35) end-to-end
80a4f8d to
d44bb67
Compare
d44bb67 to
b550cc9
Compare
|
Were make update-conformance && make prettier run? |
b550cc9 to
ba98167
Compare
ba98167 to
52c0638
Compare
|
|
||
| function sum(bytes32[] memory values) internal returns (bytes32 result) { | ||
| CoprocessorConfig storage $ = getCoprocessorConfig(); | ||
| result = IFHEVMExecutor($.CoprocessorAddress).fheSum(values); |
There was a problem hiding this comment.
here it could degenerate case (size 1 or 2)
There was a problem hiding this comment.
The check is already happening here:
fhevm/host-contracts/contracts/FHEVMExecutor.sol
Lines 665 to 680 in c69a30d
I think there is no need to add it twice in the function body. It will also duplicate maintenance if we would like to change something
There was a problem hiding this comment.
Sorry the comment was unclear, I think we should handle degenerate case 0 (sum([]) = 0), 1 and 2. S
E.g. a contract handling a table that start empty and grows and does a sum on it, it forces a developer to handle size 0 1 and 2 manually but there is no real reason for that.
There was a problem hiding this comment.
Hey @obatirou we need a second opinion here.
Would you say it is best to handle empty array or array with one element or throw error?
@rudy-6-4 has a point here that when writing generic code these are valid operations.
python
Python 3.13.5 (main, Jun 25 2025, 18:55:22) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> sum([])
0
>>> sum([1])
1
>>> sum([2])
2
>>> exit()
Additionally to that in case of 0 do you think we should return trivially encrypted 0?
There was a problem hiding this comment.
Thanks for flagging it @rudy-6-4 . Discussed this offline and agreed the following:
Uninitialized element in the array: Substitute with trivialEncrypt(0) — consistent with how binary ops handle uninitialized inputs, but leaks metadata
Array length of 0 or 1: Substitute — length 0 substitute with trivialEncrypt(0) and length 1 substitute with trivialEncrypt(val[0])
Let me know what you think. A small drawback to this approach is that we send computation to copro for handles that are already predicted.
FHE.sum native opcode (op 35) end-to-endFHE.sum native opcode (op 28) end-to-end
557fc51 to
cf2d9db
Compare
cf2d9db to
c69a30d
Compare
c69a30d to
cd69a64
Compare
e622224 to
462eaad
Compare
…ion instead of reverting
462eaad to
fc2a141
Compare
Summary
FHE.sumas a native coprocessor opcode (op 28). Solidity emits a singleFheSumevent carrying the full input array; the coprocessor executes the sum using tfhe-rs.euint8,euint16,euint32,euint64,euint128.Closes: https://github.com/zama-ai/fhevm-internal/issues/1226