triton_air/
cross_table_argument.rs

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
use std::ops::Add;
use std::ops::Mul;

use constraint_circuit::ConstraintCircuitBuilder;
use constraint_circuit::ConstraintCircuitMonad;
use constraint_circuit::DualRowIndicator;
use constraint_circuit::SingleRowIndicator;
use constraint_circuit::SingleRowIndicator::Aux;
use twenty_first::prelude::*;

use crate::challenge_id::ChallengeId;

use crate::table_column::CascadeAuxColumn;
use crate::table_column::HashAuxColumn;
use crate::table_column::JumpStackAuxColumn;
use crate::table_column::LookupAuxColumn;
use crate::table_column::MasterAuxColumn;
use crate::table_column::OpStackAuxColumn;
use crate::table_column::ProcessorAuxColumn;
use crate::table_column::ProgramAuxColumn;
use crate::table_column::RamAuxColumn;
use crate::table_column::U32AuxColumn;

pub trait CrossTableArg {
    fn default_initial() -> XFieldElement
    where
        Self: Sized;

    fn compute_terminal(
        symbols: &[BFieldElement],
        initial: XFieldElement,
        challenge: XFieldElement,
    ) -> XFieldElement
    where
        Self: Sized;
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct PermArg;

impl CrossTableArg for PermArg {
    fn default_initial() -> XFieldElement {
        1.into()
    }

