parasol_cpu 0.10.0

This crate contains the Parasol CPU, which runs programs over a mix of encrypted and plaintext data.
Documentation
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use std::{
    borrow::BorrowMut,
    collections::HashMap,
    sync::{Arc, OnceLock},
};

use fhe_processor::FheProcessor;
pub use fhe_processor::{RunProgramOptions, RunProgramOptionsBuilder};
use parasol_concurrency::AtomicRefCell;
use parasol_runtime::{
    CircuitProcessor, Encryption, Evaluation, FheCircuit, L0LweCiphertext, L1GgswCiphertext,
    L1GlweCiphertext, L1LweCiphertext, TrivialOne, TrivialZero,
    fluent::{
        DynamicGenericInt, FheCircuitCtx, GenericInt, PackedDynamicGenericInt, PackedGenericInt,
        Sign,
    },
};
use rayon::ThreadPool;
use serde::{Deserialize, Serialize};

use crate::{
    Byte, Error, Memory, Ptr32, Result, Word, proc::gas_model::GasModel,
    tomasulo::scoreboard::ScoreboardEntryRef,
};

use self::ops::trivially_encrypt_value_l1glwe;

/// Argument handling for Parasol programs.
mod args;
pub use args::*;

#[doc(hidden)]
pub mod assembly;
mod ops;

mod fhe_processor;
mod gas_model;

#[cfg(test)]
mod tests;

pub(crate) use assembly::*;

#[doc(hidden)]
pub enum Ciphertext {
    #[allow(unused)]
    L0Lwe {
        data: Vec<Arc<AtomicRefCell<L0LweCiphertext>>>,
    },
    #[allow(unused)]
    L1Lwe {
        data: Vec<Arc<AtomicRefCell<L1LweCiphertext>>>,
    },
    L1Glwe {
        data: Vec<Arc<AtomicRefCell<L1GlweCiphertext>>>,
    },
    #[allow(unused)]
    L1Ggsw {
        data: Vec<Arc<AtomicRefCell<L1GgswCiphertext>>>,
    },
}

impl Ciphertext {
    pub fn len(&self) -> usize {
        match self {
            Self::L0Lwe { data } => data.len(),
            Self::L1Lwe { data } => data.len(),
            Self::L1Glwe { data } => data.len(),
            Self::L1Ggsw { data } => data.len(),
        }
    }

    #[allow(unused)]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[allow(unused)]
    pub fn unwrap_l1glwe(&self) -> &[Arc<AtomicRefCell<L1GlweCiphertext>>] {
        match self {
            Self::L1Glwe { data } => data,
            _ => panic!("Ciphertext was not L1GlweCiphertext"),
        }
    }

    pub fn try_into_l1glwe(&self) -> Result<&[Arc<AtomicRefCell<L1GlweCiphertext>>]> {
        match self {
            Self::L1Glwe { data } => Ok(data),
            _ => Err(Error::EncryptionMismatch),
        }
    }
}

#[doc(hidden)]
/// The type of value stored in a register.
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum RegisterValueType {
    Plaintext,
    L0LweCiphertext,
    L1LweCiphertext,
    L1GlweCiphertext,
    L1GgswCiphertext,
}

#[doc(hidden)]
pub enum Register {
    Plaintext { val: u128, width: u32 },

    Ciphertext(Ciphertext),
}

impl std::fmt::Debug for Register {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Plaintext { val, width } => {
                write!(f, "v={val}, w={width}")
            }
            Self::Ciphertext(c) => {
                write!(f, "v=<encrypted>, w={}", c.len())
            }
        }
    }
}

impl Register {
    /// How many bits is the value of the register?
    pub fn width(&self) -> usize {
        match self {
            Self::Plaintext { val: _, width } => *width as usize,
            Self::Ciphertext(x) => x.len(),
        }
    }

    /// Is the register a plaintext register?
    pub fn is_plaintext(&self) -> bool {
        matches!(self, Self::Plaintext { val: _, width: _ })
    }

    /// Is the register a ciphertext register?
    pub fn is_ciphertext(&self) -> bool {
        matches!(self, Self::Ciphertext(_))
    }

    /// What type of value is stored in the register?
    pub fn register_value_type(&self) -> RegisterValueType {
        match self {
            Self::Plaintext { val: _, width: _ } => RegisterValueType::Plaintext,
            Self::Ciphertext(Ciphertext::L0Lwe { data: _ }) => RegisterValueType::L0LweCiphertext,
            Self::Ciphertext(Ciphertext::L1Lwe { data: _ }) => RegisterValueType::L1LweCiphertext,
            Self::Ciphertext(Ciphertext::L1Glwe { data: _ }) => RegisterValueType::L1GlweCiphertext,
            Self::Ciphertext(Ciphertext::L1Ggsw { data: _ }) => RegisterValueType::L1GgswCiphertext,
        }
    }

