concrete_integer/server_key/
mod.rs

1//! Module with the definition of the ServerKey.
2//!
3//! This module implements the generation of the server public key, together with all the
4//! available homomorphic integer operations.
5mod crt;
6mod crt_parallel;
7mod multi_crt;
8mod radix;
9mod radix_parallel;
10
11use crate::client_key::ClientKey;
12use concrete_shortint::server_key::MaxDegree;
13use serde::{Deserialize, Serialize};
14
15/// Error returned when the carry buffer is full.
16pub use concrete_shortint::CheckError;
17pub use multi_crt::CrtMultiServerKey;
18
19/// A structure containing the server public key.
20///
21/// The server key is generated by the client and is meant to be published: the client
22/// sends it to the server so it can compute homomorphic integer circuits.
23#[derive(Serialize, Deserialize, Clone)]
24pub struct ServerKey {
25    pub(crate) key: concrete_shortint::ServerKey,
26}
27
28impl From<ServerKey> for concrete_shortint::ServerKey {
29    fn from(key: ServerKey) -> concrete_shortint::ServerKey {
30        key.key
31    }
32}
33
34impl ServerKey {
35    /// Generates a server key.
36    ///
37    /// # Example
38    ///
39    /// ```rust
40    /// use concrete_integer::{ClientKey, ServerKey};
41    /// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
42    ///
43    /// // Generate the client key:
44    /// let cks = ClientKey::new(PARAM_MESSAGE_2_CARRY_2);
45    ///
46    /// // Generate the server key:
47    /// let sks = ServerKey::new(&cks);
48    /// ```
49    pub fn new<C>(cks: C) -> ServerKey
50    where
51        C: AsRef<ClientKey>,
52    {
53        // It should remain just enough space to add a carry
54        let client_key = cks.as_ref();
55        let max = (client_key.key.parameters.message_modulus.0 - 1)
56            * client_key.key.parameters.carry_modulus.0
57            - 1;
58
59        let sks = concrete_shortint::server_key::ServerKey::new_with_max_degree(
60            &client_key.key,
61            MaxDegree(max),
62        );
63
64        ServerKey { key: sks }
65    }
66
67    /// Creates a ServerKey from an already generated shortint::ServerKey.
68    ///
69    /// # Example
70    ///
71    /// ```rust
72    /// use concrete_integer::{ClientKey, ServerKey};
73    /// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
74    ///
75    /// let size = 4;
76    ///
77    /// // Generate the client key:
78    /// let cks = ClientKey::new(PARAM_MESSAGE_2_CARRY_2);
79    ///
80    /// // Generate the server key:
81    /// let sks = ServerKey::new(&cks);
82    /// ```
83    pub fn from_shortint(
84        cks: &ClientKey,
85        mut key: concrete_shortint::server_key::ServerKey,
86    ) -> ServerKey {
87        // It should remain just enough space add a carry
88        let max =
89            (cks.key.parameters.message_modulus.0 - 1) * cks.key.parameters.carry_modulus.0 - 1;
90
91        key.max_degree = MaxDegree(max);
92        ServerKey { key }
93    }
94}