Skip to content

Commit 1edd959

Browse files
committed
Fix cargo fmt
1 parent 0627041 commit 1edd959

File tree

6 files changed

+160
-52
lines changed

6 files changed

+160
-52
lines changed

ec/src/scalar_mul/variable_base/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use ark_ff::prelude::*;
21
use ark_ff::biginteger::{S128, S64};
2+
use ark_ff::prelude::*;
33
use ark_std::{
44
borrow::Borrow,
55
cfg_chunks, cfg_into_iter, cfg_iter,
@@ -640,9 +640,8 @@ pub fn msm_s64<V: VariableBaseMSM>(
640640
Either::Right(b)
641641
}
642642
});
643-
let (negative_scalars, non_negative_scalars): (Vec<u64>, Vec<u64>) = scalars
644-
.iter()
645-
.partition_map(|s| {
643+
let (negative_scalars, non_negative_scalars): (Vec<u64>, Vec<u64>) =
644+
scalars.iter().partition_map(|s| {
646645
let mag = s.magnitude_as_u64();
647646
if !s.sign() {
648647
Either::Left(mag)
@@ -688,9 +687,8 @@ pub fn msm_s128<V: VariableBaseMSM>(
688687
Either::Right(b)
689688
}
690689
});
691-
let (negative_scalars, non_negative_scalars): (Vec<u128>, Vec<u128>) = scalars
692-
.iter()
693-
.partition_map(|s| {
690+
let (negative_scalars, non_negative_scalars): (Vec<u128>, Vec<u128>) =
691+
scalars.iter().partition_map(|s| {
694692
let mag = s.magnitude_as_u128();
695693
if !s.sign() {
696694
Either::Left(mag)

ff/src/biginteger/i8_or_i96.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::biginteger::{S160, S224};
2-
use core::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
32
use allocative::Allocative;
43
use ark_serialize::{
54
CanonicalDeserialize, CanonicalSerialize, Compress, Read, SerializationError, Valid, Validate,
65
Write,
76
};
7+
use core::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
88

99
/// Compact signed integer optimized for the common `i8` case, widening to a 96-bit
1010
/// split representation when needed (low 64 bits in `large_lo`, next 32 bits in `large_hi`).
@@ -563,8 +563,8 @@ impl Mul<S160> for I8OrI96 {
563563
let mut c2 = c1;
564564
let r2 = mac_with_carry!(0u64, x0, b2, &mut c2);
565565

566-
let r3_low = ((c2 as u128)
567-
+ crate::biginteger::arithmetic::widening_mul(x1, b2)) as u64;
566+
let r3_low =
567+
((c2 as u128) + crate::biginteger::arithmetic::widening_mul(x1, b2)) as u64;
568568
let hi32 = (r3_low & 0xFFFF_FFFF) as u32;
569569
(r0, r1, r2, hi32)
570570
}
@@ -588,8 +588,8 @@ impl Mul<S160> for I8OrI96 {
588588
let mut r2 = mac_with_carry!(0u64, x0, b2, &mut c2);
589589
r2 = mac_with_carry!(r2, x1, b1, &mut c2);
590590

591-
let r3_low = ((c2 as u128)
592-
+ crate::biginteger::arithmetic::widening_mul(x1, b2)) as u64;
591+
let r3_low =
592+
((c2 as u128) + crate::biginteger::arithmetic::widening_mul(x1, b2)) as u64;
593593
let hi32 = (r3_low & 0xFFFF_FFFF) as u32;
594594
(r0, r1, r2, hi32)
595595
}

ff/src/biginteger/signed.rs

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ impl<const N: usize> SignedBigInt<N> {
209209
/// Debug-asserts that M <= N.
210210
#[inline]
211211
pub fn zero_extend_from<const M: usize>(smaller: &SignedBigInt<M>) -> SignedBigInt<N> {
212-
debug_assert!(M <= N, "cannot zero-extend: source has more limbs than destination");
212+
debug_assert!(
213+
M <= N,
214+
"cannot zero-extend: source has more limbs than destination"
215+
);
213216
let widened_mag = BigInt::<N>::zero_extend_from::<M>(&smaller.magnitude);
214217
SignedBigInt::from_bigint(widened_mag, smaller.is_positive)
215218
}
@@ -223,16 +226,25 @@ impl<const N: usize> SignedBigInt<N> {
223226
pub fn add_trunc<const M: usize>(&self, rhs: &SignedBigInt<N>) -> SignedBigInt<M> {
224227
if self.is_positive == rhs.is_positive {
225228
let mag = self.magnitude.add_trunc::<N, M>(&rhs.magnitude);
226-
return SignedBigInt::<M> { magnitude: mag, is_positive: self.is_positive };
229+
return SignedBigInt::<M> {
230+
magnitude: mag,
231+
is_positive: self.is_positive,
232+
};
227233
}
228234
match self.magnitude.cmp(&rhs.magnitude) {
229235
Ordering::Greater | Ordering::Equal => {
230236
let mag = self.magnitude.sub_trunc::<N, M>(&rhs.magnitude);
231-
SignedBigInt::<M> { magnitude: mag, is_positive: self.is_positive }
237+
SignedBigInt::<M> {
238+
magnitude: mag,
239+
is_positive: self.is_positive,
240+
}
232241
},
233242
Ordering::Less => {
234243
let mag = rhs.magnitude.sub_trunc::<N, M>(&self.magnitude);
235-
SignedBigInt::<M> { magnitude: mag, is_positive: rhs.is_positive }
244+
SignedBigInt::<M> {
245+
magnitude: mag,
246+
is_positive: rhs.is_positive,
247+
}
236248
},
237249
}
238250
}
@@ -242,16 +254,25 @@ impl<const N: usize> SignedBigInt<N> {
242254
pub fn sub_trunc<const M: usize>(&self, rhs: &SignedBigInt<N>) -> SignedBigInt<M> {
243255
if self.is_positive != rhs.is_positive {
244256
let mag = self.magnitude.add_trunc::<N, M>(&rhs.magnitude);
245-
return SignedBigInt::<M> { magnitude: mag, is_positive: self.is_positive };
257+
return SignedBigInt::<M> {
258+
magnitude: mag,
259+
is_positive: self.is_positive,
260+
};
246261
}
247262
match self.magnitude.cmp(&rhs.magnitude) {
248263
Ordering::Greater | Ordering::Equal => {
249264
let mag = self.magnitude.sub_trunc::<N, M>(&rhs.magnitude);
250-
SignedBigInt::<M> { magnitude: mag, is_positive: self.is_positive }
265+
SignedBigInt::<M> {
266+
magnitude: mag,
267+
is_positive: self.is_positive,
268+
}
251269
},
252270
Ordering::Less => {
253271
let mag = rhs.magnitude.sub_trunc::<N, M>(&self.magnitude);
254-
SignedBigInt::<M> { magnitude: mag, is_positive: !self.is_positive }
272+
SignedBigInt::<M> {
273+
magnitude: mag,
274+
is_positive: !self.is_positive,
275+
}
255276
},
256277
}
257278
}
@@ -265,16 +286,25 @@ impl<const N: usize> SignedBigInt<N> {
265286
) -> SignedBigInt<P> {
266287
if self.is_positive == rhs.is_positive {
267288
let mag = self.magnitude.add_trunc::<M, P>(&rhs.magnitude);
268-
return SignedBigInt::<P> { magnitude: mag, is_positive: self.is_positive };
289+
return SignedBigInt::<P> {
290+
magnitude: mag,
291+
is_positive: self.is_positive,
292+
};
269293
}
270294
match self.cmp_magnitude_mixed(rhs) {
271295
Ordering::Greater | Ordering::Equal => {
272296
let mag = self.magnitude.sub_trunc::<M, P>(&rhs.magnitude);
273-
SignedBigInt::<P> { magnitude: mag, is_positive: self.is_positive }
297+
SignedBigInt::<P> {
298+
magnitude: mag,
299+
is_positive: self.is_positive,
300+
}
274301
},
275302
Ordering::Less => {
276303
let mag = rhs.magnitude.sub_trunc::<N, P>(&self.magnitude);
277-
SignedBigInt::<P> { magnitude: mag, is_positive: rhs.is_positive }
304+
SignedBigInt::<P> {
305+
magnitude: mag,
306+
is_positive: rhs.is_positive,
307+
}
278308
},
279309
}
280310
}
@@ -373,16 +403,25 @@ impl<const N: usize> SignedBigInt<N> {
373403
) -> SignedBigInt<P> {
374404
if self.is_positive != rhs.is_positive {
375405
let mag = self.magnitude.add_trunc::<M, P>(&rhs.magnitude);
376-
return SignedBigInt::<P> { magnitude: mag, is_positive: self.is_positive };
406+
return SignedBigInt::<P> {
407+
magnitude: mag,
408+
is_positive: self.is_positive,
409+
};
377410
}
378411
match self.cmp_magnitude_mixed(rhs) {
379412
Ordering::Greater | Ordering::Equal => {
380413
let mag = self.magnitude.sub_trunc::<M, P>(&rhs.magnitude);
381-
SignedBigInt::<P> { magnitude: mag, is_positive: self.is_positive }
414+
SignedBigInt::<P> {
415+
magnitude: mag,
416+
is_positive: self.is_positive,
417+
}
382418
},
383419
Ordering::Less => {
384420
let mag = rhs.magnitude.sub_trunc::<N, P>(&self.magnitude);
385-
SignedBigInt::<P> { magnitude: mag, is_positive: !self.is_positive }
421+
SignedBigInt::<P> {
422+
magnitude: mag,
423+
is_positive: !self.is_positive,
424+
}
386425
},
387426
}
388427
}
@@ -696,7 +735,11 @@ impl<const N: usize> core::cmp::Ord for SignedBigInt<N> {
696735
(false, true) => Ordering::Less,
697736
_ => {
698737
let ord = self.magnitude.cmp(&other.magnitude);
699-
if self.is_positive { ord } else { ord.reverse() }
738+
if self.is_positive {
739+
ord
740+
} else {
741+
ord.reverse()
742+
}
700743
},
701744
}
702745
}

ff/src/biginteger/signed_hi_32.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
use crate::biginteger::{BigInt, SignedBigInt, S128, S64};
12
use allocative::Allocative;
2-
use ark_std::cmp::Ordering;
3-
use ark_std::vec::Vec;
4-
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
5-
use crate::biginteger::{BigInt, SignedBigInt, S64, S128};
63
use ark_serialize::{
74
CanonicalDeserialize, CanonicalSerialize, Compress, Read, SerializationError, Valid, Validate,
85
Write,
96
};
7+
use ark_std::cmp::Ordering;
8+
use ark_std::vec::Vec;
9+
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
1010

1111
/// Compact signed big-integer parameterized by limb count `N` (total width = `N*64 + 32` bits).
1212
///
@@ -312,7 +312,10 @@ impl<const N: usize> SignedBigIntHi32<N> {
312312
/// This ignores the sign; pair with `is_positive()` if you need a signed value.
313313
#[inline]
314314
pub fn magnitude_as_bigint_nplus1<const NPLUS1: usize>(&self) -> BigInt<NPLUS1> {
315-
debug_assert!(NPLUS1 == N + 1, "NPLUS1 must be N+1 for SignedBigIntHi32 magnitude pack");
315+
debug_assert!(
316+
NPLUS1 == N + 1,
317+
"NPLUS1 must be N+1 for SignedBigIntHi32 magnitude pack"
318+
);
316319
let mut limbs = [0u64; NPLUS1];
317320
if N > 0 {
318321
limbs[..N].copy_from_slice(&self.magnitude_lo);
@@ -327,7 +330,10 @@ impl<const N: usize> SignedBigIntHi32<N> {
327330
/// Debug-asserts that M <= N.
328331
#[inline]
329332
pub fn zero_extend_from<const M: usize>(smaller: &SignedBigIntHi32<M>) -> SignedBigIntHi32<N> {
330-
debug_assert!(M <= N, "cannot zero-extend: source has more limbs than destination");
333+
debug_assert!(
334+
M <= N,
335+
"cannot zero-extend: source has more limbs than destination"
336+
);
331337
if N == M {
332338
return SignedBigIntHi32::<N>::new(
333339
// copy to avoid borrowing issues
@@ -357,7 +363,10 @@ impl<const N: usize> SignedBigIntHi32<N> {
357363
/// Debug-asserts that NPLUS1 == N + 1.
358364
#[inline]
359365
pub fn to_signed_bigint_nplus1<const NPLUS1: usize>(&self) -> SignedBigInt<NPLUS1> {
360-
debug_assert!(NPLUS1 == N + 1, "to_signed_bigint_nplus1 requires NPLUS1 = N + 1");
366+
debug_assert!(
367+
NPLUS1 == N + 1,
368+
"to_signed_bigint_nplus1 requires NPLUS1 = N + 1"
369+
);
361370
let mut limbs = [0u64; NPLUS1];
362371
if N > 0 {
363372
limbs[..N].copy_from_slice(self.magnitude_lo());
@@ -579,7 +588,11 @@ impl<const N: usize> core::cmp::Ord for SignedBigIntHi32<N> {
579588
(false, true) => Ordering::Less,
580589
_ => {
581590
let ord = self.compare_magnitudes(other);
582-
if self.is_positive { ord } else { ord.reverse() }
591+
if self.is_positive {
592+
ord
593+
} else {
594+
ord.reverse()
595+
}
583596
},
584597
}
585598
}

ff/src/biginteger/tests.rs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ pub mod tests {
117117
limbs[1] = lo[1];
118118
limbs[2] = lo[2];
119119
limbs[3] = hi32;
120-
(num_bigint::BigUint::from(crate::biginteger::BigInt::<4>(limbs)), v.is_positive())
120+
(
121+
num_bigint::BigUint::from(crate::biginteger::BigInt::<4>(limbs)),
122+
v.is_positive(),
123+
)
121124
}
122125

123126
// Case 1: small i8 * b1-only rhs
@@ -774,7 +777,10 @@ pub mod tests {
774777
let b = S::<2>::from_u128(0x0000_0000_0000_0001_0000_0000_0000_0001);
775778
// Add and truncate to 1 limb
776779
// Respect BigInt::add_trunc contract by truncating rhs to 1 limb
777-
let b1 = S::<1>::from_bigint(crate::biginteger::BigInt::<1>::new([b.magnitude.0[0]]), b.is_positive);
780+
let b1 = S::<1>::from_bigint(
781+
crate::biginteger::BigInt::<1>::new([b.magnitude.0[0]]),
782+
b.is_positive,
783+
);
778784
let r1 = a.add_trunc_mixed::<1, 1>(&b1);
779785
// expected low limb wrap of the low words, ignoring carry to limb1
780786
let expected_low = (0xffff_ffff_ffff_fffeu64).wrapping_add(0x0000_0000_0000_0001u64);
@@ -1020,24 +1026,44 @@ pub mod tests {
10201026
let mut b: BigInt<$m> = UniformRand::rand(&mut rng);
10211027

10221028
// Clamp low P limbs to avoid any carry across limb P-1 in add_trunc.
1023-
let mut i = 0; while i < core::cmp::min($p, $n) { a.0[i] >>= 1; i += 1; }
1024-
let mut j = 0; while j < core::cmp::min($p, $m) { b.0[j] >>= 1; j += 1; }
1029+
let mut i = 0;
1030+
while i < core::cmp::min($p, $n) {
1031+
a.0[i] >>= 1;
1032+
i += 1;
1033+
}
1034+
let mut j = 0;
1035+
while j < core::cmp::min($p, $m) {
1036+
b.0[j] >>= 1;
1037+
j += 1;
1038+
}
10251039

10261040
// Build rhs respecting M <= P
10271041
let (res, b_p): (BigInt<$p>, BigInt<$p>) = if $m <= $p {
10281042
let mut b_p = BigInt::<$p>::zero();
1029-
let mut k = 0; while k < $m { b_p.0[k] = b.0[k]; k += 1; }
1043+
let mut k = 0;
1044+
while k < $m {
1045+
b_p.0[k] = b.0[k];
1046+
k += 1;
1047+
}
10301048
(a.add_trunc::<$m, $p>(&b), b_p)
10311049
} else {
10321050
let mut bl = [0u64; $p];
1033-
let mut t = 0; while t < $p { bl[t] = b.0[t]; t += 1; }
1051+
let mut t = 0;
1052+
while t < $p {
1053+
bl[t] = b.0[t];
1054+
t += 1;
1055+
}
10341056
let b_trunc = BigInt::<$p>::new(bl);
10351057
(a.add_trunc::<$p, $p>(&b_trunc), b_trunc)
10361058
};
10371059

10381060
// Expected using low-P truncated operands (after clamping)
10391061
let mut a_p = BigInt::<$p>::zero();
1040-
let mut u = 0; while u < core::cmp::min($p, $n) { a_p.0[u] = a.0[u]; u += 1; }
1062+
let mut u = 0;
1063+
while u < core::cmp::min($p, $n) {
1064+
a_p.0[u] = a.0[u];
1065+
u += 1;
1066+
}
10411067
let a_bu = BigUint::from(a_p);
10421068
let b_bu = BigUint::from(b_p);
10431069
let modulus = BigUint::from(1u8) << (64 * $p);
@@ -1064,7 +1090,10 @@ pub mod tests {
10641090
let mut a: BigInt<4> = UniformRand::rand(&mut rng);
10651091
let mut b: BigInt<4> = UniformRand::rand(&mut rng);
10661092
// Ensure no carry anywhere by masking all limbs to 62 bits
1067-
for i in 0..4 { a.0[i] &= (1u64 << 62) - 1; b.0[i] &= (1u64 << 62) - 1; }
1093+
for i in 0..4 {
1094+
a.0[i] &= (1u64 << 62) - 1;
1095+
b.0[i] &= (1u64 << 62) - 1;
1096+
}
10681097
let r_trunc = a.add_trunc::<4, 4>(&b);
10691098
let mut a2 = a;
10701099
a2.add_assign_trunc::<4>(&b);
@@ -1080,7 +1109,10 @@ pub mod tests {
10801109
for _ in 0..200 {
10811110
let mut a: BigInt<4> = UniformRand::rand(&mut rng);
10821111
let mut b: BigInt<4> = UniformRand::rand(&mut rng);
1083-
for i in 0..4 { a.0[i] &= (1u64 << 62) - 1; b.0[i] &= (1u64 << 62) - 1; }
1112+
for i in 0..4 {
1113+
a.0[i] &= (1u64 << 62) - 1;
1114+
b.0[i] &= (1u64 << 62) - 1;
1115+
}
10841116
// Respect add_trunc contract by pre-truncating rhs to P limbs
10851117
let b3 = crate::biginteger::BigInt::<3>::new([b.0[0], b.0[1], b.0[2]]);
10861118
let r_trunc = a.add_trunc::<3, 3>(&b3);
@@ -1113,7 +1145,10 @@ pub mod tests {
11131145
// Use values that don't overflow beyond N to respect debug contract
11141146
let mut a = BigInt::<4>::new([u64::MAX; 4]);
11151147
let mut b = BigInt::<4>::new([u64::MAX; 4]);
1116-
for i in 0..4 { a.0[i] >>= 1; b.0[i] >>= 1; }
1148+
for i in 0..4 {
1149+
a.0[i] >>= 1;
1150+
b.0[i] >>= 1;
1151+
}
11171152
// P = 4: result should match BigUint addition modulo 2^256
11181153
// add_assign_trunc debug-overflow behavior cannot be reliably asserted in this
11191154
// environment without std; we validate the non-mutating truncated result above.

0 commit comments

Comments
 (0)