Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ark-relations = { version = "0.5.0", default-features = false }

educe = "0.6.0"
tracing = { version = "^0.1.0", default-features = false, features = ["attributes"] }
itertools = { version = "0.14.0", default-features = false, features = [ "use_alloc" ] }
num-bigint = { version = "0.4", default-features = false }
num-traits = { version = "0.2", default-features = false }
num-integer = { version = "0.1.44", default-features = false }
Expand All @@ -45,7 +46,7 @@ tracing-subscriber = { version = "0.3", default-features = true }

[features]
default = ["std"]
std = ["ark-ff/std", "ark-relations/std", "ark-std/std", "num-bigint/std"]
std = ["ark-ff/std", "ark-relations/std", "ark-std/std", "num-bigint/std", "itertools/use_std" ]
parallel = ["std", "ark-ff/parallel", "ark-std/parallel"]

[[bench]]
Expand Down
40 changes: 39 additions & 1 deletion src/fields/cubic_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ark_ff::{
CubicExtConfig, Zero,
};
use ark_relations::gr1cs::{ConstraintSystemRef, Namespace, SynthesisError};
use core::{borrow::Borrow, marker::PhantomData};
use core::{borrow::Borrow, iter::Sum, marker::PhantomData};
use educe::Educe;

/// This struct is the `R1CS` equivalent of the cubic extension field type
Expand Down Expand Up @@ -576,3 +576,41 @@ where
Ok(Self::new(c0, c1, c2))
}
}

impl<BF, P> Sum<Self> for CubicExtVar<BF, P>
where
BF: FieldVar<P::BaseField, P::BasePrimeField>,
for<'a> &'a BF: FieldOpsBounds<'a, P::BaseField, BF>,
P: CubicExtVarConfig<BF>,
{
#[inline]
#[tracing::instrument(target = "gr1cs", skip(iter))]
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let (c0s, c1s, c2s): (Vec<_>, Vec<_>, Vec<_>) =
itertools::multiunzip(iter.map(|x| (x.c0, x.c1, x.c2)));
let c0 = c0s.into_iter().sum();
let c1 = c1s.into_iter().sum();
let c2 = c2s.into_iter().sum();

Self::new(c0, c1, c2)
}
}

impl<'a, BF, P> Sum<&'a Self> for CubicExtVar<BF, P>
where
BF: FieldVar<P::BaseField, P::BasePrimeField>,
for<'b> &'b BF: FieldOpsBounds<'b, P::BaseField, BF>,
P: CubicExtVarConfig<BF>,
{
#[inline]
#[tracing::instrument(target = "gr1cs", skip(iter))]
fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
let (c0s, c1s, c2s): (Vec<_>, Vec<_>, Vec<_>) =
itertools::multiunzip(iter.map(|x| (&x.c0, &x.c1, &x.c2)));
let c0 = c0s.into_iter().sum();
let c1 = c1s.into_iter().sum();
let c2 = c2s.into_iter().sum();

Self::new(c0, c1, c2)
}
}
16 changes: 16 additions & 0 deletions src/fields/emulated_fp/field_var.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::iter::Sum;

use super::{params::OptimizationType, AllocatedEmulatedFpVar, MulResultVar};
use crate::{
boolean::Boolean,
Expand Down Expand Up @@ -471,3 +473,17 @@ impl<TargetF: PrimeField, BaseF: PrimeField> EmulatedFpVar<TargetF, BaseF> {
}
}
}

impl<TargetF: PrimeField, BaseF: PrimeField> Sum<Self> for EmulatedFpVar<TargetF, BaseF> {
#[tracing::instrument(target = "gr1cs", skip(iter))]
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Self::zero(), |acc, x| acc + x)
}
}

impl<'a, TargetF: PrimeField, BaseF: PrimeField> Sum<&'a Self> for EmulatedFpVar<TargetF, BaseF> {
#[tracing::instrument(target = "gr1cs", skip(iter))]
fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
iter.fold(Self::zero(), |acc, x| acc + x)
}
}
Loading