Skip to content

Commit bf7396f

Browse files
committed
chore: Review changes
1 parent a2f86f8 commit bf7396f

File tree

9 files changed

+204
-145
lines changed

9 files changed

+204
-145
lines changed

apps/starknet_app/starkcurve.h

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* @file starkcurve.h
3+
* @author Cypherock X1 Team
4+
* @brief Constants for Starknet curve.
5+
* @copyright Copyright (c) 2023 HODL TECH PTE LTD
6+
* <br/> You may obtain a copy of license at <a href="https://mitcc.org/"
7+
*target=_blank>https://mitcc.org/</a>
8+
*
9+
******************************************************************************
10+
* @attention
11+
*
12+
* (c) Copyright 2023 by HODL TECH PTE LTD
13+
*
14+
* Permission is hereby granted, free of charge, to any person obtaining
15+
* a copy of this software and associated documentation files (the
16+
* "Software"), to deal in the Software without restriction, including
17+
* without limitation the rights to use, copy, modify, merge, publish,
18+
* distribute, sublicense, and/or sell copies of the Software, and to
19+
* permit persons to whom the Software is furnished to do so, subject
20+
* to the following conditions:
21+
*
22+
* The above copyright notice and this permission notice shall be
23+
* included in all copies or substantial portions of the Software.
24+
*
25+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
29+
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
30+
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32+
*
33+
*
34+
* "Commons Clause" License Condition v1.0
35+
*
36+
* The Software is provided to you by the Licensor under the License,
37+
* as defined below, subject to the following condition.
38+
*
39+
* Without limiting other conditions in the License, the grant of
40+
* rights under the License will not include, and the License does not
41+
* grant to you, the right to Sell the Software.
42+
*
43+
* For purposes of the foregoing, "Sell" means practicing any or all
44+
* of the rights granted to you under the License to provide to third
45+
* parties, for a fee or other consideration (including without
46+
* limitation fees for hosting or consulting/ support services related
47+
* to the Software), a product or service whose value derives, entirely
48+
* or substantially, from the functionality of the Software. Any license
49+
* notice or attribution required by the License must also include
50+
* this Commons Clause License Condition notice.
51+
*
52+
* Software: All X1Wallet associated files.
53+
* License: MIT
54+
* Licensor: HODL TECH PTE LTD
55+
*
56+
******************************************************************************
57+
*/
58+
59+
/*****************************************************************************
60+
* INCLUDES
61+
*****************************************************************************/
62+
63+
/*****************************************************************************
64+
* EXTERN VARIABLES
65+
*****************************************************************************/
66+
67+
/*****************************************************************************
68+
* MACROS AND DEFINES
69+
*****************************************************************************/
70+
#define SIZE_HEX 16
71+
72+
// Starknet curve constants
73+
#define STARKNET_CURVE_PRIME \
74+
"0800000000000011000000000000000000000000000000000000000000000001"
75+
#define STARKNET_CURVE_GX \
76+
"01EF15C18599971B7BECED415A40F0C7DEACFD9B0D1819E03D723D8BC943CFCA"
77+
#define STARKNET_CURVE_GY \
78+
"005668060AA49730B7BE4801DF46EC62DE53ECD11ABE43A32873000C36E8DC1F"
79+
#define STARKNET_CURVE_ORDER \
80+
"0800000000000010ffffffffffffffffb781126dcae7b2321e66a241adc64d2f"
81+
#define STARKNET_CURVE_ORDER_HALF \
82+
"04000000000000087fffffffffffffffdbc08936e573d9190f335120d6e32697"
83+
#define STARKNET_CURVE_A \
84+
"0000000000000000000000000000000000000000000000000000000000000001"
85+
#define STARKNET_CURVE_B \
86+
"06f21413efbe40de150e596d72f7a8c5609ad26c15c915c1f4cdfcb99cee9e89"
87+
88+
// starknet pedersen points
89+
// Ref: https://docs.starkware.co/starkex/crypto/pedersen-hash-function.html
90+
#define STARKNET_PEDERSEN_POINT_0_X \
91+
"049EE3EBA8C1600700EE1B87EB599F16716B0B1022947733551FDE4050CA6804"
92+
#define STARKNET_PEDERSEN_POINT_0_Y \
93+
"03CA0CFE4B3BC6DDF346D49D06EA0ED34E621062C0E056C1D0405D266E10268A"
94+
#define STARKNET_PEDERSEN_POINT_1_X \
95+
"0234287DCBAFFE7F969C748655FCA9E58FA8120B6D56EB0C1080D17957EBE47B"
96+
#define STARKNET_PEDERSEN_POINT_1_Y \
97+
"03B056F100F96FB21E889527D41F4E39940135DD7A6C94CC6ED0268EE89E5615"
98+
#define STARKNET_PEDERSEN_POINT_2_X \
99+
"04FA56F376C83DB33F9DAB2656558F3399099EC1DE5E3018B7A6932DBA8AA378"
100+
#define STARKNET_PEDERSEN_POINT_2_Y \
101+
"03FA0984C931C9E38113E0C0E47E4401562761F92A7A23B45168F4E80FF5B54D"
102+
#define STARKNET_PEDERSEN_POINT_3_X \
103+
"04BA4CC166BE8DEC764910F75B45F74B40C690C74709E90F3AA372F0BD2D6997"
104+
#define STARKNET_PEDERSEN_POINT_3_Y \
105+
"040301CF5C1751F4B971E46C4EDE85FCAC5C59A5CE5AE7C48151F27B24B219C"
106+
#define STARKNET_PEDERSEN_POINT_4_X \
107+
"054302DCB0E6CC1C6E44CCA8F61A63BB2CA65048D53FB325D36FF12C49A58202"
108+
#define STARKNET_PEDERSEN_POINT_4_Y \
109+
"01B77B3E37D13504B348046268D8AE25CE98AD783C25561A879DCC77E99C2426"
110+
111+
// starknet limit
112+
#define STARKNET_LIMIT \
113+
"F80000000000020EFFFFFFFFFFFFFFF738A13B4B920E9411AE6DA5F40B0358B1"

