Skip to content

Commit ebccb9b

Browse files
committed
feat: Add pubkey
1 parent ce66e6d commit ebccb9b

File tree

4 files changed

+84
-26
lines changed

4 files changed

+84
-26
lines changed

apps/starknet_app/starknet_crypto.c

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,8 @@ static void stark_curve_init() {
161161
"06f21413efbe40de150e596d72f7a8c5609ad26c15c915c1f4cdfcb99cee9e89",
162162
STARK_BN_LEN);
163163

164-
print_stark_curve();
165-
166164
starkCurve = &stark256;
165+
print_stark_curve();
167166
}
168167

169168
static void stark_pedersen_init() {
@@ -232,9 +231,8 @@ static void stark_pedersen_init() {
232231
"003FA0984C931C9E38113E0C0E47E4401562761F92A7A23B45168F4E80FF5B54D",
233232
STARK_BN_LEN);
234233

235-
print_stark_perdersen();
236-
237234
starkPts = &pedersen;
235+
print_stark_perdersen();
238236
}
239237

240238
static void print_stark_curve() {
@@ -244,33 +242,33 @@ static void print_stark_curve() {
244242
printf("\nPrime: %s", str);
245243

246244
bignum_to_string(&starkCurve->G.x, str, STARK_BN_LEN);
247-
printf("\nG x: %s", str);
245+
printf("\nG x : %s", str);
248246

249247
bignum_to_string(&starkCurve->G.y, str, STARK_BN_LEN);
250-
printf("\nG y: %s", str);
248+
printf("\nG y : %s", str);
251249

252250
bignum_to_string(&starkCurve->order, str, STARK_BN_LEN);
253251
printf("\nOrder: %s", str);
254252

255253
bignum_to_string(&starkCurve->order_half, str, STARK_BN_LEN);
256-
printf("\nOrder half: %s", str);
254+
printf("\nOhalf: %s", str);
257255

258256
bignum_to_string(&starkCurve->a, str, STARK_BN_LEN);
259257
printf("\nAlpha: %s", str);
260258

261259
bignum_to_string(&starkCurve->b, str, STARK_BN_LEN);
262-
printf("\nBeta: %s", str);
260+
printf("\nBeta : %s\n", str);
263261
}
264262

265263
static void print_stark_perdersen() {
266264
char str[STARK_BN_LEN];
267265

268266
for (int i = 0; i < 5; i++) {
269267
bignum_to_string(&starkPts->P[i].x, str, STARK_BN_LEN);
270-
printf("P%d (Pedersen Point) x", i, str);
268+
printf("P%d x: %s", i, str);
271269

272270
bignum_to_string(&starkPts->P[i].y, str, STARK_BN_LEN);
273-
printf("P%d (Pedersen Point) y", i, str);
271+
printf(", y: %s\n", str);
274272
}
275273
}
276274

@@ -379,4 +377,65 @@ void private_to_public_key(const uint8_t *private, uint8_t *public_65) {
379377
public_65[0] = 0x04;
380378
// bn_write_be(&R.x, public_65 + 1);
381379
// bn_write_be(&R.y, public_65 + 33);
382-
}
380+
}
381+
382+
void stark_point_multiply(const stark_curve *curve,
383+
const struct bn *k,
384+
const stark_point *p,
385+
stark_point *res) {
386+
stark_point temp;
387+
stark_point R;
388+
stark_point_set_infinity(&R); // Initialize R to the point at infinity
389+
stark_point_copy(p, &temp); // Copy the input point p to temp
390+
391+
// Iterate over each bit of k from the most significant to the least
392+
// significant
393+
for (int i = 256 - 1; i >= 0; i--) {
394+
// Double the current point temp
395+
stark_point_add(curve, &temp, &temp, &temp);
396+
397+
// If the i-th bit of k is set, add temp to the result R
398+
if (bn_is_bit_set(k, i)) {
399+
stark_point_add(curve, &temp, &R, &R);
400+
}
401+
}
402+
403+
// Copy the result R to the output parameter res
404+
stark_point_copy(&R, res);
405+
}
406+
407+
int bn_bit_length(const struct bn *k) {
408+
int bit_length = 0;
409+
410+
// Start from the most significant element of the array
411+
for (int i = BN_ARRAY_SIZE - 1; i >= 0; i--) {
412+
if (k->array[i] != 0) {
413+
DTYPE_TMP word = k->array[i];
414+
// Calculate the bit length of this word
415+
while (word) {
416+
word >>= 1;
417+
bit_length++;
418+
}
419+
// Add the offset of this word
420+
bit_length += i * (WORD_SIZE * 8);
421+
break;
422+
}
423+
}
424+
425+
return bit_length;
426+
}
427+
428+
int bn_is_bit_set(const struct bn *k, int bit_idx) {
429+
int word_idx =
430+
bit_idx / (WORD_SIZE * 8); // Determine which word contains the bit
431+
int bit_in_word =
432+
bit_idx % (WORD_SIZE * 8); // Determine which bit in the word
433+
434+
// Ensure that word_idx is within bounds
435+
if (word_idx >= BN_ARRAY_SIZE) {
436+
return 0; // Out of bounds, so the bit is not set
437+
}
438+
439+
// Check if the specific bit is set in the corresponding word
440+
return (k->array[word_idx] & ((DTYPE)1 << bit_in_word)) != 0;
441+
}