    /// Compute the product for a permutation argument as specified by `initial`, `challenge`,
    /// and `symbols`. This amounts to evaluating polynomial
    ///  `f(x) = initial · Π_i (x - symbols[i])`
    /// at point `challenge`, _i.e._, returns `f(challenge)`.
    fn compute_terminal(
        symbols: &[BFieldElement],
        initial: XFieldElement,
        challenge: XFieldElement,
    ) -> XFieldElement {
        symbols
            .iter()
            .map(|&symbol| challenge - symbol)
            .fold(initial, XFieldElement::mul)
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct EvalArg;

impl CrossTableArg for EvalArg {
    fn default_initial() -> XFieldElement {
        1.into()
    }

    /// Compute the evaluation for an evaluation argument as specified by `initial`, `challenge`,
    /// and `symbols`. This amounts to evaluating polynomial
    /// `f(x) = initial·x^n + Σ_i symbols[n-i]·x^i`
    /// at point `challenge`, _i.e._, returns `f(challenge)`.
    fn compute_terminal(
        symbols: &[BFieldElement],
        initial: XFieldElement,
        challenge: XFieldElement,
    ) -> XFieldElement {
        symbols.iter().fold(initial, |running_evaluation, &symbol| {
            challenge * running_evaluation + symbol
        })
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct LookupArg;

impl CrossTableArg for LookupArg {
    fn default_initial() -> XFieldElement {
        0.into()
    }

    fn compute_terminal(
        symbols: &[BFieldElement],
        initial: XFieldElement,
        challenge: XFieldElement,
    ) -> XFieldElement {
        symbols
            .iter()
            .map(|symbol| (challenge - symbol.lift()).inverse())
            .fold(initial, XFieldElement::add)
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct GrandCrossTableArg;

impl GrandCrossTableArg {
    pub fn initial_constraints(
        _circuit_builder: &ConstraintCircuitBuilder<SingleRowIndicator>,
    ) -> Vec<ConstraintCircuitMonad<SingleRowIndicator>> {
        // no further constraints
        vec![]
    }

    pub fn consistency_constraints(
        _circuit_builder: &ConstraintCircuitBuilder<SingleRowIndicator>,
    ) -> Vec<ConstraintCircuitMonad<SingleRowIndicator>> {
        // no further constraints
        vec![]
    }

    pub fn transition_constraints(
        _circuit_builder: &ConstraintCircuitBuilder<DualRowIndicator>,
    ) -> Vec<ConstraintCircuitMonad<DualRowIndicator>> {
        // no further constraints
        vec![]
    }

    pub fn terminal_constraints(
        circuit_builder: &ConstraintCircuitBuilder<SingleRowIndicator>,
    ) -> Vec<ConstraintCircuitMonad<SingleRowIndicator>> {
        let challenge = |c| circuit_builder.challenge(c);
        let aux_row = |col_index| circuit_builder.input(Aux(col_index));

        // Closures cannot take arguments of type `impl Trait`. Hence: some more helpers. \o/
        let program_aux_row = |column: ProgramAuxColumn| aux_row(column.master_aux_index());
        let processor_aux_row = |column: ProcessorAuxColumn| aux_row(column.master_aux_index());
        let op_stack_aux_row = |column: OpStackAuxColumn| aux_row(column.master_aux_index());
        let ram_aux_row = |column: RamAuxColumn| aux_row(column.master_aux_index());
        let j_stack_aux_row = |column: JumpStackAuxColumn| aux_row(column.master_aux_index());
        let hash_aux_row = |column: HashAuxColumn| aux_row(column.master_aux_index());
        let cascade_aux_row = |column: CascadeAuxColumn| aux_row(column.master_aux_index());
        let lookup_aux_row = |column: LookupAuxColumn| aux_row(column.master_aux_index());
        let u32_aux_row = |column: U32AuxColumn| aux_row(column.master_aux_index());

        let program_attestation = program_aux_row(ProgramAuxColumn::SendChunkRunningEvaluation)
            - hash_aux_row(HashAuxColumn::ReceiveChunkRunningEvaluation);
        let input_to_processor = challenge(ChallengeId::StandardInputTerminal)
            - processor_aux_row(ProcessorAuxColumn::InputTableEvalArg);
        let processor_to_output = processor_aux_row(ProcessorAuxColumn::OutputTableEvalArg)
            - challenge(ChallengeId::StandardOutputTerminal);
        let instruction_lookup =
            processor_aux_row(ProcessorAuxColumn::InstructionLookupClientLogDerivative)
                - program_aux_row(ProgramAuxColumn::InstructionLookupServerLogDerivative);
        let processor_to_op_stack = processor_aux_row(ProcessorAuxColumn::OpStackTablePermArg)
            - op_stack_aux_row(OpStackAuxColumn::RunningProductPermArg);
        let processor_to_ram = processor_aux_row(ProcessorAuxColumn::RamTablePermArg)
            - ram_aux_row(RamAuxColumn::RunningProductPermArg);
        let processor_to_jump_stack = processor_aux_row(ProcessorAuxColumn::JumpStackTablePermArg)
            - j_stack_aux_row(JumpStackAuxColumn::RunningProductPermArg);
        let hash_input = processor_aux_row(ProcessorAuxColumn::HashInputEvalArg)
            - hash_aux_row(HashAuxColumn::HashInputRunningEvaluation);
        let hash_digest = hash_aux_row(HashAuxColumn::HashDigestRunningEvaluation)
            - processor_aux_row(ProcessorAuxColumn::HashDigestEvalArg);
        let sponge = processor_aux_row(ProcessorAuxColumn::SpongeEvalArg)
            - hash_aux_row(HashAuxColumn::SpongeRunningEvaluation);
        let hash_to_cascade = cascade_aux_row(CascadeAuxColumn::HashTableServerLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState0HighestClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState0MidHighClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState0MidLowClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState0LowestClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState1HighestClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState1MidHighClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState1MidLowClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState1LowestClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState2HighestClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState2MidHighClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState2MidLowClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState2LowestClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState3HighestClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState3MidHighClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState3MidLowClientLogDerivative)
            - hash_aux_row(HashAuxColumn::CascadeState3LowestClientLogDerivative);
        let cascade_to_lookup = cascade_aux_row(CascadeAuxColumn::LookupTableClientLogDerivative)
            - lookup_aux_row(LookupAuxColumn::CascadeTableServerLogDerivative);
        let processor_to_u32 = processor_aux_row(ProcessorAuxColumn::U32LookupClientLogDerivative)
            - u32_aux_row(U32AuxColumn::LookupServerLogDerivative);

        let clock_jump_difference_lookup =
            processor_aux_row(ProcessorAuxColumn::ClockJumpDifferenceLookupServerLogDerivative)
                - op_stack_aux_row(OpStackAuxColumn::ClockJumpDifferenceLookupClientLogDerivative)
                - ram_aux_row(RamAuxColumn::ClockJumpDifferenceLookupClientLogDerivative)
                - j_stack_aux_row(JumpStackAuxColumn::ClockJumpDifferenceLookupClientLogDerivative);

        vec![
            program_attestation,
            input_to_processor,
            processor_to_output,
            instruction_lookup,
            processor_to_op_stack,
            processor_to_ram,
            processor_to_jump_stack,
            hash_input,
            hash_digest,
            sponge,
            hash_to_cascade,
            cascade_to_lookup,
            processor_to_u32,
            clock_jump_difference_lookup,
        ]
    }
}

#[cfg(test)]
mod tests {
    use num_traits::Zero;
    use proptest::prelude::*;
    use proptest_arbitrary_interop::arb;
    use test_strategy::proptest;

    use super::*;

    #[proptest]
    fn permutation_argument_is_identical_to_evaluating_zerofier_polynomial(
        #[strategy(arb())] roots: Vec<BFieldElement>,
        #[strategy(arb())] initial: XFieldElement,
        #[strategy(arb())] challenge: XFieldElement,
    ) {
        let poly_evaluation =
            initial * Polynomial::zerofier(&roots).evaluate::<_, XFieldElement>(challenge);
        let perm_arg_terminal = PermArg::compute_terminal(&roots, initial, challenge);
        prop_assert_eq!(poly_evaluation, perm_arg_terminal);
    }

    #[proptest]
    fn evaluation_argument_is_identical_to_evaluating_polynomial(
        #[strategy(arb())]
        #[filter(!#polynomial.is_zero())]
        polynomial: Polynomial<'static, BFieldElement>,
        #[strategy(arb())] challenge: XFieldElement,
    ) {
        let poly_evaluation: XFieldElement = polynomial.evaluate(challenge);

        let mut coefficients = polynomial.into_coefficients();
        let initial = coefficients.pop().unwrap();
        coefficients.reverse();
        let eval_arg_terminal = EvalArg::compute_terminal(&coefficients, initial.lift(), challenge);

        prop_assert_eq!(poly_evaluation, eval_arg_terminal);
    }

    #[proptest]
    fn lookup_argument_is_identical_to_inverse_of_evaluation_of_zerofier_polynomial(
        #[strategy(arb())]
        #[filter(#roots.iter().all(|r| r.lift() != #challenge))]
        roots: Vec<BFieldElement>,
        #[strategy(arb())] initial: XFieldElement,
        #[strategy(arb())] challenge: XFieldElement,
    ) {
        let polynomial = Polynomial::zerofier(&roots);
        let derivative = polynomial.formal_derivative();
        let poly_evaluation = derivative.evaluate::<_, XFieldElement>(challenge)
            / polynomial.evaluate::<_, XFieldElement>(challenge);
        let lookup_arg_terminal = LookupArg::compute_terminal(&roots, initial, challenge);
        prop_assert_eq!(initial + poly_evaluation, lookup_arg_terminal);
    }
}