Skip to content

Commit 60a0d01

Browse files
authored
Merge pull request #20 from a16z/feat/snark-composition
feat/snark composition
2 parents 5ebdb17 + 0a4504d commit 60a0d01

File tree

39 files changed

+5725
-804
lines changed

39 files changed

+5725
-804
lines changed

bench-templates/src/macros/field.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,16 +405,14 @@ macro_rules! prime_field {
405405
f[i].into_bigint()
406406
})
407407
});
408-
let u64s = (0..SAMPLES)
409-
.map(|_| rng.next_u64())
410-
.collect::<Vec<_>>();
408+
let u64s = (0..SAMPLES).map(|_| rng.next_u64()).collect::<Vec<_>>();
411409
conversions.bench_function("From u64", |b| {
412410
let mut i = 0;
413411
b.iter(|| {
414412
i = (i + 1) % SAMPLES;
415413
<$F>::from_u64(u64s[i])
416414
})
417-
});
415+
});
418416
conversions.finish()
419417
}
420418
};

curves/bn254/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ mod fields;
3939
#[cfg(feature = "curve")]
4040
pub use curves::*;
4141

42+
#[allow(unused_imports)]
4243
pub use fields::*;
4344

4445
#[cfg(feature = "r1cs")]

ec/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ use ark_std::{
2828
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
2929
vec::*,
3030
};
31-
pub use scalar_mul::{
32-
fixed_base::FixedBase,
33-
variable_base::VariableBaseMSM,
34-
ScalarMul,
35-
};
31+
pub use scalar_mul::{fixed_base::FixedBase, variable_base::VariableBaseMSM, ScalarMul};
3632
use zeroize::Zeroize;
3733

3834
pub use ark_ff::AdditiveGroup;

ec/src/pairing.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,14 @@ pub trait Pairing: Sized + 'static + Copy + Debug + Sync + Send + Eq {
102102
a: impl IntoIterator<Item = impl AsRef<Self::G1Prepared>>,
103103
b: impl IntoIterator<Item = impl AsRef<Self::G2Prepared>>,
104104
) -> MillerLoopOutput<Self> {
105-
let a_cloned = a.into_iter().map(|x| x.as_ref().clone()).collect::<Vec<_>>();
106-
let b_cloned = b.into_iter().map(|x| x.as_ref().clone()).collect::<Vec<_>>();
105+
let a_cloned = a
106+
.into_iter()
107+
.map(|x| x.as_ref().clone())
108+
.collect::<Vec<_>>();
109+
let b_cloned = b
110+
.into_iter()
111+
.map(|x| x.as_ref().clone())
112+
.collect::<Vec<_>>();
107113
Self::multi_miller_loop(a_cloned, b_cloned)
108114
}
109115

ec/src/scalar_mul/fixed_base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ impl FixedBase {
9595
.map(|e| Self::windowed_mul::<T>(outerc, window, table, e))
9696
.collect::<Vec<_>>()
9797
}
98-
}
98+
}

ec/src/scalar_mul/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
pub mod glv;
22
pub mod wnaf;
33

4-
pub mod variable_base;
54
pub mod fixed_base;
5+
pub mod variable_base;
66

