From b76ebc2842a3a7b22c17368779908fdd539b553b Mon Sep 17 00:00:00 2001 From: taskooh Date: Tue, 24 Oct 2023 21:35:41 +0900 Subject: [PATCH] :recycle: Move out most of PubUniformRand --- .vscode/settings.json | 1 + arkworks/algebra/ff/src/fields/arithmetic.rs | 1 - arkworks/algebra/ff/src/fields/mod.rs | 3 +-- .../ff/src/fields/models/cubic_extension.rs | 4 +--- .../src/fields/models/quadratic_extension.rs | 4 +--- arkworks/algebra/ff/src/lib.rs | 4 ++-- arkworks/marlin/src/ahp/verifier.rs | 8 ++++---- arkworks/std/Cargo.toml | 2 -- arkworks/std/src/rand_helper.rs | 7 ------- mpc-algebra/src/fields.rs | 18 ++++++++++++++++++ mpc-algebra/src/lib.rs | 3 +++ mpc-algebra/src/macros.rs | 6 ++++++ mpc-algebra/src/wire/field.rs | 2 +- mpc-algebra/src/wire/group.rs | 2 +- mpc-trait/Cargo.toml | 1 + mpc-trait/src/lib.rs | 3 +++ mpc-trait/src/pub_rand.rs | 9 +++++++++ 17 files changed, 52 insertions(+), 26 deletions(-) create mode 100644 mpc-algebra/src/fields.rs create mode 100644 mpc-algebra/src/macros.rs create mode 100644 mpc-trait/src/pub_rand.rs diff --git a/.vscode/settings.json b/.vscode/settings.json index cb1a38fd..399b24dd 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,7 @@ "./Cargo.toml", "./mpc-algebra/Cargo.toml", "./mpc-net/Cargo.toml", + "./mpc-trait/Cargo.toml", "./arkworks/marlin/Cargo.toml", "./arkworks/poly-commit/Cargo.toml" ] diff --git a/arkworks/algebra/ff/src/fields/arithmetic.rs b/arkworks/algebra/ff/src/fields/arithmetic.rs index 8d714608..e324c940 100644 --- a/arkworks/algebra/ff/src/fields/arithmetic.rs +++ b/arkworks/algebra/ff/src/fields/arithmetic.rs @@ -219,7 +219,6 @@ macro_rules! impl_prime_field_standard_sample { } } } - impl crate::PubUniformRand for $field

