Skip to content

Commit c6a6404

Browse files
committed
fix(coprocessor): sync with contract fhe events
1 parent 3fdb1df commit c6a6404

4 files changed

Lines changed: 48 additions & 93 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 & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ fn binary_op_to_event(
7474
#[allow(non_snake_case)]
7575
let scalarByte = s_byte(op.is_scalar);
7676
let lhs = *lhs;
77-
let use_bytes_when_avail = op.is_scalar && op.bits > 256;
78-
let rhs_bytes = to_bytes(r_scalar);
7977
let rhs = if op.is_scalar && op.bits <= 256 {
8078
as_scalar_handle(r_scalar)
8179
} else {
@@ -209,44 +207,20 @@ fn binary_op_to_event(
209207
scalarByte,
210208
result,
211209
}),
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-
}
210+
S::FheEq => E::FheEq(C::FheEq {
211+
caller,
212+
lhs,
213+
rhs,
214+
scalarByte,
215+
result,
216+
}),
217+
S::FheNe => E::FheNe(C::FheNe {
218+
caller,
219+
lhs,
220+
rhs,
221+
scalarByte,
222+
result,
223+
}),
250224
_ => panic!("unknown operation: {:?}", op.operator),
251225
}
252226
}
@@ -287,8 +261,8 @@ async fn test_fhe_binary_operands_events() -> Result<(), Box<dyn std::error::Err
287261
let rhs_handle = next_handle();
288262
let output_handle = next_handle();
289263

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

293267
println!(
294268
"Operations for binary test bits:{} op:{} is_scalar:{} lhs:{} rhs:{}",
@@ -298,8 +272,8 @@ async fn test_fhe_binary_operands_events() -> Result<(), Box<dyn std::error::Err
298272
.parse()
299273
.unwrap();
300274
listener_event_to_db
301-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
302-
TfheContract::TrivialEncryptBytes {
275+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
276+
TfheContract::TrivialEncrypt {
303277
caller,
304278
pt: lhs_bytes,
305279
toType: to_ty(op.input_types),
@@ -309,8 +283,8 @@ async fn test_fhe_binary_operands_events() -> Result<(), Box<dyn std::error::Err
309283
.await?;
310284
if !op.is_scalar {
311285
listener_event_to_db
312-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
313-
TfheContract::TrivialEncryptBytes {
286+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
287+
TfheContract::TrivialEncrypt {
314288
caller,
315289
pt: rhs_bytes,
316290
toType: to_ty(op.input_types),
@@ -402,7 +376,7 @@ async fn test_fhe_unary_operands_events() -> Result<(), Box<dyn std::error::Erro
402376
let input_handle = next_handle();
403377
let output_handle = next_handle();
404378

405-
let inp_bytes = to_bytes(&op.inp);
379+
let inp_bytes = as_scalar_uint(&op.inp);
406380

407381
println!(
408382
"Operations for unary test bits:{} op:{} input:{}",
@@ -413,8 +387,8 @@ async fn test_fhe_unary_operands_events() -> Result<(), Box<dyn std::error::Erro
413387
.parse()
414388
.unwrap();
415389
listener_event_to_db
416-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
417-
TfheContract::TrivialEncryptBytes {
390+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
391+
TfheContract::TrivialEncrypt {
418392
caller,
419393
pt: inp_bytes,
420394
toType: to_ty(op.operand_types),
@@ -472,21 +446,21 @@ async fn test_fhe_if_then_else_events() -> Result<(), Box<dyn std::error::Error>
472446
.unwrap();
473447

474448
listener_event_to_db
475-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
476-
TfheContract::TrivialEncryptBytes {
449+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
450+
TfheContract::TrivialEncrypt {
477451
caller,
478-
pt: to_bytes(&BigInt::from(0)),
452+
pt: as_scalar_uint(&BigInt::from(0)),
479453
toType: to_ty(fhe_bool_type),
480454
result: false_handle,
481455
},
482456
)))
483457
.await?;
484458

485459
listener_event_to_db
486-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
487-
TfheContract::TrivialEncryptBytes {
460+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
461+
TfheContract::TrivialEncrypt {
488462
caller,
489-
pt: to_bytes(&BigInt::from(1)),
463+
pt: as_scalar_uint(&BigInt::from(1)),
490464
toType: to_ty(fhe_bool_type),
491465
result: true_handle,
492466
},
@@ -504,21 +478,21 @@ async fn test_fhe_if_then_else_events() -> Result<(), Box<dyn std::error::Error>
504478
};
505479

506480
listener_event_to_db
507-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
508-
TfheContract::TrivialEncryptBytes {
481+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
482+
TfheContract::TrivialEncrypt {
509483
caller,
510-
pt: to_bytes(&left_input),
484+
pt: as_scalar_uint(&left_input),
511485
toType: to_ty(*input_types),
512486
result: left_handle,
513487
},
514488
)))
515489
.await?;
516490

517491
listener_event_to_db
518-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
519-
TfheContract::TrivialEncryptBytes {
492+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
493+
TfheContract::TrivialEncrypt {
520494
caller,
521-
pt: to_bytes(&right_input),
495+
pt: as_scalar_uint(&right_input),
522496
toType: to_ty(*input_types),
523497
result: right_handle,
524498
},
@@ -604,10 +578,10 @@ async fn test_fhe_cast_events() -> Result<(), Box<dyn std::error::Error>> {
604578
);
605579

606580
listener_event_to_db
607-
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncryptBytes(
608-
TfheContract::TrivialEncryptBytes {
581+
.insert_tfhe_event(&tfhe_event(TfheContractEvents::TrivialEncrypt(
582+
TfheContract::TrivialEncrypt {
609583
caller,
610-
pt: to_bytes(&BigInt::from(input)),
584+
pt: as_scalar_uint(&BigInt::from(input)),
611585
toType: to_ty(*type_from),
612586
result: input_handle,
613587
},

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",

0 commit comments

Comments
 (0)