2020-12-29 17:15:51 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* Copyright (c) 2017 Andrew Poelstra *
|
|
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
|
|
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
|
|
|
|
***********************************************************************/
|
2018-07-09 11:17:44 +00:00
|
|
|
|
2021-06-14 14:55:38 +00:00
|
|
|
#ifndef SECP256K1_SCRATCH_IMPL_H
|
|
|
|
#define SECP256K1_SCRATCH_IMPL_H
|
2018-07-09 11:17:44 +00:00
|
|
|
|
2019-05-28 12:23:28 +00:00
|
|
|
#include "util.h"
|
2018-07-09 11:17:44 +00:00
|
|
|
#include "scratch.h"
|
|
|
|
|
2023-12-06 23:21:38 +00:00
|
|
|
static size_t rustsecp256k1_v0_9_1_scratch_checkpoint(const rustsecp256k1_v0_9_1_callback* error_callback, const rustsecp256k1_v0_9_1_scratch* scratch) {
|
|
|
|
if (rustsecp256k1_v0_9_1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
|
|
|
|
rustsecp256k1_v0_9_1_callback_call(error_callback, "invalid scratch space");
|
2018-07-09 11:17:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2019-05-28 12:23:28 +00:00
|
|
|
return scratch->alloc_size;
|
2018-07-09 11:17:44 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 23:21:38 +00:00
|
|
|
static void rustsecp256k1_v0_9_1_scratch_apply_checkpoint(const rustsecp256k1_v0_9_1_callback* error_callback, rustsecp256k1_v0_9_1_scratch* scratch, size_t checkpoint) {
|
|
|
|
if (rustsecp256k1_v0_9_1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
|
|
|
|
rustsecp256k1_v0_9_1_callback_call(error_callback, "invalid scratch space");
|
2019-05-28 12:23:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (checkpoint > scratch->alloc_size) {
|
2023-12-06 23:21:38 +00:00
|
|
|
rustsecp256k1_v0_9_1_callback_call(error_callback, "invalid checkpoint");
|
2019-05-28 12:23:28 +00:00
|
|
|
return;
|
2018-07-09 11:17:44 +00:00
|
|
|
}
|
2019-05-28 12:23:28 +00:00
|
|
|
scratch->alloc_size = checkpoint;
|
2018-07-09 11:17:44 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 23:21:38 +00:00
|
|
|
static size_t rustsecp256k1_v0_9_1_scratch_max_allocation(const rustsecp256k1_v0_9_1_callback* error_callback, const rustsecp256k1_v0_9_1_scratch* scratch, size_t objects) {
|
|
|
|
if (rustsecp256k1_v0_9_1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
|
|
|
|
rustsecp256k1_v0_9_1_callback_call(error_callback, "invalid scratch space");
|
2020-09-15 01:39:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Ensure that multiplication will not wrap around */
|
|
|
|
if (ALIGNMENT > 1 && objects > SIZE_MAX/(ALIGNMENT - 1)) {
|
2019-05-28 12:23:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (scratch->max_size - scratch->alloc_size <= objects * (ALIGNMENT - 1)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return scratch->max_size - scratch->alloc_size - objects * (ALIGNMENT - 1);
|
2018-07-09 11:17:44 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 23:21:38 +00:00
|
|
|
static void *rustsecp256k1_v0_9_1_scratch_alloc(const rustsecp256k1_v0_9_1_callback* error_callback, rustsecp256k1_v0_9_1_scratch* scratch, size_t size) {
|
2018-07-09 11:17:44 +00:00
|
|
|
void *ret;
|
2020-09-15 01:39:26 +00:00
|
|
|
size_t rounded_size;
|
|
|
|
|
|
|
|
rounded_size = ROUND_TO_ALIGN(size);
|
|
|
|
/* Check that rounding did not wrap around */
|
|
|
|
if (rounded_size < size) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
size = rounded_size;
|
2019-05-28 12:23:28 +00:00
|
|
|
|
2023-12-06 23:21:38 +00:00
|
|
|
if (rustsecp256k1_v0_9_1_memcmp_var(scratch->magic, "scratch", 8) != 0) {
|
|
|
|
rustsecp256k1_v0_9_1_callback_call(error_callback, "invalid scratch space");
|
2019-05-28 12:23:28 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-07-09 11:17:44 +00:00
|
|
|
|
2019-05-28 12:23:28 +00:00
|
|
|
if (size > scratch->max_size - scratch->alloc_size) {
|
2018-07-09 11:17:44 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-28 12:23:28 +00:00
|
|
|
ret = (void *) ((char *) scratch->data + scratch->alloc_size);
|
2018-07-09 11:17:44 +00:00
|
|
|
memset(ret, 0, size);
|
2019-05-28 12:23:28 +00:00
|
|
|
scratch->alloc_size += size;
|
2018-07-09 11:17:44 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|