rssn 0.2.9

A comprehensive scientific computing library for Rust, aiming for feature parity with NumPy and SymPy.
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
//! JSON-based FFI API for cryptographic operations.
//!
//! This module provides JSON string-based FFI functions for elliptic curve cryptography.

use std::os::raw::c_char;
use std::str::FromStr;

use num_bigint::BigInt;

use crate::ffi_apis::common::from_json_string;
use crate::ffi_apis::common::to_json_string;
use crate::symbolic::cryptography::CurvePoint;
use crate::symbolic::cryptography::EcdsaSignature;
use crate::symbolic::cryptography::EllipticCurve;
use crate::symbolic::cryptography::ecdsa_sign;
use crate::symbolic::cryptography::ecdsa_verify;
use crate::symbolic::cryptography::generate_keypair;
use crate::symbolic::cryptography::generate_shared_secret;
use crate::symbolic::cryptography::point_compress;
use crate::symbolic::cryptography::point_decompress;
use crate::symbolic::finite_field::PrimeField;
use crate::symbolic::finite_field::PrimeFieldElement;

/// Helper to parse `BigInt` from string or JSON string.
pub(crate) fn parse_bigint(s: Option<String>) -> Option<BigInt> {
    s.and_then(|str_val| BigInt::from_str(&str_val).ok())
}

/// Creates a new elliptic curve.
/// Arguments: a (str), b (str), modulus (str)
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_json_elliptic_curve_new(
    a_json: *const c_char,
    b_json: *const c_char,
    modulus_json: *const c_char,
) -> *mut c_char {
    let a_str: Option<String> = from_json_string(a_json);

    let b_str: Option<String> = from_json_string(b_json);

    let mod_str: Option<String> = from_json_string(modulus_json);

    if let (Some(a), Some(b), Some(m)) = (
        parse_bigint(a_str),
        parse_bigint(b_str),
        parse_bigint(mod_str),
    ) {
        to_json_string(&EllipticCurve::new(a, b, m))
    } else {
        std::ptr::null_mut()
    }
}

/// Creates an affine curve point.
/// Arguments: x (str), y (str), modulus (str)
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_json_curve_point_affine(
    x_json: *const c_char,
    y_json: *const c_char,
    modulus_json: *const c_char,
) -> *mut c_char {
    let x_str: Option<String> = from_json_string(x_json);

    let y_str: Option<String> = from_json_string(y_json);

    let mod_str: Option<String> = from_json_string(modulus_json);

    if let (Some(x), Some(y), Some(m)) = (
        parse_bigint(x_str),
        parse_bigint(y_str),
        parse_bigint(mod_str),
    ) {
        let field = PrimeField::new(m);

        let point = CurvePoint::Affine {
            x: PrimeFieldElement::new(x, field.clone()),
            y: PrimeFieldElement::new(y, field),
        };

        to_json_string(&point)
    } else {
        std::ptr::null_mut()
    }
}

/// Creates a point at infinity.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_json_curve_point_infinity() -> *mut c_char {
    to_json_string(&CurvePoint::Infinity)
}

/// Checks if a point is on the curve.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_json_curve_is_on_curve(
    curve_json: *const c_char,
    point_json: *const c_char,
) -> *mut c_char {
    let curve: Option<EllipticCurve> = from_json_string(curve_json);

    let point: Option<CurvePoint> = from_json_string(point_json);

    if let (Some(c), Some(p)) = (curve, point) {
        to_json_string(&c.is_on_curve(&p))
    } else {
        std::ptr::null_mut()
    }
}

/// Negates a point.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_json_curve_negate(
    curve_json: *const c_char,
    point_json: *const c_char,
) -> *mut c_char {
    let curve: Option<EllipticCurve> = from_json_string(curve_json);

    let point: Option<CurvePoint> = from_json_string(point_json);

    if let (Some(c), Some(p)) = (curve, point) {
        to_json_string(&c.negate(&p))
    } else {
        std::ptr::null_mut()
    }
}

/// Doubles a point.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_json_curve_double(
    curve_json: *const c_char,
    point_json: *const c_char,
) -> *mut c_char {
    let curve: Option<EllipticCurve> = from_json_string(curve_json);

    let point: Option<CurvePoint> = from_json_string(point_json);

    if let (Some(c), Some(p)) = (curve, point) {
        to_json_string(&c.double(&p))
    } else {
        std::ptr::null_mut()
    }
}

/// Adds two points.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_json_curve_add(
    curve_json: *const c_char,
    p1_json: *const c_char,
    p2_json: *const c_char,
) -> *mut c_char {
    let curve: Option<EllipticCurve> = from_json_string(curve_json);

    let p1: Option<CurvePoint> = from_json_string(p1_json);

    let p2: Option<CurvePoint> = from_json_string(p2_json);

    if let (Some(c), Some(p1), Some(p2)) = (curve, p1, p2) {
        to_json_string(&c.add(&p1, &p2))
    } else {
        std::ptr::null_mut()
    }
}

