arcis-compiler 0.10.2

A framework for writing secure multi-party computation (MPC) circuits to be executed on the Arcium network.
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use crate::{
    core::{
        bounds::{Bounds, IsBounds},
        compile::Compiler,
        expressions::{
            bit_expr::BitExpr,
            conversion_expr::ConversionExpr,
            curve_expr::CurveExpr,
            domain::DomainElement,
            expr::{EvalFailure, EvalValue, Expr, UndefinedBehavior},
            field_expr::FieldExpr,
            macro_uses::{BoundUnFold, EvalValueUnwrap},
        },
        instruction::ArcisInstruction,
        tracking::Tracking,
    },
    utils::{
        field::{BaseField, ScalarField},
        number::Number,
        used_field::UsedField,
    },
};
use ff::Field;
use indexmap::IndexSet;
use num_traits::ToPrimitive;
use rand::Rng;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::{env, fmt, fmt::Formatter, io::Write};

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct IntermediateRepresentation {
    exprs: Vec<Expr<usize>>, // the usize are indices in exprs
    outputs: Vec<usize>,     // the items are indices in exprs
    bounds: Vec<Bounds>,     // same size as exprs
    is_plaintext: Vec<bool>, // same size as exprs
    tracking: Tracking,
    pub randomness_tracking: FxHashMap<usize, usize>,
}

impl IntermediateRepresentation {
    /// Constructor
    pub fn new(
        exprs: Vec<Expr<usize>>,
        outputs: Vec<usize>,
        bounds: Vec<Bounds>,
        is_plaintext: Vec<bool>,
    ) -> IntermediateRepresentation {
        IntermediateRepresentation {
            exprs,
            outputs,
            bounds,
            is_plaintext,
            ..Default::default()
        }
    }
    pub fn new_with_tracking(
        exprs: Vec<Expr<usize>>,
        outputs: Vec<usize>,
        bounds: Vec<Bounds>,
        is_plaintext: Vec<bool>,
        tracking: Tracking,
    ) -> IntermediateRepresentation {
        IntermediateRepresentation {
            exprs,
            outputs,
            bounds,
            is_plaintext,
            tracking,
            ..Default::default()
        }
    }
    #[allow(clippy::type_complexity)]
    pub fn destructure(
        self,
    ) -> (
        Vec<Expr<usize>>, // the usize are indices in exprs
        Vec<usize>,       // the items are indices in exprs
        Vec<Bounds>,      // same size as exprs
        Vec<bool>,        // same size as exprs
        Tracking,
    ) {
        (
            self.exprs,
            self.outputs,
            self.bounds,
            self.is_plaintext,
            self.tracking,
        )
    }

    pub fn get_exprs(&self) -> &[Expr<usize>] {
        &self.exprs
    }

    pub fn get_outputs(&self) -> &[usize] {
        &self.outputs
    }

    pub fn get_bounds(&self) -> &[Bounds] {
        &self.bounds
    }

    pub fn get_is_plaintext(&self) -> &[bool] {
        &self.is_plaintext
    }
    pub fn get_output_domains(&self) -> Vec<DomainElement<(), (), (), ()>> {
        self.outputs
            .iter()
            .map(|x| self.bounds[*x].to_domain())
            .collect()
    }

    pub fn get_expr(&self, id: usize) -> &Expr<usize> {
        &self.exprs[id]
    }
    pub fn get_tracking(&self) -> &Tracking {
        &self.tracking
    }