apps/starknet_app/starknet_crypto.c

Lines changed: 18 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
*****************************************************************************/
6262
#include "starknet_crypto.h"
6363

64+
#include <starkcurve.h>
6465
#include <stdio.h>
6566

6667
#include "mini-gmp.h"
@@ -118,119 +119,35 @@ static void stark_curve_init() {
118119
mpz_init(stark256.a);
119120
mpz_init(stark256.b);
120121

121-
// Prime
122-
mpz_set_str(
123-
stark256.prime,
124-
"0800000000000011000000000000000000000000000000000000000000000001",
125-
16);
126-
127-
// Generator_point x
128-
mpz_set_str(
129-
stark256.G.x,
130-
"01EF15C18599971B7BECED415A40F0C7DEACFD9B0D1819E03D723D8BC943CFCA",
131-
16);
132-
133-
// Generator_point y
134-
mpz_set_str(
135-
stark256.G.y,
136-
"005668060AA49730B7BE4801DF46EC62DE53ECD11ABE43A32873000C36E8DC1F",
137-
16);
138-
139-
// Order
140-
mpz_set_str(
141-
stark256.order,
142-
"0800000000000010ffffffffffffffffb781126dcae7b2321e66a241adc64d2f",
143-
16);
144-
145-
// Order half
146-
mpz_set_str(
147-
stark256.order_half,
148-
"04000000000000087fffffffffffffffdbc08936e573d9190f335120d6e32697",
149-
16);
150-
151-
// Alpha
152-
mpz_set_str(
153-
stark256.a,
154-
"0000000000000000000000000000000000000000000000000000000000000001",
155-
16);
156-
157-
// Beta
158-
mpz_set_str(
159-
stark256.b,
160-
"06f21413efbe40de150e596d72f7a8c5609ad26c15c915c1f4cdfcb99cee9e89",
161-
16);
122+
mpz_set_str(stark256.prime, STARKNET_CURVE_PRIME, SIZE_HEX);
123+
mpz_set_str(stark256.G.x, STARKNET_CURVE_GX, SIZE_HEX);
124+
mpz_set_str(stark256.G.y, STARKNET_CURVE_GY, SIZE_HEX);
125+
mpz_set_str(stark256.order, STARKNET_CURVE_ORDER, SIZE_HEX);
126+
mpz_set_str(stark256.order_half, STARKNET_CURVE_ORDER_HALF, SIZE_HEX);
127+
mpz_set_str(stark256.a, STARKNET_CURVE_A, SIZE_HEX);
128+
mpz_set_str(stark256.b, STARKNET_CURVE_B, SIZE_HEX);
162129

163130
stark_curve = &stark256;
164131
}
165132