77
use crate::{
88
short_weierstrass::{Affine, Projective, SWCurveConfig},

ec/src/scalar_mul/variable_base/mod.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use ark_ff::prelude::*;
2+
use ark_ff::biginteger::{S128, S64};
23
use ark_std::{
34
borrow::Borrow,
45
cfg_chunks, cfg_into_iter, cfg_iter,
@@ -626,6 +627,102 @@ pub fn msm_i128<V: VariableBaseMSM>(
626627
}
627628
}
628629

630+
pub fn msm_s64<V: VariableBaseMSM>(
631+
mut bases: &[V::MulBase],
632+
mut scalars: &[S64],
633+
serial: bool,
634+
) -> V {
635+
let (negative_bases, non_negative_bases): (Vec<V::MulBase>, Vec<V::MulBase>) =
636+
bases.iter().enumerate().partition_map(|(i, b)| {
637+
if !scalars[i].sign() {
638+
Either::Left(b)
639+
} else {
640+
Either::Right(b)
641+
}
642+
});
643+
let (negative_scalars, non_negative_scalars): (Vec<u64>, Vec<u64>) = scalars
644+
.iter()
645+
.partition_map(|s| {
646+
let mag = s.magnitude_as_u64();
647+
if !s.sign() {
648+
Either::Left(mag)
649+
} else {
650+
Either::Right(mag)
651+
}
652+
});
653+
if serial {
654+
return msm_serial::<V, _>(&non_negative_bases, &non_negative_scalars)
655+
- msm_serial::<V, _>(&negative_bases, &negative_scalars);
656+
} else {
657+
let chunk_size = match preamble(&mut bases, &mut scalars, serial) {
658+
Some(chunk_size) => chunk_size,
659+
None => return V::zero(),
660+
};
661+
662+
let non_negative_msm: V = cfg_chunks!(non_negative_bases, chunk_size)
663+
.zip(cfg_chunks!(non_negative_scalars, chunk_size))
664+
.map(|(non_negative_bases, non_negative_scalars)| {
665+
msm_serial::<V, _>(non_negative_bases, non_negative_scalars)
666+
})
667+
.sum();
668+
let negative_msm: V = cfg_chunks!(negative_bases, chunk_size)
669+
.zip(cfg_chunks!(negative_scalars, chunk_size))
670+
.map(|(negative_bases, negative_scalars)| {
671+
msm_serial::<V, _>(negative_bases, negative_scalars)
672+
})
673+
.sum();
674+
non_negative_msm - negative_msm
675+
}
676+
}
677+
678+
pub fn msm_s128<V: VariableBaseMSM>(
679+
mut bases: &[V::MulBase],
680+
mut scalars: &[S128],
681+
serial: bool,
682+
) -> V {
683+
let (negative_bases, non_negative_bases): (Vec<V::MulBase>, Vec<V::MulBase>) =
684+
bases.iter().enumerate().partition_map(|(i, b)| {
685+
if !scalars[i].sign() {
686+
Either::Left(b)
687+
} else {
688+
Either::Right(b)
689+
}
690+
});
691+
let (negative_scalars, non_negative_scalars): (Vec<u128>, Vec<u128>) = scalars
692+
.iter()
693+
.partition_map(|s| {
694+
let mag = s.magnitude_as_u128();
695+
if !s.sign() {
696+
Either::Left(mag)
697+
} else {
698+
Either::Right(mag)
699+
}
700+
});
701+
if serial {
702+
return msm_serial::<V, _>(&non_negative_bases, &non_negative_scalars)
703+
- msm_serial::<V, _>(&negative_bases, &negative_scalars);
704+
} else {
705+
let chunk_size = match preamble(&mut bases, &mut scalars, serial) {
706+
Some(chunk_size) => chunk_size,
707+
None => return V::zero(),
708+
};
709+
710+
let non_negative_msm: V = cfg_chunks!(non_negative_bases, chunk_size)
711+
.zip(cfg_chunks!(non_negative_scalars, chunk_size))
712+
.map(|(non_negative_bases, non_negative_scalars)| {
713+
msm_serial::<V, _>(non_negative_bases, non_negative_scalars)
714+
})
715+
.sum();
716+
let negative_msm: V = cfg_chunks!(negative_bases, chunk_size)
717+
.zip(cfg_chunks!(negative_scalars, chunk_size))
718+
.map(|(negative_bases, negative_scalars)| {
719+
msm_serial::<V, _>(negative_bases, negative_scalars)
720+
})
721+
.sum();
722+
non_negative_msm - negative_msm
723+
}
724+
}
725+
629726
pub fn msm_u128<V: VariableBaseMSM>(
630727
mut bases: &[V::MulBase],
631728
mut scalars: &[u128],

ff-asm/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub fn x86_64_asm_mul(input: TokenStream) -> TokenStream {
5959
} else {
6060
panic!("The number of limbs must be a literal");
6161
};
62+
#[allow(clippy::redundant_comparisons)]
6263
if num_limbs <= 6 && num_limbs <= 3 * MAX_REGS {
6364
let impl_block = generate_impl(num_limbs, true);
6465

@@ -110,6 +111,7 @@ pub fn x86_64_asm_square(input: TokenStream) -> TokenStream {
110111
} else {
111112
panic!("The number of limbs must be a literal");
112113
};
114+
#[allow(clippy::redundant_comparisons)]
113115
if num_limbs <= 6 && num_limbs <= 3 * MAX_REGS {
114116
let impl_block = generate_impl(num_limbs, false);
115117

ff/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ zeroize = { workspace = true, features = ["zeroize_derive"] }
2929
num-bigint.workspace = true
3030
digest = { workspace = true, features = ["alloc"] }
3131
itertools.workspace = true
32-
allocative = { version = "0.3.4", optional = true }
32+
allocative = "0.3.4"
3333

3434
[dev-dependencies]
3535
ark-test-curves = { workspace = true, features = [
@@ -53,4 +53,3 @@ default = []
5353
std = ["ark-std/std", "ark-serialize/std", "itertools/use_std"]
5454
parallel = ["std", "rayon", "ark-std/parallel", "ark-serialize/parallel"]
5555
asm = []
56-
allocative = ["dep:allocative"]

ff/src/biginteger/arithmetic.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,37 @@ pub fn mac_discard(a: u64, b: u64, c: u64, carry: &mut u64) {
123123
*carry = (tmp >> 64) as u64;
124124
}
125125

126+
/// Accumulate `limbs` into an N-limb accumulator starting at `lane_offset` (64-bit lanes),
127+
/// returning the final carry. This is a helper for building wide accumulators.
128+
#[inline(always)]
129+
pub fn add_limbs_shifted_inplace<const N: usize>(
130+
acc: &mut [u64; N],
131+
limbs: &[u64],
132+
lane_offset: usize,
133+
) -> u64 {
134+
let mut carry = 0u64;
135+
let mut i = 0usize;
136+
while i < limbs.len() {
137+
let idx = lane_offset + i;
138+
if idx >= N {
139+
break;
140+
}
141+
let tmp = (acc[idx] as u128) + (limbs[i] as u128) + (carry as u128);
142+
acc[idx] = tmp as u64;
143+
carry = (tmp >> 64) as u64;
144+
i += 1;
145+
}
146+
// propagate carry across remaining lanes if any
147+
let mut idx = lane_offset + i;
148+
while carry != 0 && idx < N {
149+
let tmp = (acc[idx] as u128) + (carry as u128);
150+
acc[idx] = tmp as u64;
151+
carry = (tmp >> 64) as u64;
152+
idx += 1;
153+
}
154+
carry
155+
}
156+
126157
macro_rules! mac_with_carry {
127158
($a:expr, $b:expr, $c:expr, &mut $carry:expr$(,)?) => {{
128159
let tmp =

0 commit comments

Comments
 (0)