bsv-sdk 0.2.8

Pure Rust implementation of the BSV Blockchain SDK
Documentation
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! ECDSA signing and verification using secp256k1.
//!
//! Implements the Elliptic Curve Digital Signature Algorithm (ECDSA) with
//! RFC 6979 deterministic nonce generation via HMAC-DRBG. Follows the
//! TS SDK ECDSA.ts implementation for cross-language compatibility.

use crate::primitives::base_point::BasePoint;
use crate::primitives::big_number::{BigNumber, Endian};
use crate::primitives::curve::Curve;
use crate::primitives::drbg::Drbg;
use crate::primitives::error::PrimitivesError;
use crate::primitives::jacobian_point::JacobianPoint;
use crate::primitives::point::Point;
use crate::primitives::signature::Signature;

/// Truncate a message hash BigNumber to the bit length of the curve order n.
///
/// If the hash has more bits than n, right-shift to truncate.
/// If truncOnly is false and the result is >= n, subtract n.
/// This follows FIPS 186-4 message truncation rules.
fn truncate_to_n(msg: &BigNumber, trunc_only: bool) -> BigNumber {
    let curve = Curve::secp256k1();
    let n_bit_length = curve.n.bit_length();
    let delta = (msg.byte_length() * 8).saturating_sub(n_bit_length);

    let mut result = msg.clone();
    if delta > 0 {
        result = result.ushrn(delta);
    }
    if !trunc_only && result.cmp(&curve.n) >= 0 {
        result = result.sub(&curve.n);
    }
    result
}

/// Sign a message hash using ECDSA with RFC 6979 deterministic nonce.
///
/// Arguments:
/// - message_hash: 32-byte SHA-256 hash of the message
/// - private_key: the private key as a BigNumber in [1, n-1]
/// - force_low_s: if true, ensure s <= n/2 (BIP 62 / BIP 146)
///
/// Returns a Signature(r, s) or an error.
pub fn ecdsa_sign(
    message_hash: &[u8; 32],
    private_key: &BigNumber,
    force_low_s: bool,
) -> Result<Signature, PrimitivesError> {
    let curve = Curve::secp256k1();
    let n = &curve.n;
    let n_byte_len = n.byte_length();

    // Convert message hash to BigNumber and truncate
    let msg_bn = BigNumber::from_bytes(message_hash, Endian::Big);
    let msg = truncate_to_n(&msg_bn, false);

    // Prepare DRBG entropy (private key bytes) and nonce (message hash bytes)
    let key_bytes = private_key.to_array(Endian::Big, Some(n_byte_len));
    let nonce_bytes = msg.to_array(Endian::Big, Some(n_byte_len));

    let mut entropy = [0u8; 32];
    let mut nonce = [0u8; 32];
    entropy.copy_from_slice(&key_bytes[..32]);
    nonce.copy_from_slice(&nonce_bytes[..32]);

    let mut drbg = Drbg::new(&entropy, &nonce);

    let ns1 = n.subn(1);
    let base_point = BasePoint::instance();

    loop {
        // Generate k from DRBG
        let k_bytes = drbg.generate(n_byte_len);
        let k_hex: String = k_bytes.iter().map(|b| format!("{:02x}", b)).collect();
        let k_bn = BigNumber::from_hex(&k_hex)
            .map_err(|_| PrimitivesError::ArithmeticError("invalid k hex".to_string()))?;

        // Truncate k to n bit length (trunc_only=true)
        let k_bn = truncate_to_n(&k_bn, true);

        // k must be in [1, n-1]
        if k_bn.cmpn(1) < 0 || k_bn.cmp(&ns1) > 0 {
            continue;
        }

        // R = k * G
        let r_point = base_point.mul(&k_bn);
        if r_point.is_infinity() {
            continue;
        }

        // r = R.x mod n
        let r_bn = r_point
            .get_x()
            .umod(n)
            .map_err(|e| PrimitivesError::ArithmeticError(format!("r mod n: {}", e)))?;

        if r_bn.is_zero() {
            continue;
        }

        // s = k^-1 * (hash + r * privkey) mod n
        let k_inv = k_bn
            .invm(n)
            .map_err(|e| PrimitivesError::ArithmeticError(format!("k inverse: {}", e)))?;

        let r_times_key = r_bn
            .mul(private_key)
            .umod(n)
            .map_err(|e| PrimitivesError::ArithmeticError(format!("r*key mod n: {}", e)))?;

        let sum = msg
            .add(&r_times_key)
            .umod(n)
            .map_err(|e| PrimitivesError::ArithmeticError(format!("hash+r*key mod n: {}", e)))?;

        let mut s_bn = k_inv
            .mul(&sum)
            .umod(n)
            .map_err(|e| PrimitivesError::ArithmeticError(format!("s mod n: {}", e)))?;

        if s_bn.is_zero() {
            continue;
        }

        // Enforce low-S if requested
        if force_low_s && s_bn.cmp(&curve.half_n) > 0 {
            s_bn = n.sub(&s_bn);
        }

        return Ok(Signature::new(r_bn, s_bn));
    }
}

