Skip to content

Commit

Permalink
SDK: Factor out pubkey on-curve test to a helper
Browse files Browse the repository at this point in the history
  • Loading branch information
t-nelson committed Apr 29, 2021
1 parent a7070a5 commit cfc1cb1
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions sdk/program/src/pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ impl TryFrom<&str> for Pubkey {
}
}

pub fn bytes_are_curve_point<T: AsRef<[u8]>>(_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(
Expand Down Expand Up @@ -200,10 +211,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);
}

Expand Down Expand Up @@ -324,6 +332,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")]
Expand Down

0 comments on commit cfc1cb1

Please sign in to comment.