Skip to content

Commit 7951133

Browse files
Implement the Sum trait for all AffineCurve (arkworks-rs#289)
* impl Sum for all AffineCurve * update the CHANGELOG * lint * lint * lint! * lint again * avoid linter battle; no more killing Co-authored-by: Dev Ojha <[email protected]>
1 parent c30fd50 commit 7951133

File tree

11 files changed

+81
-45
lines changed

11 files changed

+81
-45
lines changed

.github/.markdownlint.yml

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ MD025: false
1111
MD033: false
1212
# MD036 no-emphasis-as-heading
1313
MD036: false
14+
MD041: false

.github/workflows/mdlinter.yml

+5
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ jobs:
2828
MARKDOWN_CONFIG_FILE: .markdownlint.yml
2929
VALIDATE_PROTOBUF: false
3030
VALIDATE_JSCPD: false
31+
# use Python Pylint as the only linter to avoid conflicts
32+
VALIDATE_PYTHON_BLACK: false
33+
VALIDATE_PYTHON_FLAKE8: false
34+
VALIDATE_PYTHON_ISORT: false
35+
VALIDATE_PYTHON_MYPY: false

.hooks/pre-commit

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env bash
22

3-
rustfmt --version &>/dev/null
4-
if [ $? != 0 ]; then
3+
if rustfmt --version &>/dev/null; then
54
printf "[pre_commit] \033[0;31merror\033[0m: \"rustfmt\" not available. \n"
65
printf "[pre_commit] \033[0;31merror\033[0m: rustfmt can be installed via - \n"
76
printf "[pre_commit] $ rustup component add rustfmt \n"
@@ -12,10 +11,9 @@ problem_files=()
1211

1312
# collect ill-formatted files
1413
for file in $(git diff --name-only --cached); do
15-
if [ ${file: -3} == ".rs" ]; then
16-
rustfmt +stable --check $file &>/dev/null
17-
if [ $? != 0 ]; then
18-
problem_files+=($file)
14+
if [ "${file: -3}" == ".rs" ]; then
15+
if rustfmt +stable --check "$file" &>/dev/null; then
16+
problem_files+=("$file")
1917
fi
2018
fi
2119
done
@@ -26,10 +24,10 @@ if [ ${#problem_files[@]} == 0 ]; then
2624
else
2725
# reformat the files that need it and re-stage them.
2826
printf "[pre_commit] the following files were rustfmt'd before commit: \n"
29-
for file in ${problem_files[@]}; do
30-
rustfmt +stable $file
31-
git add $file
32-
printf "\033[0;32m $file\033[0m \n"
27+
for file in "${problem_files[@]}"; do
28+
rustfmt +stable "$file"
29+
git add "$file"
30+
printf "\033[0;32m %s\033[0m \n" "$file"
3331
done
3432
fi
3533

CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
- [\#261](https://github.com/arkworks-rs/algebra/pull/261) (ark-ff) Add support for 448-bit integers and fields.
1414
- [\#263](https://github.com/arkworks-rs/algebra/pull/263) (ark-ff) Add `From<iXXX>` implementations to fields.
1515
- [\#265](https://github.com/arkworks-rs/algebra/pull/265) (ark-serialize) Add hashing as an extension trait of `CanonicalSerialize`.
16-
- [\#280](https://github.com/arkworks-rs/algebra/pull/280) (ark-ff) Add `Into<BigUint>` and `From<BigUint>` implementations to `BigInteger` and `PrimeField`.
1716
- [\#279](https://github.com/arkworks-rs/algebra/pull/279) (ark-ec) Parallelize miller loop operations for BLS12.
17+
- [\#280](https://github.com/arkworks-rs/algebra/pull/280) (ark-ff) Add `Into<BigUint>` and `From<BigUint>` implementations to `BigInteger` and `PrimeField`.
18+
- [\#289](https://github.com/arkworks-rs/algebra/pull/289) (ark-ec) Add `Sum` implementation for all `AffineCurve`.
1819

1920
### Improvements
21+
2022
- [\#279](https://github.com/arkworks-rs/algebra/pull/279) (ark-ec) Parallelize miller loop operations for BLS12.
2123

2224
### Bug fixes
23-
- [\#252](https://github.com/arkworks-rs/algebra/pull/252) (ark-ff) Fix prime field sampling when `REPR_SHIFT_BITS` is 64.
2425

2526
- [\#252](https://github.com/arkworks-rs/algebra/pull/252) (ark-ff) Fix prime field sampling when `REPR_SHIFT_BITS` is 64.
2627
- [\#284](https://github.com/arkworks-rs/algebra/pull/284) (ark-poly-benches) Fix the panic `subgroup_fft_in_place` benchmark for MNT6-753's Fr.

ec/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ pub trait AffineCurve:
239239
+ Zero
240240
+ Neg<Output = Self>
241241
+ Zeroize
242+
+ core::iter::Sum<Self>
243+
+ for<'a> core::iter::Sum<&'a Self>
242244
+ From<<Self as AffineCurve>::Projective>
243245
{
244246
const COFACTOR: &'static [u64];

ec/src/models/short_weierstrass_jacobian.rs

+16
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,22 @@ impl<P: Parameters> Default for GroupAffine<P> {
278278
}
279279
}
280280

281+
impl<P: Parameters> core::iter::Sum<Self> for GroupAffine<P> {
282+
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
283+
iter.map(|x| x.into_projective())
284+
.sum::<GroupProjective<P>>()
285+
.into()
286+
}
287+
}
288+
289+
impl<'a, P: Parameters> core::iter::Sum<&'a Self> for GroupAffine<P> {
290+
fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
291+
iter.map(|x| x.into_projective())
292+
.sum::<GroupProjective<P>>()
293+
.into()
294+
}
295+
}
296+
281297
/// Jacobian coordinates for a point on an elliptic curve in short Weierstrass form,
282298
/// over the base field `P::BaseField`. This struct implements arithmetic
283299
/// via the Jacobian formulae

poly/src/polynomial/multivariate/mod.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ pub trait Term:
5353

5454
/// Stores a term (monomial) in a multivariate polynomial.
5555
/// Each element is of the form `(variable, power)`.
56-
#[derive(
57-
Clone, PartialOrd, PartialEq, Eq, Hash, Default, CanonicalSerialize, CanonicalDeserialize,
58-
)]
56+
#[derive(Clone, PartialEq, Eq, Hash, Default, CanonicalSerialize, CanonicalDeserialize)]
5957
pub struct SparseTerm(Vec<(usize, usize)>);
6058

6159
impl SparseTerm {
@@ -141,26 +139,32 @@ impl Deref for SparseTerm {
141139
}
142140
}
143141

144-
impl Ord for SparseTerm {
142+
impl PartialOrd for SparseTerm {
145143
/// Sort by total degree. If total degree is equal then ordering
146144
/// is given by exponent weight in lower-numbered variables
147145
/// ie. `x_1 > x_2`, `x_1^2 > x_1 * x_2`, etc.
148-
fn cmp(&self, other: &Self) -> Ordering {
146+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
149147
if self.degree() != other.degree() {
150-
self.degree().cmp(&other.degree())
148+
Some(self.degree().cmp(&other.degree()))
151149
} else {
152150
// Iterate through all variables and return the corresponding ordering
153151
// if they differ in variable numbering or power
154152
for (cur, other) in self.iter().zip(other.iter()) {
155153
if other.0 == cur.0 {
156154
if cur.1 != other.1 {
157-
return (cur.1).cmp(&other.1);
155+
return Some((cur.1).cmp(&other.1));
158156
}
159157
} else {
160-
return (other.0).cmp(&cur.0);
158+
return Some((other.0).cmp(&cur.0));
161159
}
162160
}
163-
Ordering::Equal
161+
Some(Ordering::Equal)
164162
}
165163
}
166164
}
165+
166+
impl Ord for SparseTerm {
167+
fn cmp(&self, other: &Self) -> Ordering {
168+
self.partial_cmp(other).unwrap()
169+
}
170+
}

scripts/linkify_changelog.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import re
2-
import sys
31
import fileinput
42
import os
3+
import re
4+
import sys
55

66
# Set this to the name of the repo, if you don't want it to be read from the filesystem.
77
# It assumes the changelog file is in the root of the repo.
@@ -11,7 +11,7 @@
1111
# with the valid mark down formatted link to it. e.g.
1212
# " [\#number](https://github.com/arkworks-rs/template/pull/<number>)
1313
# Note that if the number is for a an issue, github will auto-redirect you when you click the link.
14-
# It is safe to run the script multiple times in succession.
14+
# It is safe to run the script multiple times in succession.
1515
#
1616
# Example usage $ python3 linkify_changelog.py ../CHANGELOG.md
1717
changelog_path = sys.argv[1]
@@ -21,6 +21,10 @@
2121
repo_name = components[-2]
2222

2323
for line in fileinput.input(inplace=True):
24-
line = re.sub(r"\- #([0-9]*)", r"- [\\#\1](https://github.com/arkworks-rs/" + repo_name + r"/pull/\1)", line.rstrip())
24+
line = re.sub(
25+
r"\- #([0-9]*)",
26+
r"- [\#\1](https://github.com/arkworks-rs/" + repo_name + r"/pull/\1)",
27+
line.rstrip(),
28+
)
2529
# edits the current file
26-
print(line)
30+
print(line)

scripts/test_vectors.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
1-
21
def generate_from_bytes_mod_order_test_vector(modulus):
32
def gen_vector(number):
43
byte_arr = convert_int_to_byte_vec(number)
54
# s = str(number % modulus)
65
# return "(" + byte_arr + ", \"" + s + "\"),"
76
return byte_arr + ","
7+
88
data = ["vec!["]
9-
10-
small_values_to_test = [0, 1, 255, 256, 256*256 + 255]
9+
10+
small_values_to_test = [0, 1, 255, 256, 256 * 256 + 255]
1111
modulus_bits = int((len(bin(modulus)[2:]) + 7) / 8) * 8
1212
values_to_test = small_values_to_test + [
13-
modulus >> 8,
14-
(modulus >> 8) + 1,
13+
modulus >> 8,
14+
(modulus >> 8) + 1,
1515
modulus - 1,
1616
modulus,
1717
modulus + 1,
1818
modulus * 2,
1919
modulus * 256,
2020
17 + (1 << modulus_bits),
2121
19 + (1 << modulus_bits) + modulus,
22-
81 + (1 << modulus_bits) * 256 + modulus]
23-
22+
81 + (1 << modulus_bits) * 256 + modulus,
23+
]
24+
2425
for i in values_to_test:
2526
data += ["// " + str(i)]
2627
data += [gen_vector(i)]
27-
28+
2829
data += ["];"]
29-
return '\n'.join(data)
30+
return "\n".join(data)
31+
3032

3133
def convert_int_to_byte_vec(number):
3234
s = bin(number)[2:]
3335
num_bytes = int((len(s) + 7) / 8)
3436
s = s.zfill(num_bytes * 8)
35-
37+
3638
byte_arr = []
3739
for i in range(num_bytes):
38-
byte = s[i*8: (i+1)*8]
40+
byte = s[i * 8 : (i + 1) * 8]
3941
i = int(byte, 2)
4042
byte_arr += [str(i) + "u8"]
41-
42-
data = ', '.join(byte_arr)
43+
44+
data = ", ".join(byte_arr)
4345
return "vec![" + data + "]"
4446

45-
bls12_fr_mod = 52435875175126190479447740508185965837690552500527637822603658699938581184513
47+
48+
bls12_fr_mod = (
49+
52435875175126190479447740508185965837690552500527637822603658699938581184513
50+
)
4651
print(generate_from_bytes_mod_order_test_vector(bls12_fr_mod))

test-templates/src/fields.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(unused)]
2+
#![allow(clippy::eq_op)]
23
use ark_ff::fields::{FftField, FftParameters, Field, LegendreSymbol, PrimeField, SquareRootField};
34
use ark_serialize::{buffer_bit_byte_size, Flags, SWFlags};
45
use ark_std::io::Cursor;
@@ -313,11 +314,9 @@ pub fn fft_field_test<F: FftField>() {
313314
if let Some(small_subgroup_base) = F::FftParams::SMALL_SUBGROUP_BASE {
314315
let small_subgroup_base_adicity = F::FftParams::SMALL_SUBGROUP_BASE_ADICITY.unwrap();
315316
let large_subgroup_root_of_unity = F::large_subgroup_root_of_unity().unwrap();
316-
assert_eq!(
317-
large_subgroup_root_of_unity.pow([(1 << F::FftParams::TWO_ADICITY)
318-
* (small_subgroup_base as u64).pow(small_subgroup_base_adicity)]),
319-
F::one()
320-
);
317+
let pow = (1 << F::FftParams::TWO_ADICITY)
318+
* (small_subgroup_base as u64).pow(small_subgroup_base_adicity);
319+
assert_eq!(large_subgroup_root_of_unity.pow([pow]), F::one());
321320

322321
for i in 0..F::FftParams::TWO_ADICITY {
323322
for j in 0..small_subgroup_base_adicity {

test-templates/src/groups.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(unused)]
2+
#![allow(clippy::eq_op)]
23
use ark_ec::group::Group;
34
use ark_ff::{One, UniformRand, Zero};
45

0 commit comments

Comments
 (0)