apps/starknet_app/starknet_helpers.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
#include "coin_utils.h"
6868
#include "starknet_api.h"
6969
#include "starknet_context.h"
70-
#include "starknet_crypto.h"
7170

7271
/*****************************************************************************
7372
* EXTERN VARIABLES
@@ -215,22 +214,20 @@ bool starknet_derive_key_from_seed(const uint8_t *seed_key,
215214
}
216215

217216
uint8_t stark_private_key[32];
217+
stark_point p;
218218
uint8_t stark_public_key[32];
219219
if (!grind_key(starkChildNode.private_key, stark_private_key)) {
220220
return false;
221221
}
222222

223-
// Implement: ecdsa_get_public_key33(stark256, stark_private_key,
224-
// stark_public_key);
225-
stark_point R = {0}; // curve_point R = {0};
226-
struct bn k = {0}; // bignum256 k = {0};
227-
// bn_read_be(priv_key, &k);
228-
// //compute k*G
229-
// scalar_multiply(curve, &k, &R); // convert for stark curve
230-
// pub_key[0] = 0x02 | (R.y.val[0] & 0x01);
231-
// bn_write_be(&R.x, pub_key + 1);
232-
// memzero(&R, sizeof(R));
233-
// memzero(&k, sizeof(k));
223+
char str[100];
224+
// stark_curve *starkCurve;
225+
starknet_init();
226+
stark_point_multiply(starkCurve, stark_public_key, &starkCurve->G, &p);
227+
bignum_to_string(&p.x, str, STARK_BN_LEN);
228+
printf("\nstarkPubKey x: %s\n", str);
229+
bignum_to_string(&p.y, str, STARK_BN_LEN);
230+
printf("\nstarkPubKey y: %s\n", str);
234231

235232
printf("\n");
236233

apps/starknet_app/starknet_helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <stdint.h>
1919

2020
#include "coin_utils.h"
21+
#include "starknet_crypto.h"
2122

2223
/*****************************************************************************
2324
* MACROS AND DEFINES

apps/starknet_app/starknet_pedersen.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ void process_single_element(struct bn *element,
149149
bignum_rshift(&high_nibble, element, LOW_PART_BITS);
150150

151151
stark_point res1, res2;
152-
point_multiply(starkCurve, p1, &low_part, &res1); // low_part * p1
153-
point_multiply(starkCurve, p2, &high_nibble, &res2); // high_nibble * p2
154-
stark_point_add(starkCurve, &res1, &res2, result); // Combine results
152+
stark_point_multiply(starkCurve, p1, &low_part, &res1); // low_part * p1
153+
stark_point_multiply(
154+
starkCurve, p2, &high_nibble, &res2); // high_nibble * p2
155+
stark_point_add(starkCurve, &res1, &res2, result); // Combine results
155156

156157
stark_point_copy(&res2, result);
157158
}

0 commit comments

Comments
 (0)