From 529035e4629fdf1de64d74a5b53aa5e0778b0a69 Mon Sep 17 00:00:00 2001 From: xevisalle <38231508+xevisalle@users.noreply.github.com> Date: Fri, 5 Apr 2024 14:49:56 +0200 Subject: [PATCH] Use Blake for stealth addresses (#147) --- CHANGELOG.md | 1 + src/keys.rs | 8 ++++++++ src/keys/public.rs | 4 ++-- src/keys/secret.rs | 4 ++-- src/keys/view.rs | 4 ++-- src/lib.rs | 4 ++-- src/permutation.rs | 14 -------------- 7 files changed, 17 insertions(+), 22 deletions(-) delete mode 100644 src/permutation.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 13aa39b..6b7c1f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Update bls12_381-bls -> 0.2 - Update jubjub-schnorr -> 0.2 +- Use Blake for computing the stealth addresses, instead of Poseidon. ## [0.25.0] - 2024-01-24 diff --git a/src/keys.rs b/src/keys.rs index 6be28d8..45c318c 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -4,7 +4,15 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. +use dusk_jubjub::{JubJubAffine, JubJubExtended, JubJubScalar}; + pub mod public; pub mod secret; pub mod stealth; pub mod view; + +/// Hashes a JubJub's ExtendedPoint into a JubJub's Scalar using the JubJub's +/// hash to scalar function +pub fn hash(p: &JubJubExtended) -> JubJubScalar { + JubJubScalar::hash_to_scalar(&JubJubAffine::from(p).to_bytes()) +} diff --git a/src/keys/public.rs b/src/keys/public.rs index 6b709f4..3f51d7d 100644 --- a/src/keys/public.rs +++ b/src/keys/public.rs @@ -4,7 +4,7 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use crate::{permutation, SecretKey, StealthAddress, ViewKey}; +use crate::{keys::hash, SecretKey, StealthAddress, ViewKey}; use dusk_jubjub::{JubJubAffine, JubJubExtended, JubJubScalar}; @@ -50,7 +50,7 @@ impl PublicKey { let R = G * r; let rA = self.A * r; - let rA = permutation::hash(&rA); + let rA = hash(&rA); let rA = G * rA; let pk_r = rA + self.B; diff --git a/src/keys/secret.rs b/src/keys/secret.rs index 1c367e9..0677b1a 100644 --- a/src/keys/secret.rs +++ b/src/keys/secret.rs @@ -4,7 +4,7 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use crate::{permutation, StealthAddress}; +use crate::{keys::hash, StealthAddress}; use dusk_jubjub::JubJubScalar; use ff::Field; use jubjub_schnorr::SecretKey as NoteSecretKey; @@ -58,7 +58,7 @@ impl SecretKey { /// With the formula: `sk_r = H(a ยท R) + b` pub fn sk_r(&self, sa: &StealthAddress) -> NoteSecretKey { let aR = sa.R() * self.a; - let aR = permutation::hash(&aR); + let aR = hash(&aR); (aR + self.b).into() } diff --git a/src/keys/view.rs b/src/keys/view.rs index d1b8e55..a3d4a5a 100644 --- a/src/keys/view.rs +++ b/src/keys/view.rs @@ -6,7 +6,7 @@ use crate::keys::stealth; -use crate::{permutation, SecretKey}; +use crate::{keys::hash, SecretKey}; use dusk_bytes::{DeserializableSlice, Error, Serializable}; use dusk_jubjub::{ @@ -69,7 +69,7 @@ impl ViewKey { let sa = owner.stealth_address(); let aR = sa.R() * self.a(); - let aR = permutation::hash(&aR); + let aR = hash(&aR); let aR = GENERATOR_EXTENDED * aR; let pk_r = aR + self.B(); diff --git a/src/lib.rs b/src/lib.rs index dd9ed92..8e3fe3d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,8 +29,8 @@ pub mod note; /// Phoenix Core Keys & Addresses mod keys; -mod permutation; - +/// Hash function +pub use keys::hash; /// Public (Spend) Key pub use keys::public::PublicKey; /// Secret (Spend) Key diff --git a/src/permutation.rs b/src/permutation.rs deleted file mode 100644 index 1a3edd1..0000000 --- a/src/permutation.rs +++ /dev/null @@ -1,14 +0,0 @@ -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. -// -// Copyright (c) DUSK NETWORK. All rights reserved. - -use dusk_jubjub::{JubJubExtended, JubJubScalar}; -use dusk_poseidon::sponge::truncated; - -/// Hashes a JubJub's ExtendedPoint into a JubJub's Scalar using the poseidon -/// hash function -pub fn hash(p: &JubJubExtended) -> JubJubScalar { - truncated::hash(&p.to_hash_inputs()) -}