Skip to content

Commit

Permalink
fixup! feat: add libdriver
Browse files Browse the repository at this point in the history
  • Loading branch information
mpolitzer committed Nov 16, 2023
1 parent 573dc5d commit bf631fc
Show file tree
Hide file tree
Showing 28 changed files with 225 additions and 140 deletions.
8 changes: 8 additions & 0 deletions linux/libcmt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
build/
doc/theme
doc/html
*.o
*.d

tools/funsel
tools/merkle-table
8 changes: 5 additions & 3 deletions linux/libcmt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ TARGET_PREFIX ?= $(PREFIX)

TARGET_CC := $(CROSS_COMPILE)gcc
TARGET_AR := $(CROSS_COMPILE)ar
TARGET_CFLAGS := -O2 -g -Wall -pedantic -Wextra -Iinclude -Iinclude/libcmt/ioctl
CFLAGS := -O2 -g -Wall -pedantic -Wextra -fsanitize=address,undefined -Iinclude -Iinclude/libcmt/mock
TARGET_CFLAGS := -Wvla -O2 -g -Wall -pedantic -Wextra -Iinclude -Iinclude/libcmt/ioctl
CFLAGS := -Wvla -O2 -g -Wall -pedantic -Wextra -fsanitize=address,undefined -Iinclude -Iinclude/libcmt/mock

mock_BINS := build/libcmt_mock/yield-driver \
build/libcmt_mock/rollup-driver \
build/libcmt_mock/rollup \
libcmt_mock.a

ioctl_BINS := build/libcmt/yield-driver \
build/libcmt/rollup-driver \
build/libcmt/rollup libcmt.a
build/libcmt/rollup \
libcmt.a

tests_BINS := build/libcmt_mock/abi \
build/libcmt_mock/keccak \
Expand Down
2 changes: 1 addition & 1 deletion linux/libcmt/examples/abi_encode_001.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <libcmt/abi.h>
#include <libcmt/keccak.h>

int encode(cmt_buf_t wr[1], size_t n, uint8_t data[n])
int encode(cmt_buf_t wr[1], size_t n, uint8_t *data)
{
cmt_buf_t of[1];
uint32_t example = cmt_keccak_funsel("Example(bytes)");
Expand Down
4 changes: 2 additions & 2 deletions linux/libcmt/examples/abi_encode_002.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

int encode(cmt_buf_t wr[1]
,uint8_t address[CMT_ADDRESS_LENGTH]
,size_t n0, uint8_t data0[n0]
,size_t n1, uint8_t data1[n1])
,size_t n0, uint8_t *data0
,size_t n1, uint8_t *data1)
{
cmt_buf_t of[2][1];
void *base = wr->begin + 4; // skip function selector
Expand Down
2 changes: 1 addition & 1 deletion linux/libcmt/examples/abi_multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int use_000(cmt_buf_t rd[1], cmt_buf_t wr[1])
return encode_000(wr, address);
}

int encode_001(cmt_buf_t wr[1], size_t n, uint8_t data[n])
int encode_001(cmt_buf_t wr[1], size_t n, uint8_t *data)
{
cmt_buf_t of[1];
void *base = wr->begin + 4; // skip function selector
Expand Down
1 change: 1 addition & 0 deletions linux/libcmt/examples/yield-driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
if (cmt_yield_driver_progress(Y, progress * 10))
return EXIT_FAILURE;
cmt_yield_driver_fini(Y);
} else {
fprintf(stderr, "usage: %s --progress=<float>\n", argv[0]);
exit(EXIT_FAILURE);
Expand Down
10 changes: 5 additions & 5 deletions linux/libcmt/include/libcmt/abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ cmt_abi_encode_uint(size_t n, const void *data, uint8_t out[CMT_WORD_LENGTH]);
* - EDOM @p n is too large.
* @note use @ref cmt_abi_encode_uint instead */
int
cmt_abi_encode_uint_nr(size_t n, const uint8_t data[n], uint8_t out[CMT_WORD_LENGTH]);
cmt_abi_encode_uint_nr(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_LENGTH]);

/** Encode @p n bytes of @p data into @p out (up to 32) in normal order.
*
Expand All @@ -368,15 +368,15 @@ cmt_abi_encode_uint_nr(size_t n, const uint8_t data[n], uint8_t out[CMT_WORD_LEN
* - EDOM @p n is too large.
* @note use @ref cmt_abi_encode_uint instead */
int
cmt_abi_encode_uint_nn(size_t n, const uint8_t data[n], uint8_t out[CMT_WORD_LENGTH]);
cmt_abi_encode_uint_nn(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_LENGTH]);