    pub fn check_for_integrity(&self) -> Result<(), ProgramError> {
        let mut input_ids: Vec<usize> = self.exprs.iter().filter_map(Expr::get_input).collect();
        input_ids.sort();
        let has_duplicates = input_ids.windows(2).any(|x| x[0] == x[1]);
        if has_duplicates {
            return Err(ProgramError::DuplicateInputId);
        }
        Ok(())
    }
    pub fn optimize_into_circuitable(self) -> IntermediateRepresentation {
        Compiler::optimize_into_circuitable(self)
    }
    pub fn to_async_mpc_circuit(&self) -> ArcisInstruction {
        let (unimproved_circuit, metadata, _) = Compiler::ir_to_async_mpc_circuit(self);
        let circuit = Compiler::improve_async_mpc_circuit(unimproved_circuit);
        ArcisInstruction { circuit, metadata }
    }
    /// Finds the generated booleans that have been optimized out.
    /// `result[idx]` is
    /// `Some(n)` if the `idx`-th generated bool in `opt_ir` is the `n`-th generated bool in `self`.
    /// `None` if the `idx`-th generated bool in `opt_ir` was not in `self`.
    pub fn generated_bools_filter(
        &self,
        opt_ir: &IntermediateRepresentation,
    ) -> Vec<Option<usize>> {
        let mut old_ids = IndexSet::new();
        for expr in self.get_exprs() {
            if let Expr::Bit(BitExpr::Random(id)) = expr {
                old_ids.insert(id.clone());
            }
        }
        let mut result = Vec::new();
        for expr in opt_ir.get_exprs() {
            if let Expr::Bit(BitExpr::Random(id)) = expr {
                let n = old_ids.get_index_of(id);
                result.push(n);
            }
        }
        result
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        bincode::serialize(self).unwrap()
    }
    pub fn from_bytes(bytes: &[u8]) -> Result<IntermediateRepresentation, std::io::Error> {
        bincode::deserialize(bytes).map_err(|e| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("Error while reading IntermediateRepresentation: {}.\nThis is probably caused by incompatible arcis versions used by the interpreter (in encrypted-ixs) and the CLI (the `arcium` binary in `arcium build`).", e),
            )
        })
    }
    const DEFAULT_CIRCUIT_OUT_DIR: &'static str = "build";

    pub fn write_bytes(
        &self,
        circuit_name: &str,
        out_dir: Option<String>,
    ) -> Result<String, std::io::Error> {
        let current_dir = env::current_dir()?;

        let circuits_dir =
            current_dir.join(out_dir.unwrap_or(Self::DEFAULT_CIRCUIT_OUT_DIR.to_string()));
        let file_path = circuits_dir.join(format!("{circuit_name}.arcis.ir"));
        let res = String::from(file_path.to_str().unwrap());

        std::fs::create_dir_all(&circuits_dir)?;

        let mut file = std::fs::File::create(file_path)?;
        file.write_all(&self.to_bytes())?;
        Ok(res)
    }
    fn eval_inner<R: Rng + ?Sized>(
        &self,
        rng: &mut R,
        input_vals: &mut FxHashMap<usize, EvalValue>,
        eval_options: EvalOptions,
        generated_bits: &[bool],
        mut new_generated_bits: Option<&mut Vec<Option<bool>>>,
    ) -> Result<Vec<EvalValue>, UndefinedBehavior> {
        let mut vals = Vec::<EvalValue>::with_capacity(self.get_exprs().len());
        for (i, expr) in self.get_exprs().iter().enumerate() {
            let val: EvalValue = match expr {
                Expr::Scalar(FieldExpr::Input(input_id, info)) => match input_vals.get(input_id) {
                    None => {
                        let v = ScalarField::gen_inclusive_range(rng, info.min, info.max);
                        let res = EvalValue::Scalar(v);
                        input_vals.insert(*input_id, res);
                        res
                    }
                    Some(v) => *v,
                },
                Expr::Base(FieldExpr::Input(input_id, info)) => match input_vals.get(input_id) {
                    None => {
                        let v = BaseField::gen_inclusive_range(rng, info.min, info.max);
                        let res = EvalValue::Base(v);
                        input_vals.insert(*input_id, res);
                        res
                    }
                    Some(v) => *v,
                },
                Expr::Bit(BitExpr::Input(input_id, _)) => match input_vals.get(input_id) {
                    None => {
                        let v = R::gen(rng);
                        let res = EvalValue::Bit(v);
                        input_vals.insert(*input_id, res);
                        res
                    }
                    Some(v) => *v,
                },
                Expr::Curve(CurveExpr::Input(input_id, _)) => match input_vals.get(input_id) {
                    None => {
                        let v = R::gen(rng);
                        let res = EvalValue::Curve(v);
                        input_vals.insert(*input_id, res);
                        res
                    }
                    Some(v) => *v,
                },
                Expr::Scalar(FieldExpr::RandomVal(_)) => {
                    EvalValue::Scalar(ScalarField::random(&mut *rng))
                }
                Expr::Base(FieldExpr::RandomVal(_)) => {
                    EvalValue::Base(BaseField::random(&mut *rng))
                }
                Expr::Bit(BitExpr::Random(_)) => {
                    let val = self
                        .randomness_tracking
                        .get(&i)
                        .copied()
                        .and_then(|x| vals[x].to_signed_number().to_usize())
                        .and_then(|x| generated_bits.get(x).copied());
                    if let Some(output) = new_generated_bits.as_mut() {
                        output.push(val);
                    }
                    EvalValue::Bit(val.unwrap_or(rng.gen()))
                }
                Expr::ScalarConversion(ConversionExpr::EdaBit(_, k, _)) => {
                    let v = Number::gen_range(rng, &0.into(), &Number::power_of_two(*k));
                    EvalValue::Scalar(v.into())
                }
                Expr::BaseConversion(ConversionExpr::EdaBit(_, k, _)) => {
                    let v = Number::gen_range(rng, &0.into(), &Number::power_of_two(*k));
                    EvalValue::Base(v.into())
                }
                _ => {
                    let res = expr
                        .clone()
                        .apply(|x| vals[x])
                        .apply_2(&mut EvalValueUnwrap)
                        .eval();
                    match res {
                        Ok(n) => n,
                        Err(e) => match e {
                            EvalFailure::UndefinedBehavior(ub) => {
                                if eval_options.always_recover_from_ub {
                                    self.get_bounds()[i].get_sample_val()
                                } else {
                                    if eval_options.do_log {
                                        println!("vals: ");
                                        for (i, val) in vals.iter().enumerate() {
                                            println!("{i}: {val:?}");
                                        }
                                    }
                                    return Err(ub);
                                }
                            }
                            _ => {
                                panic!("Error at expr {expr:?}: {e:?}")
                            }
                        },
                    }
                }
            };
            if !eval_options.skip_bound_check && !self.get_bounds()[i].contains(val) {
                let expr_bound = expr
                    .clone()
                    .apply(|x| self.get_bounds()[x])
                    .apply_2(&mut BoundUnFold)
                    .bounds();
                panic!(
                    "{val:?} is not in {:?} for {expr:?}, or {expr_bound:?}: whose bounds are
            {:?}",
                    self.get_bounds()[i],
                    expr_bound
                );
            }
            vals.push(val);
        }
        if eval_options.do_log {
            println!("vals: ");
            for (i, val) in vals.iter().enumerate() {
                println!("{i}: {val:?}");
            }
        }
        Ok(self.get_outputs().iter().map(|i| vals[*i]).collect())
    }
    pub fn eval_with_options<R: Rng + ?Sized>(
        &self,
        rng: &mut R,
        input_vals: &mut FxHashMap<usize, EvalValue>,
        eval_options: EvalOptions,
    ) -> Result<Vec<EvalValue>, UndefinedBehavior> {
        self.eval_inner(rng, input_vals, eval_options, &[], None)
    }
    pub fn eval_with_log<R: Rng + ?Sized>(
        &self,
        rng: &mut R,
        input_vals: &mut FxHashMap<usize, EvalValue>,
    ) -> Result<Vec<EvalValue>, UndefinedBehavior> {
        self.eval_with_options(
            rng,
            input_vals,
            EvalOptions {
                do_log: true,
                ..Default::default()
            },
        )
    }
    pub fn eval<R: Rng + ?Sized>(
        &self,
        rng: &mut R,
        input_vals: &mut FxHashMap<usize, EvalValue>,
    ) -> Result<Vec<EvalValue>, UndefinedBehavior> {
        self.eval_with_options(rng, input_vals, EvalOptions::default())
    }
    pub fn eval_vec<R: Rng + ?Sized>(
        &self,
        inputs: Vec<EvalValue>,
        rng: &mut R,
        generated_bits: &[bool],
    ) -> Result<(Vec<EvalValue>, Vec<Option<bool>>), UndefinedBehavior> {
        let mut inputs = inputs.into_iter().enumerate().collect();
        let mut res_2 = Vec::new();
        let res_1 = self.eval_inner(
            rng,
            &mut inputs,
            EvalOptions {
                always_recover_from_ub: true,
                ..Default::default()
            },
            generated_bits,
            Some(&mut res_2),
        )?;
        Ok((res_1, res_2))
    }
}