    pub fn from_word(word: &Word) -> Self {
        if word.0[0].is_plaintext() {
            let mut val = 0u128;

            for (i, b) in word.0.iter().enumerate() {
                val |= (b.clone().unwrap_plaintext() as u128) << (8 * i)
            }

            Self::Plaintext { val, width: 32 }
        } else {
            let data = word
                .0
                .iter()
                .flat_map(|x| x.clone().unwrap_ciphertext())
                .collect::<Vec<_>>();

            Self::Ciphertext(Ciphertext::L1Glwe { data })
        }
    }
}

impl Default for Register {
    fn default() -> Self {
        Register::Plaintext { val: 0, width: 32 }
    }
}

/// Checks if the width of two registers is the same.
/// Used inside an instruction implementation.
pub fn check_register_width(
    a: &Register,
    b: &Register,
    instruction_id: usize,
    pc: u32,
) -> Result<()> {
    if a.width() != b.width() {
        return Err(Error::WidthMismatch {
            inst_id: instruction_id,
            pc,
        });
    }

    // TODO, relax the 128-bit limitation.
    if a.width() < 1 || a.width() > 128 {
        return Err(Error::unsupported_width(instruction_id, pc));
    }

    Ok(())
}

/// Convert a plaintext register to a L1 GLWE ciphertext register, or copy
/// the existing ciphertext register if it's already in that form.
///
/// Returns `Err` if the register is not a plaintext or L1 GLWE ciphertext
/// register.
pub fn register_to_l1glwe_by_trivial_lift(
    register: &Register,
    zero: &L1GlweCiphertext,
    one: &L1GlweCiphertext,
) -> Result<Vec<Arc<AtomicRefCell<L1GlweCiphertext>>>> {
    match register {
        Register::Plaintext { val, width } => {
            Ok(trivially_encrypt_value_l1glwe(*val, *width, zero, one))
        }
        Register::Ciphertext(Ciphertext::L1Glwe { data }) => Ok(data.clone()),
        _ => Err(Error::EncryptionMismatch),
    }
}

pub(crate) type Fault = Arc<OnceLock<Error>>;

pub(crate) struct FheProcessorAuxData {
    uop_processor: CircuitProcessor,
    flow: std::sync::mpsc::Receiver<()>,
    memory: Option<Arc<Memory>>,
    inflight_memory_ops: HashMap<Ptr32, ScoreboardEntryRef<DispatchIsaOp>>,
    l1glwe_zero: L1GlweCiphertext,
    l1glwe_one: L1GlweCiphertext,
    enc: Encryption,
    gas_model: GasModel,

    /// A sync or async error that can occur in the processor. When set, all previous in-flight
    /// instructions that haven't started become no-ops that immediately retire and notify
    /// their dependencies. This ensures that all outstanding scoreboard entries get dropped
    /// correctly and we don't leak memory.
    fault: Fault,
}

impl FheProcessorAuxData {
    pub fn new(enc: &Encryption, eval: &Evaluation, thread_pool: Option<Arc<ThreadPool>>) -> Self {
        let (uop_processor, flow) = CircuitProcessor::new(1024, thread_pool, eval, enc);

        let l1glwe_zero = L1GlweCiphertext::trivial_zero(enc);
        let l1glwe_one = L1GlweCiphertext::trivial_one(enc);

        Self {
            uop_processor,
            flow,
            memory: None,
            inflight_memory_ops: HashMap::new(),
            l1glwe_zero,
            l1glwe_one,
            enc: enc.clone(),
            gas_model: GasModel::new(),
            fault: Arc::new(OnceLock::new()),
        }
    }
}

/// The Parasol processor that can run programs over encrypted and plaintext data.
pub struct FheComputer {
    processor: FheProcessor,
}

impl FheComputer {
    /// Create a new [`FheComputer`]. Tasks will run on the global [`rayon::ThreadPool`].
    pub fn new(enc: &Encryption, eval: &Evaluation) -> Self {
        let aux_data = FheProcessorAuxData::new(enc, eval, None);

        let processor = FheProcessor::new(aux_data);

        Self { processor }
    }

    /// Create a new [`FheComputer`]. Tasks will run on the given [`rayon::ThreadPool`].
    pub fn new_with_threadpool(
        enc: &Encryption,
        eval: &Evaluation,
        thread_pool: Arc<ThreadPool>,
    ) -> Self {
        let aux_data = FheProcessorAuxData::new(enc, eval, Some(thread_pool));

        let processor = FheProcessor::new(aux_data);

        Self { processor }
    }

