Skip to content
Open
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
5,409 changes: 2,675 additions & 2,734 deletions Cargo.lock

Large diffs are not rendered by default.

51 changes: 3 additions & 48 deletions core/primitives/src/enc_key.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,17 @@
#[cfg(feature = "std")]
use serde::{Serialize, Serializer, Deserialize, Deserializer};
#[cfg(feature = "std")]
use substrate_primitives::hexdisplay::AsBytesRef;
#[cfg(feature = "std")]
use substrate_primitives::bytes;
use keys::EncryptionKey;
use fixed_hash::construct_fixed_hash;
use pairing::bls12_381::{Bls12, Fr};
use pairing::io;
use parity_codec::{Encode, Decode, Input};
use crate::{PARAMS, IntoXY};
use core::convert::TryFrom;

const SIZE: usize = 32;

construct_fixed_hash! {
pub struct H256(SIZE);
pub struct EncKey(SIZE);
}

pub type EncKey = H256;

#[cfg(feature = "std")]
impl Serialize for EncKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
bytes::serialize(&self.0, serializer)
}
}

#[cfg(feature = "std")]
impl<'de> Deserialize<'de> for EncKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
bytes::deserialize_check_len(deserializer, bytes::ExpectedLen::Exact(SIZE))
.map(|x| EncKey::from_slice(&x))
}
}

impl Encode for EncKey {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.using_encoded(f)
}
}

impl Decode for EncKey {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
<[u8; SIZE] as Decode>::decode(input).map(H256)
}
}
impl_fixed_hash_codec!(EncKey, SIZE);

impl TryFrom<EncryptionKey<Bls12>> for EncKey {
type Error = io::Error;
Expand All @@ -58,7 +20,7 @@ impl TryFrom<EncryptionKey<Bls12>> for EncKey {
let mut writer = [0u8; 32];
enc_key.write(&mut writer[..])?;

Ok(H256::from_slice(&writer))
Ok(EncKey::from_slice(&writer))
}
}

Expand All @@ -78,13 +40,6 @@ impl TryFrom<&EncKey> for EncryptionKey<Bls12> {
}
}

#[cfg(feature = "std")]
impl AsBytesRef for EncKey {
fn as_bytes_ref(&self) -> &[u8] {
self.as_ref()
}
}

impl IntoXY<Bls12> for EncKey {
fn into_xy(&self) -> Result<(Fr, Fr), io::Error> {
let point = EncryptionKey::<Bls12>::try_from(self)?
Expand Down
93 changes: 8 additions & 85 deletions core/primitives/src/g_epoch.rs
Original file line number Diff line number Diff line change
@@ -1,93 +1,25 @@
#[cfg(feature = "std")]
use serde::{Serialize, Serializer, Deserialize, Deserializer};
#[cfg(feature = "std")]
use substrate_primitives::bytes;
use crate::{PARAMS, IntoXY};
use crate::PARAMS;
use fixed_hash::construct_fixed_hash;
use jubjub::curve::{JubjubBls12, edwards, PrimeOrder, Unknown};
use jubjub::curve::{JubjubBls12, edwards, PrimeOrder};
#[cfg(test)]
use jubjub::curve::Unknown;
use jubjub::group_hash::group_hash;
use pairing::{
bls12_381::{Bls12, Fr},
bls12_381::Bls12,
io
};
use parity_codec::{Encode, Decode, Input};
use byteorder::{ByteOrder, LittleEndian};
use core::convert::TryFrom;

const SIZE: usize = 32;
const GEPOCH_PERSONALIZATION: &[u8; 8] = b"zcgepoch";

construct_fixed_hash! {
pub struct H256(SIZE);
}

pub type GEpoch = H256;

#[cfg(feature = "std")]
impl Serialize for GEpoch {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
bytes::serialize(&self.0, serializer)
}
}

#[cfg(feature = "std")]
impl<'de> Deserialize<'de> for GEpoch {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
bytes::deserialize_check_len(deserializer, bytes::ExpectedLen::Exact(SIZE))
.map(|x| GEpoch::from_slice(&x))
}
}

impl Encode for GEpoch {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.using_encoded(f)
}
}

impl Decode for GEpoch {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
<[u8; SIZE] as Decode>::decode(input).map(H256)
}
}

impl TryFrom<edwards::Point<Bls12, PrimeOrder>> for GEpoch {
type Error = io::Error;

fn try_from(point: edwards::Point<Bls12, PrimeOrder>) -> Result<Self, io::Error> {
let mut writer = [0u8; 32];
point.write(&mut &mut writer[..])?;

Ok(H256::from_slice(&writer[..]))
}
}

impl TryFrom<GEpoch> for edwards::Point<Bls12, PrimeOrder> {
type Error = io::Error;

fn try_from(g_epoch: GEpoch) -> Result<Self, io::Error> {
let mut bytes = g_epoch.as_bytes();

edwards::Point::<Bls12, Unknown>::read(&mut bytes, &PARAMS)?
.as_prime_order(&PARAMS)
.ok_or(io::Error::NotInField)
}
pub struct GEpoch(SIZE);
}

