Skip to content

Commit

Permalink
Relax Arc cfg; remove Send bound from Valid; implement Valid
Browse files Browse the repository at this point in the history
…& `CanonicalDeserialize` for `Rc` (#811)

* remove `Send` bound from `Valid`

* impl Valid & CanonicalDeserialize for Rc

* reflect the relaxed cfg for using Arc

* fixup! reflect the relaxed cfg for using Arc

* add a test for de/ser Rc & Arc

* add changelog entries

* need to patch ark-std crate
  • Loading branch information
mmagician committed Apr 6, 2024
1 parent 1bc20ce commit a9b2dea
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 38 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [\#748](https://github.com/arkworks-rs/algebra/pull/748) (`ark-ff`) Add `FromStr` for `BigInteger`.
- [\#756](https://github.com/arkworks-rs/algebra/pull/756) (`ark-ec`) Require `Neg`, `Sub`, `SubAssign` ops on `AffineRepr`.
- [\#767](https://github.com/arkworks-rs/algebra/pull/767) (`ark-curve25519`) Change (negate) generator of curve25519 for inter-operability with curve25519-dalek.
- [\#811](https://github.com/arkworks-rs/algebra/pull/811) (`ark-serialize`) Remove `Send` trait bound from `Valid`.

### Features

Expand All @@ -30,6 +31,7 @@
- [\#691](https://github.com/arkworks-rs/algebra/pull/691) (`ark-poly`) Implement `Polynomial` for `SparseMultilinearExtension` and `DenseMultilinearExtension`.
- [\#693](https://github.com/arkworks-rs/algebra/pull/693) (`ark-serialize`) Add `serialize_to_vec!` convenience macro.
- [\#713](https://github.com/arkworks-rs/algebra/pull/713) (`ark-ff`) Add support for bitwise operations AND, OR, and XOR between `BigInteger`.
- [\#811](https://github.com/arkworks-rs/algebra/pull/811) (`ark-serialize`) Implement `Valid` & `CanonicalDeserialize` for `Rc`.

### Improvements

Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,6 @@ lto = "thin"
incremental = true
debug-assertions = true
debug = true

[patch.crates-io]
ark-std = { git = "https://github.com/arkworks-rs/std/" }
4 changes: 1 addition & 3 deletions ec/src/models/short_weierstrass/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,7 @@ impl<P: SWCurveConfig> Valid for Projective<P> {
self.into_affine().check()
}

fn batch_check<'a>(
batch: impl Iterator<Item = &'a Self> + Send,
) -> Result<(), SerializationError>
fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
Expand Down
4 changes: 1 addition & 3 deletions ec/src/models/twisted_edwards/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,7 @@ impl<P: TECurveConfig> Valid for Projective<P> {
self.into_affine().check()
}

fn batch_check<'a>(
batch: impl Iterator<Item = &'a Self> + Send,
) -> Result<(), SerializationError>
fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
Expand Down
2 changes: 1 addition & 1 deletion serialize-derive/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn impl_valid(ast: &syn::DeriveInput) -> TokenStream {
Ok(())
}
#[allow(unused_mut, unused_variables)]
fn batch_check<'a>(batch: impl Iterator<Item = &'a Self> + Send) -> Result<(), ark_serialize::SerializationError>
fn batch_check<'a>(batch: impl Iterator<Item = &'a Self> ) -> Result<(), ark_serialize::SerializationError>
where
Self: 'a
{
Expand Down
66 changes: 39 additions & 27 deletions serialize/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ impl<T: Valid> Valid for Option<T> {
}

#[inline]
fn batch_check<'a>(
batch: impl Iterator<Item = &'a Self> + Send,
) -> Result<(), SerializationError>
fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
Expand Down Expand Up @@ -306,7 +304,35 @@ impl<T: CanonicalSerialize + ToOwned> CanonicalSerialize for Rc<T> {
}
}

#[cfg(feature = "std")]
impl<T: Valid + Sync> Valid for Rc<T> {
#[inline]
fn check(&self) -> Result<(), SerializationError> {
self.as_ref().check()
}

#[inline]
fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
T::batch_check(batch.map(|v| v.as_ref()))
}
}

impl<T: CanonicalDeserialize + Sync> CanonicalDeserialize for Rc<T> {
#[inline]
fn deserialize_with_mode<R: Read>(
reader: R,
compress: Compress,
validate: Validate,
) -> Result<Self, SerializationError> {
Ok(Rc::new(T::deserialize_with_mode(
reader, compress, validate,
)?))
}
}

#[cfg(target_has_atomic = "ptr")]
impl<T: CanonicalSerialize + ToOwned> CanonicalSerialize for ark_std::sync::Arc<T> {
#[inline]
fn serialize_with_mode<W: Write>(
Expand All @@ -323,7 +349,7 @@ impl<T: CanonicalSerialize + ToOwned> CanonicalSerialize for ark_std::sync::Arc<
}
}

#[cfg(feature = "std")]
#[cfg(target_has_atomic = "ptr")]
impl<T: Valid + Sync + Send> Valid for ark_std::sync::Arc<T> {
#[inline]
fn check(&self) -> Result<(), SerializationError> {
Expand All @@ -332,17 +358,15 @@ impl<T: Valid + Sync + Send> Valid for ark_std::sync::Arc<T> {

#[inline]

fn batch_check<'a>(
batch: impl Iterator<Item = &'a Self> + Send,
) -> Result<(), SerializationError>
fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
T::batch_check(batch.map(|v| v.as_ref()))
}
}

#[cfg(feature = "std")]
#[cfg(target_has_atomic = "ptr")]
impl<T: CanonicalDeserialize + ToOwned + Sync + Send> CanonicalDeserialize
for ark_std::sync::Arc<T>
{
Expand Down Expand Up @@ -386,9 +410,7 @@ where

#[inline]

fn batch_check<'a>(
batch: impl Iterator<Item = &'a Self> + Send,
) -> Result<(), SerializationError>
fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
Expand Down Expand Up @@ -441,9 +463,7 @@ impl<T: CanonicalDeserialize, const N: usize> Valid for [T; N] {
}

#[inline]
fn batch_check<'a>(
batch: impl Iterator<Item = &'a Self> + Send,
) -> Result<(), SerializationError>
fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
Expand Down Expand Up @@ -491,9 +511,7 @@ impl<T: Valid> Valid for Vec<T> {
}

#[inline]
fn batch_check<'a>(
batch: impl Iterator<Item = &'a Self> + Send,
) -> Result<(), SerializationError>
fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
Expand Down Expand Up @@ -587,9 +605,7 @@ impl<T: Valid> Valid for VecDeque<T> {
}

#[inline]
fn batch_check<'a>(
batch: impl Iterator<Item = &'a Self> + Send,
) -> Result<(), SerializationError>
fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
Expand Down Expand Up @@ -648,9 +664,7 @@ impl<T: Valid> Valid for LinkedList<T> {
}

