Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(app): Add starknet gen pubkey #614

Merged
merged 26 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4f4dbc6
feat(app): Add starknet gen pubkey
vrockz747 Dec 11, 2024
bc9dd9c
chore: Add function docs
vrockz747 Dec 12, 2024
5e805ed
chore: Ci fix
vrockz747 Dec 13, 2024
a2f86f8
chore: Refactor starknet ecdsa operations
vrockz747 Dec 17, 2024
83fc294
feat(app): Add sign txn
vrockz747 Dec 12, 2024
d939931
fix(app): Proto options size
vrockz747 Dec 13, 2024
aeef9d9
chore: Add submodules
vrockz747 Dec 14, 2024
1b17339
fix: Sign gen minor bug
vrockz747 Dec 14, 2024
a46aefd
chore: Update submodule
vrockz747 Dec 14, 2024
2590c3c
fix: Relocate starknet init
vrockz747 Dec 16, 2024
fc02c36
chore: Add starknet verify amount
vrockz747 Dec 16, 2024
4cc6484
chore: Update starknet utxn proto
vrockz747 Dec 16, 2024
f8823f3
fix: Verify txn data
vrockz747 Dec 18, 2024
266e827
chore: Update base
vrockz747 Dec 18, 2024
462c196
chore: Complete verification data all txns
vrockz747 Dec 18, 2024
ea3033f
chore: Refactor sign utilities
vrockz747 Dec 18, 2024
bf7396f
chore: Review changes
vrockz747 Dec 23, 2024
9cc6e72
chore: Review changes
vrockz747 Dec 23, 2024
42479e6
Merge branch 'feat/starknet/refactored/base' into feat/starknet/refac…
vrockz747 Dec 23, 2024
f625d0a
chore: Update submodule and review changes
vrockz747 Jan 3, 2025
1bff1dd
chore: Remove dev build ci
vrockz747 Jan 3, 2025
d99c888
chore: Version bump(v0.6.1538++)
vrockz747 Jan 3, 2025
44a4e78
Merge pull request #617 from Cypherock/feat/starknet/refactored/sign-txn
TejasvOnly Jan 8, 2025
41d8d78
fix: Apped zeros when converting mpz to string
TejasvOnly Jan 9, 2025
33ca59f
chore: Version bump (patch++)
TejasvOnly Jan 9, 2025
0eab180
chore: Fix ci
TejasvOnly Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "nanopb"]
path = vendor/nanopb
url = ../nanopb
[submodule "mini-gmp"]
path = vendor/mini-gmp
url = ../mini-gmp
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ include(utilities/cmake/version.cmake)
file(GLOB_RECURSE PROTO_SRCS "generated/proto/*.*")
list(APPEND PROTO_SRCS "vendor/nanopb/pb_common.c" "vendor/nanopb/pb_decode.c" "vendor/nanopb/pb_encode.c" "vendor/nanopb/pb_common.h" "vendor/nanopb/pb_decode.h" "vendor/nanopb/pb_encode.h" "vendor/nanopb/pb.h")

list (APPEND MINI_GMP_SRCS "vendor/mini-gmp/mini-gmp-helpers.c" "vendor/mini-gmp/mini-gmp.c")

OPTION(DEV_SWITCH "Additional features/logs to aid developers" OFF)
OPTION(UNIT_TESTS_SWITCH "Compile build for main firmware or unit tests" OFF)

Expand All @@ -49,7 +51,7 @@ else()
endif()

# Include nanopb source headers
target_include_directories( ${EXECUTABLE} PRIVATE vendor/nanopb generated/proto )
target_include_directories( ${EXECUTABLE} PRIVATE vendor/nanopb generated/proto vendor/mini-gmp)

# Enable support for dynamically allocated fields in nanopb
# Ref: vendor/nanopb/pb.h
Expand Down
113 changes: 113 additions & 0 deletions apps/starknet_app/starkcurve.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* @file starkcurve.h
* @author Cypherock X1 Team
* @brief Constants for Starknet curve.
* @copyright Copyright (c) 2023 HODL TECH PTE LTD
* <br/> You may obtain a copy of license at <a href="https://mitcc.org/"
*target=_blank>https://mitcc.org/</a>
*
******************************************************************************
* @attention
*
* (c) Copyright 2023 by HODL TECH PTE LTD
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*
* "Commons Clause" License Condition v1.0
*
* The Software is provided to you by the Licensor under the License,
* as defined below, subject to the following condition.
*
* Without limiting other conditions in the License, the grant of
* rights under the License will not include, and the License does not
* grant to you, the right to Sell the Software.
*
* For purposes of the foregoing, "Sell" means practicing any or all
* of the rights granted to you under the License to provide to third
* parties, for a fee or other consideration (including without
* limitation fees for hosting or consulting/ support services related
* to the Software), a product or service whose value derives, entirely
* or substantially, from the functionality of the Software. Any license
* notice or attribution required by the License must also include
* this Commons Clause License Condition notice.
*
* Software: All X1Wallet associated files.
* License: MIT
* Licensor: HODL TECH PTE LTD
*
******************************************************************************
*/