impl TryFrom<&GEpoch> for edwards::Point<Bls12, PrimeOrder> {
type Error = io::Error;

fn try_from(g_epoch: &GEpoch) -> Result<Self, io::Error> {
let mut bytes = g_epoch.as_bytes();

edwards::Point::<Bls12, Unknown>::read(&mut bytes, &PARAMS)?
.as_prime_order(&PARAMS)
.ok_or(io::Error::NotInField)
}
}
impl_fixed_hash_codec!(GEpoch, SIZE);
impl_edwards_point_conversions!(GEpoch, SIZE);

impl GEpoch {
pub fn try_new() -> Result<Self, io::Error> {
Expand All @@ -109,15 +41,6 @@ impl GEpoch {
}
}

impl IntoXY<Bls12> for GEpoch {
fn into_xy(&self) -> Result<(Fr, Fr), io::Error> {
let point = edwards::Point::<Bls12, PrimeOrder>::try_from(self)?
.into_xy();

Ok(point)
}
}

fn find_group_hash(
m: &[u8],
personalization: &[u8; 8],
Expand Down
123 changes: 8 additions & 115 deletions core/primitives/src/left_ciphertext.rs
Original file line number Diff line number Diff line change
@@ -1,127 +1,20 @@
#[cfg(feature = "std")]
use serde::{Serialize, Serializer, Deserialize, Deserializer};
#[cfg(feature = "std")]
use substrate_primitives::bytes;
#[cfg(feature = "std")]
use substrate_primitives::hexdisplay::AsBytesRef;
use crate::{PARAMS, IntoXY};
use fixed_hash::construct_fixed_hash;
use pairing::bls12_381::{Bls12, Fr};
use pairing::io;
#[cfg(test)]
use pairing::bls12_381::Bls12;
#[cfg(test)]
use jubjub::curve::{edwards, PrimeOrder, Unknown};
#[cfg(test)]
use zcrypto::elgamal;
use parity_codec::{Encode, Decode, Input};
use core::convert::TryFrom;

const SIZE: usize = 32;

construct_fixed_hash! {
pub struct H256(SIZE);
}

pub type LeftCiphertext = H256;

#[cfg(feature = "std")]
impl Serialize for LeftCiphertext {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
bytes::serialize(&self.0, serializer)
}
}

#[cfg(feature = "std")]
impl<'de> Deserialize<'de> for LeftCiphertext {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
bytes::deserialize_check_len(deserializer, bytes::ExpectedLen::Exact(SIZE))
.map(|x| LeftCiphertext::from_slice(&x))
}
}

impl Encode for LeftCiphertext {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.using_encoded(f)
}
}

impl Decode for LeftCiphertext {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
<[u8; SIZE] as Decode>::decode(input).map(H256)
}
}

impl TryFrom<elgamal::Ciphertext<Bls12>> for LeftCiphertext {
type Error = io::Error;

fn try_from(ciphertext: elgamal::Ciphertext<Bls12>) -> Result<Self, io::Error> {
let mut writer = [0u8; 32];
ciphertext.left.write(&mut &mut writer[..])?;

Ok(H256::from_slice(&writer[..]))
}
}

impl TryFrom<LeftCiphertext> for edwards::Point<Bls12, PrimeOrder> {
type Error = io::Error;

fn try_from(left_ciphertext: LeftCiphertext) -> Result<Self, io::Error> {
let mut bytes = left_ciphertext.as_bytes();

edwards::Point::<Bls12, Unknown>::read(&mut bytes, &PARAMS)?
.as_prime_order(&PARAMS)
.ok_or(io::Error::NotInField)
}
}

impl TryFrom<&LeftCiphertext> for edwards::Point<Bls12, PrimeOrder> {
type Error = io::Error;

fn try_from(left_ciphertext: &LeftCiphertext) -> Result<Self, io::Error> {
let mut bytes = left_ciphertext.as_bytes();

edwards::Point::<Bls12, Unknown>::read(&mut bytes, &PARAMS)?
.as_prime_order(&PARAMS)
.ok_or(io::Error::NotInField)
}
pub struct LeftCiphertext(SIZE);
}

impl TryFrom<edwards::Point<Bls12, PrimeOrder>> for LeftCiphertext {
type Error = io::Error;

fn try_from(point: edwards::Point<Bls12, PrimeOrder>) -> Result<Self, io::Error> {
let mut writer = [0u8; 32];
point.write(&mut &mut writer[..])?;

Ok(H256::from_slice(&writer[..]))
}
}

#[cfg(feature = "std")]
impl AsBytesRef for LeftCiphertext {
fn as_bytes_ref(&self) -> &[u8] {
self.as_ref()
}
}

impl IntoXY<Bls12> for LeftCiphertext {
fn into_xy(&self) -> Result<(Fr, Fr), io::Error> {
let point = edwards::Point::<Bls12, PrimeOrder>::try_from(self)?
.into_xy();

Ok(point)
}
}

impl IntoXY<Bls12> for &LeftCiphertext {
fn into_xy(&self) -> Result<(Fr, Fr), io::Error> {
let point = edwards::Point::<Bls12, PrimeOrder>::try_from(**self)?
.into_xy();

Ok(point)
}
}
impl_fixed_hash_codec!(LeftCiphertext, SIZE);
impl_edwards_point_conversions!(LeftCiphertext, SIZE);
impl_ciphertext_component_conversion!(LeftCiphertext, left, SIZE);

#[cfg(test)]
mod tests {
Expand Down
Loading