mina_signer/lib.rs
1#![deny(missing_docs)]
2#![doc = include_str!("../README.md")]
3#![no_std]
4
5extern crate alloc;
6use alloc::{vec, vec::Vec};
7use ark_ec::AffineRepr;
8pub use keypair::Keypair;
9pub use mina_curves::pasta::Pallas as CurvePoint;
10use mina_hasher::{DomainParameter, Hashable};
11pub use pubkey::{CompressedPubKey, PubKey};
12pub use schnorr::Schnorr;
13pub use seckey::SecKey;
14pub use signature::Signature;
15
16pub mod keypair;
17pub mod pubkey;
18pub mod schnorr;
19pub mod seckey;
20pub mod signature;
21
22/// Base field element type
23pub type BaseField = <CurvePoint as AffineRepr>::BaseField;
24
25/// Scalar field element type
26pub type ScalarField = <CurvePoint as AffineRepr>::ScalarField;
27
28/// Mina network (or blockchain) identifier
29#[derive(Debug, Clone)]
30pub enum NetworkId {
31 /// Id for all testnets
32 TESTNET = 0x00,
33
34 /// Id for mainnet
35 MAINNET = 0x01,
36}
37
38impl From<NetworkId> for u8 {
39 fn from(id: NetworkId) -> u8 {
40 id as u8
41 }
42}
43
44impl DomainParameter for NetworkId {
45 fn into_bytes(self) -> Vec<u8> {
46 vec![self as u8]
47 }
48}
49
50/// Interface for signed objects
51///
52/// Signer interface for signing [`Hashable`] inputs and verifying [`Signatures`](Signature) using [`Keypairs`](Keypair) and [`PubKeys`](PubKey)
53pub trait Signer<H: Hashable> {
54 /// Sign `input` (see [`Hashable`]) using keypair `kp` and return the corresponding signature.
55 fn sign(&mut self, kp: &Keypair, input: &H) -> Signature;
56
57 /// Verify that the signature `sig` on `input` (see [`Hashable`]) is signed with the secret key corresponding to `pub_key`.
58 /// Return `true` if the signature is valid and `false` otherwise.
59 fn verify(&mut self, sig: &Signature, pub_key: &PubKey, input: &H) -> bool;
60}
61
62/// Create a legacy signer context with domain parameters initialized with `domain_param`
63///
64/// **Example**
65///
66/// ```
67/// #[path = "../tests/transaction.rs"]
68/// mod transaction;
69/// use mina_signer::{NetworkId, self, Signer};
70/// use transaction::Transaction;
71///
72/// let mut ctx = mina_signer::create_legacy::<Transaction>(NetworkId::TESTNET);
73/// ```
74pub fn create_legacy<H: 'static + Hashable>(domain_param: H::D) -> impl Signer<H> {
75 schnorr::create_legacy::<H>(domain_param)
76}
77
78/// Create an experimental kimchi signer context with domain parameters initialized with `domain_param`
79///
80/// **Example**
81///
82/// ```
83/// #[path = "../tests/transaction.rs"]
84/// mod transaction;
85/// use mina_signer::{NetworkId, self, Signer};
86/// use transaction::Transaction;
87///
88/// let mut ctx = mina_signer::create_kimchi::<Transaction>(NetworkId::TESTNET);
89/// ```
90pub fn create_kimchi<H: 'static + Hashable>(domain_param: H::D) -> impl Signer<H> {
91 schnorr::create_kimchi::<H>(domain_param)
92}