/// Sign a message hash using ECDSA with a caller-specified k value.
///
/// This is used by RPuzzle to produce a signature whose R-value is
/// deterministic and known to the signer. The k value must be in [1, n-1].
///
/// Arguments:
/// - message_hash: 32-byte SHA-256 hash of the message
/// - private_key: the private key as a BigNumber in [1, n-1]
/// - k: the nonce value to use (must be in [1, n-1])
/// - force_low_s: if true, ensure s <= n/2 (BIP 62 / BIP 146)
///
/// Returns a Signature(r, s) or an error.
pub fn ecdsa_sign_with_k(
    message_hash: &[u8; 32],
    private_key: &BigNumber,
    k: &BigNumber,
    force_low_s: bool,
) -> Result<Signature, PrimitivesError> {
    let curve = Curve::secp256k1();
    let n = &curve.n;

    // Convert message hash to BigNumber and truncate
    let msg_bn = BigNumber::from_bytes(message_hash, Endian::Big);
    let msg = truncate_to_n(&msg_bn, false);

    let ns1 = n.subn(1);

    // Validate k is in [1, n-1]
    if k.cmpn(1) < 0 || k.cmp(&ns1) > 0 {
        return Err(PrimitivesError::ArithmeticError(
            "k must be in [1, n-1]".to_string(),
        ));
    }

    let base_point = BasePoint::instance();

    // R = k * G
    let r_point = base_point.mul(k);
    if r_point.is_infinity() {
        return Err(PrimitivesError::ArithmeticError(
            "k*G is point at infinity".to_string(),
        ));
    }

    // r = R.x mod n
    let r_bn = r_point
        .get_x()
        .umod(n)
        .map_err(|e| PrimitivesError::ArithmeticError(format!("r mod n: {}", e)))?;

    if r_bn.is_zero() {
        return Err(PrimitivesError::ArithmeticError("r is zero".to_string()));
    }

    // s = k^-1 * (hash + r * privkey) mod n
    let k_inv = k
        .invm(n)
        .map_err(|e| PrimitivesError::ArithmeticError(format!("k inverse: {}", e)))?;

    let r_times_key = r_bn
        .mul(private_key)
        .umod(n)
        .map_err(|e| PrimitivesError::ArithmeticError(format!("r*key mod n: {}", e)))?;

    let sum = msg
        .add(&r_times_key)
        .umod(n)
        .map_err(|e| PrimitivesError::ArithmeticError(format!("hash+r*key mod n: {}", e)))?;

    let mut s_bn = k_inv
        .mul(&sum)
        .umod(n)
        .map_err(|e| PrimitivesError::ArithmeticError(format!("s mod n: {}", e)))?;

    if s_bn.is_zero() {
        return Err(PrimitivesError::ArithmeticError("s is zero".to_string()));
    }

    // Enforce low-S if requested
    if force_low_s && s_bn.cmp(&curve.half_n) > 0 {
        s_bn = n.sub(&s_bn);
    }

    Ok(Signature::new(r_bn, s_bn))
}