166133
static void stark_pedersen_init() {
167-
// Ref: https://docs.starkware.co/starkex/crypto/pedersen-hash-function.html
168-
169134
static mpz_pedersen pedersen;
170135
// Initialize all mpz_t variables in the pedersen structure
171136
for (int i = 0; i < 5; i++) {
172137
mpz_init(pedersen.P[i].x);
173138
mpz_init(pedersen.P[i].y);
174139
}
175140

176-
// Shift_point x
177-
mpz_set_str(
178-
pedersen.P[0].x,
179-
"049EE3EBA8C1600700EE1B87EB599F16716B0B1022947733551FDE4050CA6804",
180-
16);
181-
182-
// Shift_point y
183-
mpz_set_str(
184-
pedersen.P[0].y,
185-
"03CA0CFE4B3BC6DDF346D49D06EA0ED34E621062C0E056C1D0405D266E10268A",
186-
16);
187-
188-
// Pedersen_point_1 x
189-
mpz_set_str(
190-
pedersen.P[1].x,
191-
"0234287DCBAFFE7F969C748655FCA9E58FA8120B6D56EB0C1080D17957EBE47B",
192-
16);
193-
194-
// Pedersen_point_1 y
195-
mpz_set_str(
196-
pedersen.P[1].y,
197-
"03B056F100F96FB21E889527D41F4E39940135DD7A6C94CC6ED0268EE89E5615",
198-
16);
199-
200-
// Pedersen_point_2 x
201-
mpz_set_str(
202-
pedersen.P[2].x,
203-
"04FA56F376C83DB33F9DAB2656558F3399099EC1DE5E3018B7A6932DBA8AA378",
204-
16);
205-
206-
// Pedersen_point_2 y
207-
mpz_set_str(
208-
pedersen.P[2].y,
209-
"03FA0984C931C9E38113E0C0E47E4401562761F92A7A23B45168F4E80FF5B54D",
210-
16);
211-
212-
// Pedersen_point_3 x
213-
mpz_set_str(
214-
pedersen.P[3].x,
215-
"04BA4CC166BE8DEC764910F75B45F74B40C690C74709E90F3AA372F0BD2D6997",
216-
16);
217-
218-
// Pedersen_point_3 y
219-
mpz_set_str(pedersen.P[3].y,
220-
"040301CF5C1751F4B971E46C4EDE85FCAC5C59A5CE5AE7C48151F27B24B219C",
221-
16);
222-
223-
// Pedersen_point_4 x
224-
mpz_set_str(
225-
pedersen.P[4].x,
226-
"054302DCB0E6CC1C6E44CCA8F61A63BB2CA65048D53FB325D36FF12C49A58202",
227-
16);
228-
229-
// Pedersen_point_4 y
230-
mpz_set_str(
231-
pedersen.P[4].y,
232-
"01B77B3E37D13504B348046268D8AE25CE98AD783C25561A879DCC77E99C2426",
233-
16);
141+
mpz_set_str(pedersen.P[0].x, STARKNET_PEDERSEN_POINT_0_X, SIZE_HEX);
142+
mpz_set_str(pedersen.P[0].y, STARKNET_PEDERSEN_POINT_0_Y, SIZE_HEX);
143+
mpz_set_str(pedersen.P[1].x, STARKNET_PEDERSEN_POINT_1_X, SIZE_HEX);
144+
mpz_set_str(pedersen.P[1].y, STARKNET_PEDERSEN_POINT_1_Y, SIZE_HEX);
145+
mpz_set_str(pedersen.P[2].x, STARKNET_PEDERSEN_POINT_2_X, SIZE_HEX);
146+
mpz_set_str(pedersen.P[2].y, STARKNET_PEDERSEN_POINT_2_Y, SIZE_HEX);
147+
mpz_set_str(pedersen.P[3].x, STARKNET_PEDERSEN_POINT_3_X, SIZE_HEX);
148+
mpz_set_str(pedersen.P[3].y, STARKNET_PEDERSEN_POINT_3_Y, SIZE_HEX);
149+
mpz_set_str(pedersen.P[4].x, STARKNET_PEDERSEN_POINT_4_X, SIZE_HEX);
150+
mpz_set_str(pedersen.P[4].y, STARKNET_PEDERSEN_POINT_4_Y, SIZE_HEX);
234151

235152
starknet_pedersen_points = &pedersen;
236153
}

apps/starknet_app/starknet_helpers.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include "starknet_helpers.h"
6464

6565
#include <error.pb.h>
66+
#include <starkcurve.h>
6667

6768
#include "coin_utils.h"
6869
#include "mini-gmp-helpers.h"
@@ -105,43 +106,41 @@ static bool grind_key(const uint8_t *grind_seed, uint8_t *out);
105106
*****************************************************************************/
106107

