Skip to content

Commit a2f86f8

Browse files
committed
chore: Refactor starknet ecdsa operations
1 parent 5e805ed commit a2f86f8

File tree

13 files changed

+824
-487
lines changed

13 files changed

+824
-487
lines changed

apps/starknet_app/starknet_crypto.c

Lines changed: 36 additions & 231 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#include <stdio.h>
6565

6666
#include "mini-gmp.h"
67+
#include "mpz_ecdsa.h"
6768

6869
/*****************************************************************************
6970
* EXTERN VARIABLES
@@ -80,62 +81,33 @@
8081
/*****************************************************************************
8182
* STATIC FUNCTION PROTOTYPES
8283
*****************************************************************************/
84+
/**
85+
* @brief Initializes @ref stark_curve instance based on star curve params
86+
ref:
87+
https://github.com/xJonathanLEI/starknet-rs/blob/f31e426a65225b9830bbf3c148f7ea05bf9dc257/starknet-curve/src/curve_params.rs
88+
89+
*/
90+
static void stark_curve_init();
8391

92+
/**
93+
* @brief Initializes starknet pedersen points
94+
*/
95+
static void stark_pedersen_init();
8496
/*****************************************************************************
8597
* STATIC VARIABLES
8698
*****************************************************************************/
8799

88100
/*****************************************************************************
89101
* GLOBAL VARIABLES
90102
*****************************************************************************/
91-
stark_curve *starkCurve;
92-
stark_pedersen *starkPts;
103+
mpz_curve *stark_curve;
104+
mpz_pedersen *starknet_pedersen_points;
93105