/// Scalar multiplication.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_json_curve_scalar_mult(
    curve_json: *const c_char,
    k_json: *const c_char,
    p_json: *const c_char,
) -> *mut c_char {
    let curve: Option<EllipticCurve> = from_json_string(curve_json);

    let k_str: Option<String> = from_json_string(k_json);

    let p: Option<CurvePoint> = from_json_string(p_json);

    if let (Some(c), Some(k), Some(p)) = (curve, parse_bigint(k_str), p) {
        to_json_string(&c.scalar_mult(&k, &p))
    } else {
        std::ptr::null_mut()
    }
}

/// Generates a key pair.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_json_generate_keypair(
    curve_json: *const c_char,
    generator_json: *const c_char,
) -> *mut c_char {
    let curve: Option<EllipticCurve> = from_json_string(curve_json);

    let r#gen: Option<CurvePoint> = from_json_string(generator_json);

    if let (Some(c), Some(g)) = (curve, r#gen) {
        to_json_string(&generate_keypair(&c, &g))
    } else {
        std::ptr::null_mut()
    }
}

/// Generates a shared secret.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_json_generate_shared_secret(
    curve_json: *const c_char,
    private_key_json: *const c_char,
    other_public_key_json: *const c_char,
) -> *mut c_char {
    let curve: Option<EllipticCurve> = from_json_string(curve_json);

    let pk_str: Option<String> = from_json_string(private_key_json);

    let other_pub: Option<CurvePoint> = from_json_string(other_public_key_json);

    if let (Some(c), Some(pk), Some(opub)) = (curve, parse_bigint(pk_str), other_pub) {
        to_json_string(&generate_shared_secret(&c, &pk, &opub))
    } else {
        std::ptr::null_mut()
    }
}

/// Signs a message.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_json_ecdsa_sign(
    message_hash_json: *const c_char,
    private_key_json: *const c_char,
    curve_json: *const c_char,
    generator_json: *const c_char,
    order_json: *const c_char,
) -> *mut c_char {
    let hash_str: Option<String> = from_json_string(message_hash_json);

    let pk_str: Option<String> = from_json_string(private_key_json);

    let curve: Option<EllipticCurve> = from_json_string(curve_json);

    let r#gen: Option<CurvePoint> = from_json_string(generator_json);

    let order_str: Option<String> = from_json_string(order_json);

    if let (Some(h), Some(pk), Some(c), Some(g), Some(o)) = (
        parse_bigint(hash_str),
        parse_bigint(pk_str),
        curve,
        r#gen,
        parse_bigint(order_str),
    ) {
        if let Some(sig) = ecdsa_sign(&h, &pk, &c, &g, &o) {
            to_json_string(&sig)
        } else {
            std::ptr::null_mut()
        }
    } else {
        std::ptr::null_mut()
    }
}

/// Verifies a signature.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_json_ecdsa_verify(
    message_hash_json: *const c_char,
    signature_json: *const c_char,
    public_key_json: *const c_char,
    curve_json: *const c_char,
    generator_json: *const c_char,
    order_json: *const c_char,
) -> *mut c_char {
    let hash_str: Option<String> = from_json_string(message_hash_json);

    let sig: Option<EcdsaSignature> = from_json_string(signature_json);

    let pub_key: Option<CurvePoint> = from_json_string(public_key_json);

    let curve: Option<EllipticCurve> = from_json_string(curve_json);

    let r#gen: Option<CurvePoint> = from_json_string(generator_json);

    let order_str: Option<String> = from_json_string(order_json);

    if let (Some(h), Some(sig), Some(pk), Some(c), Some(g), Some(o)) = (
        parse_bigint(hash_str),
        sig,
        pub_key,
        curve,
        r#gen,
        parse_bigint(order_str),
    ) {
        to_json_string(&ecdsa_verify(&h, &sig, &pk, &c, &g, &o))
    } else {
        std::ptr::null_mut()
    }
}

/// Compresses a point.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_json_point_compress(point_json: *const c_char) -> *mut c_char {
    let point: Option<CurvePoint> = from_json_string(point_json);

    if let Some(p) = point {
        if let Some((x, is_odd)) = point_compress(&p) {
            let obj = serde_json::json!({
                "x": x.to_string(),
                "is_odd": is_odd
            });

            to_json_string(&obj)
        } else {
            std::ptr::null_mut()
        }
    } else {
        std::ptr::null_mut()
    }
}

/// Decompresses a point.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_json_point_decompress(
    x_json: *const c_char,
    is_odd_json: *const c_char,
    curve_json: *const c_char,
) -> *mut c_char {
    let x_str: Option<String> = from_json_string(x_json);

    let is_odd: Option<bool> = from_json_string(is_odd_json);

    let curve: Option<EllipticCurve> = from_json_string(curve_json);

    if let (Some(x), Some(io), Some(c)) = (parse_bigint(x_str), is_odd, curve) {
        if let Some(p) = point_decompress(x, io, &c) {
            to_json_string(&p)
        } else {
            std::ptr::null_mut()
        }
    } else {
        std::ptr::null_mut()
    }
}