Skip to main content

sigma_proofs/linear_relation/
convert.rs

1use alloc::vec;
2use alloc::vec::Vec;
3use ff::Field;
4use group::Group;
5
6use super::{GroupVar, ScalarTerm, ScalarVar, Sum, Term, Weighted};
7
8impl<G> From<ScalarVar<G>> for ScalarTerm<G> {
9    fn from(value: ScalarVar<G>) -> Self {
10        Self::Var(value)
11    }
12}
13
14impl<G: Group> From<ScalarVar<G>> for Weighted<ScalarTerm<G>, G::Scalar> {
15    fn from(value: ScalarVar<G>) -> Self {
16        ScalarTerm::from(value).into()
17    }
18}
19
20impl<G: Group> From<Weighted<ScalarVar<G>, G::Scalar>> for Weighted<ScalarTerm<G>, G::Scalar> {
21    fn from(value: Weighted<ScalarVar<G>, G::Scalar>) -> Self {
22        Self {
23            term: value.term.into(),
24            weight: value.weight,
25        }
26    }
27}
28
29// NOTE: Rust does not accept an impl over From<G::Scalar>
30impl<T: Field + Into<G::Scalar>, G: Group> From<T> for Weighted<ScalarTerm<G>, G::Scalar> {
31    fn from(value: T) -> Self {
32        Self {
33            term: ScalarTerm::Unit,
34            weight: value.into(),
35        }
36    }
37}
38
39impl<G> From<(ScalarVar<G>, GroupVar<G>)> for Term<G> {
40    fn from((scalar, elem): (ScalarVar<G>, GroupVar<G>)) -> Self {
41        Self {
42            scalar: scalar.into(),
43            elem,
44        }
45    }
46}
47
48impl<G> From<(ScalarTerm<G>, GroupVar<G>)> for Term<G> {
49    fn from((scalar, elem): (ScalarTerm<G>, GroupVar<G>)) -> Self {
50        Self { scalar, elem }
51    }
52}
53
54impl<G> From<GroupVar<G>> for Term<G> {
55    fn from(value: GroupVar<G>) -> Self {
56        Term {
57            scalar: ScalarTerm::Unit,
58            elem: value,
59        }
60    }
61}
62
63impl<G: Group> From<(ScalarVar<G>, GroupVar<G>)> for Weighted<Term<G>, G::Scalar> {
64    fn from(pair: (ScalarVar<G>, GroupVar<G>)) -> Self {
65        Term::from(pair).into()
66    }
67}
68
69impl<G: Group> From<(ScalarTerm<G>, GroupVar<G>)> for Weighted<Term<G>, G::Scalar> {
70    fn from(pair: (ScalarTerm<G>, GroupVar<G>)) -> Self {
71        Term::from(pair).into()
72    }
73}
74
75impl<G: Group> From<GroupVar<G>> for Weighted<Term<G>, G::Scalar> {
76    fn from(value: GroupVar<G>) -> Self {
77        Term::from(value).into()
78    }
79}
80
81impl<G: Group> From<Weighted<GroupVar<G>, G::Scalar>> for Weighted<Term<G>, G::Scalar> {
82    fn from(value: Weighted<GroupVar<G>, G::Scalar>) -> Self {
83        Weighted {
84            term: value.term.into(),
85            weight: value.weight,
86        }
87    }
88}
89
90impl<T, F: Field> From<T> for Weighted<T, F> {
91    fn from(term: T) -> Self {
92        Self {
93            term,
94            weight: F::ONE,
95        }
96    }
97}
98
99// NOTE: This is implemented directly for each of the key types to avoid collision with the blanket
100// Into impl provided by the standard library.
101macro_rules! impl_from_for_sum {
102    ($($type:ty),+) => {
103        $(
104        impl<G: Group, T: Into<$type>> From<T> for Sum<$type> {
105            fn from(value: T) -> Self {
106                Sum(vec![value.into()])
107            }
108        }
109
110        impl<G: Group, T: Into<$type>> From<Vec<T>> for Sum<$type> {
111            fn from(terms: Vec<T>) -> Self {
112                Self::from_iter(terms)
113            }
114        }
115
116        impl<G: Group, T: Into<$type>, const N: usize> From<[T; N]> for Sum<$type> {
117            fn from(terms: [T; N]) -> Self {
118                Self::from_iter(terms)
119            }
120        }
121
122        impl<G: Group, T: Into<$type>> FromIterator<T> for Sum<$type> {
123            fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
124                Self(iter.into_iter().map(|x| x.into()).collect())
125            }
126        }
127        )+
128    };
129}
130
131impl_from_for_sum!(
132    ScalarVar<G>,
133    GroupVar<G>,
134    Term<G>,
135    Weighted<ScalarVar<G>, G::Scalar>,
136    Weighted<GroupVar<G>, G::Scalar>,
137    Weighted<Term<G>, G::Scalar>
138);
139
140impl<T, F: Field> From<Sum<T>> for Sum<Weighted<T, F>> {
141    fn from(sum: Sum<T>) -> Self {
142        Self(sum.0.into_iter().map(|x| x.into()).collect())
143    }
144}
145
146// Manual implementation for ScalarTerm sum conversion
147impl<G: Group> From<ScalarTerm<G>> for Sum<Weighted<ScalarTerm<G>, G::Scalar>> {
148    fn from(value: ScalarTerm<G>) -> Self {
149        Sum(vec![value.into()])
150    }
151}
152
153impl<G: Group> From<Sum<Weighted<GroupVar<G>, G::Scalar>>> for Sum<Weighted<Term<G>, G::Scalar>> {
154    fn from(sum: Sum<Weighted<GroupVar<G>, G::Scalar>>) -> Self {
155        let sum = sum.0.into_iter().map(|x| x.into()).collect::<Vec<_>>();
156        Self(sum)
157    }
158}