94106
/*****************************************************************************
95107
* STATIC FUNCTIONS
96108
*****************************************************************************/
97-
static void stark_curve_init();
98-
static void stark_pedersen_init();
99-
100-
/*****************************************************************************
101-
* GLOBAL FUNCTIONS
102-
*****************************************************************************/
103-
104-
void starknet_init() {
105-
stark_curve_init();
106-
stark_pedersen_init();
107-
}
108-
109-
void stark_point_init(stark_point *p) {
110-
mpz_init(p->x);
111-
mpz_init(p->y);
112-
}
113-
114-
void stark_point_clear(stark_point *p) {
115-
mpz_clear(p->x);
116-
mpz_clear(p->y);
117-
}
118-
119-
void stark_pedersen_clear() {
120-
for (int i = 0; i < 5; i++) {
121-
stark_point_clear(&starkPts->P[i]);
122-
}
123-
}
124-
125-
void stark_curve_init() {
126-
static stark_curve stark256;
127-
// char str[STARK_BN_LEN] = {0};
128-
129-
/* stark_curve_params ref:
130-
https://github.com/xJonathanLEI/starknet-rs/blob/f31e426a65225b9830bbf3c148f7ea05bf9dc257/starknet-curve/src/curve_params.rs
131-
132-
struct bn prime; // prime order of the finite field
133-
stark_point G; // initial curve point
134-
struct bn order; // order of G
135-
struct bn order_half; // order of G divided by 2
136-
struct bn a; // coefficient 'a' of the elliptic curve OR alpha
137-
struct bn b; // coefficient 'b' of the elliptic curve OR beta
138-
*/
109+
static void stark_curve_init() {
110+
static mpz_curve stark256;
139111

140112
// Initialize mpz_t variables in stark256
141113
mpz_init(stark256.prime);
@@ -188,14 +160,13 @@ void stark_curve_init() {
188160
"06f21413efbe40de150e596d72f7a8c5609ad26c15c915c1f4cdfcb99cee9e89",
189161
16);
190162

191-
starkCurve = &stark256;
192-
// print_stark_curve();
163+
stark_curve = &stark256;
193164
}
194165

195166
static void stark_pedersen_init() {
196167
// Ref: https://docs.starkware.co/starkex/crypto/pedersen-hash-function.html
197168

198-
static stark_pedersen pedersen;
169+
static mpz_pedersen pedersen;
199170
// Initialize all mpz_t variables in the pedersen structure
200171
for (int i = 0; i < 5; i++) {
201172
mpz_init(pedersen.P[i].x);
@@ -261,194 +232,28 @@ static void stark_pedersen_init() {
261232
"01B77B3E37D13504B348046268D8AE25CE98AD783C25561A879DCC77E99C2426",
262233
16);
263234

264-
starkPts = &pedersen;
265-
}
266-
267-
// Set cp2 = cp1
268-
void stark_point_copy(const stark_point *cp1, stark_point *cp2) {
269-
stark_point_init(cp2);
270-
271-
mpz_set(cp2->x, cp1->x);
272-
mpz_set(cp2->y, cp1->y);
273-
}
274-
275-
void stark_point_add(const stark_curve *curve,
276-
const stark_point *cp1,
277-
stark_point *cp2) {
278-
mpz_t lambda, inv, xr, yr;
279-
280-
mpz_init(lambda);
281-
mpz_init(inv);
282-
mpz_init(xr);
283-
mpz_init(yr);
284-
285-
if (stark_point_is_infinity(cp1)) {
286-
return;
287-
}
288-
if (stark_point_is_infinity(cp2)) {
289-
stark_point_copy(cp1, cp2);
290-
return;
291-
}
292-
if (stark_point_is_equal(cp1, cp2)) {
293-
stark_point_double(curve, cp2);
294-
return;
295-
}
296-
if (stark_point_is_negative_of(cp1, cp2)) {
297-
stark_point_set_infinity(cp2);
298-
return;
299-
}
300-
301-
// inv = (cp2->x - cp1->x) mod prime
302-
mpz_sub(inv, cp2->x, cp1->x);
303-
mpz_mod(inv, inv, curve->prime);
304-
305-
// inv = inv^-1 mod prime
306-
mpz_invert(inv, inv, curve->prime);
307-
308-
// lambda = (cp2->y - cp1->y) mod prime
309-
mpz_sub(lambda, cp2->y, cp1->y);
310-
mpz_mod(lambda, lambda, curve->prime);
311-
312-
// lambda = lambda * inv mod prime
313-
mpz_mul(lambda, lambda, inv);
314-
mpz_mod(lambda, lambda, curve->prime);
315-
316-
// xr = lambda^2 - cp1->x - cp2->x mod prime
317-
mpz_mul(xr, lambda, lambda);
318-
mpz_sub(xr, xr, cp1->x);
319-
mpz_sub(xr, xr, cp2->x);
320-
mpz_mod(xr, xr, curve->prime);
321-
322-
// yr = lambda * (cp1->x - xr) - cp1->y mod prime
323-
mpz_sub(yr, cp1->x, xr);
324-
mpz_mul(yr, yr, lambda);
325-
mpz_sub(yr, yr, cp1->y);
326-
mpz_mod(yr, yr, curve->prime);
327-
328-
// Set cp2 to the result
329-
mpz_set(cp2->x, xr);
330-
mpz_set(cp2->y, yr);
331-
332-
mpz_clear(lambda);
333-
mpz_clear(inv);
334-
mpz_clear(xr);
335-
mpz_clear(yr);
336-
}
337-
338-
void stark_point_double(const stark_curve *curve, stark_point *cp) {
339-
// Ref:
340-
// https://github.com/starkware-libs/starkex-for-spot-trading/blob/607f0b4ce507e1d95cd018d206a2797f6ba4aab4/src/starkware/crypto/starkware/crypto/signature/math_utils.py
341-
if (mpz_cmp_ui(cp->y, 0) == 0) {
342-
return;
343-
}
344-
345-
mpz_t lambda, xr, yr, inv;
346-
mpz_init(lambda);
347-
mpz_init(xr);
348-
mpz_init(yr);
349-
mpz_init(inv);
350-
351-
// lambda = (3 * cp->x^2 + curve->a) * (2 * cp->y)^-1 mod prime
352-
mpz_mul(lambda, cp->x, cp->x);
353-
mpz_mul_ui(lambda, lambda, 3);
354-
mpz_add(lambda, lambda, curve->a);
355-
mpz_mul_ui(inv, cp->y, 2); // using inv to store 2 * y
356-
mpz_invert(inv, inv, curve->prime);
357-
mpz_mul(lambda, lambda, inv);
358-
mpz_mod(lambda, lambda, curve->prime);
359-
360-
// xr = lambda^2 - 2 * cp->x mod prime
361-
mpz_mul(xr, lambda, lambda);
362-
mpz_submul_ui(xr, cp->x, 2);
363-
mpz_mod(xr, xr, curve->prime);
364-
365-
// yr = lambda * (cp->x - xr) - cp->y mod prime
366-
mpz_sub(yr, cp->x, xr);
367-
mpz_mul(yr, yr, lambda);
368-
mpz_sub(yr, yr, cp->y);
369-
mpz_mod(yr, yr, curve->prime);
370-
371-
mpz_set(cp->x, xr);
372-
mpz_set(cp->y, yr);
373-
374-
mpz_clear(lambda);
375-
mpz_clear(xr);
376-
mpz_clear(yr);
377-
mpz_clear(inv);
378-
}
379-
380-
// set point to internal representation of point at infinity
381-
void stark_point_set_infinity(stark_point *p) {
382-
mpz_set_ui(p->x, 0);
383-
mpz_set_ui(p->y, 0);
384-
}
385-
386-
// return true iff p represent point at infinity
387-
// both coords are zero in internal representation
388-
int stark_point_is_infinity(const stark_point *p) {
389-
return mpz_cmp_ui(p->x, 0) == 0 && mpz_cmp_ui(p->y, 0) == 0;
390-
}
391-
392-
// return true iff both points are equal
393-
int stark_point_is_equal(const stark_point *p, const stark_point *q) {
394-
return (mpz_cmp(p->x, q->x) == 0) && (mpz_cmp(p->y, q->y) == 0);
235+
starknet_pedersen_points = &pedersen;
395236
}
396237

397-
// returns true iff p == -q
398-
// expects p and q be valid points on curve other than point at infinity
399-
int stark_point_is_negative_of(const stark_point *p, const stark_point *q) {
400-
// if P == (x, y), then -P would be (x, -y) on this curve
401-
if (mpz_cmp(p->x, q->x) != 0) {
402-
return 0;
403-
}
404-
405-
// we shouldn't hit this for a valid point
406-
if (mpz_cmp_ui(p->y, 0) == 0) {
407-
return 0;
408-
}
409-
410-
return mpz_cmp(p->y, q->y) != 0;
411-
}
412-
413-
void stark_point_multiply(const stark_curve *curve,
414-
const mpz_t k,
415-
const stark_point *p,
416-
stark_point *res) {
417-
// Ref: https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication
418-
419-
stark_point temp;
420-
stark_point R;
421-
stark_point_init(&temp);
422-
stark_point_init(&R);
423-
stark_point_set_infinity(&R); // Initialize R to the point at infinity
424-
stark_point_copy(p, &temp); // Copy the input point p to temp
425-
426-
// Iterate over each bit of k from the least significant to the most
427-
// significant
428-
for (int i = 0; i < 256; i++) {
429-
// If the i-th bit of k is set, add temp to the result R
430-
if (mpz_tstbit(k, i)) {
431-
stark_point_add(curve, &temp, &R);
432-
}
433-
434-
// Double the current point temp
435-
stark_point_double(curve, &temp);
436-
}
437-
438-
// Copy the result R to the output parameter res
439-
stark_point_copy(&R, res);
238+
/*****************************************************************************
239+
* GLOBAL FUNCTIONS
240+
*****************************************************************************/
440241

441-
stark_point_clear(&temp);
442-
stark_point_clear(&R);
242+
void starknet_init() {
243+
stark_curve_init();
244+
stark_pedersen_init();
443245
}
444246

445-
int bn_bit_length(const mpz_t k) {
446-
if (mpz_cmp_ui(k, 0) == 0) {
447-
return 0;
247+
void stark_clear() {
248+
// clear pedersen points
249+
for (int i = 0; i < 5; i++) {
250+
mpz_curve_point_clear(&starknet_pedersen_points->P[i]);
448251
}
449-
return mpz_sizeinbase(k, 2);
450-
}
451-
452-
int bn_is_bit_set(const mpz_t k, int bit_idx) {
453-
return mpz_tstbit(k, bit_idx);
252+
// clear stark curve points
253+
mpz_clear(stark_curve->a);
254+
mpz_clear(stark_curve->b);
255+
mpz_curve_point_clear(&stark_curve->G);
256+
mpz_clear(stark_curve->order);
257+
mpz_clear(stark_curve->order_half);
258+
mpz_clear(stark_curve->prime);
454259
}

0 commit comments

Comments
 (0)