shamir.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Low level API for Daan Sprenkels' Shamir secret sharing library
  3. * Copyright (c) 2017 Daan Sprenkels <hello@dsprenkels.com>
  4. * Copyright (c) 2019 SatoshiLabs
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  20. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Usage of this API is hazardous and is only reserved for beings with a
  25. * good understanding of the Shamir secret sharing scheme and who know how
  26. * crypto code is implemented. If you are unsure about this, use the
  27. * intermediate level API. You have been warned!
  28. */
  29. #ifndef __SHAMIR_H__
  30. #define __SHAMIR_H__
  31. #include <stdbool.h>
  32. #include <stddef.h>
  33. #include <stdint.h>
  34. #define SHAMIR_MAX_LEN 32
  35. /*
  36. * Computes f(x) given the Shamir shares (x_1, f(x_1)), ... , (x_m, f(x_m)).
  37. * The x coordinates of the shares must be pairwise distinct. Returns true on
  38. * success, otherwise false.
  39. * result: Array of length len where the evaluations of the polynomials in x
  40. * will be written.
  41. * result_index: The x coordinate of the result.
  42. * share_indices: Points to the array of integers x_1, ... , x_m.
  43. * share_values: Points to the array of y_1, ... , y_m, where each y_i is an
  44. * array of bytes of length len representing the evaluations of the
  45. * polynomials in x_i.
  46. * share_count: The number of shares m.
  47. * len: The length of the result array and of each of the y_1, ... , y_m arrays.
  48. *
  49. * The number of shares used to compute the result may be larger than the
  50. * required threshold.
  51. *
  52. * This function does *not* do *any* checking for integrity. If any of the
  53. * shares are not original, this will result in an invalid restored value.
  54. * All values written to `result` should be treated as secret. Even if some of
  55. * the shares that were provided as input were incorrect, the result *still*
  56. * allows an attacker to gain information about the correct result.
  57. *
  58. * This function treats `shares_values`, `share_indices` and `result` as secret
  59. * values. `share_count` is treated as a public value (for performance reasons).
  60. */
  61. bool shamir_interpolate(
  62. uint8_t* result,
  63. uint8_t result_index,
  64. const uint8_t* share_indices,
  65. const uint8_t** share_values,
  66. uint8_t share_count,
  67. size_t len);
  68. #endif /* __SHAMIR_H__ */