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
162
163
164
165
166
167
use ark_ff::Field;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{
cmp::Ordering,
fmt::{Debug, Error, Formatter},
hash::Hash,
ops::Deref,
vec::Vec,
};
#[cfg(feature = "parallel")]
use rayon::prelude::*;
mod sparse;
pub use sparse::SparsePolynomial;
pub trait Term:
Clone
+ PartialOrd
+ Ord
+ PartialEq
+ Eq
+ Hash
+ Default
+ Debug
+ Deref<Target = [(usize, usize)]>
+ Send
+ Sync
+ CanonicalSerialize
+ CanonicalDeserialize
{
fn new(term: Vec<(usize, usize)>) -> Self;
fn degree(&self) -> usize;
fn vars(&self) -> Vec<usize>;
fn powers(&self) -> Vec<usize>;
fn is_constant(&self) -> bool;
fn evaluate<F: Field>(&self, p: &[F]) -> F;
}
#[derive(Clone, PartialEq, Eq, Hash, Default, CanonicalSerialize, CanonicalDeserialize)]
pub struct SparseTerm(Vec<(usize, usize)>);
impl SparseTerm {
fn combine(term: &[(usize, usize)]) -> Vec<(usize, usize)> {
let mut term_dedup: Vec<(usize, usize)> = Vec::new();
for (var, pow) in term {
if let Some(prev) = term_dedup.last_mut() {
if prev.0 == *var {
prev.1 += pow;
continue;
}
}
term_dedup.push((*var, *pow));
}
term_dedup
}
}
impl Term for SparseTerm {
fn new(mut term: Vec<(usize, usize)>) -> Self {
term.retain(|(_, pow)| *pow != 0);
if term.len() > 1 {
term.sort_by(|(v1, _), (v2, _)| v1.cmp(v2));
term = Self::combine(&term);
}
Self(term)
}
fn degree(&self) -> usize {
self.iter().fold(0, |sum, acc| sum + acc.1)
}
fn vars(&self) -> Vec<usize> {
self.iter().map(|(v, _)| *v).collect()
}
fn powers(&self) -> Vec<usize> {
self.iter().map(|(_, p)| *p).collect()
}
fn is_constant(&self) -> bool {
self.len() == 0 || self.degree() == 0
}
fn evaluate<F: Field>(&self, point: &[F]) -> F {
cfg_into_iter!(self)
.map(|(var, power)| point[*var].pow([*power as u64]))
.product()
}
}
impl Debug for SparseTerm {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
for variable in self.iter() {
if variable.1 == 1 {
write!(f, " * x_{}", variable.0)?;
} else {
write!(f, " * x_{}^{}", variable.0, variable.1)?;
}
}
Ok(())
}
}
impl Deref for SparseTerm {
type Target = [(usize, usize)];
fn deref(&self) -> &[(usize, usize)] {
&self.0
}
}
impl PartialOrd for SparseTerm {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.degree() != other.degree() {
Some(self.degree().cmp(&other.degree()))
} else {
for (cur, other) in self.iter().zip(other.iter()) {
if other.0 == cur.0 {
if cur.1 != other.1 {
return Some((cur.1).cmp(&other.1));
}
} else {
return Some((other.0).cmp(&cur.0));
}
}
Some(Ordering::Equal)
}
}
}
impl Ord for SparseTerm {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
}
}