SDK: Factor out pubkey on-curve test to a helper (#16935)

(cherry picked from commit cfc1cb1aee)

Co-authored-by: Trent Nelson <trent@solana.com>
This commit is contained in:
mergify[bot]
2021-05-05 06:19:19 +00:00
committed by GitHub
parent 524b380a71
commit 7600be946a

View File

@ -108,6 +108,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 { impl Pubkey {
pub fn new(pubkey_vec: &[u8]) -> Self { pub fn new(pubkey_vec: &[u8]) -> Self {
Self( Self(
@ -199,10 +210,7 @@ impl Pubkey {
hasher.hashv(&[program_id.as_ref(), "ProgramDerivedAddress".as_ref()]); hasher.hashv(&[program_id.as_ref(), "ProgramDerivedAddress".as_ref()]);
let hash = hasher.result(); let hash = hasher.result();
if curve25519_dalek::edwards::CompressedEdwardsY::from_slice(hash.as_ref()) if bytes_are_curve_point(hash) {
.decompress()
.is_some()
{
return Err(PubkeyError::InvalidSeeds); return Err(PubkeyError::InvalidSeeds);
} }
@ -323,6 +331,10 @@ impl Pubkey {
self.0 self.0
} }
pub fn is_on_curve(&self) -> bool {
bytes_are_curve_point(self)
}
/// Log a `Pubkey` from a program /// Log a `Pubkey` from a program
pub fn log(&self) { pub fn log(&self) {
#[cfg(target_arch = "bpf")] #[cfg(target_arch = "bpf")]