halo2_axiom/dev/
util.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
use group::ff::Field;
use std::collections::BTreeMap;

use super::{metadata, CellValue, InstanceValue, Value};
use crate::{
    plonk::{
        Advice, AdviceQuery, Any, Column, ColumnType, Expression, FixedQuery, Gate, InstanceQuery,
        VirtualCell,
    },
    poly::Rotation,
};

pub(crate) struct AnyQuery {
    /// Query index
    pub index: Option<usize>,
    /// Column type
    pub column_type: Any,
    /// Column index
    pub column_index: usize,
    /// Rotation of this query
    pub rotation: Rotation,
}

impl From<FixedQuery> for AnyQuery {
    fn from(query: FixedQuery) -> Self {
        Self {
            index: query.index,
            column_type: Any::Fixed,
            column_index: query.column_index,
            rotation: query.rotation,
        }
    }
}

impl From<AdviceQuery> for AnyQuery {
    fn from(query: AdviceQuery) -> Self {
        Self {
            index: query.index,
            column_type: Any::Advice(Advice { phase: query.phase }),
            column_index: query.column_index,
            rotation: query.rotation,
        }
    }
}

impl From<InstanceQuery> for AnyQuery {
    fn from(query: InstanceQuery) -> Self {
        Self {
            index: query.index,
            column_type: Any::Instance,
            column_index: query.column_index,
            rotation: query.rotation,
        }
    }
}

pub(super) fn format_value<F: Field>(v: F) -> String {
    if v.is_zero_vartime() {
        "0".into()
    } else if v == F::ONE {
        "1".into()
    } else if v == -F::ONE {
        "-1".into()
    } else {
        // Format value as hex.
        let s = format!("{:?}", v);
        // Remove leading zeroes.
        let s = s.strip_prefix("0x").unwrap();
        let s = s.trim_start_matches('0');
        format!("0x{}", s)
    }
}

pub(super) fn load<'a, F: Field, T: ColumnType, Q: Into<AnyQuery> + Copy>(
    n: i32,
    row: i32,
    queries: &'a [(Column<T>, Rotation)],
    cells: &'a [Vec<CellValue<F>>],
) -> impl Fn(Q) -> Value<F> + 'a {
    move |query| {
        let (column, at) = &queries[query.into().index.unwrap()];
        let resolved_row = (row + at.0) % n;
        cells[column.index()][resolved_row as usize].into()
    }
}

pub(super) fn load_instance<'a, F: Field, T: ColumnType, Q: Into<AnyQuery> + Copy>(
    n: i32,
    row: i32,
    queries: &'a [(Column<T>, Rotation)],
    cells: &'a [Vec<InstanceValue<F>>],
) -> impl Fn(Q) -> Value<F> + 'a {
    move |query| {
        let (column, at) = &queries[query.into().index.unwrap()];
        let resolved_row = (row + at.0) % n;
        let cell = &cells[column.index()][resolved_row as usize];
        Value::Real(cell.value())
    }
}

fn cell_value<'a, F: Field, Q: Into<AnyQuery> + Copy>(
    virtual_cells: &'a [VirtualCell],
    load: impl Fn(Q) -> Value<F> + 'a,
) -> impl Fn(Q) -> BTreeMap<metadata::VirtualCell, String> + 'a {
    move |query| {
        let AnyQuery {
            column_type,
            column_index,
            rotation,
            ..
        } = query.into();
        virtual_cells
            .iter()
            .find(|c| {
                c.column.column_type() == &column_type
                    && c.column.index() == column_index
                    && c.rotation == rotation
            })
            // None indicates a selector, which we don't bother showing.
            .map(|cell| {
                (
                    cell.clone().into(),
                    match load(query) {
                        Value::Real(v) => format_value(v),
                        Value::Poison => unreachable!(),
                    },
                )
            })
            .into_iter()
            .collect()
    }
}

pub(super) fn cell_values<'a, F: Field>(
    gate: &Gate<F>,
    poly: &Expression<F>,
    load_fixed: impl Fn(FixedQuery) -> Value<F> + 'a,
    load_advice: impl Fn(AdviceQuery) -> Value<F> + 'a,
    load_instance: impl Fn(InstanceQuery) -> Value<F> + 'a,
) -> Vec<(metadata::VirtualCell, String)> {
    let virtual_cells = gate.queried_cells();
    let cell_values = poly.evaluate(
        &|_| BTreeMap::default(),
        &|_| panic!("virtual selectors are removed during optimization"),
        &cell_value(virtual_cells, load_fixed),
        &cell_value(virtual_cells, load_advice),
        &cell_value(virtual_cells, load_instance),
        &|_| BTreeMap::default(),
        &|a| a,
        &|mut a, mut b| {
            a.append(&mut b);
            a
        },
        &|mut a, mut b| {
            a.append(&mut b);
            a
        },
        &|a, _| a,
    );
    cell_values.into_iter().collect()
}