BlockchainCommons / bc-shamir

Shamir Secret Sharing reference library in C
Other
6 stars 15 forks source link

Simple case (secret recovery from two random key shares) fails #45

Closed ssantos21 closed 6 months ago

ssantos21 commented 6 months ago

The code below prints result: -104, which according to the function comment indicates an error. Why does the code below result in an error?

static void custom_test_shamir() {

  size_t share_len = 32;

  uint8_t keyshare01[share_len];
  if (!fill_random(keyshare01, sizeof(keyshare01))) {
      printf("Failed to generate randomness\n");
      return;
  }

  uint8_t keyshare02[share_len];
  if (!fill_random(keyshare02, sizeof(keyshare02))) {
      printf("Failed to generate randomness\n");
      return;
  }

  uint8_t threshold = 2;

  uint8_t* shares[threshold];

  shares[0] = keyshare01;
  shares[1] = keyshare02;

  const uint8_t recovery_share_indexes[] = {0, 1};

  uint8_t secret_data[share_len];

  int32_t result = recover_secret(threshold, recovery_share_indexes, (const uint8_t **)shares, share_len, secret_data);

  printf("result: %d\n", result);

}
wolfmcnally commented 6 months ago

This is an invalid test case. The implementation includes checksum bytes that cannot be matched by randomly-generated shares.

Please see test.c for valid test cases:

https://github.com/BlockchainCommons/bc-shamir/blob/master/test/test.c