sigma_rs/linear_relation/
convert.rs1use ff::Field;
2use group::Group;
3
4use super::{GroupVar, ScalarTerm, ScalarVar, Sum, Term, Weighted};
5
6impl<G> From<ScalarVar<G>> for ScalarTerm<G> {
7 fn from(value: ScalarVar<G>) -> Self {
8 Self::Var(value)
9 }
10}
11
12impl<G: Group> From<ScalarVar<G>> for Weighted<ScalarTerm<G>, G::Scalar> {
13 fn from(value: ScalarVar<G>) -> Self {
14 ScalarTerm::from(value).into()
15 }
16}
17
18impl<G: Group> From<Weighted<ScalarVar<G>, G::Scalar>> for Weighted<ScalarTerm<G>, G::Scalar> {
19 fn from(value: Weighted<ScalarVar<G>, G::Scalar>) -> Self {
20 Self {
21 term: value.term.into(),
22 weight: value.weight,
23 }
24 }
25}
26
27impl<T: Field + Into<G::Scalar>, G: Group> From<T> for Weighted<ScalarTerm<G>, G::Scalar> {
29 fn from(value: T) -> Self {
30 Self {
31 term: ScalarTerm::Unit,
32 weight: value.into(),
33 }
34 }
35}
36
37impl<G> From<(ScalarVar<G>, GroupVar<G>)> for Term<G> {
38 fn from((scalar, elem): (ScalarVar<G>, GroupVar<G>)) -> Self {
39 Self {
40 scalar: scalar.into(),
41 elem,
42 }
43 }
44}
45
46impl<G> From<(ScalarTerm<G>, GroupVar<G>)> for Term<G> {
47 fn from((scalar, elem): (ScalarTerm<G>, GroupVar<G>)) -> Self {
48 Self { scalar, elem }
49 }
50}
51
52impl<G> From<GroupVar<G>> for Term<G> {
53 fn from(value: GroupVar<G>) -> Self {
54 Term {
55 scalar: ScalarTerm::Unit,
56 elem: value,
57 }
58 }
59}
60
61impl<G: Group> From<(ScalarVar<G>, GroupVar<G>)> for Weighted<Term<G>, G::Scalar> {
62 fn from(pair: (ScalarVar<G>, GroupVar<G>)) -> Self {
63 Term::from(pair).into()
64 }
65}
66
67impl<G: Group> From<(ScalarTerm<G>, GroupVar<G>)> for Weighted<Term<G>, G::Scalar> {
68 fn from(pair: (ScalarTerm<G>, GroupVar<G>)) -> Self {
69 Term::from(pair).into()
70 }
71}
72
73impl<G: Group> From<GroupVar<G>> for Weighted<Term<G>, G::Scalar> {
74 fn from(value: GroupVar<G>) -> Self {
75 Term::from(value).into()
76 }
77}
78
79impl<G: Group> From<Weighted<GroupVar<G>, G::Scalar>> for Weighted<Term<G>, G::Scalar> {
80 fn from(value: Weighted<GroupVar<G>, G::Scalar>) -> Self {
81 Weighted {
82 term: value.term.into(),
83 weight: value.weight,
84 }
85 }
86}
87
88impl<T, F: Field> From<T> for Weighted<T, F> {
89 fn from(term: T) -> Self {
90 Self {
91 term,
92 weight: F::ONE,
93 }
94 }
95}
96
97macro_rules! impl_from_for_sum {
100 ($($type:ty),+) => {
101 $(
102 impl<G: Group, T: Into<$type>> From<T> for Sum<$type> {
103 fn from(value: T) -> Self {
104 Sum(vec![value.into()])
105 }
106 }
107
108 impl<G: Group, T: Into<$type>> From<Vec<T>> for Sum<$type> {
109 fn from(terms: Vec<T>) -> Self {
110 Self::from_iter(terms)
111 }
112 }
113
114 impl<G: Group, T: Into<$type>, const N: usize> From<[T; N]> for Sum<$type> {
115 fn from(terms: [T; N]) -> Self {
116 Self::from_iter(terms)
117 }
118 }
119
120 impl<G: Group, T: Into<$type>> FromIterator<T> for Sum<$type> {
121 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
122 Self(iter.into_iter().map(|x| x.into()).collect())
123 }
124 }
125 )+
126 };
127}
128
129impl_from_for_sum!(
130 ScalarVar<G>,
131 GroupVar<G>,
132 Term<G>,
133 Weighted<ScalarVar<G>, G::Scalar>,
134 Weighted<GroupVar<G>, G::Scalar>,
135 Weighted<Term<G>, G::Scalar>
136);
137
138impl<T, F: Field> From<Sum<T>> for Sum<Weighted<T, F>> {
139 fn from(sum: Sum<T>) -> Self {
140 Self(sum.0.into_iter().map(|x| x.into()).collect())
141 }
142}