/*****************************************************************************
* INCLUDES
*****************************************************************************/

/*****************************************************************************
* EXTERN VARIABLES
*****************************************************************************/

/*****************************************************************************
* MACROS AND DEFINES
*****************************************************************************/
#define SIZE_HEX 16

// Starknet curve constants
#define STARKNET_CURVE_PRIME \
"0800000000000011000000000000000000000000000000000000000000000001"
#define STARKNET_CURVE_GX \
"01EF15C18599971B7BECED415A40F0C7DEACFD9B0D1819E03D723D8BC943CFCA"
#define STARKNET_CURVE_GY \
"005668060AA49730B7BE4801DF46EC62DE53ECD11ABE43A32873000C36E8DC1F"
#define STARKNET_CURVE_ORDER \
"0800000000000010ffffffffffffffffb781126dcae7b2321e66a241adc64d2f"
#define STARKNET_CURVE_ORDER_HALF \
"04000000000000087fffffffffffffffdbc08936e573d9190f335120d6e32697"
#define STARKNET_CURVE_A \
"0000000000000000000000000000000000000000000000000000000000000001"
#define STARKNET_CURVE_B \
"06f21413efbe40de150e596d72f7a8c5609ad26c15c915c1f4cdfcb99cee9e89"

// starknet pedersen points
// Ref: https://docs.starkware.co/starkex/crypto/pedersen-hash-function.html
#define STARKNET_PEDERSEN_POINT_0_X \
"049EE3EBA8C1600700EE1B87EB599F16716B0B1022947733551FDE4050CA6804"
#define STARKNET_PEDERSEN_POINT_0_Y \
"03CA0CFE4B3BC6DDF346D49D06EA0ED34E621062C0E056C1D0405D266E10268A"
#define STARKNET_PEDERSEN_POINT_1_X \
"0234287DCBAFFE7F969C748655FCA9E58FA8120B6D56EB0C1080D17957EBE47B"
#define STARKNET_PEDERSEN_POINT_1_Y \
"03B056F100F96FB21E889527D41F4E39940135DD7A6C94CC6ED0268EE89E5615"
#define STARKNET_PEDERSEN_POINT_2_X \
"04FA56F376C83DB33F9DAB2656558F3399099EC1DE5E3018B7A6932DBA8AA378"
#define STARKNET_PEDERSEN_POINT_2_Y \
"03FA0984C931C9E38113E0C0E47E4401562761F92A7A23B45168F4E80FF5B54D"
#define STARKNET_PEDERSEN_POINT_3_X \
"04BA4CC166BE8DEC764910F75B45F74B40C690C74709E90F3AA372F0BD2D6997"
#define STARKNET_PEDERSEN_POINT_3_Y \
"040301CF5C1751F4B971E46C4EDE85FCAC5C59A5CE5AE7C48151F27B24B219C"
#define STARKNET_PEDERSEN_POINT_4_X \
"054302DCB0E6CC1C6E44CCA8F61A63BB2CA65048D53FB325D36FF12C49A58202"
#define STARKNET_PEDERSEN_POINT_4_Y \
"01B77B3E37D13504B348046268D8AE25CE98AD783C25561A879DCC77E99C2426"

// starknet limit
#define STARKNET_LIMIT \
"F80000000000020EFFFFFFFFFFFFFFF738A13B4B920E9411AE6DA5F40B0358B1"
199 changes: 199 additions & 0 deletions apps/starknet_app/starknet_api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/**
* @file starknet_api.c
* @author Cypherock X1 Team
* @brief Defines helpers apis for Starknet app.
* @copyright Copyright (c) 2023 HODL TECH PTE LTD
* <br/> You may obtain a copy of license at <a href="https://mitcc.org/"
*target=_blank>https://mitcc.org/</a>
*
******************************************************************************
* @attention
*
* (c) Copyright 2023 by HODL TECH PTE LTD
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*
* "Commons Clause" License Condition v1.0
*
* The Software is provided to you by the Licensor under the License,
* as defined below, subject to the following condition.
*
* Without limiting other conditions in the License, the grant of
* rights under the License will not include, and the License does not
* grant to you, the right to Sell the Software.
*
* For purposes of the foregoing, "Sell" means practicing any or all
* of the rights granted to you under the License to provide to third
* parties, for a fee or other consideration (including without
* limitation fees for hosting or consulting/ support services related
* to the Software), a product or service whose value derives, entirely
* or substantially, from the functionality of the Software. Any license
* notice or attribution required by the License must also include
* this Commons Clause License Condition notice.
*
* Software: All X1Wallet associated files.
* License: MIT
* Licensor: HODL TECH PTE LTD
*
******************************************************************************
*/