#[inline]
fn batch_check<'a>(
batch: impl Iterator<Item = &'a Self> + Send,
) -> Result<(), SerializationError>
fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
Expand Down Expand Up @@ -904,9 +918,7 @@ impl<V: Valid> Valid for BTreeSet<V> {
}

#[inline]
fn batch_check<'a>(
batch: impl Iterator<Item = &'a Self> + Send,
) -> Result<(), SerializationError>
fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
Expand Down
6 changes: 2 additions & 4 deletions serialize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,10 @@ pub enum Validate {
No,
}

pub trait Valid: Sized + Sync {
pub trait Valid: Sized {
fn check(&self) -> Result<(), SerializationError>;

fn batch_check<'a>(
batch: impl Iterator<Item = &'a Self> + Send,
) -> Result<(), SerializationError>
fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
Expand Down
11 changes: 11 additions & 0 deletions serialize/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,17 @@ fn test_bool() {
}
}

#[test]
fn test_rc_arc() {
use ark_std::rc::Rc;
test_serialize(Rc::new(Dummy));
test_serialize(Rc::new(10u64));

use ark_std::sync::Arc;
test_serialize(Arc::new(Dummy));
test_serialize(Arc::new(10u64));
}

#[test]
fn test_btreemap() {
let mut map = BTreeMap::new();
Expand Down

0 comments on commit a9b2dea

Please sign in to comment.