107108
bool grind_key(const uint8_t *grind_seed, uint8_t *out) {
108-
uint8_t key[32] = {0};
109+
uint8_t key[STARKNET_BIGNUM_SIZE] = {0};
109110
mpz_t strk_limit;
110111
mpz_t strk_key;
111-
mpz_t stark_order;
112112

113-
// Initialize stark_order
114-
mpz_init_set_str(
115-
stark_order,
116-
"0800000000000010FFFFFFFFFFFFFFFFB781126DCAE7B2321E66A241ADC64D2F",
117-
16);
118-
119-
// Initialize strk_limit
120-
mpz_init_set_str(
121-
strk_limit,
122-
"F80000000000020EFFFFFFFFFFFFFFF738A13B4B920E9411AE6DA5F40B0358B1",
123-
16);
113+
mpz_init_set_str(strk_limit, STARKNET_LIMIT, SIZE_HEX);
124114

125115
SHA256_CTX ctx = {0};
126116
mpz_init(strk_key);
127117
for (uint8_t itr = 0; itr < 200; itr++) {
128118
sha256_Init(&ctx);
129-
sha256_Update(&ctx, grind_seed, 32);
119+
sha256_Update(&ctx, grind_seed, STARKNET_BIGNUM_SIZE);
130120
sha256_Update(&ctx, &itr, 1);
131121
sha256_Final(&ctx, key);
132122

133-
byte_array_to_mpz(strk_key, key, 32);
123+
byte_array_to_mpz(strk_key, key, STARKNET_BIGNUM_SIZE);
134124
if (mpz_cmp(strk_key, strk_limit) == -1) {
135125
mpz_t f_key;
136126
mpz_init(f_key);
137-
mpz_mod(f_key, strk_key, stark_order);
138-
mpz_to_byte_array(f_key, out, 32);
127+
mpz_mod(f_key, strk_key, stark_curve->order);
128+
mpz_to_byte_array(f_key, out, STARKNET_BIGNUM_SIZE);
129+
130+
// clear mpz variables
131+
mpz_clear(f_key);
132+
mpz_clear(strk_key);
133+
mpz_clear(strk_limit);
139134
return true;
140135
}
141136
}
142137

143138
starknet_send_error(ERROR_COMMON_ERROR_UNKNOWN_ERROR_TAG, 0);
144139
LOG_CRITICAL("ERROR: grind 200 iterations failed\n");
140+
141+
// clear mpz variables
142+
mpz_clear(strk_key);
143+
mpz_clear(strk_limit);
145144
return false;
146145
}
147146

@@ -184,27 +183,28 @@ bool starknet_derive_key_from_seed(const uint8_t *seed,
184183
return false;
185184
}
186185

187-
uint8_t stark_private_key[32] = {0};
186+
uint8_t stark_private_key[STARKNET_BIGNUM_SIZE] = {0};
188187
mpz_curve_point p;
189188
mpz_curve_point_init(&p);
190189
if (!grind_key(stark_child_node.private_key, stark_private_key)) {
190+
mpz_curve_point_clear(&p);
191191
return false;
192192
}
193193

194194
// copy stark priv key if required
195195
if (key_priv != NULL) {
196-
memzero(key_priv, 32);
197-
memcpy(key_priv, stark_private_key, 32);
196+
memzero(key_priv, STARKNET_BIGNUM_SIZE);
197+
memcpy(key_priv, stark_private_key, STARKNET_BIGNUM_SIZE);
198198
}
199199

200200
// derive stark pub key from stark priv key
201201
mpz_t priv_key;
202202
mpz_init(priv_key);
203-
byte_array_to_mpz(priv_key, stark_private_key, 32);
203+
byte_array_to_mpz(priv_key, stark_private_key, STARKNET_BIGNUM_SIZE);
204204
mpz_curve_point_multiply(stark_curve, priv_key, &stark_curve->G, &p);
205205
mpz_clear(priv_key); // clear priv key when no longer required
206206

207-
uint8_t stark_public_key[32] = {0};
207+
uint8_t stark_public_key[STARKNET_BIGNUM_SIZE] = {0};
208208
mpz_to_byte_array(p.x, stark_public_key, STARKNET_PUB_KEY_SIZE);
209209

210210
// copy stark pub key if required

apps/starknet_app/starknet_pedersen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void compute_hash_on_elements(uint8_t data[][STARKNET_BIGNUM_SIZE],
123123
pederson_hash(result, data[index], STARKNET_BIGNUM_SIZE, result);
124124
}
125125

126-
uint8_t num_elem_bn[32];
126+
uint8_t num_elem_bn[STARKNET_BIGNUM_SIZE];
127127
starknet_uli_to_bn_byte_array(num_elem, num_elem_bn);
128128

129129
pederson_hash(result, num_elem_bn, STARKNET_BIGNUM_SIZE, result);

apps/starknet_app/starknet_pedersen.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@
7676
#define LOW_PART_BYTES (LOW_PART_BITS / 8)
7777
#define LOW_PART_MASK ((1ULL << LOW_PART_BITS) - 1)
7878

79-
#define STARKNET_BIGNUM_SIZE 32
79+
#define STARKNET_BIGNUM_SIZE \
80+
32 ///< Max byte size of a bignum in starknet context
8081
#define PEDERSEN_HASH_SIZE 32
8182

8283
#define CALL_DATA_PARAMETER_SIZE 3

0 commit comments

Comments
 (0)