Skip to content

Commit

Permalink
Extend codec api
Browse files Browse the repository at this point in the history
  • Loading branch information
davxy committed Feb 27, 2025
1 parent 40d7485 commit 8ec708f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
36 changes: 23 additions & 13 deletions src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,28 @@ use super::*;
pub trait Codec<S: Suite> {
const BIG_ENDIAN: bool;

/// Point encode into the given buffer.
fn point_encode_into(pt: &AffinePoint<S>, buf: &mut Vec<u8>);

/// Point encode.
fn point_encode(pt: &AffinePoint<S>, buf: &mut Vec<u8>);
fn point_encode(pt: &AffinePoint<S>) -> Vec<u8> {
let mut buf = Vec::new();
Self::point_encode_into(pt, &mut buf);
buf
}

/// Point decode.
fn point_decode(buf: &[u8]) -> Result<AffinePoint<S>, Error>;

/// Scalar encode
fn scalar_encode(sc: &ScalarField<S>, buf: &mut Vec<u8>);
/// Scalar encode into the given buffer.
fn scalar_encode_into(sc: &ScalarField<S>, buf: &mut Vec<u8>);

/// Scalar encode.
fn scalar_encode(sc: &ScalarField<S>) -> Vec<u8> {
let mut buf = Vec::new();
Self::scalar_encode_into(sc, &mut buf);
buf
}

/// Scalar decode.
fn scalar_decode(buf: &[u8]) -> ScalarField<S>;
Expand All @@ -28,15 +42,15 @@ pub struct ArkworksCodec;
impl<S: Suite> Codec<S> for ArkworksCodec {
const BIG_ENDIAN: bool = false;

fn point_encode(pt: &AffinePoint<S>, buf: &mut Vec<u8>) {
fn point_encode_into(pt: &AffinePoint<S>, buf: &mut Vec<u8>) {
pt.serialize_compressed(buf).unwrap();
}

fn point_decode(buf: &[u8]) -> Result<AffinePoint<S>, Error> {
AffinePoint::<S>::deserialize_compressed_unchecked(buf).map_err(Into::into)
}

fn scalar_encode(sc: &ScalarField<S>, buf: &mut Vec<u8>) {
fn scalar_encode_into(sc: &ScalarField<S>, buf: &mut Vec<u8>) {
sc.serialize_compressed(buf).unwrap();
}

Expand All @@ -58,7 +72,7 @@ where
{
const BIG_ENDIAN: bool = true;

fn point_encode(pt: &AffinePoint<S>, buf: &mut Vec<u8>) {
fn point_encode_into(pt: &AffinePoint<S>, buf: &mut Vec<u8>) {
use ark_ff::biginteger::BigInteger;
use te_sw_map::SWMapping;

Expand Down Expand Up @@ -101,7 +115,7 @@ where
Ok(AffinePoint::<S>::from_sw(sw))
}

fn scalar_encode(sc: &ScalarField<S>, buf: &mut Vec<u8>) {
fn scalar_encode_into(sc: &ScalarField<S>, buf: &mut Vec<u8>) {
let mut tmp = Vec::new();
sc.serialize_compressed(&mut tmp).unwrap();
tmp.reverse();
Expand All @@ -115,9 +129,7 @@ where

/// Point encoder wrapper using `Suite::Codec`.
pub fn point_encode<S: Suite>(pt: &AffinePoint<S>) -> Vec<u8> {
let mut buf = Vec::new();
S::Codec::point_encode(pt, &mut buf);
buf
S::Codec::point_encode(pt)
}

/// Point decoder wrapper using `Suite::Codec`.
Expand All @@ -127,9 +139,7 @@ pub fn point_decode<S: Suite>(buf: &[u8]) -> Result<AffinePoint<S>, Error> {

/// Scalar encoder wrapper using `Suite::Codec`.
pub fn scalar_encode<S: Suite>(sc: &ScalarField<S>) -> Vec<u8> {
let mut buf = Vec::new();
S::Codec::scalar_encode(sc, &mut buf);
buf
S::Codec::scalar_encode(sc)
}

/// Scalar decoder wrapper using `Suite::Codec`.
Expand Down
6 changes: 3 additions & 3 deletions src/ietf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<S: IetfSuite> CanonicalDeserialize for Proof<S> {
if reader.read_exact(&mut c_buf[..]).is_err() {
return Err(ark_serialize::SerializationError::InvalidData);
}
let c = codec::scalar_decode::<S>(&c_buf);
let c = S::Codec::scalar_decode(&c_buf);
let s = <ScalarField<S> as CanonicalDeserialize>::deserialize_with_mode(
&mut reader,
compress,
Expand Down Expand Up @@ -192,8 +192,8 @@ pub mod testing {

fn from_map(map: &common::TestVectorMap) -> Self {
let base = common::TestVector::from_map(map);
let c = codec::scalar_decode::<S>(&map.get_bytes("proof_c"));
let s = codec::scalar_decode::<S>(&map.get_bytes("proof_s"));
let c = S::Codec::scalar_decode(&map.get_bytes("proof_c"));
let s = S::Codec::scalar_decode(&map.get_bytes("proof_s"));
Self { base, c, s }
}

Expand Down
12 changes: 6 additions & 6 deletions src/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub trait PedersenSuite: IetfSuite {
const DOM_SEP_START: u8 = 0xCC;
const DOM_SEP_END: u8 = 0x00;
let mut buf = [Self::SUITE_ID, &[DOM_SEP_START]].concat();
Self::Codec::scalar_encode(secret, &mut buf);
Self::Codec::point_encode(input, &mut buf);
Self::Codec::scalar_encode_into(secret, &mut buf);
Self::Codec::point_encode_into(input, &mut buf);
buf.extend_from_slice(ad);
buf.push(DOM_SEP_END);
let hash = &utils::hash::<Self::Hasher>(&buf);
Expand Down Expand Up @@ -224,12 +224,12 @@ pub(crate) mod testing {

fn from_map(map: &common::TestVectorMap) -> Self {
let base = common::TestVector::from_map(map);
let blind = codec::scalar_decode::<S>(&map.get_bytes("blinding"));
let pk_com = codec::point_decode::<S>(&map.get_bytes("proof_pk_com")).unwrap();
let blind = S::Codec::scalar_decode(&map.get_bytes("blinding"));
let pk_com = S::Codec::point_decode(&map.get_bytes("proof_pk_com")).unwrap();
let r = codec::point_decode::<S>(&map.get_bytes("proof_r")).unwrap();
let ok = codec::point_decode::<S>(&map.get_bytes("proof_ok")).unwrap();
let s = codec::scalar_decode::<S>(&map.get_bytes("proof_s"));
let sb = codec::scalar_decode::<S>(&map.get_bytes("proof_sb"));
let s = S::Codec::scalar_decode(&map.get_bytes("proof_s"));
let sb = S::Codec::scalar_decode(&map.get_bytes("proof_sb"));
let proof = Proof {
pk_com,
r,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub fn challenge_rfc_9381<S: Suite>(pts: &[&AffinePoint<S>], ad: &[u8]) -> Scala
const DOM_SEP_END: u8 = 0x00;
let mut buf = [S::SUITE_ID, &[DOM_SEP_START]].concat();
pts.iter().for_each(|p| {
S::Codec::point_encode(p, &mut buf);
S::Codec::point_encode_into(p, &mut buf);
});
buf.extend_from_slice(ad);
buf.push(DOM_SEP_END);
Expand All @@ -133,7 +133,7 @@ pub fn point_to_hash_rfc_9381<S: Suite>(pt: &AffinePoint<S>) -> HashOutput<S> {
const DOM_SEP_START: u8 = 0x03;
const DOM_SEP_END: u8 = 0x00;
let mut buf = [S::SUITE_ID, &[DOM_SEP_START]].concat();
S::Codec::point_encode(pt, &mut buf);
S::Codec::point_encode_into(pt, &mut buf);
buf.push(DOM_SEP_END);
hash::<S::Hasher>(&buf)
}
Expand Down

0 comments on commit 8ec708f

Please sign in to comment.