1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use concrete_shortint::parameters::{
    CarryModulus, DecompositionBaseLog, DecompositionLevelCount, GlweDimension, LweDimension,
    MessageModulus, Parameters, PolynomialSize, StandardDev,
};

use crate::errors::{UninitializedClientKey, UninitializedServerKey};
use crate::{ClientKey, GenericShortInt};

use super::{
    ShortIntegerClientKey, ShortIntegerParameter, ShortIntegerServerKey,
    StaticShortIntegerParameter,
};

use paste::paste;

/// Generic Parameter struct for short integers.
///
/// It allows to customize the same parameters as the ones
/// from the underlying `concrete_shortint` with the exception of
/// the number of bits of message as its embeded in the type.
#[derive(Copy, Clone, Debug)]
pub struct ShortIntegerParameterSet<const MESSAGE_BITS: u8> {
    pub lwe_dimension: LweDimension,
    pub glwe_dimension: GlweDimension,
    pub polynomial_size: PolynomialSize,
    pub lwe_modular_std_dev: StandardDev,
    pub glwe_modular_std_dev: StandardDev,
    pub pbs_base_log: DecompositionBaseLog,
    pub pbs_level: DecompositionLevelCount,
    pub ks_base_log: DecompositionBaseLog,
    pub ks_level: DecompositionLevelCount,
    pub pfks_level: DecompositionLevelCount,
    pub pfks_base_log: DecompositionBaseLog,
    pub pfks_modular_std_dev: StandardDev,
    pub cbs_level: DecompositionLevelCount,
    pub cbs_base_log: DecompositionBaseLog,
    pub carry_modulus: CarryModulus,
}

impl<const MESSAGE_BITS: u8> ShortIntegerParameterSet<MESSAGE_BITS> {
    const fn from_static(params: &'static Parameters) -> Self {
        if params.message_modulus.0 != 1 << MESSAGE_BITS as usize {
            panic!("Invalid bit number");
        }
        Self {
            lwe_dimension: params.lwe_dimension,
            glwe_dimension: params.glwe_dimension,
            polynomial_size: params.polynomial_size,
            lwe_modular_std_dev: params.lwe_modular_std_dev,
            glwe_modular_std_dev: params.glwe_modular_std_dev,
            pbs_base_log: params.pbs_base_log,
            pbs_level: params.pbs_level,
            ks_base_log: params.ks_base_log,
            ks_level: params.ks_level,
            pfks_level: params.pfks_level,
            pfks_base_log: params.pfks_base_log,
            pfks_modular_std_dev: params.pfks_modular_std_dev,
            cbs_level: params.cbs_level,
            cbs_base_log: params.cbs_base_log,
            carry_modulus: params.carry_modulus,
        }
    }
}

impl<const MESSAGE_BITS: u8> From<ShortIntegerParameterSet<MESSAGE_BITS>> for Parameters {
    fn from(params: ShortIntegerParameterSet<MESSAGE_BITS>) -> Self {
        Self {
            lwe_dimension: params.lwe_dimension,
            glwe_dimension: params.glwe_dimension,
            polynomial_size: params.polynomial_size,
            lwe_modular_std_dev: params.lwe_modular_std_dev,
            glwe_modular_std_dev: params.glwe_modular_std_dev,
            pbs_base_log: params.pbs_base_log,
            pbs_level: params.pbs_level,
            ks_base_log: params.ks_base_log,
            ks_level: params.ks_level,
            pfks_level: params.pfks_level,
            pfks_base_log: params.pfks_base_log,
            pfks_modular_std_dev: params.pfks_modular_std_dev,
            cbs_level: params.cbs_level,
            cbs_base_log: params.cbs_base_log,
            message_modulus: MessageModulus(1 << MESSAGE_BITS as usize),
            carry_modulus: params.carry_modulus,
        }
    }
}

/// The Id that is used to identify and retrieve the corresponding keys
#[derive(Copy, Clone, Default)]
pub struct ShorIntId<const MESSAGE_BITS: u8>;

impl<const MESSAGE_BITS: u8> ShortIntegerParameter for ShortIntegerParameterSet<MESSAGE_BITS> {
    type Id = ShorIntId<MESSAGE_BITS>;
}

impl<const MESSAGE_BITS: u8> StaticShortIntegerParameter
    for ShortIntegerParameterSet<MESSAGE_BITS>
{
    const MESSAGE_BITS: u8 = MESSAGE_BITS;
}

