Skip to content

Commit c88f5f7

Browse files
committed
fix(coprocessor): sync with contract fhe events
1 parent 781839b commit c88f5f7

5 files changed

Lines changed: 49 additions & 95 deletions

File tree

.github/workflows/coprocessor-cargo-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jobs:
2626
- .github/workflows/coprocessor-cargo-tests.yml
2727
- coprocessor/fhevm-engine/**
2828
- coprocessor/proto/**
29+
- host-contracts/**
2930
cargo-tests:
3031
needs: check-changes
3132
if: ${{ needs.check-changes.outputs.changes-rust-files == 'true' }}

coprocessor/fhevm-engine/coprocessor/src/tests/operators_from_events.rs

Lines changed: 38 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ pub fn supported_types() -> &'static [i32] {
1717
&[
1818
0, // bool
1919
8, // 256 bit
20-
9, // ebytes 64
2120
]
2221
}
2322

@@ -74,8 +73,6 @@ fn binary_op_to_event(
7473
#[allow(non_snake_case)]
7574
let scalarByte = s_byte(op.is_scalar);
7675
let lhs = *lhs;
77-
let use_bytes_when_avail = op.is_scalar && op.bits > 256;
78-
let rhs_bytes = to_bytes(r_scalar);
7976
let rhs = if op.is_scalar && op.bits <= 256 {
8077
as_scalar_handle(r_scalar)
8178
} else {
@@ -209,44 +206,20 @@ fn binary_op_to_event(
209206
scalarByte,
210207
result,
211208
}),
212-
S::FheEq => {
213-
if use_bytes_when_avail {
214-
E::FheEqBytes(C::FheEqBytes {
215-
caller,
216-
lhs,
217-
rhs: rhs_bytes,
218-
scalarByte,
219-
result,
220-
})
221-
} else {
222-
E::FheEq(C::FheEq {
223-
caller,
224-
lhs,
225-
rhs,
226-
scalarByte,
227-
result,
228-
})
229-
}
230-
}
231-
S::FheNe => {
232-
if use_bytes_when_avail {
233-
E::FheNeBytes(C::FheNeBytes {
234-
caller,
235-
lhs,
236-
rhs: rhs_bytes,
237-
scalarByte,
238-
result,
239-
})
240-
} else {
241-
E::FheNe(C::FheNe {
242-
caller,
243-
lhs,
244-
rhs,
245-
scalarByte,
246-
result,
247-
})
248-
}
249-
}
209+
S::FheEq => E::FheEq(C::FheEq {
210+
caller,
211+
lhs,
212+
rhs,
213+
scalarByte,
214+
result,
215+
}),
216+
S::FheNe => E::FheNe(C::FheNe {
217+
caller,
218+
lhs,
219+
rhs,
220+
scalarByte,
221+
result,
222+
}),
250223
_ => panic!("unknown operation: {:?}", op.operator),
251224
}
252225
}
@@ -287,8 +260,8 @@ async fn test_fhe_binary_operands_events() -> Result<(), Box<dyn std::error::Err
287260
let rhs_handle = next_handle();
288261
let output_handle = next_handle();
289262

290-
let lhs_bytes = to_bytes(&op.lhs);
291-
let rhs_bytes = to_bytes(&op.rhs);
263+
let lhs_bytes = as_scalar_uint(&op.lhs);
264+
let rhs_bytes = as_scalar_uint(&op.rhs);
292265

293266
println!(
294267
"Operations for binary test bits:{} op:{} is_scalar:{} lhs:{} rhs:{}",
@@ -298,8 +271,8 @@ async fn test_fhe_binary_operands_events() -> Result<(), Box<dyn std::error::Err
298271
.parse()
299272
.unwrap();
300273
listener_event_to_db
301-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
302-
TfheContract::TrivialEncryptBytes {
274+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
275+
TfheContract::TrivialEncrypt {
303276
caller,
304277
pt: lhs_bytes,
305278
toType: to_ty(op.input_types),
@@ -309,8 +282,8 @@ async fn test_fhe_binary_operands_events() -> Result<(), Box<dyn std::error::Err
309282
.await?;
310283
if !op.is_scalar {
311284
listener_event_to_db
312-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
313-
TfheContract::TrivialEncryptBytes {
285+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
286+
TfheContract::TrivialEncrypt {
314287
caller,
315288
pt: rhs_bytes,
316289
toType: to_ty(op.input_types),
@@ -402,7 +375,7 @@ async fn test_fhe_unary_operands_events() -> Result<(), Box<dyn std::error::Erro
402375
let input_handle = next_handle();
403376
let output_handle = next_handle();
404377

405-
let inp_bytes = to_bytes(&op.inp);
378+
let inp_bytes = as_scalar_uint(&op.inp);
406379

407380
println!(
408381
"Operations for unary test bits:{} op:{} input:{}",
@@ -413,8 +386,8 @@ async fn test_fhe_unary_operands_events() -> Result<(), Box<dyn std::error::Erro
413386
.parse()
414387
.unwrap();
415388
listener_event_to_db
416-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
417-
TfheContract::TrivialEncryptBytes {
389+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
390+
TfheContract::TrivialEncrypt {
418391
caller,
419392
pt: inp_bytes,
420393
toType: to_ty(op.operand_types),
@@ -472,21 +445,21 @@ async fn test_fhe_if_then_else_events() -> Result<(), Box<dyn std::error::Error>
472445
.unwrap();
473446

474447
listener_event_to_db
475-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
476-
TfheContract::TrivialEncryptBytes {
448+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
449+
TfheContract::TrivialEncrypt {
477450
caller,
478-
pt: to_bytes(&BigInt::from(0)),
451+
pt: as_scalar_uint(&BigInt::from(0)),
479452
toType: to_ty(fhe_bool_type),
480453
result: false_handle,
481454
},
482455
)))
483456
.await?;
484457

485458
listener_event_to_db
486-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
487-
TfheContract::TrivialEncryptBytes {
459+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
460+
TfheContract::TrivialEncrypt {
488461
caller,
489-
pt: to_bytes(&BigInt::from(1)),
462+
pt: as_scalar_uint(&BigInt::from(1)),
490463
toType: to_ty(fhe_bool_type),
491464
result: true_handle,
492465
},
@@ -504,21 +477,21 @@ async fn test_fhe_if_then_else_events() -> Result<(), Box<dyn std::error::Error>
504477
};
505478

506479
listener_event_to_db
507-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
508-
TfheContract::TrivialEncryptBytes {
480+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
481+
TfheContract::TrivialEncrypt {
509482
caller,
510-
pt: to_bytes(&left_input),
483+
pt: as_scalar_uint(&left_input),
511484
toType: to_ty(*input_types),
512485
result: left_handle,
513486
},
514487
)))
515488
.await?;
516489

517490
listener_event_to_db
518-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
519-
TfheContract::TrivialEncryptBytes {
491+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
492+
TfheContract::TrivialEncrypt {
520493
caller,
521-
pt: to_bytes(&right_input),
494+
pt: as_scalar_uint(&right_input),
522495
toType: to_ty(*input_types),
523496
result: right_handle,
524497
},
@@ -604,10 +577,10 @@ async fn test_fhe_cast_events() -> Result<(), Box<dyn std::error::Error>> {
604577
);
605578

606579
listener_event_to_db
607-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
608-
TfheContract::TrivialEncryptBytes {
580+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
581+
TfheContract::TrivialEncrypt {
609582
caller,
610-
pt: to_bytes(&BigInt::from(input)),
583+
pt: as_scalar_uint(&BigInt::from(input)),
611584
toType: to_ty(*type_from),
612585
result: input_handle,
613586
},

coprocessor/fhevm-engine/fhevm-listener/contracts/FHEVMExecutorTest.sol

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,10 @@ contract FHEVMExecutorTest is FHEEvents {
5757
bytes32 result = bytes32(keccak256(abi.encodePacked("fheEq", lhs, rhs, scalarByte)));
5858
emit FheEq(msg.sender, lhs, rhs, scalarByte, result);
5959
}
60-
function fheEq(bytes32 lhs, bytes memory rhs, bytes1 scalarByte) public {
61-
bytes32 result = bytes32(keccak256(abi.encodePacked("fheEqBytes", lhs, rhs, scalarByte)));
62-
emit FheEqBytes(msg.sender, lhs, rhs, scalarByte, result);
63-
}
6460
function fheNe(bytes32 lhs, bytes32 rhs, bytes1 scalarByte) public {
6561
bytes32 result = bytes32(keccak256(abi.encodePacked("fheNe", lhs, rhs, scalarByte)));
6662
emit FheNe(msg.sender, lhs, rhs, scalarByte, result);
6763
}
68-
function fheNe(bytes32 lhs, bytes memory rhs, bytes1 scalarByte) public {
69-
bytes32 result = bytes32(keccak256(abi.encodePacked("fheNeBytes", lhs, rhs, scalarByte)));
70-
emit FheNeBytes(msg.sender, lhs, rhs, scalarByte, result);
71-
}
7264
function fheGe(bytes32 lhs, bytes32 rhs, bytes1 scalarByte) public {
7365
bytes32 result = bytes32(keccak256(abi.encodePacked("fheGe", lhs, rhs, scalarByte)));
7466
emit FheGe(msg.sender, lhs, rhs, scalarByte, result);
@@ -120,14 +112,9 @@ contract FHEVMExecutorTest is FHEEvents {
120112
emit Cast(msg.sender, ct, toType, result);
121113
}
122114

123-
function trivialEncrypt(uint256 pt, FheType toType) public {
124-
bytes32 result = bytes32(keccak256(abi.encodePacked("trivialEncrypt", pt, toType)));
125-
emit TrivialEncrypt(msg.sender, pt, toType, result);
126-
}
127-
128-
function trivialEncrypt(bytes memory pt, FheType toType) public {
129-
bytes32 result = bytes32(keccak256(abi.encodePacked("trivialEncryptBytes", pt, toType)));
130-
emit TrivialEncryptBytes(msg.sender, pt, toType, result);
115+
function trivialEncrypt(uint256 val, FheType toType) public {
116+
bytes32 result = bytes32(keccak256(abi.encodePacked("trivialEncrypt", val, toType)));
117+
emit TrivialEncrypt(msg.sender, val, toType, result);
131118
}
132119

133120
function verifyCiphertext(

coprocessor/fhevm-engine/fhevm-listener/src/database/tfhe_event_propagate.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,6 @@ impl Database {
263263
| E::FheNot(C::FheNot {ct, result, ..})
264264
=> self.insert_computation(tenant_id, result, &[ct], fhe_operation, &NO_SCALAR).await,
265265

266-
| E::FheEqBytes(C::FheEqBytes {lhs, rhs, scalarByte, result, ..})
267-
| E::FheNeBytes(C::FheNeBytes {lhs, rhs, scalarByte, result, ..})
268-
=> self.insert_computation_bytes(tenant_id, result, &[lhs], &[rhs.to_vec()], fhe_operation, scalarByte).await,
269-
270266
| E::FheRand(C::FheRand {randType, seed, result, ..})
271267
=> self.insert_computation_bytes(tenant_id, result, &[], &[seed.to_vec(), ty(randType)], fhe_operation, &HAS_SCALAR).await,
272268

@@ -276,9 +272,6 @@ impl Database {
276272
| E::TrivialEncrypt(C::TrivialEncrypt {pt, toType, result, ..})
277273
=> self.insert_computation_bytes(tenant_id, result, &[], &[as_bytes(pt), ty(toType)], fhe_operation, &HAS_SCALAR).await,
278274

279-
| E::TrivialEncryptBytes(C::TrivialEncryptBytes {pt, toType, result, ..})
280-
=> self.insert_computation_bytes(tenant_id, result, &[], &[pt.to_vec(), ty(toType)], fhe_operation, &HAS_SCALAR).await,
281-
282275
| E::Initialized(_)
283276
| E::OwnershipTransferStarted(_)
284277
| E::OwnershipTransferred(_)
@@ -528,8 +521,8 @@ fn event_to_op_int(op: &TfheContractEvents) -> FheOperation {
528521
E::FheShr(_) => O::FheShr as i32,
529522
E::FheRotl(_) => O::FheRotl as i32,
530523
E::FheRotr(_) => O::FheRotr as i32,
531-
E::FheEq(_) | E::FheEqBytes(_) => O::FheEq as i32,
532-
E::FheNe(_) | E::FheNeBytes(_) => O::FheNe as i32,
524+
E::FheEq(_) => O::FheEq as i32,
525+
E::FheNe(_) => O::FheNe as i32,
533526
E::FheGe(_) => O::FheGe as i32,
534527
E::FheGt(_) => O::FheGt as i32,
535528
E::FheLe(_) => O::FheLe as i32,
@@ -539,7 +532,7 @@ fn event_to_op_int(op: &TfheContractEvents) -> FheOperation {
539532
E::FheNeg(_) => O::FheNeg as i32,
540533
E::FheNot(_) => O::FheNot as i32,
541534
E::Cast(_) => O::FheCast as i32,
542-
E::TrivialEncrypt(_) | E::TrivialEncryptBytes(_) => {
535+
E::TrivialEncrypt(_) => {
543536
O::FheTrivialEncrypt as i32
544537
}
545538
E::FheIfThenElse(_) => O::FheIfThenElse as i32,
@@ -569,8 +562,8 @@ pub fn event_name(op: &TfheContractEvents) -> &'static str {
569562
E::FheShr(_) => "FheShr",
570563
E::FheRotl(_) => "FheRotl",
571564
E::FheRotr(_) => "FheRotr",
572-
E::FheEq(_) | E::FheEqBytes(_) => "FheEq",
573-
E::FheNe(_) | E::FheNeBytes(_) => "FheNe",
565+
E::FheEq(_) => "FheEq",
566+
E::FheNe(_) => "FheNe",
574567
E::FheGe(_) => "FheGe",
575568
E::FheGt(_) => "FheGt",
576569
E::FheLe(_) => "FheLe",
@@ -580,7 +573,7 @@ pub fn event_name(op: &TfheContractEvents) -> &'static str {
580573
E::FheNeg(_) => "FheNeg",
581574
E::FheNot(_) => "FheNot",
582575
E::Cast(_) => "FheCast",
583-
E::TrivialEncrypt(_) | E::TrivialEncryptBytes(_) => "FheTrivialEncrypt",
576+
E::TrivialEncrypt(_) => "FheTrivialEncrypt",
584577
E::FheIfThenElse(_) => "FheIfThenElse",
585578
E::FheRand(_) => "FheRand",
586579
E::FheRandBounded(_) => "FheRandBounded",

coprocessor/fhevm-engine/fhevm-listener/tests/integration_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async fn emit_events<P, N>(
7373
let to_type: ToType = 4_u8;
7474
let pt = U256::from(UNIQUE_INT.fetch_add(1, Ordering::SeqCst));
7575
let txn_req = tfhe_contract
76-
.trivialEncrypt_1(pt.clone(), to_type.clone())
76+
.trivialEncrypt(pt.clone(), to_type.clone())
7777
.into_transaction_request()
7878
.into();
7979
let pending_txn =

0 commit comments

Comments
 (0)