#[derive(Debug, Clone, Copy, Default)]
pub struct EvalOptions {
    pub always_recover_from_ub: bool,
    pub do_log: bool,
    pub skip_bound_check: bool,
}

impl fmt::Display for IntermediateRepresentation {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let mut i = 0;
        for gate in &self.exprs {
            writeln!(f, "{i}: {gate:?};")?;
            i += 1;
        }
        for output in &self.outputs {
            writeln!(f, "{i}: Output({output});")?;
            i += 1;
        }
        write!(f, "")
    }
}

#[derive(Debug)]
pub enum ProgramError {
    DuplicateInputId,
}

#[cfg(test)]
mod tests {
    use crate::core::{expressions::expr::EvalValue, ir::IntermediateRepresentation};
    use rand::Rng;
    use rustc_hash::FxHashMap;

    impl IntermediateRepresentation {
        pub fn test_eq_with_vals<R: Rng + ?Sized>(
            rng: &mut R,
            ctrl_ir: &IntermediateRepresentation,
            test_ir: &IntermediateRepresentation,
            input_vals: &mut FxHashMap<usize, EvalValue>,
        ) {
            let ctrl_res = ctrl_ir.eval(rng, input_vals);
            if ctrl_res.is_err() {
                return;
            }
            let test_res = test_ir.eval(rng, input_vals);
            if ctrl_res != test_res {
                if test_ir.get_exprs().len() < 65536 {
                    println!("ctrl: {}", ctrl_ir);
                    println!("test: {}", test_ir);
                    println!("input_vals: {input_vals:?}");
                    println!("ctrl_res: {ctrl_res:?}");
                    println!("test_res: {test_res:?}");
                    let _ = test_ir.eval_with_log(rng, input_vals);
                }
                assert_eq!(ctrl_res, test_res);
            }
        }
        pub fn test_eq<R: Rng + ?Sized>(
            rng: &mut R,
            ctrl_ir: &IntermediateRepresentation,
            test_ir: &IntermediateRepresentation,
            n_tests: usize,
        ) {
            for _ in 0..n_tests {
                let mut input_vals = FxHashMap::<usize, _>::default();
                Self::test_eq_with_vals(rng, ctrl_ir, test_ir, &mut input_vals)
            }
        }
    }
}