/// Defines a new static shortint type.
///
/// It needs as input the:
///     - name of the type
///     - the number of bits of message the type has
///     - the keychain member where ClientKey / Server Key is stored
///
/// It generates code:
///     - type alias for the client key, server key, parameter and shortint types
///     - the trait impl on the id type to access the keys
macro_rules! static_shortint_type {
    (
        $(#[$outer:meta])*
        $name:ident {
            num_bits: $num_bits:literal,
            keychain_member: $($member:ident).*,
        }
    ) => {
        paste! {

            #[doc = concat!("Parameters for the [", stringify!($name), "] data type.")]
            #[cfg_attr(doc, cfg(feature = "shortints"))]
            pub type [<$name Parameters>] = ShortIntegerParameterSet<$num_bits>;

            pub(in crate::shortints) type [<$name ClientKey>] = ShortIntegerClientKey<[<$name Parameters>]>;
            pub(in crate::shortints) type [<$name ServerKey>] = ShortIntegerServerKey<[<$name Parameters>]>;

            $(#[$outer])*
            #[doc=concat!("An unsigned integer type with ", stringify!($num_bits), " bits.")]
            #[cfg_attr(doc, cfg(feature = "shortints"))]
            pub type $name = GenericShortInt<[<$name Parameters>]>;

            impl_ref_key_from_keychain!(
                for <[<$name Parameters>] as ShortIntegerParameter>::Id {
                    key_type: [<$name ClientKey>],
                    keychain_member: $($member).*,
                    type_variant: crate::errors::Type::$name,
                }
            );

            impl_with_global_key!(
                for <[<$name Parameters>] as ShortIntegerParameter>::Id {
                    key_type: [<$name ServerKey>],
                    keychain_member: $($member).*,
                    type_variant: crate::errors::Type::$name,
                }
            );
        }
    };
}

static_shortint_type! {
    FheUint2 {
        num_bits: 2,
        keychain_member: shortint_key.uint2_key,
    }
}

static_shortint_type! {
    FheUint3 {
        num_bits: 3,
        keychain_member: shortint_key.uint3_key,
    }
}

static_shortint_type! {
    FheUint4 {
        num_bits: 4,
        keychain_member: shortint_key.uint4_key,
    }
}

impl FheUint2Parameters {
    // pub fn with_carry_0() -> Self {
    //     Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_0)
    // }

    pub fn with_carry_1() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_1)
    }

    pub fn with_carry_2() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2)
    }

    pub fn with_carry_3() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_3)
    }

    pub fn with_carry_4() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_4)
    }

    pub fn with_carry_5() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_5)
    }

    pub fn with_carry_6() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_6)
    }

    pub fn wopbs_default() -> Self {
        Self::from_static(&concrete_shortint::parameters::parameters_wopbs_message_carry::WOPBS_PARAM_MESSAGE_2_CARRY_2)
    }
}

impl Default for FheUint2Parameters {
    fn default() -> Self {
        Self::with_carry_2()
    }
}

impl FheUint3Parameters {
    // pub fn with_carry_0() -> Self {
    //     Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_3_CARRY_0)
    // }

    pub fn with_carry_1() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_3_CARRY_1)
    }

    pub fn with_carry_2() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_3_CARRY_2)
    }

    pub fn with_carry_3() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_3_CARRY_3)
    }

    pub fn with_carry_4() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_3_CARRY_4)
    }

    pub fn with_carry_5() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_3_CARRY_5)
    }

    pub fn wopbs_default() -> Self {
        Self::from_static(&concrete_shortint::parameters::parameters_wopbs_message_carry::WOPBS_PARAM_MESSAGE_3_CARRY_3)
    }
}

impl Default for FheUint3Parameters {
    fn default() -> Self {
        Self::with_carry_3()
    }
}

impl FheUint4Parameters {
    // pub fn with_carry_0() -> Self {
    //     Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_4_CARRY_0)
    // }

    pub fn with_carry_1() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_4_CARRY_1)
    }

    pub fn with_carry_2() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_4_CARRY_2)
    }

    pub fn with_carry_3() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_4_CARRY_3)
    }

    pub fn with_carry_4() -> Self {
        Self::from_static(&concrete_shortint::parameters::PARAM_MESSAGE_4_CARRY_4)
    }

    pub fn wopbs_default() -> Self {
        Self::from_static(&concrete_shortint::parameters::parameters_wopbs_message_carry::WOPBS_PARAM_MESSAGE_4_CARRY_4)
    }
}

impl Default for FheUint4Parameters {
    fn default() -> Self {
        Self::with_carry_4()
    }
}