cryptonote_raw_crypto/
key.rs

1use super::scalar::EllipticCurveScalar;
2
3extern "C" {
4  fn generate_keys(public: *mut u8, secret: *mut u8);
5  fn check_public_key(public_key: *const u8) -> bool;
6  fn secret_key_to_public_key(secret_key: *const u8, public_key: *mut u8) -> bool;
7  fn generate_key_derivation(
8    public_key: *const u8,
9    secret_key: *const u8,
10    derivation: *mut u8,
11  ) -> bool;
12  fn derive_public_key(
13    derivation: *const u8,
14    output_index: u64,
15    base_public_key: *const u8,
16    derived_key: *mut u8,
17  ) -> bool;
18  fn underive_public_key(
19    derivation: *const u8,
20    output_index: u64,
21    base_public_key: *const u8,
22    derived_key: *mut u8,
23  ) -> bool;
24  fn derive_secret_key(
25    derivation: *const u8,
26    output_index: u64,
27    base_secret_key: *const u8,
28    derived_key: *mut u8,
29  ) -> bool;
30
31  fn generate_signature(
32    prefix_hash: *const u8,
33    public_key: *const u8,
34    secret_key: *const u8,
35    signature: *mut u8,
36  );
37
38  fn check_signature(prefix_hash: *const u8, public_key: *const u8, signature: *const u8) -> bool;
39
40  fn generate_key_image(public_key: *const u8, secret_key: *const u8, image: *mut u8);
41}
42
43pub struct Key {}
44
45impl Key {
46  pub fn generate_key_pair(public_key: &mut [u8; 32], secret_key: &mut [u8; 32]) {
47    unsafe { generate_keys(public_key.as_mut_ptr(), secret_key.as_mut_ptr()) }
48  }
49
50  pub fn generate_secret_key() -> [u8; 32] {
51    let mut secret_key: [u8; 32] = [0; 32];
52    EllipticCurveScalar::random(&mut secret_key);
53    secret_key
54  }
55
56  pub fn check_public_key(public_key: &[u8; 32]) -> bool {
57    unsafe { return check_public_key(public_key.as_ptr()) }
58  }
59  pub fn secret_to_public(secret_key: &[u8; 32], public_key: &mut [u8; 32]) -> bool {
60    unsafe { return secret_key_to_public_key(secret_key.as_ptr(), public_key.as_mut_ptr()) }
61  }
62  pub fn generate_key_derivation(public_key: &[u8; 32], secret_key: &[u8; 32]) -> [u8; 32] {
63    let mut derived: [u8; 32] = [0; 32];
64    unsafe {
65      generate_key_derivation(
66        public_key.as_ptr(),
67        secret_key.as_ptr(),
68        derived.as_mut_ptr(),
69      );
70    }
71    derived
72  }
73  pub fn derive_public_key(
74    derivation: &[u8; 32],
75    output_index: u64,
76    base_public_key: &[u8; 32],
77  ) -> [u8; 32] {
78    let mut derived: [u8; 32] = [0; 32];
79    unsafe {
80      derive_public_key(
81        derivation.as_ptr(),
82        output_index,
83        base_public_key.as_ptr(),
84        derived.as_mut_ptr(),
85      );
86    }
87    derived
88  }
89
90  pub fn underive_public_key(
91    derivation: &[u8; 32],
92    output_index: u64,
93    base_public_key: &[u8; 32],
94  ) -> [u8; 32] {
95    let mut derived: [u8; 32] = [0; 32];
96    unsafe {
97      underive_public_key(
98        derivation.as_ptr(),
99        output_index,
100        base_public_key.as_ptr(),
101        derived.as_mut_ptr(),
102      );
103    }
104    derived
105  }
106  pub fn derive_secret_key(
107    derivation: &[u8; 32],
108    output_index: u64,
109    base_secret_key: &[u8; 32],
110  ) -> [u8; 32] {
111    let mut derived: [u8; 32] = [0; 32];
112    unsafe {
113      derive_secret_key(
114        derivation.as_ptr(),
115        output_index,
116        base_secret_key.as_ptr(),
117        derived.as_mut_ptr(),
118      );
119    }
120    derived
121  }
122
123  pub fn generate_signature(
124    prefix_hash: &[u8; 32],
125    public_key: &[u8; 32],
126    secret_key: &[u8; 32],
127  ) -> [u8; 64] {
128    let mut signature: [u8; 64] = [0; 64];
129    unsafe {
130      generate_signature(
131        prefix_hash.as_ptr(),
132        public_key.as_ptr(),
133        secret_key.as_ptr(),
134        signature.as_mut_ptr(),
135      );
136    }
137    signature
138  }
139
140  pub fn check_signature(
141    prefix_hash: &[u8; 32],
142    public_key: &[u8; 32],
143    signature: &[u8; 64],
144  ) -> bool {
145    unsafe {
146      return check_signature(
147        prefix_hash.as_ptr(),
148        public_key.as_ptr(),
149        signature.as_ptr(),
150      );
151    }
152  }
153
154  pub fn generate_key_image(public_key: &[u8; 32], secret_key: &[u8; 32]) -> [u8; 32] {
155    let mut image: [u8; 32] = [0; 32];
156    unsafe {
157      generate_key_image(public_key.as_ptr(), secret_key.as_ptr(), image.as_mut_ptr());
158    }
159    image
160  }
161}
162
163#[cfg(test)]
164mod tests {
165  use super::*;
166
167  #[test]
168  fn should_get_public_key_from_secret_key() {
169    let secret_key = [
170      50, 228, 229, 247, 39, 151, 194, 252, 14, 45, 218, 78, 128, 230, 27, 208, 9, 57, 52, 163, 5,
171      175, 8, 201, 211, 185, 66, 113, 88, 68, 170, 8,
172    ];
173    let mut public_key: [u8; 32] = [0; 32];
174    Key::secret_to_public(&secret_key, &mut public_key);
175    // println!("{:?}", public_key);
176    // println!("{:?}", public_key);
177    assert!(
178      public_key
179        == [
180          81, 76, 248, 201, 237, 192, 109, 39, 58, 159, 67, 13, 120, 203, 91, 70, 36, 216, 162,
181          222, 0, 100, 243, 152, 32, 48, 89, 129, 252, 169, 180, 36
182        ]
183    );
184    assert!(Key::check_public_key(&public_key));
185  }
186
187  #[test]
188  fn should_get_public_key_from_generated_secret_key() {
189    let secret_key = Key::generate_secret_key();
190    let mut public_key: [u8; 32] = [0; 32];
191    Key::secret_to_public(&secret_key, &mut public_key);
192    // println!("{:?}", secret_key);
193    // println!("{:?}", public_key);
194    assert!(public_key.len() == 32);
195    assert!(Key::check_public_key(&public_key));
196  }
197}