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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! Public key type derived from a secp256k1 private key.
//!
//! PublicKey wraps a Point on the secp256k1 curve and provides
//! DER encoding/decoding, address derivation, and signature
//! verification. Mirrors the TS SDK PublicKey.ts API.
use crate::primitives::base_point::BasePoint;
use crate::primitives::big_number::{BigNumber, Endian};
use crate::primitives::ecdsa::ecdsa_verify;
use crate::primitives::error::PrimitivesError;
use crate::primitives::hash::{hash160, sha256, sha256_hmac};
use crate::primitives::point::Point;
use crate::primitives::private_key::PrivateKey;
use crate::primitives::signature::Signature;
use crate::primitives::utils::{base58_check_encode, from_hex, to_hex};
/// A secp256k1 public key (a point on the curve).
///
/// Uses composition with Point, following Rust conventions.
/// The TS SDK uses class inheritance (PublicKey extends Point);
/// we mirror the public API names.
#[derive(Clone, Debug)]
pub struct PublicKey {
point: Point,
}
impl PublicKey {
/// Create a PublicKey from a Point.
pub fn from_point(point: Point) -> Self {
PublicKey { point }
}
/// Derive a public key from a private key.
pub fn from_private_key(key: &crate::primitives::private_key::PrivateKey) -> Self {
key.to_public_key()
}
/// Parse a public key from a hex string (compressed or uncompressed DER).
///
/// Compressed: 33 bytes (66 hex chars), starts with 02 or 03
/// Uncompressed: 65 bytes (130 hex chars), starts with 04
pub fn from_string(s: &str) -> Result<Self, PrimitivesError> {
let bytes = from_hex(s)?;
Self::from_der_bytes(&bytes)
}
/// Parse a public key from DER-encoded bytes.
pub fn from_der_bytes(bytes: &[u8]) -> Result<Self, PrimitivesError> {
if bytes.is_empty() {
return Err(PrimitivesError::InvalidPublicKey(
"empty public key data".to_string(),
));
}
match bytes[0] {
0x02 | 0x03 => {
// Compressed: prefix(1) + x(32) = 33 bytes
if bytes.len() != 33 {
return Err(PrimitivesError::InvalidPublicKey(format!(
"compressed key should be 33 bytes, got {}",
bytes.len()
)));
}
let odd = bytes[0] == 0x03;
let x = BigNumber::from_bytes(&bytes[1..], Endian::Big);
let point = Point::from_x(&x, odd)?;
Ok(PublicKey { point })
}
0x04 => {
// Uncompressed: prefix(1) + x(32) + y(32) = 65 bytes
if bytes.len() != 65 {
return Err(PrimitivesError::InvalidPublicKey(format!(
"uncompressed key should be 65 bytes, got {}",
bytes.len()
)));
}
let x = BigNumber::from_bytes(&bytes[1..33], Endian::Big);
let y = BigNumber::from_bytes(&bytes[33..], Endian::Big);
let point = Point::new(x, y);
if !point.validate() {
return Err(PrimitivesError::InvalidPublicKey(
"point not on curve".to_string(),
));
}
Ok(PublicKey { point })
}
prefix => Err(PrimitivesError::InvalidPublicKey(format!(
"unknown prefix byte: 0x{:02x}",
prefix
))),
}
}
/// Encode the public key in compressed DER format (33 bytes).
///
/// Format: prefix(1) || x(32)
/// prefix = 0x02 if y is even, 0x03 if y is odd
pub fn to_der(&self) -> Vec<u8> {
self.point.to_der(true)
}
/// Encode the public key in compressed DER format as a hex string.
pub fn to_der_hex(&self) -> String {
to_hex(&self.to_der())
}
/// Encode the public key in uncompressed DER format (65 bytes).
///
/// Format: 0x04 || x(32) || y(32)
pub fn to_der_uncompressed(&self) -> Vec<u8> {
self.point.to_der(false)
}
/// Hash the compressed public key with hash160 (RIPEMD-160(SHA-256)).
///
/// Returns 20 bytes -- the public key hash used in P2PKH addresses.
pub fn to_hash(&self) -> Vec<u8> {
let der = self.to_der();
hash160(&der).to_vec()
}
/// Derive a P2PKH Bitcoin address from this public key.
///
/// Format: Base58Check(prefix || hash160(compressed_der))
/// Default prefix `[0x00]` for mainnet.
pub fn to_address(&self, prefix: &[u8]) -> String {
let pkh = self.to_hash();
base58_check_encode(&pkh, prefix)
}
/// Verify a message signature using this public key.
///
/// The message is hashed with SHA-256 before verification.
pub fn verify(&self, message: &[u8], signature: &Signature) -> bool {
let msg_hash = sha256(message);
ecdsa_verify(&msg_hash, signature, &self.point)
}
/// Compute ECDH shared secret: private_key.bn * self.point.
///
/// Returns the resulting point on the curve.
pub fn derive_shared_secret(&self, private_key: &PrivateKey) -> Result<Point, PrimitivesError> {
private_key.derive_shared_secret(self)
}
/// Derive a child public key using Type-42 key derivation (BRC-42).
///
/// Computes: child_point = self.point + G * HMAC-SHA256(shared_secret_compressed, invoice_number)
/// where shared_secret = private_key * self.
pub fn derive_child(
&self,
private_key: &PrivateKey,
invoice_number: &str,
) -> Result<PublicKey, PrimitivesError> {
let shared_secret = private_key.derive_shared_secret(self)?;
let shared_secret_bytes = shared_secret.to_der(true); // 33-byte compressed
let hmac_result = sha256_hmac(&shared_secret_bytes, invoice_number.as_bytes());
let hmac_bn = BigNumber::from_bytes(&hmac_result, Endian::Big);
let base_point = BasePoint::instance();
let offset_point = base_point.mul(&hmac_bn);
let child_point = self.point.add(&offset_point);
Ok(PublicKey::from_point(child_point))
}
/// Access the underlying Point.
pub fn point(&self) -> &Point {
&self.point
}
}
impl PartialEq for PublicKey {
fn eq(&self, other: &Self) -> bool {
self.point.eq(&other.point)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::primitives::private_key::PrivateKey;
// -----------------------------------------------------------------------
// PublicKey: from_private_key
// -----------------------------------------------------------------------
#[test]
fn test_public_key_from_private_key() {
let priv_key = PrivateKey::from_hex("1").unwrap();
let pub_key = PublicKey::from_private_key(&priv_key);
// G point compressed
assert_eq!(
pub_key.to_der_hex(),
"0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
);
}
// -----------------------------------------------------------------------
// PublicKey: from_string (compressed and uncompressed)
// -----------------------------------------------------------------------
#[test]
fn test_public_key_from_string_compressed() {
let hex = "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798";
let pub_key = PublicKey::from_string(hex).unwrap();
assert_eq!(pub_key.to_der_hex(), hex);
}
#[test]
fn test_public_key_from_string_uncompressed() {
let hex = "0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8";
let pub_key = PublicKey::from_string(hex).unwrap();
// Should produce the same compressed key
assert_eq!(
pub_key.to_der_hex(),
"0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
);
}
// -----------------------------------------------------------------------
// PublicKey: DER compression roundtrip
// -----------------------------------------------------------------------
#[test]
fn test_public_key_der_roundtrip() {
let priv_key = PrivateKey::from_hex("ff").unwrap();
let pub_key = PublicKey::from_private_key(&priv_key);
let der_hex = pub_key.to_der_hex();
let recovered = PublicKey::from_string(&der_hex).unwrap();
assert_eq!(pub_key, recovered, "DER compression roundtrip should work");
}
// -----------------------------------------------------------------------
// PublicKey: to_hash
// -----------------------------------------------------------------------
#[test]
fn test_public_key_to_hash() {
let priv_key = PrivateKey::from_hex("1").unwrap();
let pub_key = PublicKey::from_private_key(&priv_key);
let hash = pub_key.to_hash();
assert_eq!(hash.len(), 20, "hash160 should be 20 bytes");
}
// -----------------------------------------------------------------------
// PublicKey: to_address
// -----------------------------------------------------------------------
#[test]
fn test_public_key_to_address_mainnet() {
// Key = 1 -> G -> known address
let priv_key = PrivateKey::from_hex("1").unwrap();
let pub_key = PublicKey::from_private_key(&priv_key);
let address = pub_key.to_address(&[0x00]);
assert_eq!(address, "1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH");
}
// -----------------------------------------------------------------------
// PublicKey: verify
// -----------------------------------------------------------------------
#[test]
fn test_public_key_verify() {
let priv_key = PrivateKey::from_hex("1").unwrap();
let pub_key = PublicKey::from_private_key(&priv_key);
let sig = priv_key.sign(b"test verify", true).unwrap();
assert!(
pub_key.verify(b"test verify", &sig),
"Should verify valid signature"
);
assert!(
!pub_key.verify(b"wrong message", &sig),
"Should reject wrong message"
);
}
// -----------------------------------------------------------------------
// PublicKey: uncompressed encoding
// -----------------------------------------------------------------------
#[test]
fn test_public_key_uncompressed() {
let priv_key = PrivateKey::from_hex("1").unwrap();
let pub_key = PublicKey::from_private_key(&priv_key);
let uncompressed = pub_key.to_der_uncompressed();
assert_eq!(uncompressed.len(), 65);
assert_eq!(uncompressed[0], 0x04);
}
// -----------------------------------------------------------------------
// PublicKey: test vectors from JSON
// -----------------------------------------------------------------------
#[test]
fn test_public_key_der_vectors() {
use serde::Deserialize;
#[derive(Deserialize)]
struct DerVector {
private_key_hex: String,
public_key_compressed: String,
public_key_uncompressed: String,
address_mainnet: String,
#[allow(dead_code)]
address_prefix: String,
#[allow(dead_code)]
description: String,
}
let data = include_str!("../../test-vectors/public_key_der.json");
let vectors: Vec<DerVector> = serde_json::from_str(data).unwrap();
for (i, v) in vectors.iter().enumerate() {
let priv_key = PrivateKey::from_hex(&v.private_key_hex).unwrap();
let pub_key = PublicKey::from_private_key(&priv_key);
// Compressed DER
assert_eq!(
pub_key.to_der_hex(),
v.public_key_compressed,
"Vector {}: compressed mismatch",
i
);
// Uncompressed DER
let uncompressed_hex = to_hex(&pub_key.to_der_uncompressed());
assert_eq!(
uncompressed_hex, v.public_key_uncompressed,
"Vector {}: uncompressed mismatch",
i
);
// Address
let address = pub_key.to_address(&[0x00]);
assert_eq!(address, v.address_mainnet, "Vector {}: address mismatch", i);
}
}
// -----------------------------------------------------------------------
// PublicKey: sign then verify roundtrip with multiple keys
// -----------------------------------------------------------------------
#[test]
fn test_sign_verify_roundtrip_multiple_keys() {
for i in 1..=5 {
let priv_key = PrivateKey::from_hex(&format!("{:064x}", i * 1000)).unwrap();
let pub_key = PublicKey::from_private_key(&priv_key);
let msg = format!("Message number {}", i);
let sig = priv_key.sign(msg.as_bytes(), true).unwrap();
assert!(
pub_key.verify(msg.as_bytes(), &sig),
"Key {} should verify",
i
);
}
}
}