{} }; } diff --git a/arkworks/algebra/ff/src/fields/mod.rs b/arkworks/algebra/ff/src/fields/mod.rs index be2c6a31..75d71f23 100644 --- a/arkworks/algebra/ff/src/fields/mod.rs +++ b/arkworks/algebra/ff/src/fields/mod.rs @@ -2,7 +2,7 @@ use crate::{ biginteger::BigInteger, bytes::{FromBytes, ToBytes}, fields::utils::k_adicity, - PubUniformRand, UniformRand, + UniformRand, }; use ark_serialize::{ CanonicalDeserialize, CanonicalDeserializeWithFlags, CanonicalSerialize, @@ -88,7 +88,6 @@ pub trait Field: + Ord + Neg + UniformRand - + PubUniformRand + Zeroize + Sized + Hash diff --git a/arkworks/algebra/ff/src/fields/models/cubic_extension.rs b/arkworks/algebra/ff/src/fields/models/cubic_extension.rs index 8b83fb5e..7547f23c 100644 --- a/arkworks/algebra/ff/src/fields/models/cubic_extension.rs +++ b/arkworks/algebra/ff/src/fields/models/cubic_extension.rs @@ -23,7 +23,7 @@ use ark_std::rand::{ use crate::{ bytes::{FromBytes, ToBytes}, fields::{Field, PrimeField}, - PubUniformRand, ToConstraintField, UniformRand, + ToConstraintField, UniformRand, }; pub trait CubicExtParameters: 'static + Send + Sync { @@ -455,8 +455,6 @@ impl Distribution> for Standard { } } -impl PubUniformRand for CubicExtField

{} - impl<'a, P: CubicExtParameters> Add<&'a CubicExtField

> for CubicExtField

{ type Output = Self; diff --git a/arkworks/algebra/ff/src/fields/models/quadratic_extension.rs b/arkworks/algebra/ff/src/fields/models/quadratic_extension.rs index c83b08c0..9079d9f7 100644 --- a/arkworks/algebra/ff/src/fields/models/quadratic_extension.rs +++ b/arkworks/algebra/ff/src/fields/models/quadratic_extension.rs @@ -23,7 +23,7 @@ use ark_std::rand::{ use crate::{ bytes::{FromBytes, ToBytes}, fields::{Field, LegendreSymbol, PrimeField, SquareRootField}, - PubUniformRand, ToConstraintField, UniformRand, + ToConstraintField, UniformRand, }; /// Defines a Quadratic extension field from a quadratic non-residue. @@ -566,8 +566,6 @@ impl Distribution> for Standard { } } -impl PubUniformRand for QuadExtField

{} - impl<'a, P: QuadExtParameters> Add<&'a QuadExtField

> for QuadExtField

{ type Output = Self; diff --git a/arkworks/algebra/ff/src/lib.rs b/arkworks/algebra/ff/src/lib.rs index caddf0de..b5c2ac0d 100644 --- a/arkworks/algebra/ff/src/lib.rs +++ b/arkworks/algebra/ff/src/lib.rs @@ -27,7 +27,7 @@ pub use self::fields::*; #[cfg(test)] mod test_field; -pub use ark_std::{PubUniformRand, UniformRand}; +pub use ark_std::UniformRand; mod to_field_vec; pub use to_field_vec::ToConstraintField; @@ -41,7 +41,7 @@ pub mod prelude { pub use crate::fields::{Field, FpParameters, PrimeField, SquareRootField}; - pub use ark_std::{PubUniformRand, UniformRand}; + pub use ark_std::UniformRand; pub use num_traits::{One, Zero}; } diff --git a/arkworks/marlin/src/ahp/verifier.rs b/arkworks/marlin/src/ahp/verifier.rs index 3dcc53bd..f89be34d 100644 --- a/arkworks/marlin/src/ahp/verifier.rs +++ b/arkworks/marlin/src/ahp/verifier.rs @@ -56,9 +56,9 @@ impl AHPForR1CS { .ok_or(SynthesisError::PolynomialDegreeTooLarge)?; let alpha = domain_h.sample_element_outside_domain(rng, true); - let eta_a = F::pub_rand(rng); - let eta_b = F::pub_rand(rng); - let eta_c = F::pub_rand(rng); + let eta_a = F::rand(rng); + let eta_b = F::rand(rng); + let eta_c = F::rand(rng); let msg = VerifierFirstMsg { alpha, @@ -95,7 +95,7 @@ impl AHPForR1CS { mut state: VerifierState, rng: &mut R, ) -> VerifierState { - state.gamma = Some(F::pub_rand(rng)); + state.gamma = Some(F::rand(rng)); state } diff --git a/arkworks/std/Cargo.toml b/arkworks/std/Cargo.toml index d391798a..c5e19520 100644 --- a/arkworks/std/Cargo.toml +++ b/arkworks/std/Cargo.toml @@ -18,8 +18,6 @@ rayon = { version = "1", optional = true } colored = { version = "2", optional = true } num-traits = { version = "0.2", default-features = false } -mpc-trait = {path = "../../mpc-trait" } - [features] default = [ "std" ] std = [] diff --git a/arkworks/std/src/rand_helper.rs b/arkworks/std/src/rand_helper.rs index 177fbe64..59ee3852 100644 --- a/arkworks/std/src/rand_helper.rs +++ b/arkworks/std/src/rand_helper.rs @@ -4,19 +4,12 @@ use rand::{ Rng, }; -use mpc_trait::MpcWire; pub use rand; pub trait UniformRand: Sized { fn rand(rng: &mut R) -> Self; } -pub trait PubUniformRand: Sized + MpcWire + UniformRand { - fn pub_rand(rng: &mut R) -> Self { - ::rand(rng) - } -} - impl UniformRand for T where Standard: Distribution, diff --git a/mpc-algebra/src/fields.rs b/mpc-algebra/src/fields.rs new file mode 100644 index 00000000..ccbd65ab --- /dev/null +++ b/mpc-algebra/src/fields.rs @@ -0,0 +1,18 @@ +use ark_ff::{CubicExtField, CubicExtParameters, QuadExtField, QuadExtParameters}; +use ark_ff::{ + Fp256, Fp256Parameters, Fp320, Fp320Parameters, Fp384, Fp384Parameters, Fp448, Fp448Parameters, + Fp64, Fp64Parameters, Fp768, Fp768Parameters, Fp832, Fp832Parameters, +}; +use mpc_trait::PubUniformRand; + +use crate::impl_Fp_mpc; +impl PubUniformRand for QuadExtField

{} +impl PubUniformRand for CubicExtField

{} + +impl_Fp_mpc!(Fp64, Fp64Parameters); +impl_Fp_mpc!(Fp256, Fp256Parameters); +impl_Fp_mpc!(Fp320, Fp320Parameters); +impl_Fp_mpc!(Fp384, Fp384Parameters); +impl_Fp_mpc!(Fp448, Fp448Parameters); +impl_Fp_mpc!(Fp768, Fp768Parameters); +impl_Fp_mpc!(Fp832, Fp832Parameters); diff --git a/mpc-algebra/src/lib.rs b/mpc-algebra/src/lib.rs index d8872ef2..a2eb1c00 100644 --- a/mpc-algebra/src/lib.rs +++ b/mpc-algebra/src/lib.rs @@ -1,6 +1,9 @@ // #![feature(associated_type_defaults)] pub mod reveal; +mod fields; +#[macro_use] +pub mod macros; pub use reveal::*; pub mod share; pub use share::*; diff --git a/mpc-algebra/src/macros.rs b/mpc-algebra/src/macros.rs new file mode 100644 index 00000000..b401b7df --- /dev/null +++ b/mpc-algebra/src/macros.rs @@ -0,0 +1,6 @@ +#[macro_export] +macro_rules! impl_Fp_mpc { + ($Fp:ident, $FpParameters:ident) => { + impl crate::PubUniformRand for $Fp

{} + } +} \ No newline at end of file diff --git a/mpc-algebra/src/wire/field.rs b/mpc-algebra/src/wire/field.rs index f38797a8..2c6914ff 100644 --- a/mpc-algebra/src/wire/field.rs +++ b/mpc-algebra/src/wire/field.rs @@ -1,5 +1,5 @@ use derivative::Derivative; -use mpc_trait::MpcWire; +use mpc_trait::{MpcWire, PubUniformRand}; use num_bigint::BigUint; use rand::Rng; use std::fmt::{self, Debug, Display}; diff --git a/mpc-algebra/src/wire/group.rs b/mpc-algebra/src/wire/group.rs index 8689edf7..df46d37f 100644 --- a/mpc-algebra/src/wire/group.rs +++ b/mpc-algebra/src/wire/group.rs @@ -12,7 +12,7 @@ use ark_serialize::{ CanonicalSerializeWithFlags, }; use ark_serialize::{Flags, SerializationError}; -use mpc_trait::MpcWire; +use mpc_trait::{MpcWire, PubUniformRand}; use crate::share::group::GroupShare; use crate::Reveal; diff --git a/mpc-trait/Cargo.toml b/mpc-trait/Cargo.toml index 32eb94da..44928c13 100644 --- a/mpc-trait/Cargo.toml +++ b/mpc-trait/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +ark-std = { path = "../arkworks/std", version = "0.3.0", features = ["std", "print-trace"] } diff --git a/mpc-trait/src/lib.rs b/mpc-trait/src/lib.rs index 407fcd7d..7c2ade5b 100644 --- a/mpc-trait/src/lib.rs +++ b/mpc-trait/src/lib.rs @@ -1,3 +1,6 @@ +pub mod pub_rand; +pub use pub_rand::*; + use std::borrow::Cow; pub trait MpcWire: Clone { diff --git a/mpc-trait/src/pub_rand.rs b/mpc-trait/src/pub_rand.rs new file mode 100644 index 00000000..e60f884c --- /dev/null +++ b/mpc-trait/src/pub_rand.rs @@ -0,0 +1,9 @@ +use crate::MpcWire; +use ark_std::rand::Rng; +pub use ark_std::UniformRand; + +pub trait PubUniformRand: Sized + MpcWire + UniformRand { + fn pub_rand(rng: &mut R) -> Self { + ::rand(rng) + } +}