openvm_native_recursion/fri/
witness.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
use openvm_native_compiler::ir::{Builder, Witness};
use openvm_stark_sdk::p3_bn254_fr::Bn254Fr;

use super::types::BatchOpeningVariable;
use crate::{
    config::outer::{
        OuterBatchOpening, OuterCommitPhaseStep, OuterConfig, OuterFriProof, OuterQueryProof,
    },
    digest::DigestVal,
    fri::types::{FriCommitPhaseProofStepVariable, FriProofVariable, FriQueryProofVariable},
    witness::{VectorWitnessable, Witnessable},
};

type C = OuterConfig;

fn to_digest_val_vec(v: &[[Bn254Fr; 1]]) -> Vec<DigestVal<C>> {
    v.iter().map(|x| DigestVal::N(x.to_vec())).collect()
}

impl Witnessable<C> for OuterCommitPhaseStep {
    type WitnessVariable = FriCommitPhaseProofStepVariable<C>;

    fn read(&self, builder: &mut Builder<C>) -> Self::WitnessVariable {
        let sibling_value = self.sibling_value.read(builder);
        let opening_proof = to_digest_val_vec(&self.opening_proof).read(builder);
        Self::WitnessVariable {
            sibling_value,
            opening_proof,
        }
    }

    fn write(&self, witness: &mut Witness<OuterConfig>) {
        self.sibling_value.write(witness);
        to_digest_val_vec(&self.opening_proof).write(witness);
    }
}

impl VectorWitnessable<C> for OuterCommitPhaseStep {}

impl Witnessable<C> for OuterQueryProof {
    type WitnessVariable = FriQueryProofVariable<C>;

    fn read(&self, builder: &mut Builder<C>) -> Self::WitnessVariable {
        let input_proof = self.input_proof.read(builder);
        let commit_phase_openings = self.commit_phase_openings.read(builder);
        Self::WitnessVariable {
            input_proof,
            commit_phase_openings,
        }
    }

    fn write(&self, witness: &mut Witness<OuterConfig>) {
        self.input_proof.write(witness);
        self.commit_phase_openings.write(witness);
    }
}

impl VectorWitnessable<C> for OuterQueryProof {}

impl Witnessable<C> for OuterFriProof {
    type WitnessVariable = FriProofVariable<C>;

    fn read(&self, builder: &mut Builder<C>) -> Self::WitnessVariable {
        let commit_phase_commits = self.commit_phase_commits.read(builder);
        let query_proofs = self.query_proofs.read(builder);
        let final_poly = self.final_poly.read(builder);
        let pow_witness = self.pow_witness.read(builder);
        Self::WitnessVariable {
            commit_phase_commits,
            query_proofs,
            final_poly,
            pow_witness,
        }
    }

    fn write(&self, witness: &mut Witness<OuterConfig>) {
        self.commit_phase_commits.write(witness);
        <Vec<_> as Witnessable<C>>::write(&self.query_proofs, witness);
        self.final_poly.write(witness);
        self.pow_witness.write(witness);
    }
}

impl Witnessable<C> for OuterBatchOpening {
    type WitnessVariable = BatchOpeningVariable<C>;

    fn read(&self, builder: &mut Builder<C>) -> Self::WitnessVariable {
        let opened_values = self.opened_values.read(builder);
        let opening_proof = to_digest_val_vec(&self.opening_proof).read(builder);
        Self::WitnessVariable {
            opened_values,
            opening_proof,
        }
    }

    fn write(&self, witness: &mut Witness<OuterConfig>) {
        self.opened_values.write(witness);
        to_digest_val_vec(&self.opening_proof).write(witness);
    }
}

impl VectorWitnessable<C> for OuterBatchOpening {}
impl VectorWitnessable<C> for Vec<OuterBatchOpening> {}