/** Decode @p n bytes of @p data into @p out (up to 32).
*
* @param [in] data integer value to decode into @p out
* @param [in] n size of @p data in bytes
* @param [out] out decoded output */
int
cmt_abi_decode_uint(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t out[n]);
cmt_abi_decode_uint(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out);

/** Decode @p n bytes of @p data into @p out (up to 32) in reverse order.
*
Expand All @@ -385,7 +385,7 @@ cmt_abi_decode_uint(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t out[n
* @param [out] out decoded output
* @note if in doubt, use @ref cmt_abi_decode_uint */
int
cmt_abi_decode_uint_nr(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t out[n]);
cmt_abi_decode_uint_nr(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out);

/** Decode @p n bytes of @p data into @p out (up to 32) in normal order.
*
Expand All @@ -394,7 +394,7 @@ cmt_abi_decode_uint_nr(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t ou
* @param [out] out decoded output
* @note if in doubt, use @ref cmt_abi_decode_uint */
int
cmt_abi_decode_uint_nn(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t out[n]);
cmt_abi_decode_uint_nn(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out);

#endif /* CMT_ABI_H */
/** @} */
20 changes: 2 additions & 18 deletions linux/libcmt/include/libcmt/buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,15 @@
#include<stdint.h>
#include<stddef.h>

/** A slice of contiguous memory from @b begin to @b end.
* Size can be taken with: `q - p`.
/** A slice of contiguous memory from @b begin to @b end, as an open range.
* Size can be taken with: `end - begin`.
*
* `begin == end` indicate an empty buffer */
typedef struct {
uint8_t *begin; /**< begin of memory region */
uint8_t *end; /**< end of memory region */
} cmt_buf_t;

/** Declare a cmt_buf_t with stack backed memory.
* @param [in] N - size in bytes
* @note don't port */
#define CMT_BUF_DECL(S, L) cmt_buf_t S[1] = {{ \
.begin = (uint8_t [L]){0}, \
.end = (S)->begin + L, \
}}

/** Declare a cmt_buf_t with parameters backed memory.
* @param [in] L - size in bytes
* @note don't port */
#define CMT_BUF_DECL3(S, L, P) cmt_buf_t S[1] = {{ \
.begin = P, \
.end = (S)->begin + L, \
}}

/** Initialize @p me buffer backed by @p data, @p length bytes in size
*
* @param [out] me a uninitialized instance
Expand Down
2 changes: 1 addition & 1 deletion linux/libcmt/include/libcmt/ioctl/rollup-driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ struct cmt_rollup_driver {
uint64_t length;
} tx[1], rx[1];
};
#include <libcmt/rollup-driver-api.h>
#include "libcmt/rollup-driver-api.h"
#endif /* EVM_ROLLUP_DRIVER_H */
2 changes: 1 addition & 1 deletion linux/libcmt/include/libcmt/ioctl/yield-driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
struct cmt_yield_driver {
int fd;
};
#include <libcmt/yield-driver-api.h>
#include "libcmt/yield-driver-api.h"
#endif /* CMT_YIELD_DRIVER_H */
14 changes: 7 additions & 7 deletions linux/libcmt/include/libcmt/keccak.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ void cmt_keccak_init(cmt_keccak_t *state);

/** Hash @b n bytes of @b data
*
* @param [in,out] state initialize the hasher state
* @param [in] n bytes in @b data to process
* @param [in] data data to hash */
* @param [in,out] state initialize the hasher state
* @param [in] length bytes in @b data to process
* @param [in] data data to hash */
void cmt_keccak_update(cmt_keccak_t *state, size_t n, const void *data);

