Skip to content

Commit

Permalink
chore: remove old h2c and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Feb 25, 2024
1 parent ff07532 commit 61e7311
Showing 1 changed file with 10 additions and 65 deletions.
75 changes: 10 additions & 65 deletions moksha-core/src/dhke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,6 @@ impl Dhke {
}
}

fn get_hash(message: &[u8]) -> Vec<u8> {
let hash = sha256::Hash::hash(message);
hash.as_byte_array().to_vec()
}

/// Generates a point from the message hash and checks if the point lies on the curve.
/// If it does not, iteratively tries to compute a new point from the hash.
fn hash_to_curve(message: &[u8]) -> PublicKey {
let mut point: Option<PublicKey> = None;
let mut msg_to_hash = message.to_vec();
while point.is_none() {
let hash = Self::get_hash(&msg_to_hash);
let input = &once(&0x02).chain(hash.iter()).cloned().collect::<Vec<u8>>();
PublicKey::from_slice(input).map_or_else(|_| msg_to_hash = hash, |p| point = Some(p))
}
point.unwrap()
}

/// The point is generated by hashing the message with a domain separator and then
/// iteratively trying to compute a point from the hash. An increasing uint32 counter
/// (byte order little endian) is appended to the hash until a point is found that lies on the curve.
Expand All @@ -91,7 +73,7 @@ impl Dhke {
/// The domain separator is b"Secp256k1_HashToCurve_Cashu_" or
/// bytes.fromhex("536563703235366b315f48617368546f43757276655f43617368755f").
fn hash_to_curve_domain_separated(message: &[u8]) -> Result<PublicKey, MokshaCoreError> {
fn hash_to_curve(message: &[u8]) -> Result<PublicKey, MokshaCoreError> {
let msg_to_hash = sha256::Hash::hash(&[b"Secp256k1_HashToCurve_Cashu_", message].concat());
(0..2u32.pow(16))
.map(|counter| sha256::Hash::hash(&[&msg_to_hash[..], &counter.to_le_bytes()].concat()))
Expand All @@ -114,7 +96,7 @@ impl Dhke {
) -> Result<(PublicKey, SecretKey), MokshaCoreError> {
let mut rng = rand::thread_rng();

let y = Self::hash_to_curve(secret_msg.into().as_bytes());
let y = Self::hash_to_curve(secret_msg.into().as_bytes())?;
let secret_key = match blinding_factor {
Some(f) => SecretKey::from_slice(f)?,
None => SecretKey::new(&mut rng),
Expand Down Expand Up @@ -148,7 +130,7 @@ impl Dhke {
c: PublicKey,
secret_msg: impl Into<String>,
) -> Result<bool, MokshaCoreError> {
let y = Self::hash_to_curve(secret_msg.into().as_bytes());
let y = Self::hash_to_curve(secret_msg.into().as_bytes())?;
Some(c == y.mul_tweak(&self.secp, &Scalar::from(a))?).ok_or(
MokshaCoreError::Secp256k1Error(secp256k1::Error::InvalidPublicKey),
)
Expand Down Expand Up @@ -183,31 +165,20 @@ mod tests {
fn test_hash_to_curve_zero() -> anyhow::Result<()> {
let input_str =
hex_to_string("0000000000000000000000000000000000000000000000000000000000000000");
let expected_result = "0266687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925";
let expected_result = "024cce997d3b518f739663b757deaec95bcd9473c30a14ac2fd04023a739d1a725";

let pk = Dhke::hash_to_curve(input_str.as_bytes()).to_string();
let pk = Dhke::hash_to_curve(input_str.as_bytes())?.to_string();
assert_eq!(pk, expected_result);
Ok(())
}

#[test]
fn test_hash_to_curve_zero_one() -> anyhow::Result<()> {
fn test_hash_to_curve_one() -> anyhow::Result<()> {
let input_str =
hex_to_string("0000000000000000000000000000000000000000000000000000000000000001");
let expected_result = "02ec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5";

let pk = Dhke::hash_to_curve(input_str.as_bytes()).to_string();
assert_eq!(pk, expected_result);
Ok(())
}

#[test]
fn test_hash_to_curve_iterate() -> anyhow::Result<()> {
let input_str =
hex_to_string("0000000000000000000000000000000000000000000000000000000000000002");
let expected_result = "02076c988b353fcbb748178ecb286bc9d0b4acf474d4ba31ba62334e46c97c416a";
let expected_result = "022e7158e11c9506f1aa4248bf531298daa7febd6194f003edcd9b93ade6253acf";

let pk = Dhke::hash_to_curve(input_str.as_bytes()).to_string();
let pk = Dhke::hash_to_curve(input_str.as_bytes())?.to_string();
assert_eq!(pk, expected_result);
Ok(())
}
Expand All @@ -223,7 +194,7 @@ mod tests {

assert_eq!(
pub_key_str,
"02a9acc1e48c25eeeb9289b5031cc57da9fe72f3fe2861d264bdc074209b107ba2"
"025cc16fe33b953e2ace39653efb3e7a7049711ae1d8a2f7a9108753f1cdea742b"
);

assert_eq!(
Expand All @@ -245,7 +216,7 @@ mod tests {
let c = dhke.step2_bob(pub_key, &a)?;
let c_str = c.to_string();
assert_eq!(
"02a9acc1e48c25eeeb9289b5031cc57da9fe72f3fe2861d264bdc074209b107ba2".to_string(),
"025cc16fe33b953e2ace39653efb3e7a7049711ae1d8a2f7a9108753f1cdea742b".to_string(),
c_str
);

Expand Down Expand Up @@ -309,30 +280,4 @@ mod tests {

Ok(())
}

#[test]
fn test_hash_to_curve_domain_separated_zero() -> anyhow::Result<()> {
let input_str =
hex_to_string("0000000000000000000000000000000000000000000000000000000000000000");
let expected_result = "024cce997d3b518f739663b757deaec95bcd9473c30a14ac2fd04023a739d1a725";

let pk = Dhke::hash_to_curve_domain_separated(input_str.as_bytes())
.unwrap()
.to_string();
assert_eq!(pk, expected_result);
Ok(())
}

#[test]
fn test_hash_to_curve_domain_separated_one() -> anyhow::Result<()> {
let input_str =
hex_to_string("0000000000000000000000000000000000000000000000000000000000000001");
let expected_result = "022e7158e11c9506f1aa4248bf531298daa7febd6194f003edcd9b93ade6253acf";

let pk = Dhke::hash_to_curve_domain_separated(input_str.as_bytes())
.unwrap()
.to_string();
assert_eq!(pk, expected_result);
Ok(())
}
}

0 comments on commit 61e7311

Please sign in to comment.