Skip to content

Commit

Permalink
Finish porting code
Browse files Browse the repository at this point in the history
  • Loading branch information
davxy committed Jul 8, 2024
1 parent 5d4d169 commit e742d26
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 79 deletions.
8 changes: 4 additions & 4 deletions src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ impl<S: Suite> Codec<S> for Sec1Codec
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: SWCurveConfig,
AffinePoint<S>: utils::IntoSW<CurveConfig<S>> + utils::FromSW<CurveConfig<S>>,
AffinePoint<S>: utils::SWMapping<CurveConfig<S>>,
{
const BIG_ENDIAN: bool = true;

/// Encode point according to Section 2.3.3 "SEC 1: Elliptic Curve Cryptography",
/// (https://www.secg.org/sec1-v2.pdf) with point compression on.
fn point_encode(pt: &AffinePoint<S>, buf: &mut Vec<u8>) {
use ark_ff::biginteger::BigInteger;
let mut tmp = Vec::new();
use utils::IntoSW;
use utils::SWMapping;

if pt.is_zero() {
buf.push(0x00);
return;
}
let mut tmp = Vec::new();
let sw = pt.into_sw();

let is_odd = sw.y.into_bigint().is_odd();
Expand All @@ -78,7 +78,7 @@ where
/// (https://www.secg.org/sec1-v2.pdf) with point compression on.
fn point_decode(buf: &[u8]) -> AffinePoint<S> {
use ark_ff::biginteger::BigInteger;
use utils::FromSW;
use utils::SWMapping;
type SWAffine<C> = ark_ec::short_weierstrass::Affine<C>;
if buf.len() == 1 && buf[0] == 0x00 {
return AffinePoint::<S>::zero();
Expand Down
13 changes: 7 additions & 6 deletions src/ring.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::utils::SWMapping;
use crate::*;
use ark_ec::short_weierstrass::SWCurveConfig;
use pedersen::{PedersenSuite, Proof as PedersenProof};
Expand Down Expand Up @@ -118,7 +119,7 @@ impl<S: RingSuite> Verifier<S> for Public<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: SWCurveConfig,
AffinePoint<S>: IntoSW<CurveConfig<S>>,
AffinePoint<S>: SWMapping<CurveConfig<S>>,
{
fn verify(
input: Input<S>,
Expand Down Expand Up @@ -156,7 +157,7 @@ impl<S: RingSuite> RingContext<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: SWCurveConfig + Clone,
AffinePoint<S>: IntoSW<CurveConfig<S>>,
AffinePoint<S>: SWMapping<CurveConfig<S>>,
{
/// Construct a new ring context suitable to manage the given ring size.
pub fn from_seed(ring_size: usize, seed: [u8; 32]) -> Self {
Expand Down Expand Up @@ -232,7 +233,7 @@ impl<S: RingSuite> CanonicalSerialize for RingContext<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: SWCurveConfig + Clone,
AffinePoint<S>: IntoSW<CurveConfig<S>>,
AffinePoint<S>: SWMapping<CurveConfig<S>>,
{
fn serialize_with_mode<W: ark_serialize::Write>(
&self,
Expand All @@ -252,7 +253,7 @@ impl<S: RingSuite> CanonicalDeserialize for RingContext<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: SWCurveConfig + Clone,
AffinePoint<S>: IntoSW<CurveConfig<S>>,
AffinePoint<S>: SWMapping<CurveConfig<S>>,
{
fn deserialize_with_mode<R: ark_serialize::Read>(
mut reader: R,
Expand All @@ -277,7 +278,7 @@ impl<S: RingSuite> ark_serialize::Valid for RingContext<S>
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: SWCurveConfig + Clone,
AffinePoint<S>: IntoSW<CurveConfig<S>>,
AffinePoint<S>: SWMapping<CurveConfig<S>>,
{
fn check(&self) -> Result<(), ark_serialize::SerializationError> {
self.pcs_params.check()
Expand All @@ -288,7 +289,7 @@ pub(crate) fn make_piop_params<S: RingSuite>(domain_size: usize) -> PiopParams<S
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: SWCurveConfig,
AffinePoint<S>: IntoSW<CurveConfig<S>>,
AffinePoint<S>: SWMapping<CurveConfig<S>>,
{
let domain = ring_proof::Domain::new(domain_size, true);
PiopParams::<S>::setup(
Expand Down
50 changes: 0 additions & 50 deletions src/suites/secp256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,56 +74,6 @@ impl Suite for P256Sha256Tai {
fn data_to_point(data: &[u8]) -> Option<AffinePoint> {
utils::hash_to_curve_tai_rfc_9381::<Self>(data, true)
}

// /// Encode point according to Section 2.3.3 "SEC 1: Elliptic Curve Cryptography",
// /// (https://www.secg.org/sec1-v2.pdf) with point compression on.
// fn point_encode(pt: &AffinePoint, buf: &mut Vec<u8>) {
// use ark_ff::biginteger::BigInteger;
// let mut tmp = Vec::new();

// if pt.is_zero() {
// buf.push(0x00);
// return;
// }
// let is_odd = pt.y.into_bigint().is_odd();
// buf.push(if is_odd { 0x03 } else { 0x02 });

// pt.x.serialize_compressed(&mut tmp).unwrap();
// tmp.reverse();
// buf.extend_from_slice(&tmp[..]);
// }

// /// Encode point according to Section 2.3.3 "SEC 1: Elliptic Curve Cryptography",
// /// (https://www.secg.org/sec1-v2.pdf) with point compression on.
// fn point_decode(buf: &[u8]) -> AffinePoint {
// use ark_ff::biginteger::BigInteger;
// if buf.len() == 1 && buf[0] == 0x00 {
// return AffinePoint::zero();
// }
// let mut tmp = buf.to_vec();
// tmp.reverse();
// let y_flag = tmp.pop().unwrap();

// let x = BaseField::deserialize_compressed(&mut &tmp[..]).unwrap();
// let (y1, y2) = AffinePoint::get_ys_from_x_unchecked(x).unwrap();
// let y = if ((y_flag & 0x01) != 0) == y1.into_bigint().is_odd() {
// y1
// } else {
// y2
// };
// AffinePoint::new_unchecked(x, y)
// }

// fn scalar_encode(sc: &ScalarField, buf: &mut Vec<u8>) {
// let mut tmp = Vec::new();
// sc.serialize_compressed(&mut tmp).unwrap();
// tmp.reverse();
// buf.extend_from_slice(&tmp[..]);
// }

// fn scalar_decode(buf: &[u8]) -> ScalarField {
// ScalarField::from_be_bytes_mod_order(buf)
// }
}

impl PedersenSuite for P256Sha256Tai {
Expand Down
6 changes: 3 additions & 3 deletions src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn ring_prove_verify<S: ring::RingSuite>()
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: ark_ec::short_weierstrass::SWCurveConfig + Clone,
AffinePoint<S>: ring::IntoSW<CurveConfig<S>>,
AffinePoint<S>: utils::SWMapping<CurveConfig<S>>,
{
use ring::{Prover, RingContext, Verifier};

Expand Down Expand Up @@ -105,9 +105,9 @@ pub fn check_complement_point<S: ring::RingSuite>()
where
BaseField<S>: ark_ff::PrimeField,
CurveConfig<S>: ark_ec::short_weierstrass::SWCurveConfig + Clone,
AffinePoint<S>: ring::IntoSW<CurveConfig<S>>,
AffinePoint<S>: utils::SWMapping<CurveConfig<S>>,
{
use ring::IntoSW;
use utils::SWMapping;
let pt = S::COMPLEMENT_POINT.into_sw();
assert!(pt.is_on_curve());
assert!(!pt.is_in_correct_subgroup_assuming_on_curve());
Expand Down
35 changes: 19 additions & 16 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,35 +303,38 @@ pub(crate) mod ark_next {
}
}

pub trait IntoSW<C: ark_ec::short_weierstrass::SWCurveConfig> {
pub trait SWMapping<C: ark_ec::short_weierstrass::SWCurveConfig> {
fn from_sw(sw: ark_ec::short_weierstrass::Affine<C>) -> Self;
fn into_sw(self) -> ark_ec::short_weierstrass::Affine<C>;
}

impl<C: ark_ec::short_weierstrass::SWCurveConfig> IntoSW<C>
impl<C: ark_ec::short_weierstrass::SWCurveConfig> SWMapping<C>
for ark_ec::short_weierstrass::Affine<C>
{
#[inline(always)]
fn from_sw(sw: ark_ec::short_weierstrass::Affine<C>) -> Self {
sw
}

#[inline(always)]
fn into_sw(self) -> ark_ec::short_weierstrass::Affine<C> {
self
}
}

impl<C: ark_next::MapConfig> IntoSW<C> for ark_ec::twisted_edwards::Affine<C> {
fn into_sw(self) -> ark_ec::short_weierstrass::Affine<C> {
impl<C: ark_next::MapConfig> SWMapping<C> for ark_ec::twisted_edwards::Affine<C> {
#[inline(always)]
fn from_sw(sw: ark_ec::short_weierstrass::Affine<C>) -> Self {
const ERR_MSG: &str =
"'IntoSW' is expected to be implemented only for curves supporting the mapping";
ark_next::map_te_to_sw(&self).expect(ERR_MSG)
"SW to TE is expected to be implemented only for curves supporting the mapping";
ark_next::map_sw_to_te(&sw).expect(ERR_MSG)
}
}

pub trait FromSW<C: ark_ec::short_weierstrass::SWCurveConfig> {
fn from_sw(sw: ark_ec::short_weierstrass::Affine<C>) -> Self;
}

impl<C: ark_ec::short_weierstrass::SWCurveConfig> FromSW<C>
for ark_ec::short_weierstrass::Affine<C>
{
fn from_sw(sw: ark_ec::short_weierstrass::Affine<C>) -> Self {
sw
#[inline(always)]
fn into_sw(self) -> ark_ec::short_weierstrass::Affine<C> {
const ERR_MSG: &str =
"TE to SW is expected to be implemented only for curves supporting the mapping";
ark_next::map_te_to_sw(&self).expect(ERR_MSG)
}
}

Expand Down

0 comments on commit e742d26

Please sign in to comment.