zkp_context.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Copyright (c) SatoshiLabs
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  18. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <assert.h>
  23. #include <stdatomic.h>
  24. #include <stdbool.h>
  25. #include "memzero.h"
  26. #include "rand.h"
  27. #include "zkp_context.h"
  28. #include "vendor/secp256k1-zkp/include/secp256k1.h"
  29. static uint8_t context_buffer[SECP256K1_CONTEXT_SIZE];
  30. static secp256k1_context *context;
  31. static volatile atomic_flag locked;
  32. // returns 0 on success
  33. int secp256k1_context_writable_randomize(secp256k1_context *context_writable) {
  34. uint8_t seed[32] = {0};
  35. random_buffer(seed, sizeof(seed));
  36. int returned = secp256k1_context_randomize(context_writable, seed);
  37. memzero(seed, sizeof(seed));
  38. if (returned != 1) {
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. bool zkp_context_is_initialized(void) { return context != NULL; }
  44. // returns 0 on success
  45. int zkp_context_init(void) {
  46. assert(context == NULL);
  47. const unsigned int context_flags =
  48. SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY;
  49. const size_t context_size =
  50. secp256k1_context_preallocated_size(context_flags);
  51. // Assert the context is as small as possible
  52. assert(context_size == SECP256K1_CONTEXT_SIZE);
  53. if (context_size == 0 || context_size > SECP256K1_CONTEXT_SIZE) {
  54. return 1;
  55. }
  56. context =
  57. secp256k1_context_preallocated_create(context_buffer, context_flags);
  58. if (context == NULL) {
  59. return 1;
  60. }
  61. secp256k1_context_writable_randomize(context);
  62. atomic_flag_clear(&locked);
  63. return 0;
  64. }
  65. void zkp_context_destroy(void) {
  66. assert(context != NULL);
  67. secp256k1_context_preallocated_destroy(context);
  68. memzero(context_buffer, sizeof(context_buffer));
  69. atomic_flag_clear(&locked);
  70. context = NULL;
  71. }
  72. const secp256k1_context *zkp_context_get_read_only(void) {
  73. assert(context != NULL);
  74. return context;
  75. }
  76. // returns NULL if context cannot be acquired
  77. secp256k1_context *zkp_context_acquire_writable(void) {
  78. assert(context != NULL);
  79. // We don't expect the context to be used by multiple threads
  80. if (atomic_flag_test_and_set(&locked)) {
  81. return NULL;
  82. }
  83. return context;
  84. }
  85. void zkp_context_release_writable(void) {
  86. assert(context != NULL);
  87. atomic_flag_clear(&locked);
  88. }