-
Notifications
You must be signed in to change notification settings - Fork 41
/
common.rs
96 lines (87 loc) · 2.88 KB
/
common.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
use axiom_codec::types::field_elements::AnySubqueryResult;
use axiom_eth::{
halo2_base::AssignedValue,
utils::component::{
types::{FixLenLogical, Flatten},
utils::get_logical_value,
ComponentPromiseResultsInMerkle, ComponentType, FlattenVirtualTable, LogicalResult,
},
};
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use crate::Field;
/// Generic type for output of a subquery shard circuit
#[derive(Clone, Default, Debug, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OutputSubqueryShard<I, O> {
/// Vector is assumed to be resized to the capacity.
pub results: Vec<AnySubqueryResult<I, O>>,
}
impl<I, O> OutputSubqueryShard<I, O> {
pub fn len(&self) -> usize {
self.results.len()
}
pub fn is_empty(&self) -> bool {
self.results.is_empty()
}
pub fn into_flattened_pairs<T>(self) -> Vec<(Flatten<T>, Flatten<T>)>
where
I: Into<Flatten<T>>,
O: Into<Flatten<T>>,
T: Copy,
{
self.results.into_iter().map(|r| (r.subquery.into(), r.value.into())).collect()
}
// cannot do blanket From implementation because it conflicts with Rust std T: From<T> impl
pub fn convert_into<S, T>(self) -> OutputSubqueryShard<S, T>
where
I: Into<S>,
O: Into<T>,
{
OutputSubqueryShard {
results: self
.results
.into_iter()
.map(|r| AnySubqueryResult { subquery: r.subquery.into(), value: r.value.into() })
.collect(),
}
}
}
/// Helper function to convert OutputSubqueryShard into ComponentPromiseResults
pub fn shard_into_component_promise_results<F: Field, T: ComponentType<F>>(
shard: OutputSubqueryShard<T::LogicalInput, T::OutputValue>,
) -> ComponentPromiseResultsInMerkle<F> {
ComponentPromiseResultsInMerkle::from_single_shard(
shard
.results
.into_iter()
.map(|r| LogicalResult::<F, T>::new(r.subquery, r.value))
.collect_vec(),
)
}
pub(crate) fn extract_virtual_table<
F: Field,
S: Into<Flatten<AssignedValue<F>>>,
T: Into<Flatten<AssignedValue<F>>>,
>(
outputs: impl Iterator<Item = AnySubqueryResult<S, T>>,
) -> FlattenVirtualTable<AssignedValue<F>> {
outputs.map(|output| (output.subquery.into(), output.value.into())).collect()
}
pub(crate) fn extract_logical_results<
F: Field,
S: FixLenLogical<AssignedValue<F>>,
FS: FixLenLogical<F>,
T: ComponentType<F, InputValue = FS, InputWitness = S, LogicalInput = FS>,
>(
outputs: impl Iterator<Item = AnySubqueryResult<S, T::OutputWitness>>,
) -> Vec<LogicalResult<F, T>> {
outputs
.map(|output| {
LogicalResult::<F, T>::new(
get_logical_value(&output.subquery),
get_logical_value(&output.value),
)
})
.collect()
}