1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyTuple};
use pyo3::{wrap_pyfunction, IntoPy, PyObject, exceptions};
use std::convert::TryFrom;
use ed25519_dalek::{Keypair, PublicKey, SecretKey, Signature, Verifier, Signer};
pub struct PyKeypair([u8; 32], [u8; 32]);
pub struct PySignature([u8; 64]);
pub const SEED_KEY_LENGTH: usize = 32;
fn create_from_pair(pair: &[u8]) -> PyResult<Keypair> {
match Keypair::from_bytes(pair) {
Ok(pair) => Ok(pair),
Err(_) => Err(exceptions::ValueError::py_err("Provided pair is invalid."))
}
}
fn create_from_parts(public: &[u8], secret: &[u8]) -> PyResult<Keypair> {
let mut pair = vec![];
pair.extend_from_slice(secret);
pair.extend_from_slice(public);
create_from_pair(&pair)
}
fn create_from_seed(seed: &[u8]) -> PyResult<Keypair> {
if seed.len() != SEED_KEY_LENGTH {
return Err(exceptions::ValueError::py_err(
format!("Expected bytes of length {}, got {}", SEED_KEY_LENGTH, seed.len()))
)
}
let secret = SecretKey::from_bytes(seed).unwrap();
let public: PublicKey = (&secret).into();
create_from_parts(public.as_bytes(), seed)
}
fn create_public(public: &[u8]) -> PublicKey {
match PublicKey::from_bytes(public) {
Ok(public) => return public,
Err(_) => panic!("Provided public key is invalid.")
}
}
#[pyfunction]
#[text_signature = "(seed)"]
pub fn ed_from_seed(seed: &[u8]) -> PyResult<PyKeypair> {
let keypair = match create_from_seed(seed) {
Ok(keypair) => keypair,
Err(err) => return Err(err)
};
Ok(PyKeypair(keypair.secret.to_bytes(), keypair.public.to_bytes()))
}
#[pyfunction]
#[text_signature = "(public, secret, message)"]
pub fn ed_sign(public: &[u8], secret: &[u8], message: &[u8]) -> PyResult<PySignature> {
let keypair = match create_from_parts(public, secret) {
Ok(keypair) => keypair,
Err(err) => return Err(err)
};
Ok(PySignature(keypair.sign(message).to_bytes()))
}
#[pyfunction]
#[text_signature = "(signature, message, public)"]
pub fn ed_verify(signature: &[u8], message: &[u8], public: &[u8]) -> bool {
let signature = match Signature::try_from(signature) {
Ok(signature) => signature,
Err(_) => return false
};
create_public(public)
.verify(message, &signature)
.is_ok()
}
#[pymodule]
fn ed25519(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(ed_from_seed))?;
m.add_wrapped(wrap_pyfunction!(ed_sign))?;
m.add_wrapped(wrap_pyfunction!(ed_verify))?;
Ok(())
}
impl IntoPy<PyObject> for PyKeypair {
fn into_py(self, py: Python) -> PyObject {
let secret = PyBytes::new(py, &self.0);
let public = PyBytes::new(py, &self.1);
PyTuple::new(py, vec![secret, public]).into_py(py)
}
}
impl IntoPy<PyObject> for PySignature {
fn into_py(self, py: Python) -> PyObject {
let sig = PyBytes::new(py, &self.0);
sig.into_py(py)
}
}