Creating a function to handle multiple ciphertext types by identifying a common trait for generic

I wanted to create a function that can accept various ciphertext data types, such as FheUint8, FheUint16, and FheUint64. I was looking to identify the appropriate trait that the generic type should implement, allowing the function to process and calculate outputs for different ciphertext data types.

Something like this

fn calculate_sum<Id>(a:FheUint<Id>, b:FheUint<Id>) 
    where
        Id : FheUintId

-> FheUint<Id> {

      a + b

     }

I can’t access the FheUintId trait because it’s private. Is there another way to accomplish this?

Yes there is a way,

You have to do it a bit like if you wanted to have a function working on u8,u16,u32: have the types be generic and use traits

pub fn add_ct<FheType>(a: &FheType, b: &FheType) -> FheType
    where
        for<'a> &'a FheType: Add<&'a FheType, Output = FheType>,
{
    a + b
}

You can use this page to have some guidance

1 Like