@@ -161,9 +161,8 @@ static void stark_curve_init() {
161
161
"06f21413efbe40de150e596d72f7a8c5609ad26c15c915c1f4cdfcb99cee9e89" ,
162
162
STARK_BN_LEN );
163
163
164
- print_stark_curve ();
165
-
166
164
starkCurve = & stark256 ;
165
+ print_stark_curve ();
167
166
}
168
167
169
168
static void stark_pedersen_init () {
@@ -232,9 +231,8 @@ static void stark_pedersen_init() {
232
231
"003FA0984C931C9E38113E0C0E47E4401562761F92A7A23B45168F4E80FF5B54D" ,
233
232
STARK_BN_LEN );
234
233
235
- print_stark_perdersen ();
236
-
237
234
starkPts = & pedersen ;
235
+ print_stark_perdersen ();
238
236
}
239
237
240
238
static void print_stark_curve () {
@@ -244,33 +242,33 @@ static void print_stark_curve() {
244
242
printf ("\nPrime: %s" , str );
245
243
246
244
bignum_to_string (& starkCurve -> G .x , str , STARK_BN_LEN );
247
- printf ("\nG x: %s" , str );
245
+ printf ("\nG x : %s" , str );
248
246
249
247
bignum_to_string (& starkCurve -> G .y , str , STARK_BN_LEN );
250
- printf ("\nG y: %s" , str );
248
+ printf ("\nG y : %s" , str );
251
249
252
250
bignum_to_string (& starkCurve -> order , str , STARK_BN_LEN );
253
251
printf ("\nOrder: %s" , str );
254
252
255
253
bignum_to_string (& starkCurve -> order_half , str , STARK_BN_LEN );
256
- printf ("\nOrder half : %s" , str );
254
+ printf ("\nOhalf : %s" , str );
257
255
258
256
bignum_to_string (& starkCurve -> a , str , STARK_BN_LEN );
259
257
printf ("\nAlpha: %s" , str );
260
258
261
259
bignum_to_string (& starkCurve -> b , str , STARK_BN_LEN );
262
- printf ("\nBeta: %s" , str );
260
+ printf ("\nBeta : %s\n " , str );
263
261
}
264
262
265
263
static void print_stark_perdersen () {
266
264
char str [STARK_BN_LEN ];
267
265
268
266
for (int i = 0 ; i < 5 ; i ++ ) {
269
267
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 );
271
269
272
270
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 );
274
272
}
275
273
}
276
274
@@ -379,4 +377,65 @@ void private_to_public_key(const uint8_t *private, uint8_t *public_65) {
379
377
public_65 [0 ] = 0x04 ;
380
378
// bn_write_be(&R.x, public_65 + 1);
381
379
// 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
+ }
0 commit comments