/** Finalize the hash calculation from @b state and store it in @b md
Expand All @@ -99,9 +99,9 @@ void cmt_keccak_final(cmt_keccak_t *state, void *md);

/** Hash all @b n bytes of @b data at once
*
* @param [in] n - bytes in @b data to process
* @param [in] data - data to hash
* @param [out] md - 32bytes to store the computed hash
* @param [in] length bytes in @b data to process
* @param [in] data data to hash
* @param [out] md 32bytes to store the computed hash
* @return pointer to @b md
*
* Equivalent to:
Expand All @@ -111,7 +111,7 @@ void cmt_keccak_final(cmt_keccak_t *state, void *md);
* cmt_keccak_final(&st, md);
* return md;
* @endcode */
uint8_t *cmt_keccak_data(size_t n, const void *data
uint8_t *cmt_keccak_data(size_t length, const void *data
,uint8_t md[CMT_KECCAK_LENGTH]);

/** Compute the function selector from the solidity declaration @p decl
Expand Down
10 changes: 5 additions & 5 deletions linux/libcmt/include/libcmt/merkle.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ typedef struct {
/** Initialize a @ref cmt_merkle_t tree state.
*
* @param [in] me uninitialized state */
void cmt_merkle_init(cmt_merkle_t me[1]);
void cmt_merkle_init(cmt_merkle_t *me);

/** Finalize a @ref cmt_merkle_t tree state.
*
* @param [in] me initialized state
*
* @note use of @p me after this call is undefined behavior. */
void cmt_merkle_fini(cmt_merkle_t me[1]);
void cmt_merkle_fini(cmt_merkle_t *me);

/** Append a leaf node
*
Expand All @@ -39,20 +39,20 @@ void cmt_merkle_fini(cmt_merkle_t me[1]);
* @return
* - 0 success
* - -ENOBUFS indicates the tree is full */
int cmt_merkle_push_back(cmt_merkle_t me[1], uint8_t hash[CMT_KECCAK_LENGTH]);
int cmt_merkle_push_back(cmt_merkle_t *me, uint8_t hash[CMT_KECCAK_LENGTH]);

/** Compute the keccak-256 hash of @p data and append it as a leaf node
*
* @param [in,out] me initialized state
* @return
* - 0 success
* - -ENOBUFS indicates that the tree is full */
int cmt_merkle_push_back_data(cmt_merkle_t me[1], size_t n, void *data);
int cmt_merkle_push_back_data(cmt_merkle_t *me, size_t n, void *data);

/** Retrieve the root hash of the merkle tree
*
* @param [in] me initialized state
* @param [out] root root hash of the merkle tree */
void cmt_merkle_get_root_hash(cmt_merkle_t me[1], uint8_t root[CMT_KECCAK_LENGTH]);
void cmt_merkle_get_root_hash(cmt_merkle_t *me, uint8_t root[CMT_KECCAK_LENGTH]);

#endif /* CMT_MERKLE_H */
2 changes: 1 addition & 1 deletion linux/libcmt/include/libcmt/mock/rollup-driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ struct cmt_rollup_driver {
size_t len;
} tx[1], rx[1];
};
#include <libcmt/rollup-driver-api.h>
#include "libcmt/rollup-driver-api.h"
#endif /* CMT_ROLLUP_DRIVER_H */
2 changes: 1 addition & 1 deletion linux/libcmt/include/libcmt/mock/yield-driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ struct cmt_yield_driver {
int dummy;
};

#include <libcmt/yield-driver-api.h>
#include "libcmt/yield-driver-api.h"
#endif /* CMT_YIELD_DRIVER_H */
44 changes: 19 additions & 25 deletions linux/libcmt/include/libcmt/rollup-driver-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ enum {
CMT_ROLLUP_INSPECT_STATE = 1, /**< @ref rollup_driver_accept_and_wait_next_input inspect state */
};

/** contents of @ref rollup_driver_t are implementation specific.
/** contents of @ref cmt_rollup_driver are implementation specific.
* Define it in @p <rollup.h> */
typedef struct cmt_rollup_driver cmt_rollup_driver_t;

/** Open the rollup device and initialize the driver
*
* @param [in] me A uninitialized @ref rollup_driver_t state
* @param [in] me A uninitialized @ref cmt_rollup_driver state
* @returns
* - 0 on success
* - negative value on error. Any of -ENOBUFS, or errno values from `open` and `ioctl`. */
Expand All @@ -34,54 +34,48 @@ int cmt_rollup_driver_init(struct cmt_rollup_driver *me);
/** Release the driver resources and close the rollup device
*
* @param [in] me A sucessfuly initialized state by @ref rollup_driver_init
* @returns
* - 0 on success
* - Any of `close` errno values to indicate an error
* - negative value on error. errno values from `close`.
* @note usage of @p me after this call is a BUG and will cause undefined behaviour */
int cmt_rollup_driver_fini(struct cmt_rollup_driver *me);
void cmt_rollup_driver_fini(struct cmt_rollup_driver *me);

