#elliptic-curve #bluesky #at-proto #helper

atrium-crypto

Cryptographic library providing basic helpers for AT Protocol

3 releases

0.1.2 Oct 28, 2024
0.1.1 Sep 20, 2024
0.1.0 Jul 19, 2024

#410 in Cryptography

Download history 3/week @ 2024-08-14 2/week @ 2024-09-04 79/week @ 2024-09-11 288/week @ 2024-09-18 36/week @ 2024-09-25 4/week @ 2024-10-02 10/week @ 2024-10-09 13/week @ 2024-10-16 113/week @ 2024-10-23 37/week @ 2024-10-30 16/week @ 2024-11-06 9/week @ 2024-11-13 20/week @ 2024-11-20 1/week @ 2024-11-27

51 downloads per month

MIT license

35KB
642 lines

ATrium Crypto

Cryptographic library providing basic helpers for AT Protocol.

This package implements the two currently supported cryptographic systems:

  • p256 elliptic curve: aka "NIST P-256", aka secp256r1 (note the r), aka prime256v1
  • k256 elliptic curve: aka "NIST K-256", aka secp256k1 (note the k)

The details of cryptography in atproto are described in the specification. This includes string encodings, validity of "low-S" signatures, byte representation "compression", hashing, and more.

Usage

use atrium_crypto::keypair::{Secp256k1Keypair, Did};
use atrium_crypto::verify::verify_signature;
use rand::rngs::ThreadRng;

fn main() -> Result<(), Box<dyn std::error::Error>>{
    // generate a new random K-256 private key
    let keypair = Secp256k1Keypair::create(&mut ThreadRng::default());

    // sign binary data, resulting signature bytes.
    // SHA-256 hash of data is what actually gets signed.
    let msg = [1, 2, 3, 4, 5, 6, 7, 8];
    let signature = keypair.sign(&msg)?;

    // serialize the public key as a did:key string, which includes key type metadata
    let pub_did_key = keypair.did();
    println!("{pub_did_key}");
    // output would look something like: 'did:key:zQ3shVRtgqTRHC7Lj4DYScoDgReNpsDp3HBnuKBKt1FSXKQ38'

    // verify signature using public key
    match verify_signature(&pub_did_key, &msg, &signature) {
        Ok(()) => println!("Success"),
        Err(_) => panic!("Uh oh, something is fishy"),
    }
    Ok(())
}

Dependencies

~5MB
~102K SLoC