Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

feat: add syscalls using Barreto-Naehrig (BN) curve construction #134

Merged
merged 2 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 27 additions & 0 deletions c/generator_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
#define GW_SYS_PAY_FEE 3501
#define GW_SYS_LOG 3502
#define GW_SYS_RECOVER_ACCOUNT 3503
/* Syscall for make use the Barreto-Naehrig (BN) curve construction */
#define GW_SYS_BN_ADD 3601
#define GW_SYS_BN_MUL 3602
#define GW_SYS_BN_PAIRING 3603

typedef struct gw_context_t {
/* verification context */
Expand All @@ -57,6 +61,9 @@ typedef struct gw_context_t {
gw_store_data_fn sys_store_data;
gw_get_block_hash_fn sys_get_block_hash;
gw_recover_account_fn sys_recover_account;
gw_bn_add sys_bn_add;
gw_bn_mul sys_bn_mul;
gw_bn_pairing sys_bn_pairing;
gw_log_fn sys_log;
gw_pay_fee_fn sys_pay_fee;
gw_get_registry_address_by_script_hash_fn
Expand Down Expand Up @@ -346,6 +353,23 @@ int sys_recover_account(struct gw_context_t *ctx, uint8_t message[32],
return ret;
}

int sys_bn_add(const uint8_t *input, const size_t input_size, uint8_t *output) {
volatile uint64_t output_len = 64;
return syscall(GW_SYS_BN_ADD, output, &output_len, 0, input, input_size, 0);
}

int sys_bn_mul(const uint8_t *input, const size_t input_size, uint8_t *output) {
volatile uint64_t output_len = 64;
return syscall(GW_SYS_BN_MUL, output, &output_len, 0, input, input_size, 0);
}

int sys_bn_pairing(const uint8_t *input, const size_t input_size,
uint8_t *output) {
volatile uint64_t output_size = 32;
return syscall(GW_SYS_BN_PAIRING, output, &output_size, 0 /* offset = 0 */,
input, input_size, 0);
}

int sys_log(gw_context_t *ctx, uint32_t account_id, uint8_t service_flag,
uint64_t data_length, const uint8_t *data) {
if (ctx == NULL) {
Expand Down Expand Up @@ -415,6 +439,9 @@ int gw_context_init(gw_context_t *ctx) {
ctx->sys_load_data = sys_load_data;
ctx->sys_get_block_hash = sys_get_block_hash;
ctx->sys_recover_account = sys_recover_account;
ctx->sys_bn_add = sys_bn_add;
ctx->sys_bn_mul = sys_bn_mul;
ctx->sys_bn_pairing = sys_bn_pairing;
ctx->sys_pay_fee = sys_pay_fee;
ctx->sys_log = sys_log;
ctx->sys_get_registry_address_by_script_hash =
Expand Down
36 changes: 36 additions & 0 deletions c/gw_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,42 @@ int gw_verify_sudt_account(struct gw_context_t *ctx, uint32_t sudt_id);
typedef int (*gw_create_fn)(struct gw_context_t *ctx, uint8_t *script,
uint64_t script_len, uint32_t *account_id);

/**
* @param input two curve points (x, y)
* @param output curve point x + y, where + is point addition on the elliptic
* curve
*
* Fails on invalid input
*/
typedef int (*gw_bn_add)(const uint8_t *input, const size_t input_size,
uint8_t *output);

/**
* @param Input two curve points (x, y)
* @param output curve point s * x, where * is the scalar multiplication on the
* elliptic curve
*
* Fails on invalid input
*/
typedef int (*gw_bn_mul)(const uint8_t *input, const size_t input_size,
uint8_t *output);

/**
* @param input Input: (a1, b1, a2, b2, ..., ak, bk) from (G_1 x G_2)^k
* Note that k is the input_size divided by 192
* @param output curve point s * x, where * is the scalar multiplication on the
* elliptic curve
*
* @return Empty input is valid and results in returning one.
*
* Fails on:
* 1. the input_size is not a multiple of 192
* 2. any of the inputs are not elements of the respective group are not
* encoded correctly
*/
typedef int (*gw_bn_pairing)(const uint8_t *input, const size_t input_size,
uint8_t *output);

/**
* Load value by key from current contract account
*
Expand Down
1 change: 1 addition & 0 deletions c/gw_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#define GW_ERROR_NOT_FOUND 83
#define GW_ERROR_RECOVER 84
#define GW_ERROR_ACCOUNT_NOT_EXISTS 85
#define GW_UNIMPLEMENTED 86

/* sUDT errors */
#define GW_SUDT_ERROR_INSUFFICIENT_BALANCE 92
Expand Down
22 changes: 22 additions & 0 deletions c/validator_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ typedef struct gw_context_t {
gw_recover_account_fn sys_recover_account;
gw_log_fn sys_log;
gw_pay_fee_fn sys_pay_fee;
gw_bn_add sys_bn_add;
gw_bn_mul sys_bn_mul;
gw_bn_pairing sys_bn_pairing;
gw_get_registry_address_by_script_hash_fn
sys_get_registry_address_by_script_hash;
gw_get_script_hash_by_registry_address_fn
Expand Down Expand Up @@ -548,6 +551,22 @@ int sys_recover_account(gw_context_t *ctx, uint8_t message[32],
return GW_FATAL_SIGNATURE_CELL_NOT_FOUND;
}

int sys_bn_add(const uint8_t *input, const size_t input_size, uint8_t *output) {
// TODO
return GW_UNIMPLEMENTED;
}

int sys_bn_mul(const uint8_t *input, const size_t input_size, uint8_t *output) {
// TODO
return GW_UNIMPLEMENTED;
}

int sys_bn_pairing(const uint8_t *input, const size_t input_size,
uint8_t *output) {
// TODO
return GW_UNIMPLEMENTED;
}

int sys_create(gw_context_t *ctx, uint8_t *script, uint64_t script_len,
uint32_t *account_id) {
if (ctx == NULL) {
Expand Down Expand Up @@ -1595,6 +1614,9 @@ int gw_context_init(gw_context_t *ctx) {
ctx->sys_load_data = sys_load_data;
ctx->sys_get_block_hash = sys_get_block_hash;
ctx->sys_recover_account = sys_recover_account;
ctx->sys_bn_add = sys_bn_add;
ctx->sys_bn_mul = sys_bn_mul;
ctx->sys_bn_pairing = sys_bn_pairing;
ctx->sys_log = sys_log;
ctx->sys_pay_fee = sys_pay_fee;
ctx->sys_get_registry_address_by_script_hash =
Expand Down