/** Create a output, with the first @p n bytes of @ref cmt_rollup_driver_t.tx
/** Create a output, with the first @p length bytes of @ref cmt_rollup_driver_t.tx
*
* @param [in] me A sucessfuly initialized state by @ref rollup_driver_init
* @param [in] n size in bytes of the output, contents from @p tx buffer.
* @param [in] length size in bytes of the output, contents from @p tx buffer.
* @return
* - 0 on success
* - negative value on error. -ENOBUFS, or errno values from `ioctl` */
int cmt_rollup_driver_write_output(struct cmt_rollup_driver *me, uint64_t n);
int cmt_rollup_driver_write_output(struct cmt_rollup_driver *me, uint64_t length);

/** Create a report, with the first @p n bytes of @ref rollup_driver_t.tx
/** Create a report, with the first @p length bytes of @ref cmt_rollup_driver.tx
*
* @param [in] me A sucessfuly initialized state by @ref rollup_driver_init
* @param [in] n size in bytes of the output, contents from @p tx buffer.
* @param [in] length size in bytes of the output, contents from @p tx buffer.
* @return
* - 0 on success
* - negative value on error. -ENOBUFS, or errno values from `ioctl` */
int cmt_rollup_driver_write_report(struct cmt_rollup_driver *me, uint64_t n);
int cmt_rollup_driver_write_report(struct cmt_rollup_driver *me, uint64_t length);

/** Create a exception, with the first @p n bytes of @ref rollup_driver_t.tx
/** Create a exception, with the first @p length bytes of @ref cmt_rollup_driver tx
*
* @param [in] me A sucessfuly initialized state by @ref rollup_driver_init
* @param [in] verifiable @p true if this output should be verifiable
* @param [in] n size in bytes of the message, contents from @p tx buffer.
* @param [in] length size in bytes of the message, contents from @p tx buffer.
* @return
* - 0 on success
* - negative value on error. -ENOBUFS, or errno values from `ioctl` */
int cmt_rollup_driver_write_exception(struct cmt_rollup_driver *me, uint64_t n);
* - negative value on error. -ENOBUFS, or -errno values from `ioctl` */
int cmt_rollup_driver_write_exception(struct cmt_rollup_driver *me, uint64_t length);

/** Accept this input, wait for the next one.
*
* @param [in] me A sucessfuly initialized state by @ref rollup_driver_init
* @param [in] accept @p true if this input was processed successfuly
* @param [out] n size in bytes of the current input.
* @param [out] length size in bytes of the newly received input.
* @return
* - ROLLUP_ADVANCE_STATE On advancing state
* - ROLLUP_INSPECT_STATE On inspecting state
* - negative value on error.
*
* @note This call expects the tx buffer to have the merkle root hash of the
* outputs. It as part of its interface. */
int cmt_rollup_driver_accept_and_wait_next_input(struct cmt_rollup_driver *me, size_t *n);
int cmt_rollup_driver_accept_and_wait_next_input(struct cmt_rollup_driver *me, size_t *length);

/** Revert state back to how it was the last time @ref
* rollup_driver_accept_and_wait_next_input got called and reject this input
Expand All @@ -94,21 +88,21 @@ int cmt_rollup_driver_revert(struct cmt_rollup_driver *me);

/** Retrieve the writable memory region @p tx
* @param [in] me A sucessfuly initialized state by @ref rollup_driver_init
* @param [out] max size of the region in bytes.
* @param [out] max size of the region in bytes (optional).
*
* @return
* - pointer to the buffer.
* @note memory is valid until @ref rollup_driver_fini is called. */
void *cmt_rollup_driver_get_tx(struct cmt_rollup_driver *me, size_t *max);
void *cmt_rollup_driver_get_tx(struct cmt_rollup_driver *me, size_t *length);

/** Retrieve the writable memory region @p rx
* @param [in] me A sucessfuly initialized state by @ref rollup_driver_init
* @param [out] max size of the region in bytes.
* @param [out] max size of the region in bytes (optional).
*
* @return
* - pointer to the buffer.
* @note memory is valid until @ref rollup_driver_fini is called. */
void *cmt_rollup_driver_get_rx(struct cmt_rollup_driver *me, size_t *max);
void *cmt_rollup_driver_get_rx(struct cmt_rollup_driver *me, size_t *length);

#endif /* ROLLUP_API_H */
/** @} */
Loading

0 comments on commit bf631fc

Please sign in to comment.