/// Verify an ECDSA signature against a message hash and public key.
///
/// Arguments:
/// - message_hash: 32-byte SHA-256 hash of the message
/// - signature: the (r, s) signature to verify
/// - public_key: the signer's public key as a Point
///
/// Returns true if the signature is valid.
pub fn ecdsa_verify(message_hash: &[u8; 32], signature: &Signature, public_key: &Point) -> bool {
    let curve = Curve::secp256k1();
    let n = &curve.n;

    // Convert message hash to BigNumber
    let msg_bn = BigNumber::from_bytes(message_hash, Endian::Big);

    let r = signature.r();
    let s = signature.s();

    // Check r and s are in [1, n-1]
    if r.cmpn(1) < 0 || r.cmp(n) >= 0 {
        return false;
    }
    if s.cmpn(1) < 0 || s.cmp(n) >= 0 {
        return false;
    }

    // s_inv = s^-1 mod n
    let s_inv = match s.invm(n) {
        Ok(inv) => inv,
        Err(_) => return false,
    };

    // u1 = hash * s_inv mod n
    let u1 = match msg_bn.mul(&s_inv).umod(n) {
        Ok(val) => val,
        Err(_) => return false,
    };

    // u2 = r * s_inv mod n
    let u2 = match r.mul(&s_inv).umod(n) {
        Ok(val) => val,
        Err(_) => return false,
    };

    // R = u1*G + u2*Q using Shamir's trick (shared doublings)
    let base_point = BasePoint::instance();
    let q_jac = JacobianPoint::from_affine(&public_key.x, &public_key.y);
    let r_jac = JacobianPoint::shamir_mul_wnaf(&u1, base_point.table(), &u2, &q_jac);

    if r_jac.is_infinity() {
        return false;
    }

    let (rx, _ry) = r_jac.to_affine();

    // v = R.x mod n
    let v = match rx.umod(n) {
        Ok(val) => val,
        Err(_) => return false,
    };

    // Check v == r
    v.cmp(r) == 0
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::primitives::hash::sha256;

    fn hex_to_bytes(hex: &str) -> Vec<u8> {
        (0..hex.len())
            .step_by(2)
            .map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap())
            .collect()
    }

    // -----------------------------------------------------------------------
    // ECDSA sign: deterministic output
    // -----------------------------------------------------------------------

    #[test]
    fn test_ecdsa_sign_deterministic() {
        // Same key + message should always produce the same signature
        let key = BigNumber::from_number(1);
        let msg_hash = sha256(b"test message");

        let sig1 = ecdsa_sign(&msg_hash, &key, false).unwrap();
        let sig2 = ecdsa_sign(&msg_hash, &key, false).unwrap();

        assert_eq!(
            sig1.r().to_hex(),
            sig2.r().to_hex(),
            "r should be deterministic"
        );
        assert_eq!(
            sig1.s().to_hex(),
            sig2.s().to_hex(),
            "s should be deterministic"
        );
    }

    // -----------------------------------------------------------------------
    // ECDSA sign then verify roundtrip
    // -----------------------------------------------------------------------

    #[test]
    fn test_ecdsa_sign_verify_roundtrip() {
        let key = BigNumber::from_number(42);
        let msg_hash = sha256(b"Hello, BSV!");

        let sig = ecdsa_sign(&msg_hash, &key, true).unwrap();

        // Derive public key: pubkey = key * G
        let base_point = BasePoint::instance();
        let pubkey = base_point.mul(&key);

        assert!(
            ecdsa_verify(&msg_hash, &sig, &pubkey),
            "Valid signature should verify"
        );
    }

    // -----------------------------------------------------------------------
    // ECDSA verify: wrong public key
    // -----------------------------------------------------------------------

    #[test]
    fn test_ecdsa_verify_wrong_key() {
        let key = BigNumber::from_number(1);
        let wrong_key = BigNumber::from_number(2);
        let msg_hash = sha256(b"test");

        let sig = ecdsa_sign(&msg_hash, &key, false).unwrap();

        let base_point = BasePoint::instance();
        let wrong_pubkey = base_point.mul(&wrong_key);

        assert!(
            !ecdsa_verify(&msg_hash, &sig, &wrong_pubkey),
            "Wrong public key should fail verification"
        );
    }

    // -----------------------------------------------------------------------
    // ECDSA verify: wrong message
    // -----------------------------------------------------------------------

    #[test]
    fn test_ecdsa_verify_wrong_message() {
        let key = BigNumber::from_number(1);
        let msg_hash = sha256(b"correct message");
        let wrong_hash = sha256(b"wrong message");

        let sig = ecdsa_sign(&msg_hash, &key, false).unwrap();

        let base_point = BasePoint::instance();
        let pubkey = base_point.mul(&key);

        assert!(
            !ecdsa_verify(&wrong_hash, &sig, &pubkey),
            "Wrong message should fail verification"
        );
    }

    // -----------------------------------------------------------------------
    // ECDSA sign: low-S enforcement
    // -----------------------------------------------------------------------

    #[test]
    fn test_ecdsa_sign_low_s() {
        let curve = Curve::secp256k1();
        let key = BigNumber::from_number(12345);
        let msg_hash = sha256(b"low-s test");

        let sig = ecdsa_sign(&msg_hash, &key, true).unwrap();
        assert!(
            sig.s().cmp(&curve.half_n) <= 0,
            "S should be <= n/2 when force_low_s is true"
        );
    }

    // -----------------------------------------------------------------------
    // ECDSA sign: test vectors from JSON
    // -----------------------------------------------------------------------

    #[test]
    fn test_ecdsa_sign_vectors() {
        use serde::Deserialize;

        #[derive(Deserialize)]
        struct SignVector {
            private_key_hex: String,
            message_hash_hex: String,
            expected_r: String,
            expected_s: String,
            force_low_s: bool,
            #[allow(dead_code)]
            description: String,
            #[allow(dead_code)]
            note: String,
        }

        let data = include_str!("../../test-vectors/ecdsa_sign.json");
        let vectors: Vec<SignVector> = serde_json::from_str(data).unwrap();

        for (i, v) in vectors.iter().enumerate() {
            let key = BigNumber::from_hex(&v.private_key_hex).unwrap();
            let msg_bytes = hex_to_bytes(&v.message_hash_hex);
            let mut msg_hash = [0u8; 32];
            msg_hash.copy_from_slice(&msg_bytes);

            let sig = ecdsa_sign(&msg_hash, &key, v.force_low_s).unwrap();

            let r_hex = sig.r().to_hex();
            let s_hex = sig.s().to_hex();

            // Pad r and s to 64 hex chars for comparison
            let r_padded = format!("{:0>64}", r_hex);
            let s_padded = format!("{:0>64}", s_hex);

            assert_eq!(r_padded, v.expected_r, "Vector {}: r mismatch", i);
            assert_eq!(s_padded, v.expected_s, "Vector {}: s mismatch", i);
        }
    }

    // -----------------------------------------------------------------------
    // ECDSA verify: test vectors from JSON
    // -----------------------------------------------------------------------

    #[test]
    fn test_ecdsa_verify_vectors() {
        use serde::Deserialize;

        #[derive(Deserialize)]
        struct VerifyVector {
            message_hash_hex: String,
            public_key_x: String,
            public_key_y: String,
            signature_r: String,
            signature_s: String,
            expected_valid: bool,
            #[allow(dead_code)]
            description: String,
            #[allow(dead_code)]
            note: String,
        }

        let data = include_str!("../../test-vectors/ecdsa_verify.json");
        let vectors: Vec<VerifyVector> = serde_json::from_str(data).unwrap();

        for (i, v) in vectors.iter().enumerate() {
            let msg_bytes = hex_to_bytes(&v.message_hash_hex);
            let mut msg_hash = [0u8; 32];
            msg_hash.copy_from_slice(&msg_bytes);

            let pub_x = BigNumber::from_hex(&v.public_key_x).unwrap();
            let pub_y = BigNumber::from_hex(&v.public_key_y).unwrap();
            let pubkey = Point::new(pub_x, pub_y);

            let r = BigNumber::from_hex(&v.signature_r).unwrap();
            let s = BigNumber::from_hex(&v.signature_s).unwrap();
            let sig = Signature::new(r, s);

            let result = ecdsa_verify(&msg_hash, &sig, &pubkey);
            assert_eq!(
                result, v.expected_valid,
                "Vector {}: expected valid={}, got {}",
                i, v.expected_valid, result
            );
        }
    }

    // -----------------------------------------------------------------------
    // ECDSA verify: tampered signature
    // -----------------------------------------------------------------------

    #[test]
    fn test_ecdsa_verify_tampered_signature() {
        let key = BigNumber::from_number(7);
        let msg_hash = sha256(b"tamper test");

        let sig = ecdsa_sign(&msg_hash, &key, false).unwrap();

        let base_point = BasePoint::instance();
        let pubkey = base_point.mul(&key);

        // Tamper with r
        let bad_r = sig.r().addn(1);
        let bad_sig = Signature::new(bad_r, sig.s().clone());
        assert!(
            !ecdsa_verify(&msg_hash, &bad_sig, &pubkey),
            "Tampered r should fail"
        );

        // Tamper with s
        let bad_s = sig.s().addn(1);
        let bad_sig = Signature::new(sig.r().clone(), bad_s);
        assert!(
            !ecdsa_verify(&msg_hash, &bad_sig, &pubkey),
            "Tampered s should fail"
        );
    }

    // -----------------------------------------------------------------------
    // ECDSA: multiple keys roundtrip
    // -----------------------------------------------------------------------

    #[test]
    fn test_ecdsa_multiple_keys() {
        let base_point = BasePoint::instance();

        for i in 1..=5 {
            let key = BigNumber::from_number(i * 1000);
            let msg_hash = sha256(format!("message {}", i).as_bytes());

            let sig = ecdsa_sign(&msg_hash, &key, true).unwrap();
            let pubkey = base_point.mul(&key);

            assert!(
                ecdsa_verify(&msg_hash, &sig, &pubkey),
                "Key {} should verify",
                i
            );
        }
    }
}