CoolrfulDARkness/moontlcp/crypto/sm2 does not have a README file

    Sm2Error

    pub suberror Sm2Error {
    InvalidPrivateKey
    InvalidPublicKey
    InvalidSignature
    InvalidCiphertext
    DecryptionFailed
    KeyExchangeFailed
    InvalidLength(Int)
    } derive(Eq,
    Debug
    )

    KeyExchangeResult

    pub struct KeyExchangeResult {
    shared_key : Bytes
    s1 : Bytes
    s2 : Bytes
    } derive(Eq,
    Debug
    )

    PrivateKey

    pub struct PrivateKey {
    // private fields
    }

    impl Eq for PrivateKey

    PrivateKey::from_bytes

    fn PrivateKey::from_bytes(bytes : Bytes) -> PrivateKey raise Sm2Error

    Constructs a validated SM2 private key from a 32-byte big-endian scalar. Enforces 1 <= d <= n - 2 per GB/T 32918.1.

    PrivateKey::public_key

    fn PrivateKey::public_key(self : PrivateKey) -> PublicKey

    Returns the corresponding SM2 public key P = d * G.

    PrivateKey::to_bytes

    fn PrivateKey::to_bytes(self : PrivateKey) -> Bytes

    Serializes the private key scalar to 32-byte big-endian Bytes.

    PublicKey

    pub struct PublicKey {
    // private fields
    } derive(
    Debug
    )

    impl Eq for PublicKey

    PublicKey::from_bytes

    fn PublicKey::from_bytes(bytes : Bytes) -> PublicKey raise Sm2Error

    Parses and strictly validates an SM2 public key from:
    • 65-byte uncompressed SEC1 format 0x04 || X || Y
    • 64-byte raw coordinates X || Y
    • 33-byte compressed SEC1 format 0x02/0x03 || X Rejects non-canonical coordinates (>= p), point at infinity, and off-curve points.

    PublicKey::is_valid

    fn PublicKey::is_valid(self : PublicKey) -> Bool

    Validates that the public key point is on the SM2 curve with canonical coordinates.

    PublicKey::to_compressed_bytes

    fn PublicKey::to_compressed_bytes(self : PublicKey) -> Bytes

    Encodes the public key as 33-byte compressed SEC1 format 0x02/0x03 || X.

    PublicKey::to_uncompressed_bytes

    fn PublicKey::to_uncompressed_bytes(self : PublicKey) -> Bytes

    Encodes the public key as 65-byte uncompressed SEC1 format 0x04 || X || Y.

    PublicKey::to_xy_bytes

    fn PublicKey::to_xy_bytes(self : PublicKey) -> Bytes

    Encodes the public key as 64-byte raw X || Y.

    PublicKey::x_bytes

    fn PublicKey::x_bytes(self : PublicKey) -> Bytes

    Returns the 32-byte big-endian X coordinate.

    PublicKey::y_bytes

    fn PublicKey::y_bytes(self : PublicKey) -> Bytes

    Returns the 32-byte big-endian Y coordinate.

    DEFAULT_SM2_ID

    let DEFAULT_SM2_ID : Bytes

    compute_digest

    fn compute_digest(id : Bytes, pubkey : PublicKey, msg : Bytes) -> Bytes

    Computes the SM2 message digest e = SM3(Z_A || msg).

    compute_za

    fn compute_za(id : Bytes, pubkey : PublicKey) -> Bytes

    Computes the user identity hash Z_A = SM3(ENTL_A || ID_A || a || b || x_G || y_G || x_A || y_A).

    decode_sig_der

    fn decode_sig_der(der : Bytes) -> (Bytes, Bytes) raise Sm2Error

    Strictly decodes an ASN.1 DER SM2 signature into 32-byte big-endian (r, s).

    decrypt_asn1

    fn decrypt_asn1(priv_key : PrivateKey, der_cipher : Bytes) -> Bytes raise Sm2Error

    Strictly decodes and decrypts an ASN.1 DER SM2Cipher. Verifies C1 on curve and validates C3 in constant time using @ct.ct_eq.

    decrypt_c1c3c2

    fn decrypt_c1c3c2(priv_key : PrivateKey, ciphertext : Bytes) -> Bytes raise Sm2Error

    Decrypts a C1C3C2 SM2 ciphertext (accepts both 0x04-prefixed and unprefixed C1). Validates C1 on curve and verifies C3 in constant time using @ct.ct_eq.

    derive_keypair_from_seed

    fn derive_keypair_from_seed(seed : Bytes) -> PrivateKey raise Sm2Error

    Deterministically derives an SM2 private key from seed using SM3, guaranteed to lie in [1, n - 2].

    encode_sig_der

    fn encode_sig_der(r : Bytes, s : Bytes) -> Bytes

    Encodes (r, s) as an ASN.1 DER SEQUENCE { INTEGER r, INTEGER s }.

    encrypt_asn1

    fn encrypt_asn1(pubkey : PublicKey, plaintext : Bytes, seed : Bytes) -> Bytes raise Sm2Error

    Encrypts plaintext into strict ASN.1 DER SM2Cipher: SEQUENCE { x INTEGER, y INTEGER, hash OCTET STRING(32), cipher OCTET STRING }.

    encrypt_c1c3c2

    fn encrypt_c1c3c2(pubkey : PublicKey, plaintext : Bytes, seed : Bytes) -> Bytes raise Sm2Error

    Encrypts plaintext with a seed-derived ephemeral scalar in C1C3C2 format: 0x04 || C1(64) || C3(32) || C2(len).

    encrypt_with_k

    fn encrypt_with_k(pubkey : PublicKey, plaintext : Bytes, k : Bytes) -> Bytes raise Sm2Error

    Encrypts plaintext using an explicit 32-byte ephemeral scalar k. Returns 0x04 || C1(64) || C3(32) || C2(plaintext.length()).

    key_exchange_initiator

    fn key_exchange_initiator(id_a : Bytes, priv_a : PrivateKey, eph_priv_a : PrivateKey, id_b : Bytes, pub_b : PublicKey, eph_pub_b : PublicKey, klen : Int) -> KeyExchangeResult raise Sm2Error

    GB/T 32918.3 SM2 Key Exchange — Initiator (User A) side. Returns KeyExchangeResult { shared_key, s1 (tag 0x02), s2 (tag 0x03) }.

    key_exchange_responder

    fn key_exchange_responder(id_b : Bytes, priv_b : PrivateKey, eph_priv_b : PrivateKey, id_a : Bytes, pub_a : PublicKey, eph_pub_a : PublicKey, klen : Int) -> KeyExchangeResult raise Sm2Error

    GB/T 32918.3 SM2 Key Exchange — Responder (User B) side. Returns KeyExchangeResult { shared_key, s1 (tag 0x02 / S_B), s2 (tag 0x03 / S_2) }.

    sign_der

    fn sign_der(priv_key : PrivateKey, id : Bytes, msg : Bytes) -> Bytes raise Sm2Error

    Deterministically signs (id, msg) and returns the ASN.1 DER-encoded signature.

    sign_deterministic

    fn sign_deterministic(priv_key : PrivateKey, id : Bytes, msg : Bytes) -> (Bytes, Bytes) raise Sm2Error

    Deterministically signs (id, msg) using RFC 6979-style HMAC-SM3 nonce generation. Returns (r, s) as 32-byte big-endian integers.

    sign_with_k

    fn sign_with_k(priv_key : PrivateKey, id : Bytes, msg : Bytes, k : Bytes) -> (Bytes, Bytes) raise Sm2Error

    Signs (id, msg) with an explicit 32-byte nonce k (GB/T 32918.2). Returns (r, s) as 32-byte big-endian integers.

    verify_der

    fn verify_der(pubkey : PublicKey, id : Bytes, msg : Bytes, der_sig : Bytes) -> Bool

    Verifies an ASN.1 DER-encoded SM2 signature against (pubkey, id, msg).

    verify_raw

    fn verify_raw(pubkey : PublicKey, id : Bytes, msg : Bytes, r : Bytes, s : Bytes) -> Bool

    Verifies a raw (r, s) SM2 signature against (pubkey, id, msg).