Skip to content

Commit

Permalink
basic update of _try_and_increment
Browse files Browse the repository at this point in the history
  • Loading branch information
skaunov committed Sep 30, 2023
1 parent 5297908 commit cad5744
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions rust-arkworks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ark-ec = "~0.3.0"
ark-ec = "~0.4.0"
ark-ff = "0.3.0"
ark-std = "0.3.0"
ark-serialize = "0.3.0"
ark-serialize = "~0.4.0"
ark-serialize-derive = "0.3.0"
thiserror = "1.0.30"
secp256k1 = { git = "https://github.com/geometryresearch/ark-secp256k1.git" }
Expand Down
2 changes: 1 addition & 1 deletion rust-arkworks/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use thiserror::Error;

/// This is an error that could occur when running a cryptograhic primitive
#[derive(Error, Debug, PartialEq)]
pub enum CryptoError {
pub enum EcError {
#[error("Cannot hash to curve")]
CannotHashToCurve,

Expand Down
18 changes: 10 additions & 8 deletions rust-arkworks/src/hash_to_curve.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use crate::error::CryptoError;
use ark_ec::{AffineCurve, ProjectiveCurve};
use crate::error::EcError;
// use ark_ec::{AffineCurve, ProjectiveCurve};
use ark_ec::{AffineRepr, CurveGroup};
use tiny_keccak::{Hasher, Shake, Xof};
use elliptic_curve::hash2curve::{ExpandMsgXmd, GroupDigest};
use k256::AffinePoint;
use k256::sha2::Sha256;
use elliptic_curve::sec1::ToEncodedPoint;
use ark_ec::short_weierstrass_jacobian::GroupAffine;
// use ark_ec::short_weierstrass_jacobian::GroupAffine;
use k256::{ProjectivePoint, Secp256k1};
use ark_ff::FromBytes;
use secp256k1::Sec1EncodePoint;

pub fn hash_to_curve<
Fp: ark_ff::PrimeField,
P: ark_ec::SWModelParameters,
// P: ark_ec::SWModelParameters,
>(
msg: &[u8],
pk: &GroupAffine<P>,
Expand All @@ -35,7 +36,7 @@ pub fn hash_to_curve<
}

pub fn k256_affine_to_arkworks_secp256k1_affine<
P: ark_ec::SWModelParameters
// P: ark_ec::SWModelParameters
>(
k_pt: AffinePoint,
) -> GroupAffine<P> {
Expand Down Expand Up @@ -70,12 +71,13 @@ pub fn k256_affine_to_arkworks_secp256k1_affine<
}

/// Kobi's hash_to_curve function, here for reference only
pub fn _try_and_increment<C: ProjectiveCurve>(msg: &[u8]) -> Result<C::Affine, CryptoError> {
pub fn _try_and_increment<C: CurveGroup>(msg: &[u8]) -> Result<C::Affine, EcError> {
for nonce in 0u8..=255 {
let mut h = Shake::v128();
h.update(&[nonce]);
h.update(msg.as_ref());
let output_size = C::zero().serialized_size();
let output_size = C::zero().serialized_size(ark_serialize::Compress::Yes);
// TODO try to replace with an array sized with a generic number
let mut output = vec![0u8; output_size];
h.squeeze(&mut output);

Expand All @@ -84,5 +86,5 @@ pub fn _try_and_increment<C: ProjectiveCurve>(msg: &[u8]) -> Result<C::Affine, C
}
}

Err(CryptoError::CannotHashToCurve)
Err(EcError::CannotHashToCurve)
}
20 changes: 10 additions & 10 deletions rust-arkworks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod hash_to_curve;
mod tests;

pub mod sig {
use crate::error::CryptoError;
use crate::error::EcError;
use crate::hash_to_curve;
use ark_ec::short_weierstrass_jacobian::GroupAffine;
use ark_ec::{models::SWModelParameters, AffineCurve, ProjectiveCurve};
Expand Down Expand Up @@ -42,7 +42,7 @@ pub mod sig {
fn compute_h<'a, C: ProjectiveCurve, Fq: PrimeField, P: SWModelParameters>(
pk: &GroupAffine<P>,
message: &'a [u8],
) -> Result<GroupAffine<P>, CryptoError> {
) -> Result<GroupAffine<P>, EcError> {
//let pk_affine_bytes_vec = affine_to_bytes::<P>(pk);
//let m_pk = [message, pk_affine_bytes_vec.as_slice()].concat();
//hash_to_curve::try_and_increment::<C>(m_pk.as_slice())
Expand Down Expand Up @@ -96,7 +96,7 @@ pub mod sig {
fn keygen<R: Rng>(
pp: &Self::Parameters,
rng: &mut R,
) -> Result<(Self::PublicKey, Self::SecretKey), CryptoError>;
) -> Result<(Self::PublicKey, Self::SecretKey), EcError>;

/// Sign a message.
fn sign<R: Rng>(
Expand All @@ -105,7 +105,7 @@ pub mod sig {
keypair: (&Self::PublicKey, &Self::SecretKey),
message: Self::Message,
version: PlumeVersion,
) -> Result<Self::Signature, CryptoError>;
) -> Result<Self::Signature, EcError>;

/// Sign a message using an specified r value
fn sign_with_r(
Expand All @@ -114,15 +114,15 @@ pub mod sig {
message: Self::Message,
r: Self::SecretKey,
version: PlumeVersion,
) -> Result<Self::Signature, CryptoError>;
) -> Result<Self::Signature, EcError>;

fn verify_non_zk(
pp: &Self::Parameters,
pk: &Self::PublicKey,
sig: &Self::Signature,
message: Self::Message,
version: PlumeVersion,
) -> Result<bool, CryptoError>;
) -> Result<bool, EcError>;
}

#[derive(
Expand Down Expand Up @@ -161,7 +161,7 @@ pub mod sig {
fn keygen<R: Rng>(
pp: &Self::Parameters,
rng: &mut R,
) -> Result<(Self::PublicKey, Self::SecretKey), CryptoError> {
) -> Result<(Self::PublicKey, Self::SecretKey), EcError> {
let secret_key = Self::SecretKey::rand(rng).into();
let public_key = pp.g.mul(secret_key).into();
Ok((public_key, secret_key))
Expand All @@ -173,7 +173,7 @@ pub mod sig {
message: Self::Message,
r: P::ScalarField,
version: PlumeVersion,
) -> Result<Self::Signature, CryptoError> {
) -> Result<Self::Signature, EcError> {
let g = pp.g;
let g_r = g.mul(r).into_affine();

Expand Down Expand Up @@ -214,7 +214,7 @@ pub mod sig {
keypair: (&Self::PublicKey, &Self::SecretKey),
message: Self::Message,
version: PlumeVersion,
) -> Result<Self::Signature, CryptoError> {
) -> Result<Self::Signature, EcError> {
// Pick a random r from Fp
let r: P::ScalarField = Self::SecretKey::rand(rng).into();

Expand All @@ -227,7 +227,7 @@ pub mod sig {
sig: &Self::Signature,
message: Self::Message,
version: PlumeVersion,
) -> Result<bool, CryptoError> {
) -> Result<bool, EcError> {
// Compute h = htc([m, pk])
let h = compute_h::<C, Fq, P>(pk, message).unwrap();

Expand Down

0 comments on commit cad5744

Please sign in to comment.