/*****************************************************************************
* INCLUDES
*****************************************************************************/

#include "starknet_api.h"

#include <pb_decode.h>
#include <pb_encode.h>

#include "common_error.h"
#include "core_api.h"
#include "events.h"

/*****************************************************************************
* EXTERN VARIABLES
*****************************************************************************/

/*****************************************************************************
* PRIVATE MACROS AND DEFINES
*****************************************************************************/

/*****************************************************************************
* PRIVATE TYPEDEFS
*****************************************************************************/

/*****************************************************************************
* STATIC VARIABLES
*****************************************************************************/

/*****************************************************************************
* GLOBAL VARIABLES
*****************************************************************************/

/*****************************************************************************
* STATIC FUNCTION PROTOTYPES
*****************************************************************************/

/*****************************************************************************
* STATIC FUNCTIONS
*****************************************************************************/

/*****************************************************************************
* GLOBAL FUNCTIONS
*****************************************************************************/
bool decode_starknet_query(const uint8_t *data,
uint16_t data_size,
starknet_query_t *query_out) {
if (NULL == data || NULL == query_out || 0 == data_size) {
starknet_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
ERROR_DATA_FLOW_DECODING_FAILED);
return false;
}

// zeroise for safety from garbage in the query reference
memzero(query_out, sizeof(starknet_query_t));

/* Create a stream that reads from the buffer. */
pb_istream_t stream = pb_istream_from_buffer(data, data_size);

/* Now we are ready to decode the message. */
bool status = pb_decode(&stream, STARKNET_QUERY_FIELDS, query_out);

/* Send error to host if status is false*/
if (false == status) {
starknet_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
ERROR_DATA_FLOW_DECODING_FAILED);
}

return status;
}

bool encode_starknet_result(const starknet_result_t *result,
uint8_t *buffer,
uint16_t max_buffer_len,
size_t *bytes_written_out) {
if (NULL == result || NULL == buffer || NULL == bytes_written_out)
return false;

/* Create a stream that will write to our buffer. */
pb_ostream_t stream = pb_ostream_from_buffer(buffer, max_buffer_len);

/* Now we are ready to encode the message! */
bool status = pb_encode(&stream, STARKNET_RESULT_FIELDS, result);

if (true == status) {
*bytes_written_out = stream.bytes_written;
}

return status;
}

bool check_starknet_query(const starknet_query_t *query,
pb_size_t exp_query_tag) {
if ((NULL == query) || (exp_query_tag != query->which_request)) {
starknet_send_error(ERROR_COMMON_ERROR_CORRUPT_DATA_TAG,
ERROR_DATA_FLOW_INVALID_QUERY);
return false;
}
return true;
}

starknet_result_t init_starknet_result(pb_size_t result_tag) {
starknet_result_t result = STARKNET_RESULT_INIT_ZERO;
result.which_response = result_tag;
return result;
}

void starknet_send_error(pb_size_t which_error, uint32_t error_code) {
starknet_result_t result =
init_starknet_result(STARKNET_RESULT_COMMON_ERROR_TAG);
result.common_error = init_common_error(which_error, error_code);
starknet_send_result(&result);
}

void starknet_send_result(const starknet_result_t *result) {
// TODO: Set the options file for all
uint8_t buffer[1700] = {0};
size_t bytes_encoded = 0;
ASSERT(
encode_starknet_result(result, buffer, sizeof(buffer), &bytes_encoded));
send_response_to_host(&buffer[0], bytes_encoded);
}

bool starknet_get_query(starknet_query_t *query, pb_size_t exp_query_tag) {
evt_status_t event = get_events(EVENT_CONFIG_USB, MAX_INACTIVITY_TIMEOUT);

if (true == event.p0_event.flag) {
return false;
}

if (!decode_starknet_query(
event.usb_event.p_msg, event.usb_event.msg_size, query)) {
return false;
}

if (!check_starknet_query(query, exp_query_tag)) {
return false;
}

return true;
}
Loading
Loading