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
#![feature(test)]

#[cfg(test)]
extern crate test;

// simple utility functions
#[macro_use]
mod util;

#[cfg(test)]
mod tests;

// traits and implementations of the underlying ring
// exposed to enable uses to define programs for the supported rings.
pub mod algebra;

// field switching
pub mod fieldswitching;

// pre-processing
pub mod preprocessing;

// online phase
pub mod online;

// abstraction for Fiat-Shamir
mod oracle;

// puncturable PRF abstractions
mod crypto;

// internal constants
mod consts;

mod fieldswitching_proof;
mod proof;

pub use proof::{ProofGf2P64, ProofGf2P64_64, ProofGf2P64_85, ProofGf2P8};

use crate::algebra::RingElement;

use crate::algebra::gf2::BitScalar;
use crate::algebra::z64::Scalar;

pub use util::eval::{evaluate_fieldswitching_btoa_program, evaluate_program};

#[macro_use]
extern crate serde_big_array;

big_array! { BigArray; }

use serde::{Deserialize, Serialize};

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub enum Instruction<E: RingElement> {
    NrOfWires(usize), // Total nr of wires, should be first (and only first) in circuit
    AddConst(usize, usize, E), // addition of constant
    MulConst(usize, usize, E), // multiplication by constant
    LocalOp(usize, usize), // apply domain-specific local operation
    Mul(usize, usize, usize), // multiplication of two wires
    Add(usize, usize, usize), // addition of two wires
    Sub(usize, usize, usize), // subtraction of one wire from another
    Branch(usize),    // load next branch element
    Input(usize),     // read next field element from input tape
    Output(usize),    // output wire (write wire-value to output tape)
    Const(usize, E),  // fixed constant value
}

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub enum ConnectionInstruction {
    BToA(usize, #[serde(with = "BigArray")] [usize; 64]), // Change field from GF(2) to GF(2^k) //TODO(gvl): make more flexible, max size of arithmetic ring is now 64 bits
    AToB(#[serde(with = "BigArray")] [usize; 64], usize), // Change field from GF(2^k) to GF(2) //TODO(gvl): make more flexible, max size of arithmetic ring is now 64 bits
    Challenge(usize),                                     // Input a challenge on a wire
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ProgramTriple {
    pub boolean: Vec<Instruction<BitScalar>>,
    pub arithmetic: Vec<Instruction<Scalar>>,
    pub connection: Vec<ConnectionInstruction>,
}

type Instructions<D> = Vec<Instruction<<D as algebra::Domain>::Scalar>>;