Skip to content

Commit cd809fe

Browse files
committed
abi: bring back eth_abi_mpint
1 parent 4f4fb98 commit cd809fe

File tree

4 files changed

+74
-31
lines changed

4 files changed

+74
-31
lines changed

include/ethc/abi.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
#include "ethc-common.h"
99
#include <stddef.h>
1010
#include <stdint.h>
11+
#include <tommath.h>
1112

1213
#define ETH_ABI_WORD_SIZE 32
1314
#define ETH_ABI_DYNAMIC_TYPE_POOL_SIZE 64
@@ -155,7 +156,7 @@ ETHC_EXPORT ETH_OP eth_abi_uint64(struct eth_abi *abi, uint64_t *d);
155156
* @param[inout] mpz Initialized mpz_t to read/write the data from/to.
156157
* @return `1` on success, `-1` otherwise.
157158
*/
158-
// ETHC_EXPORT ETH_OP eth_abi_mpint(struct eth_abi *abi, mpz_t mpz);
159+
ETHC_EXPORT ETH_OP eth_abi_mpint(struct eth_abi *abi, mp_int *mpint);
159160

160161
/*!
161162
* @brief Encodes/decodes address.

src/abi.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,40 @@ ETH_OP eth_abi_bytes(struct eth_abi *abi, uint8_t *bytes, size_t *len) {
704704
return ETH_ERR_INVALID_ARGS;
705705
}
706706

707+
ETH_OP eth_abi_mpint(struct eth_abi *abi, mp_int *mpint) {
708+
struct ethc_abi_dynamic_type *ctype = NULL;
709+
uint8_t buf[32] = {0}, size = 0;
710+
ETH_OP op;
711+
712+
if (abi == NULL || mpint == NULL)
713+
return ETH_ERR_INVALID_ARGS;
714+
715+
ethc_abi_type_stack_peek(&ctype, &abi->stack);
716+
717+
if (abi->m == ETH_ABI_ENCODE) {
718+
size = mp_unsigned_bin_size(mpint);
719+
if (size == 0 || size > 32)
720+
return ETH_ERR_INVALID_ARGS;
721+
722+
if (mp_to_unsigned_bin(mpint, buf + (32 - size)) != MP_OKAY)
723+
return ETH_ERR_UNKNOWN;
724+
725+
return eth_abi_bytes32(abi, buf);
726+
}
727+
728+
if (abi->m == ETH_ABI_DECODE) {
729+
if ((op = eth_abi_bytes32(abi, buf)) != ETH_OK)
730+
return op;
731+
732+
if (mp_read_unsigned_bin(mpint, buf, 32) != MP_OKAY)
733+
return ETH_ERR_INVALID_ARGS;
734+
735+
return ETH_OK;
736+
}
737+
738+
return ETH_ERR_INVALID_ARGS;
739+
}
740+
707741
ETH_OP eth_abi_from_hex(struct eth_abi *abi, char *hex, int len) {
708742
struct ethc_abi_dynamic_type *ntype = NULL;
709743
uint8_t *buf = NULL;

test/run-tests.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ int main() {
4343
test_eth_abi_bytes32();
4444
test_eth_abi_address();
4545
test_eth_abi_bytes();
46+
test_eth_abi_mpint();
4647
test_eth_abi_array();
4748
test_eth_abi_call();
4849

test/test-eth-abi.c

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -245,36 +245,43 @@ void test_eth_abi_int64(void) {
245245
ok(d2 == -0x7cf23097b81adc30 && d3 == 0x45fd66a734b67d50, "encode -0x7cf23097b81adc30 and 0x45fd66a734b67d50");
246246
}
247247

248-
// void test_eth_abi_mpint(void) {
249-
// struct eth_abi abi0, abi1;
250-
// size_t hexlen;
251-
// char *hex;
252-
// mpz_t mpz0, mpz1, mpz2;
253-
//
254-
// mpz_init_set_str(mpz0, "0xff", 0);
255-
// mpz_init_set_str(mpz1, "0xfff", 0);
256-
// mpz_init_set_str(mpz2, "0xbbffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0);
257-
//
258-
// ok(eth_abi_init(&abi0, ETH_ABI_ENCODE) == 1);
259-
// ok(eth_abi_mpint(&abi0, mpz0) == 1);
260-
// ok(eth_abi_mpint(&abi0, mpz1) == 1);
261-
// ok(eth_abi_mpint(&abi0, mpz2) == 1);
262-
// ok(eth_abi_to_hex(&abi0, &hex, &hexlen) == 1);
263-
//
264-
// is(hex, "00000000000000000000000000000000000000000000000000000000000000ff"
265-
// "0000000000000000000000000000000000000000000000000000000000000fff"
266-
// "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
267-
// mpz_clears(mpz0, mpz1, mpz2, NULL);
268-
// free(hex);
269-
//
270-
// ok(eth_abi_from_hex(&abi1,
271-
// "0000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffff", -1) == 1);
272-
// mpz_init_set_str(mpz0, "0x0000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0);
273-
// mpz_init(mpz1);
274-
// ok(eth_abi_mpint(&abi1, mpz1) == 1);
275-
// ok(mpz_cmp(mpz0, mpz1) == 0);
276-
// mpz_clears(mpz0, mpz1, NULL);
277-
// }
248+
void test_eth_abi_mpint(void) {
249+
struct eth_abi abi0={0}, abi1={0};
250+
mp_int int0, int1, int2, int3, int4;
251+
size_t hexlen;
252+
char *hex;
253+
254+
diag("eth_abi_mpint()");
255+
256+
assert(mp_init_multi(&int0, &int1, &int2, &int3, &int4, NULL) == MP_OKAY);
257+
258+
assert(mp_read_radix(&int0, "ff", 16) == MP_OKAY);
259+
assert(mp_read_radix(&int1, "fff", 16) == MP_OKAY);
260+
assert(mp_read_radix(&int2, "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16) == MP_OKAY);
261+
262+
assert(eth_abi_init(&abi0, ETH_ABI_ENCODE) == ETH_OK);
263+
264+
assert(eth_abi_mpint(&abi0, &int0) == ETH_OK);
265+
assert(eth_abi_mpint(&abi0, &int1) == ETH_OK);
266+
assert(eth_abi_mpint(&abi0, &int2) == ETH_OK);
267+
268+
assert(eth_abi_to_hex(&abi0, &hex, &hexlen) == ETH_OK);
269+
is(hex, "00000000000000000000000000000000000000000000000000000000000000ff"
270+
"0000000000000000000000000000000000000000000000000000000000000fff"
271+
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
272+
"encode 0xff, 0xfff, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
273+
free(hex);
274+
275+
276+
assert(eth_abi_from_hex(&abi1,
277+
"0000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffff", -1) == ETH_OK);
278+
assert(mp_read_radix(&int3, "0000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16) == ETH_OK);
279+
280+
assert(eth_abi_mpint(&abi1, &int4) == ETH_OK);
281+
282+
ok(mp_cmp(&int3, &int4) == MP_EQ, "decode 0x0000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffff");
283+
mp_clear_multi(&int0, &int1, &int2, &int3, &int4, NULL);
284+
}
278285

279286
void test_eth_abi_bytes8() {
280287
struct eth_abi abi0={0}, abi1={0};

0 commit comments

Comments
 (0)