Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ark version differentiation #266

Merged
merged 8 commits into from
Jul 24, 2023
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
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Introduce `ark-ff-04` feature flag for conversion to `ark-ff@0.4` types.
- Support for [`alloy-rlp`](https://github.com/alloy-rs/rlp)
- MSRV (Minimum Supported Rust Version) is now set at 1.65.0, from previously undefined.

Expand All @@ -19,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Make `serde::Deserialize` impl more permissive
- Use Ethereum `Quantity` encoding for serde serialization when human-readable
- Fix error in `from_base_be` that allowed instantiation of overflowing `Uint`.
- Updated `ark` to `0.4`, `fastrlp` to `0.3`, `pyo3` to `0.19`, and `sqlx-core` to `0.7`.
- Updated `fastrlp` to `0.3`, `pyo3` to `0.19`, and `sqlx-core` to `0.7`.
- Improved `fastrlp` perfomance
- Improved `proptest` performance
- Made `support` module and its modules public
Expand Down
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ thiserror = "1.0"
# support
alloy-rlp = { version = "0.3", optional = true }
arbitrary = { version = "1", optional = true }
ark-ff = { version = "0.4", optional = true }
ark-ff-04 = { version = "0.4.0", package = "ark-ff", optional = true }
ark-ff-03 = { version = "0.3.0", package = "ark-ff", optional = true }
bn-rs = { version = "0.2", optional = true }
fastrlp = { version = "0.3", optional = true }
num-bigint = { version = "0.4", optional = true }
Expand All @@ -72,11 +73,13 @@ sqlx-core = { version = "0.7", optional = true }
[dev-dependencies]
ruint = { workspace = true, features = ["arbitrary", "proptest"] }

ark-bn254-04 = { version = "0.4.0", package = "ark-bn254" }
ark-bn254-03 = { version = "0.3.0", package = "ark-bn254" }

criterion = "0.5"
rand = "0.8"

approx = "0.5"
ark-bn254 = "0.4"
bincode = "1.3"
coverage-helper = "0.1"
hex = "0.4"
Expand All @@ -93,7 +96,8 @@ generic_const_exprs = ["nightly"]
# support
alloy-rlp = ["dep:alloy-rlp"]
arbitrary = ["dep:arbitrary"]
ark-ff = ["dep:ark-ff"]
ark-ff = ["dep:ark-ff-03"]
ark-ff-04 = ["dep:ark-ff-04"]
bn-rs = ["dep:bn-rs"]
fastrlp = ["dep:fastrlp"]
num-bigint = ["dep:num-bigint"]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ named feature flag.
* [`primitive-types`](https://docs.rs/primitive-types): Implements the [`From<_>`] conversions between corresponding types.
* [`postgres`](https://docs.rs/postgres): Implements the [`ToSql`](https://docs.rs/postgres/latest/postgres/types/trait.ToSql.html) trait supporting many column types.
* [`num-bigint`](https://docs.rs/num-bigint): Implements conversion to/from [`BigUint`](https://docs.rs/num-bigint/latest/num_bigint/struct.BigUint.html) and [`BigInt`](https://docs.rs/num-bigint/latest/num_bigint/struct.BigInt.html).
* [`ark-ff`](https://docs.rs/ark-ff): Implements conversion to/from [`BigInt`](https://docs.rs/ark-ff/0.4.2/ark_ff/biginteger/struct.BigInt.html) and [`Fp`](https://docs.rs/ark-ff/0.4.2/ark_ff/fields/models/fp/struct.Fp.html) types.
* [`ark-ff`](https://docs.rs/ark-ff): Implements conversion to/from the [`BigInteger*`](https://docs.rs/ark-ff/0.3.0/ark_ff/biginteger/index.html) types and the [`Fp*`](https://docs.rs/ark-ff/0.3.0/ark_ff/fields/models/index.html) types from `ark-ff@0.3`.
* [`ark-ff-04`](https://docs.rs/ark-ff): Implements conversion to/from [`BigInt`](https://docs.rs/ark-ff/0.4.2/ark_ff/biginteger/struct.BigInt.html) and [`Fp`](https://docs.rs/ark-ff/0.4.2/ark_ff/fields/models/fp/struct.Fp.html) types from `ark-ff@0.4`.
* [`sqlx`](https://docs.rs/sqlx): Implements database agnostic storage as byte array. Requires
`sqlx` to be used with the `tokio-native-tls` runtime, due to issue [sqlx#1627](https://github.com/launchbadge/sqlx/issues/1627).
* [`zeroize`](https://docs.rs/zeroize): Implements the [`Zeroize`](https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html) trait. This makes [`Uint`] and [`Bits`] compatible with the [`secrecy`](https://crates.io/crates/secrecy) crate.
Expand Down
140 changes: 79 additions & 61 deletions src/support/ark_ff.rs
Original file line number Diff line number Diff line change
@@ -1,87 +1,106 @@
//! Support for the [`ark-ff`](https://crates.io/crates/ark-ff) crate.

//! Support for the [`ark-ff`](https://crates.io/crates/ark-ff) crate 0.3
//! version.
#![cfg(feature = "ark-ff")]
#![cfg_attr(docsrs, doc(cfg(feature = "ark-ff")))]

use crate::{ToFieldError, Uint};
use ark_ff::{
biginteger::BigInt,
fields::models::{Fp, FpConfig},
use ark_ff_03::{
biginteger::{
BigInteger128, BigInteger256, BigInteger320, BigInteger384, BigInteger448, BigInteger64,
BigInteger768, BigInteger832,
},
fields::models::{
Fp256, Fp256Parameters, Fp320, Fp320Parameters, Fp384, Fp384Parameters, Fp448,
Fp448Parameters, Fp64, Fp64Parameters, Fp768, Fp768Parameters, Fp832, Fp832Parameters,
},
PrimeField,
};

// FEATURE: Implement the `BigInteger` trait.

// BigInt
macro_rules! impl_from_ark {
($ark:ty, $bits:expr, $limbs:expr) => {
impl From<$ark> for Uint<$bits, $limbs> {
fn from(value: $ark) -> Self {
Self::from_limbs(value.0)
}
}

impl<const BITS: usize, const LIMBS: usize> From<BigInt<LIMBS>> for Uint<BITS, LIMBS> {
fn from(value: BigInt<LIMBS>) -> Self {
Self::from_limbs(value.0)
}
}
impl From<&$ark> for Uint<$bits, $limbs> {
fn from(value: &$ark) -> Self {
Self::from_limbs(value.0)
}
}

impl<const BITS: usize, const LIMBS: usize> From<&BigInt<LIMBS>> for Uint<BITS, LIMBS> {
fn from(value: &BigInt<LIMBS>) -> Self {
Self::from_limbs(value.0)
}
}
impl From<Uint<$bits, $limbs>> for $ark {
fn from(value: Uint<$bits, $limbs>) -> Self {
Self(value.into_limbs())
}
}

impl<const BITS: usize, const LIMBS: usize> From<Uint<BITS, LIMBS>> for BigInt<LIMBS> {
fn from(value: Uint<BITS, LIMBS>) -> Self {
Self::new(value.into_limbs())
}
impl From<&Uint<$bits, $limbs>> for $ark {
fn from(value: &Uint<$bits, $limbs>) -> Self {
Self(value.into_limbs())
}
}
};
}

impl<const BITS: usize, const LIMBS: usize> From<&Uint<BITS, LIMBS>> for BigInt<LIMBS> {
fn from(value: &Uint<BITS, LIMBS>) -> Self {
Self::new(value.into_limbs())
}
}
impl_from_ark!(BigInteger64, 64, 1);
impl_from_ark!(BigInteger128, 128, 2);
impl_from_ark!(BigInteger256, 256, 4);
impl_from_ark!(BigInteger320, 320, 5);
impl_from_ark!(BigInteger384, 384, 6);
impl_from_ark!(BigInteger448, 448, 7);
impl_from_ark!(BigInteger768, 768, 12);
impl_from_ark!(BigInteger832, 832, 13);

macro_rules! impl_from_ark_field {
($field:ident, $params:ident, $bits:expr, $limbs:expr) => {
impl<P: $params> From<$field<P>> for Uint<$bits, $limbs> {
fn from(value: $field<P>) -> Self {
value.into_repr().into()
}
}

// Fp
impl<P: $params> From<&$field<P>> for Uint<$bits, $limbs> {
fn from(value: &$field<P>) -> Self {
value.into_repr().into()
}
}

impl<P: FpConfig<LIMBS>, const BITS: usize, const LIMBS: usize> From<Fp<P, LIMBS>>
for Uint<BITS, LIMBS>
{
fn from(value: Fp<P, LIMBS>) -> Self {
value.into_bigint().into()
}
}
impl<P: $params> TryFrom<Uint<$bits, $limbs>> for $field<P> {
type Error = ToFieldError;

impl<P: FpConfig<LIMBS>, const BITS: usize, const LIMBS: usize> From<&Fp<P, LIMBS>>
for Uint<BITS, LIMBS>
{
fn from(value: &Fp<P, LIMBS>) -> Self {
value.into_bigint().into()
}
}
fn try_from(value: Uint<$bits, $limbs>) -> Result<Self, ToFieldError> {
Self::from_repr(value.into()).ok_or(ToFieldError::NotInField)
}
}

impl<P: FpConfig<LIMBS>, const BITS: usize, const LIMBS: usize> TryFrom<Uint<BITS, LIMBS>>
for Fp<P, LIMBS>
{
type Error = ToFieldError;
impl<P: $params> TryFrom<&Uint<$bits, $limbs>> for $field<P> {
type Error = ToFieldError;

fn try_from(value: Uint<BITS, LIMBS>) -> Result<Self, ToFieldError> {
Self::from_bigint(value.into()).ok_or(ToFieldError::NotInField)
}
fn try_from(value: &Uint<$bits, $limbs>) -> Result<Self, ToFieldError> {
Self::from_repr(value.into()).ok_or(ToFieldError::NotInField)
}
}
};
}

impl<P: FpConfig<LIMBS>, const BITS: usize, const LIMBS: usize> TryFrom<&Uint<BITS, LIMBS>>
for Fp<P, LIMBS>
{
type Error = ToFieldError;

fn try_from(value: &Uint<BITS, LIMBS>) -> Result<Self, ToFieldError> {
Self::from_bigint(value.into()).ok_or(ToFieldError::NotInField)
}
}
impl_from_ark_field!(Fp64, Fp64Parameters, 64, 1);
impl_from_ark_field!(Fp256, Fp256Parameters, 256, 4);
impl_from_ark_field!(Fp320, Fp320Parameters, 320, 5);
impl_from_ark_field!(Fp384, Fp384Parameters, 384, 6);
impl_from_ark_field!(Fp448, Fp448Parameters, 448, 7);
impl_from_ark_field!(Fp768, Fp768Parameters, 768, 12);
impl_from_ark_field!(Fp832, Fp832Parameters, 832, 13);

#[cfg(test)]
mod tests {
use super::*;
use crate::aliases::U256;
use ark_bn254::{Fq, FqConfig, Fr, FrConfig};
use ark_ff::MontConfig;
use ark_bn254_03::{Fq, FqParameters, Fr, FrParameters};
use ark_ff_03::FpParameters;
use proptest::proptest;

macro_rules! test_roundtrip {
Expand All @@ -96,7 +115,6 @@ mod tests {

#[test]
fn test_roundtrip() {
use ark_ff::*;
test_roundtrip!(BigInteger64, 64, 1);
test_roundtrip!(BigInteger128, 128, 2);
test_roundtrip!(BigInteger256, 256, 4);
Expand All @@ -109,7 +127,7 @@ mod tests {

#[test]
fn test_fq_roundtrip() {
let modulus: U256 = FqConfig::MODULUS.into();
let modulus: U256 = FqParameters::MODULUS.into();
proptest!(|(value: U256)| {
let value: U256 = value % modulus;
let f: Fq = value.try_into().unwrap();
Expand All @@ -120,7 +138,7 @@ mod tests {

#[test]
fn test_fr_roundtrip() {
let modulus: U256 = FrConfig::MODULUS.into();
let modulus: U256 = FrParameters::MODULUS.into();
proptest!(|(value: U256)| {
let value: U256 = value % modulus;
let f: Fr = value.try_into().unwrap();
Expand Down
130 changes: 130 additions & 0 deletions src/support/ark_ff_04.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//! Support for the [`ark-ff`](https://crates.io/crates/ark-ff) crate.
#![cfg(feature = "ark-ff-04")]
#![cfg_attr(has_doc_cfg, doc(cfg(feature = "ark-ff-04")))]

use crate::{ToFieldError, Uint};
use ark_ff_04::{
biginteger::BigInt,
fields::models::{Fp, FpConfig},
PrimeField,
};

// FEATURE: Implement the `BigInteger` trait.

// BigInt

impl<const BITS: usize, const LIMBS: usize> From<BigInt<LIMBS>> for Uint<BITS, LIMBS> {
fn from(value: BigInt<LIMBS>) -> Self {
Self::from_limbs(value.0)
}
}

impl<const BITS: usize, const LIMBS: usize> From<&BigInt<LIMBS>> for Uint<BITS, LIMBS> {
fn from(value: &BigInt<LIMBS>) -> Self {
Self::from_limbs(value.0)
}
}

impl<const BITS: usize, const LIMBS: usize> From<Uint<BITS, LIMBS>> for BigInt<LIMBS> {
fn from(value: Uint<BITS, LIMBS>) -> Self {
Self::new(value.into_limbs())
}
}

impl<const BITS: usize, const LIMBS: usize> From<&Uint<BITS, LIMBS>> for BigInt<LIMBS> {
fn from(value: &Uint<BITS, LIMBS>) -> Self {
Self::new(value.into_limbs())
}
}

// Fp

impl<P: FpConfig<LIMBS>, const BITS: usize, const LIMBS: usize> From<Fp<P, LIMBS>>
for Uint<BITS, LIMBS>
{
fn from(value: Fp<P, LIMBS>) -> Self {
value.into_bigint().into()
}
}

impl<P: FpConfig<LIMBS>, const BITS: usize, const LIMBS: usize> From<&Fp<P, LIMBS>>
for Uint<BITS, LIMBS>
{
fn from(value: &Fp<P, LIMBS>) -> Self {
value.into_bigint().into()
}
}

impl<P: FpConfig<LIMBS>, const BITS: usize, const LIMBS: usize> TryFrom<Uint<BITS, LIMBS>>
for Fp<P, LIMBS>
{
type Error = ToFieldError;

fn try_from(value: Uint<BITS, LIMBS>) -> Result<Self, ToFieldError> {
Self::from_bigint(value.into()).ok_or(ToFieldError::NotInField)
}
}

impl<P: FpConfig<LIMBS>, const BITS: usize, const LIMBS: usize> TryFrom<&Uint<BITS, LIMBS>>
for Fp<P, LIMBS>
{
type Error = ToFieldError;

fn try_from(value: &Uint<BITS, LIMBS>) -> Result<Self, ToFieldError> {
Self::from_bigint(value.into()).ok_or(ToFieldError::NotInField)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::aliases::U256;
use ark_bn254_04::{Fq, FqConfig, Fr, FrConfig};
use ark_ff_04::MontConfig;
use proptest::proptest;

macro_rules! test_roundtrip {
($ark:ty, $bits:expr, $limbs:expr) => {
proptest!(|(value: Uint<$bits, $limbs>)| {
let ark: $ark = value.into();
let back: Uint<$bits, $limbs> = ark.into();
assert_eq!(back, value);
});
}
}

#[test]
fn test_roundtrip() {
use ark_ff_04::*;
test_roundtrip!(BigInteger64, 64, 1);
test_roundtrip!(BigInteger128, 128, 2);
test_roundtrip!(BigInteger256, 256, 4);
test_roundtrip!(BigInteger320, 320, 5);
test_roundtrip!(BigInteger384, 384, 6);
test_roundtrip!(BigInteger448, 448, 7);
test_roundtrip!(BigInteger768, 768, 12);
test_roundtrip!(BigInteger832, 832, 13);
}

#[test]
fn test_fq_roundtrip() {
let modulus: U256 = FqConfig::MODULUS.into();
proptest!(|(value: U256)| {
let value: U256 = value % modulus;
let f: Fq = value.try_into().unwrap();
let back: U256 = f.into();
assert_eq!(back, value);
});
}

#[test]
fn test_fr_roundtrip() {
let modulus: U256 = FrConfig::MODULUS.into();
proptest!(|(value: U256)| {
let value: U256 = value % modulus;
let f: Fr = value.try_into().unwrap();
let back: U256 = f.into();
assert_eq!(back, value);
});
}
}
1 change: 1 addition & 0 deletions src/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod alloy_rlp;
mod arbitrary;
mod ark_ff;
mod ark_ff_04;
mod bn_rs;
mod fastrlp;
mod num_bigint;
Expand Down