Skip to content

Commit

Permalink
Remove Curve::bytes_to_curve_unchecked() corresponding implementation…
Browse files Browse the repository at this point in the history
…s and tests; rename to Cipher::from_bytes(); the method uses deserial() instead of Curve::bytes_to_curve_unchecked()
  • Loading branch information
annenkov committed Jan 12, 2024
1 parent cc8e78e commit b3f02d7
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 61 deletions.
2 changes: 2 additions & 0 deletions rust-src/concordium_base/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- Upgrade `ed25519-dalek` to `v2.0`.
- Bump the `rand` version to `v0.8`
- Add implementations of `Field`, `PrimeField` and `Curve` for the Ristretto representation of `curve25519`.
- Remove `Curve::bytes_to_curve_unchecked()`.
- Rename `Cipher::from_bytes_unchecked()` to `Cipher::from_bytes()`; the method uses `deserial()` instead of `Curve::bytes_to_curve_unchecked()`.
- Support `P7` protocol version.
- The `Debug` implementation for `ContractEvent` displays the value in `hex`.
The alternate formatter (using `#`) displays it as a list of bytes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//! `arkworks` field/curve traits.
use super::{Curve, CurveDecodingError, Field, GenericMultiExp, PrimeField};
use crate::common::{Deserial, Serial, Serialize};
use anyhow::anyhow;
use ark_ec::hashing::HashToCurve;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use core::fmt;
Expand Down Expand Up @@ -164,14 +163,6 @@ where

fn mul_by_scalar(&self, scalar: &Self::Scalar) -> Self { ArkGroup(self.0 * scalar.0) }

fn bytes_to_curve_unchecked<R: byteorder::ReadBytesExt>(b: &mut R) -> anyhow::Result<Self> {
// TODO: this is not the most efficient implementation, since there might be
// some additional checks during deserialization. However, it seems
// there are no unchecked methods available through traits.
let res = G::Affine::deserialize_compressed(b).map_err(|e| anyhow!(e))?;
Ok(ArkGroup(res.into()))
}

fn generate<R: rand::prelude::Rng>(rng: &mut R) -> Self { ArkGroup(G::rand(rng)) }

fn generate_scalar<R: rand::prelude::Rng>(rng: &mut R) -> Self::Scalar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,24 +262,6 @@ mod tests {
};
}

/// A macro for testing that serializing a point and converting it back
/// using `bytes_to_curve_unchecked` gives the same point.
macro_rules! macro_test_group_byte_conversion_unchecked {
($function_name:ident, $p:path) => {
#[test]
pub fn $function_name() {
let mut csprng = thread_rng();
for _ in 0..1000 {
let curve = <$p>::generate(&mut csprng);
let bytes = to_bytes(&curve);
let curve_res = <$p>::bytes_to_curve_unchecked(&mut Cursor::new(&bytes));
assert!(curve_res.is_ok());
assert_eq!(curve, curve_res.unwrap());
}
}
};
}

type G1 = ArkGroup<G1Projective>;
type G2 = ArkGroup<G2Projective>;

Expand All @@ -289,7 +271,4 @@ mod tests {

macro_test_group_byte_conversion!(curve_bytes_conv_g1, G1);
macro_test_group_byte_conversion!(curve_bytes_conv_g2, G2);

macro_test_group_byte_conversion_unchecked!(u_curve_bytes_conv_g1, G1);
macro_test_group_byte_conversion_unchecked!(u_curve_bytes_conv_g2, G2);
}
23 changes: 0 additions & 23 deletions rust-src/concordium_base/src/curve_arithmetic/ed25519_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,6 @@ impl Curve for RistrettoPoint {

fn mul_by_scalar(&self, scalar: &Self::Scalar) -> Self { self * scalar.0 }

fn bytes_to_curve_unchecked<R: byteorder::ReadBytesExt>(
source: &mut R,
) -> anyhow::Result<Self> {
let mut buf: [u8; 32] = [0; 32];
source.read_exact(&mut buf)?;
let res = CompressedRistretto::from_slice(&buf)?;
let point = res.decompress().ok_or(anyhow::anyhow!("Failed!"))?;
Ok(point)
}

fn generate<R: rand::Rng>(rng: &mut R) -> Self {
let mut uniform_bytes = [0u8; 64];
rng.fill_bytes(&mut uniform_bytes);
Expand Down Expand Up @@ -254,19 +244,6 @@ pub(crate) mod tests {
}
}

/// Turn curve points into representations and back again, and compare.
#[test]
fn test_point_byte_conversion_unchecked() {
let mut csprng = rand::thread_rng();
for _ in 0..1000 {
let point = RistrettoPoint::generate(&mut csprng);
let bytes = to_bytes(&point);
let point_res = RistrettoPoint::bytes_to_curve_unchecked(&mut Cursor::new(&bytes));
assert!(point_res.is_ok());
assert_eq!(point, point_res.unwrap());
}
}

/// Test that `into_repr()` correclty converts a scalar constructed from a
/// byte array to an array of limbs with least significant digits first.
#[test]
Expand Down
5 changes: 0 additions & 5 deletions rust-src/concordium_base/src/curve_arithmetic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub mod secret_value;
pub use secret_value::{Secret, Value};

use crate::common::{Serial, Serialize};
use byteorder::ReadBytesExt;
use rand::*;
use std::{borrow::Borrow, fmt, fmt::Debug};
use thiserror::Error;
Expand Down Expand Up @@ -136,10 +135,6 @@ pub trait Curve:
/// Exponentiation by a scalar, i.e., compute n * x for a group element x
/// and integer n.
fn mul_by_scalar(&self, scalar: &Self::Scalar) -> Self;
/// Deserialize a value from a byte source, but do not check that it is in
/// the group itself. This can be cheaper if the source of the value is
/// trusted, but it must not be used on untrusted sources.
fn bytes_to_curve_unchecked<R: ReadBytesExt>(b: &mut R) -> anyhow::Result<Self>;
/// Generate a random group element, uniformly distributed.
fn generate<R: Rng>(rng: &mut R) -> Self;
/// Generate a random scalar value, uniformly distributed.
Expand Down
6 changes: 3 additions & 3 deletions rust-src/concordium_base/src/elgamal/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ impl<C: Curve> Cipher<C> {
/// A `Result` whose okay value is a cipher key or whose error value
/// is an `ElgamalError` wrapping the internal error that occurred.
#[inline]
pub fn from_bytes_unchecked<R: ReadBytesExt>(bytes: &mut R) -> anyhow::Result<Cipher<C>> {
let g = C::bytes_to_curve_unchecked(bytes)?;
let h = C::bytes_to_curve_unchecked(bytes)?;
pub fn from_bytes<R: ReadBytesExt>(bytes: &mut R) -> anyhow::Result<Cipher<C>> {
let g = C::deserial(bytes)?;
let h = C::deserial(bytes)?;
Ok(Cipher(g, h))
}

Expand Down

0 comments on commit b3f02d7

Please sign in to comment.