    /// Run the given FHE program with user specified data and options including gas limit, etc,
    /// return the used gas and ([`Vec<Byte>`]) return value to be interpreted by the caller
    pub fn run_program_with_options_and_dynamic_return(
        &mut self,
        initial_pc: Ptr32,
        memory: &Arc<Memory>,
        args: CallData<Vec<Byte>>,
        options: &RunProgramOptions,
    ) -> Result<(u32, Vec<Byte>)> {
        self.processor.run_program_with_options_and_dynamic_return(
            memory,
            initial_pc,
            &args.to_dyn(),
            options,
        )
    }

    /// Run the given FHE program with user specified data and options including gas limit, etc,
    /// return the used gas and program return value
    pub fn run_program_with_options<T: ToArg>(
        &mut self,
        initial_pc: Ptr32,
        memory: &Arc<Memory>,
        args: CallData<T>,
        options: &RunProgramOptions,
    ) -> Result<(u32, T)> {
        self.processor
            .run_program_with_options(memory, initial_pc, &args, options)
    }

    /// Run the given FHE program with user specified data.
    pub fn run_program<T: ToArg>(
        &mut self,
        initial_pc: Ptr32,
        memory: &Arc<Memory>,
        args: CallData<T>,
    ) -> Result<T> {
        self.processor.run_program(memory, initial_pc, &args)
    }

    /// Run a graph in blocking mode.
    pub(crate) fn run_graph_blocking(&mut self, circuit: &FheCircuit) -> Result<()> {
        let uproc = self.processor.aux_data.uop_processor.borrow_mut();
        let fc = &self.processor.aux_data.flow;

        uproc.run_graph_blocking(circuit, fc)?;

        Ok(())
    }

    /// Packs a `GenericInt<N, L1GlweCiphertext, U>` into a `PackedGenericInt<N, L1GlweCiphertext, U>`.
    pub fn pack_int<const N: usize, U: Sign>(
        &mut self,
        input: GenericInt<N, L1GlweCiphertext, U>,
    ) -> Result<PackedGenericInt<N, L1GlweCiphertext, U>> {
        let ctx = FheCircuitCtx::new();

        let packed_ct = input
            .graph_inputs(&ctx)
            .pack(&ctx, &self.processor.aux_data.enc)
            .collect_output(&ctx, &self.processor.aux_data.enc);

        self.run_graph_blocking(&ctx.circuit.borrow())?;

        Ok(PackedGenericInt::from(packed_ct))
    }

    /// Similar to [`FheComputer::pack_int`] but works on [`DynamicGenericInt`]
    pub fn pack_int_dyn<U: Sign>(
        &mut self,
        input: DynamicGenericInt<L1GlweCiphertext, U>,
    ) -> Result<PackedDynamicGenericInt<L1GlweCiphertext, U>> {
        let ctx = FheCircuitCtx::new();

        let packed_ct = input
            .graph_inputs(&ctx)
            .pack(&ctx, &self.processor.aux_data.enc)
            .collect_output(&ctx, &self.processor.aux_data.enc);

        self.run_graph_blocking(&ctx.circuit.borrow())?;
        Ok(packed_ct)
    }

    /// Unpacks a `PackedGenericInt<N, L1GlweCiphertext, U>` into a `GenericInt<N, L1GlweCiphertext, U>`.
    pub fn unpack_int<const N: usize, U: Sign>(
        &mut self,
        input: PackedGenericInt<N, L1GlweCiphertext, U>,
    ) -> Result<GenericInt<N, L1GlweCiphertext, U>> {
        let ctx = FheCircuitCtx::new();

        let unpacked_ct = input
            .graph_input(&ctx)
            .unpack(&ctx)
            .convert(&ctx)
            .collect_outputs(&ctx, &self.processor.aux_data.enc);

        self.run_graph_blocking(&ctx.circuit.borrow())?;

        Ok(GenericInt::from(unpacked_ct))
    }

    /// Similar to [`FheComputer::unpack_int`] but works on [`PackedDynamicGenericInt`]
    pub fn unpack_int_dyn<U: Sign>(
        &mut self,
        input: PackedDynamicGenericInt<L1GlweCiphertext, U>,
    ) -> Result<DynamicGenericInt<L1GlweCiphertext, U>> {
        let ctx = FheCircuitCtx::new();

        let unpacked_ct = input
            .graph_input(&ctx)
            .unpack(&ctx)
            .convert(&ctx)
            .collect_outputs(&ctx, &self.processor.aux_data.enc);

        self.run_graph_blocking(&ctx.circuit.borrow())?;
        Ok(unpacked_ct)
    }
}