decodeSSS static method

Uint8List decodeSSS(
  1. List<Uint8List> parts
)

Implementation

static Uint8List decodeSSS(List<Uint8List> parts) {
  if (parts.length < 1) throw Exception('No parts provided');
  int shareLen = parts[0].length;
  if (shareLen < 2) throw Exception('Invalid share length');
  BytesBuilder bb = BytesBuilder();
  List<int> xs = [];
  List<List<int>> ys = [];
  for (int i = 0; i < parts.length; i++) {
    Uint8List bytes = parts[i];
    if (bytes.length != shareLen) {
      throw Exception('Inconsistent share lengths');
    }
    int x = bytes[0];
    if (x < 1 || x > 255) {
      throw Exception('Invalid share index $x');
    }
    xs.add(x);
    ys.add(bytes.sublist(1));
  }

  List<int> lis = [];
  for (int i = 0; i < xs.length; i++) {
    lis.add(_computeLi0(i, xs));
  }

  int numChunks = shareLen - 1;
  for (int j = 0; j < numChunks; j++) {
    int result = 0;
    for (int i = 0; i < xs.length; i++) {
      int y = ys[i][j];
      int contrib = _gfMult(y, lis[i]);
      result = _gfAdd(result, contrib);
    }
    bb.addByte(result);
  }

  return bb.takeBytes();
}