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
use crate::{
    columns::{Column, ColumnIndexer},
    serialization::interpreter::{N_LIMBS_LARGE, N_LIMBS_SMALL},
};

/// Number of columns in the FEC circuits.
pub const FEC_N_COLUMNS: usize =
    FECColumnInput::N_COL + FECColumnOutput::N_COL + FECColumnInter::N_COL;

/// FEC ADD inputs: two points = four coordinates, and each in 4
/// "large format" limbs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum FECColumnInput {
    XP(usize), // 4
    YP(usize), // 4
    XQ(usize), // 4
    YQ(usize), // 4
}

/// FEC ADD outputs: one point, each in 17 limb output format.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum FECColumnOutput {
    XR(usize), // 17
    YR(usize), // 17
}

/// FEC ADD intermediate (work) columns.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum FECColumnInter {
    F(usize),      // 4
    S(usize),      // 17
    Q1(usize),     // 17
    Q2(usize),     // 17
    Q3(usize),     // 17
    Q1Sign,        // 1
    Q2Sign,        // 1
    Q3Sign,        // 1
    Q1L(usize),    // 4
    Q2L(usize),    // 4
    Q3L(usize),    // 4
    Carry1(usize), // 36
    Carry2(usize), // 36
    Carry3(usize), // 36
}

/// Columns used by the FEC Addition subcircuit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum FECColumn {
    Input(FECColumnInput),
    Output(FECColumnOutput),
    Inter(FECColumnInter),
}

impl ColumnIndexer for FECColumnInput {
    const N_COL: usize = 4 * N_LIMBS_LARGE;
    fn to_column(self) -> Column {
        match self {
            FECColumnInput::XP(i) => {
                assert!(i < N_LIMBS_LARGE);
                Column::Relation(i)
            }
            FECColumnInput::YP(i) => {
                assert!(i < N_LIMBS_LARGE);
                Column::Relation(N_LIMBS_LARGE + i)
            }
            FECColumnInput::XQ(i) => {
                assert!(i < N_LIMBS_LARGE);
                Column::Relation(2 * N_LIMBS_LARGE + i)
            }
            FECColumnInput::YQ(i) => {
                assert!(i < N_LIMBS_LARGE);
                Column::Relation(3 * N_LIMBS_LARGE + i)
            }
        }
    }
}

impl ColumnIndexer for FECColumnOutput {
    const N_COL: usize = 2 * N_LIMBS_SMALL;
    fn to_column(self) -> Column {
        match self {
            FECColumnOutput::XR(i) => {
                assert!(i < N_LIMBS_SMALL);
                Column::Relation(i)
            }
            FECColumnOutput::YR(i) => {
                assert!(i < N_LIMBS_SMALL);
                Column::Relation(N_LIMBS_SMALL + i)
            }
        }
    }
}

impl ColumnIndexer for FECColumnInter {
    const N_COL: usize = 4 * N_LIMBS_LARGE + 10 * N_LIMBS_SMALL + 9;
    fn to_column(self) -> Column {
        match self {
            FECColumnInter::F(i) => {
                assert!(i < N_LIMBS_LARGE);
                Column::Relation(i)
            }
            FECColumnInter::S(i) => {
                assert!(i < N_LIMBS_SMALL);
                Column::Relation(N_LIMBS_LARGE + i)
            }
            FECColumnInter::Q1(i) => {
                assert!(i < N_LIMBS_SMALL);
                Column::Relation(N_LIMBS_LARGE + N_LIMBS_SMALL + i)
            }
            FECColumnInter::Q2(i) => {
                assert!(i < N_LIMBS_SMALL);
                Column::Relation(N_LIMBS_LARGE + 2 * N_LIMBS_SMALL + i)
            }
            FECColumnInter::Q3(i) => {
                assert!(i < N_LIMBS_SMALL);
                Column::Relation(N_LIMBS_LARGE + 3 * N_LIMBS_SMALL + i)
            }
            FECColumnInter::Q1Sign => Column::Relation(N_LIMBS_LARGE + 4 * N_LIMBS_SMALL),
            FECColumnInter::Q2Sign => Column::Relation(N_LIMBS_LARGE + 4 * N_LIMBS_SMALL + 1),
            FECColumnInter::Q3Sign => Column::Relation(N_LIMBS_LARGE + 4 * N_LIMBS_SMALL + 2),
            FECColumnInter::Q1L(i) => {
                assert!(i < N_LIMBS_LARGE);
                Column::Relation(N_LIMBS_LARGE + 4 * N_LIMBS_SMALL + 3 + i)
            }
            FECColumnInter::Q2L(i) => {
                assert!(i < N_LIMBS_LARGE);
                Column::Relation(2 * N_LIMBS_LARGE + 4 * N_LIMBS_SMALL + 3 + i)
            }
            FECColumnInter::Q3L(i) => {
                assert!(i < N_LIMBS_LARGE);
                Column::Relation(3 * N_LIMBS_LARGE + 4 * N_LIMBS_SMALL + 3 + i)
            }
            FECColumnInter::Carry1(i) => {
                assert!(i < 2 * N_LIMBS_SMALL + 2);
                Column::Relation(4 * N_LIMBS_LARGE + 4 * N_LIMBS_SMALL + 3 + i)
            }
            FECColumnInter::Carry2(i) => {
                assert!(i < 2 * N_LIMBS_SMALL + 2);
                Column::Relation(4 * N_LIMBS_LARGE + 6 * N_LIMBS_SMALL + 5 + i)
            }
            FECColumnInter::Carry3(i) => {
                assert!(i < 2 * N_LIMBS_SMALL + 2);
                Column::Relation(4 * N_LIMBS_LARGE + 8 * N_LIMBS_SMALL + 7 + i)
            }
        }
    }
}

impl ColumnIndexer for FECColumn {
    const N_COL: usize = FEC_N_COLUMNS;
    fn to_column(self) -> Column {
        match self {
            FECColumn::Input(input) => input.to_column(),
            FECColumn::Inter(inter) => inter.to_column().add_rel_offset(FECColumnInput::N_COL),
            FECColumn::Output(output) => output
                .to_column()
                .add_rel_offset(FECColumnInput::N_COL + FECColumnInter::N_COL),
        }
    }
}