Skip to content

Commit 1ecd4ff

Browse files
nategrafmmaker
andauthored
Introduce ScalarTerm struct to support Add<Scalar> for ScalarVar and Add<GroupVar> for Term (#53)
Co-authored-by: Michele Orrù <[email protected]>
1 parent b87c00e commit 1ecd4ff

File tree

5 files changed

+552
-161
lines changed

5 files changed

+552
-161
lines changed

src/linear_relation/convert.rs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
use ff::Field;
2+
use group::Group;
3+
4+
use super::{GroupVar, ScalarTerm, ScalarVar, Sum, Term, Weighted};
5+
6+
impl<G> From<ScalarVar<G>> for ScalarTerm<G> {
7+
fn from(value: ScalarVar<G>) -> Self {
8+
Self::Var(value)
9+
}
10+
}
11+
12+
impl<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+
18+
impl<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+
27+
// NOTE: Rust does not accept an impl over From<G::Scalar>
28+
impl<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+
37+
impl<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+
46+
impl<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+
52+
impl<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+
61+
impl<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+
67+
impl<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+
73+
impl<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+
79+
impl<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+
88+
impl<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+
97+
// NOTE: This is implemented directly for each of the key types to avoid collision with the blanket
98+
// Into impl provided by the standard library.
99+
macro_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+
129+
impl_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+
138+
impl<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+
}

0 commit comments

Comments
 (0)