defuse_crypto/curve/
ed25519.rs

1use ed25519_dalek::VerifyingKey;
2use near_sdk::env;
3
4use super::{Curve, CurveType, TypedCurve};
5
6pub struct Ed25519;
7
8impl Curve for Ed25519 {
9    type PublicKey = [u8; 32];
10    type Signature = [u8; 64];
11
12    type Message = [u8];
13    type VerifyingKey = Self::PublicKey;
14
15    #[inline]
16    fn verify(
17        signature: &Self::Signature,
18        message: &Self::Message,
19        public_key: &Self::VerifyingKey,
20    ) -> Option<Self::PublicKey> {
21        if VerifyingKey::from_bytes(public_key).ok()?.is_weak() {
22            // prevent using weak (i.e. low order) public keys, see
23            // https://github.com/dalek-cryptography/ed25519-dalek#weak-key-forgery-and-verify_strict
24            return None;
25        }
26
27        env::ed25519_verify(signature, message, public_key)
28            .then_some(public_key)
29            .copied()
30    }
31}
32
33impl TypedCurve for Ed25519 {
34    const CURVE_TYPE: CurveType = CurveType::Ed25519;
35}