学点有的没的。
#[no_mangle]
pub unsafe extern "C" fn shortint_server_key_smart_add(
server_key: *const ShortintServerKey,
ct_left: *mut ShortintCiphertext,
ct_right: *mut ShortintCiphertext,
result: *mut *mut ShortintCiphertext,
) -> c_int {
catch_panic(|| {
check_ptr_is_non_null_and_aligned(result).unwrap();
let server_key = get_ref_checked(server_key).unwrap();
let ct_left = get_mut_checked(ct_left).unwrap();
let ct_right = get_mut_checked(ct_right).unwrap();
let res = server_key.0.smart_add(&mut ct_left.0, &mut ct_right.0);
let heap_allocated_ct_result = Box::new(ShortintCiphertext(res));
*result = Box::into_raw(heap_allocated_ct_result);
})
}
通过比较shortint类型的两种不同add,看出shortint_server_key_smart_add是两个密文之间的加法,而shortint_server_key_smart_scalar_add是密文与明文的加法
#[no_mangle]
pub unsafe extern "C" fn shortint_server_key_smart_scalar_add(
server_key: *const ShortintServerKey,
ct_left: *mut ShortintCiphertext,
scalar_right: u8,
result: *mut *mut ShortintCiphertext,
) -> c_int {
catch_panic(|| {
check_ptr_is_non_null_and_aligned(result).unwrap();
let server_key = get_ref_checked(server_key).unwrap();
let ct_left = get_mut_checked(ct_left).unwrap();
let res = server_key.0.smart_scalar_add(&mut ct_left.0, scalar_right);
let heap_allocated_ct_result = Box::new(ShortintCiphertext(res));
*result = Box::into_raw(heap_allocated_ct_result);
})
}
总结:
smart: 将其输入作为加密值的操作。
scalar: 标量运算采用至少一个未加密的值作为输入。
例:
ServerKey::unchecked_add,它获取两个加密值并将它们相加。
ServerKey::unchecked_scaler_add,它获取一个加密值和一个清除值(所谓的标量)并将它们相加。
unchecked:始终执行操作,不检查结果是否超过明文空间的容量。
checked:在计算操作之前进行检查,如果操作无法安全完成,则返回错误。
smart: 始终执行操作,如果无法安全地计算操作,则smart-操作将传播进位缓冲区以使操作成为可能。其中一些将需要一个可变引用作为输入:这是因为输入的进位可能会被清除,但这不会改变底层加密值。
default: 始终计算运算并始终清除进位。可能慢于智能,但要确保从一个呼叫到另一个呼叫的时间一致。