Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
new serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Achim Schneider committed Mar 19, 2023
1 parent 15898da commit e56a088
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 43 deletions.
15 changes: 4 additions & 11 deletions primitives/arkworks/src/ed_on_bls12_377.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#![warn(missing_docs)]

use crate::utils::{deserialize_argument, serialize_result};
use crate::utils::{deserialize_argument, serialize_iter_to_vec, serialize_result};
use ark_ec::{models::CurveConfig, twisted_edwards, AffineRepr, Group, VariableBaseMSM};
use ark_ed_on_bls12_377::{EdwardsConfig, EdwardsProjective};
use ark_ff::Zero;
Expand All @@ -28,16 +28,9 @@ use sp_std::vec::Vec;

/// Compute a multi scalar multiplication on G! through arkworks
pub fn msm(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
let bases: Vec<_> = bases
.chunks(twisted_edwards::Affine::<EdwardsConfig>::generator().serialized_size(Compress::No))
.into_iter()
.map(|a| deserialize_argument::<twisted_edwards::Affine<EdwardsConfig>>(&a.to_vec()))
.collect();
let scalars: Vec<_> = scalars
.chunks(<EdwardsConfig as CurveConfig>::ScalarField::zero().serialized_size(Compress::No))
.into_iter()
.map(|a| deserialize_argument::<<EdwardsConfig as CurveConfig>::ScalarField>(&a.to_vec()))
.collect();
let bases: Vec<_> = serialize_iter_to_Vec::<twisted_edwards::Affine<EdwardsConfig>>(bases);
let scalars: Vec<_> =
serialize_iter_to_vec::<<EdwardsConfig as CurveConfig>::ScalarField>(scalars);

let result = <EdwardsProjective as VariableBaseMSM>::msm(&bases, &scalars).unwrap();

Expand Down
28 changes: 7 additions & 21 deletions primitives/arkworks/src/ed_on_bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#![warn(missing_docs)]

use crate::utils::{deserialize_argument, serialize_result};
use crate::utils::{deserialize_argument, serialize_iter_to_vec, serialize_result};
use ark_ec::{
models::CurveConfig, short_weierstrass::Affine as SWAffine,
twisted_edwards::Affine as TEAffine, AffineRepr, Group, VariableBaseMSM,
Expand All @@ -31,16 +31,9 @@ use sp_std::vec::Vec;

/// Compute a multi scalar multiplication on G! through arkworks
pub fn te_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
let bases: Vec<_> = bases
.chunks(TEAffine::<JubjubConfig>::generator().serialized_size(Compress::No))
.into_iter()
.map(|a| deserialize_argument::<TEAffine<JubjubConfig>>(&a.to_vec()))
.collect();
let scalars: Vec<_> = scalars
.chunks(<JubjubConfig as CurveConfig>::ScalarField::zero().serialized_size(Compress::No))
.into_iter()
.map(|a| deserialize_argument::<<JubjubConfig as CurveConfig>::ScalarField>(&a.to_vec()))
.collect();
let bases: Vec<_> = serialize_iter_to_vec::<TEAffine<JubjubConfig>>(bases);
let scalars: Vec<_> =
serialize_iter_to_vec::<<JubjubConfig as CurveConfig>::ScalarField>(scalars);

let result = <EdwardsProjective as VariableBaseMSM>::msm(&bases, &scalars).unwrap();

Expand All @@ -49,16 +42,9 @@ pub fn te_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {

/// Compute a multi scalar multiplication on G! through arkworks
pub fn sw_msm(bases: Vec<u8>, scalars: Vec<u8>) -> Vec<u8> {
let bases: Vec<_> = bases
.chunks(SWAffine::<JubjubConfig>::generator().serialized_size(Compress::No))
.into_iter()
.map(|a| deserialize_argument::<SWAffine<JubjubConfig>>(&a.to_vec()))
.collect();
let scalars: Vec<_> = scalars
.chunks(<JubjubConfig as CurveConfig>::ScalarField::zero().serialized_size(Compress::No))
.into_iter()
.map(|a| deserialize_argument::<<JubjubConfig as CurveConfig>::ScalarField>(&a.to_vec()))
.collect();
let bases: Vec<_> = serialize_iter_to_vec::<SWAffine<JubjubConfig>>(bases);
let scalars: Vec<_> =
serialize_iter_to_vec::<<JubjubConfig as CurveConfig>::ScalarField>(scalars);

let result = <SWProjective as VariableBaseMSM>::msm(&bases, &scalars).unwrap();

Expand Down
53 changes: 42 additions & 11 deletions primitives/arkworks/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use ark_ec::{
AffineRepr, Group,
};
use ark_ff::Zero;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Compress, Validate};
use ark_serialize::{
CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError, Validate,
};
use ark_std::{io::Cursor, vec, vec::Vec};

pub fn serialize_result(result: impl CanonicalSerialize) -> Vec<u8> {
Expand All @@ -19,20 +21,49 @@ pub fn deserialize_argument<Field: CanonicalDeserialize>(argument: &Vec<u8>) ->
Field::deserialize_with_mode(cursor, Compress::No, Validate::No).unwrap()
}

pub fn serialize_iter_to_vec<T>(
iter: impl IntoIterator<Item = T>,
) -> Result<Vec<u8>, SerializationError>
where
T: CanonicalSerialize + Sized + Zero,
{
let iter = iter.into_iter();
let element_size = T::zero().uncompressed_size();
let length: usize =
iter.size_hint().0.try_into().map_err(|_| SerializationError::InvalidData)?;
let mut w = Cursor::new(Vec::with_capacity(8 + element_size * length));
length.serialize_uncompressed(&mut w)?;
let mut length = 0u32;
for elem in iter {
elem.serialize_uncompressed(&mut w)?;
length += 1;
}
let result = w.into_inner();
// elem.serialize_uncompressed::<&mut &mut T>(&mut result.as_mut())?;
Ok(result)
}

pub fn deserialize_iter_to_vec<T>(mut bytes: &[u8]) -> Result<Vec<T>, SerializationError>
where
T: CanonicalDeserialize + Sized,
{
let cursor = Cursor::new(bytes.to_vec());
let length = u32::deserialize_uncompressed_unchecked(cursor.clone())?;
let mut result = Vec::with_capacity(length as usize);
for _ in 0..length {
result.push(T::deserialize_uncompressed_unchecked(cursor.clone())?);
}
Ok(result)
}

pub fn multi_miller_loop_generic<Curve: Pairing>(
a_vec: Vec<u8>,
b_vec: Vec<u8>,
) -> Result<Vec<u8>, PairingError> {
let g1: Vec<_> = a_vec
.chunks(<Curve as Pairing>::G1Affine::generator().serialized_size(Compress::No))
.into_iter()
.map(|elem| deserialize_argument::<<Curve as Pairing>::G1Affine>(&elem.to_vec()))
.collect();
let g2: Vec<_> = b_vec
.chunks(<Curve as Pairing>::G2Affine::generator().serialized_size(Compress::No))
.into_iter()
.map(|elem| deserialize_argument::<<Curve as Pairing>::G2Affine>(&elem.to_vec()))
.collect();
let g1: Vec<_> = deserialize_iter_to_vec::<<Curve as Pairing>::G1Affine>(&a_vec)
.map_err(|_| PairingError::InternalPanic)?;
let g2: Vec<_> = deserialize_iter_to_vec::<<Curve as Pairing>::G2Affine>(&b_vec)
.map_err(|_| PairingError::InternalPanic)?;

let result = Curve::multi_miller_loop(g1, g2);

Expand Down

0 comments on commit e56a088

Please sign in to comment.