From 7600be946a980080855970bcc38241d3cd493664 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 5 May 2021 06:19:19 +0000 Subject: [PATCH] SDK: Factor out pubkey on-curve test to a helper (#16935) (cherry picked from commit cfc1cb1aee0d7b5b4c02fc57f585f8af20be40f8) Co-authored-by: Trent Nelson --- sdk/program/src/pubkey.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/sdk/program/src/pubkey.rs b/sdk/program/src/pubkey.rs index ca7bd9c373..cc60a65f1b 100644 --- a/sdk/program/src/pubkey.rs +++ b/sdk/program/src/pubkey.rs @@ -108,6 +108,17 @@ impl TryFrom<&str> for Pubkey { } } +pub fn bytes_are_curve_point>(_bytes: T) -> bool { + #[cfg(not(target_arch = "bpf"))] + { + curve25519_dalek::edwards::CompressedEdwardsY::from_slice(_bytes.as_ref()) + .decompress() + .is_some() + } + #[cfg(target_arch = "bpf")] + unimplemented!(); +} + impl Pubkey { pub fn new(pubkey_vec: &[u8]) -> Self { Self( @@ -199,10 +210,7 @@ impl Pubkey { hasher.hashv(&[program_id.as_ref(), "ProgramDerivedAddress".as_ref()]); let hash = hasher.result(); - if curve25519_dalek::edwards::CompressedEdwardsY::from_slice(hash.as_ref()) - .decompress() - .is_some() - { + if bytes_are_curve_point(hash) { return Err(PubkeyError::InvalidSeeds); } @@ -323,6 +331,10 @@ impl Pubkey { self.0 } + pub fn is_on_curve(&self) -> bool { + bytes_are_curve_point(self) + } + /// Log a `Pubkey` from a program pub fn log(&self) { #[cfg(target_arch = "bpf")]