Skip to content

Commit

Permalink
Fix padding/reassembly to be more correct
Browse files Browse the repository at this point in the history
  • Loading branch information
Vagabond committed Jul 14, 2018
1 parent e01db38 commit 369e514
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
14 changes: 10 additions & 4 deletions c_src/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ if [ ! -d build ]; then
./autogen.sh
./configure --prefix=`pwd`/build --with-pic
fi
make -j
make install

if [ ! -f build/lib/libgf_complete.a ]; then
make -j
make install
fi

cd ../..

Expand All @@ -47,5 +50,8 @@ if [ ! -d build ]; then
autoreconf --force --install
LDFLAGS="-L`pwd`/../gf-complete/build/lib -fPIC" CPPFLAGS="-I`pwd`/../gf-complete/build/include" ./configure --prefix=`pwd`/build --enable-static --with-pic
fi
make -j
make install

if [ ! -f build/lib/libJerasure.a ]; then
make -j
make install
fi
30 changes: 22 additions & 8 deletions c_src/erasure.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,28 @@ encode(ErlNifEnv * env, int argc, const ERL_NIF_TERM argv[])
}
}

int bytes_per_shard = input.size / k;
int extra_bytes = input.size % k;


char *shards[k+m];

unsigned char *p = input.data;
for (int i = 0; i < k+m; i++) {
shards[i] = (char *)malloc(sizeof(char)*blocksize);
memset(shards[i], 0, blocksize);
if (i < k) {
if (i == 0) {
memcpy(shards[i]+remainder, input.data+(i*(blocksize)), blocksize - remainder);
} else {
memcpy(shards[i], input.data-remainder+(i*(blocksize)), blocksize);
memcpy(shards[i], p, bytes_per_shard);
p += bytes_per_shard;
if (extra_bytes > 0) {
memcpy(shards[i]+bytes_per_shard, p, 1);
p++;
extra_bytes--;
}
}
}


int w = 8;
int *matrix = reed_sol_vandermonde_coding_matrix(k, m, w);
jerasure_matrix_encode(k, m, w, matrix, shards, shards+k, blocksize);
Expand Down Expand Up @@ -188,6 +195,9 @@ decode(ErlNifEnv * env, int argc, const ERL_NIF_TERM argv[])
erasures = malloc(sizeof(int)*(k+m));
int j = 0;

int bytes_per_shard = totalsize / k;
int extra_bytes = totalsize % k;

// calculate the missing shards and fill them in with 0s
for (int i = 0; i < k+m; i++) {
if (shards[i] == NULL) {
Expand All @@ -211,12 +221,16 @@ decode(ErlNifEnv * env, int argc, const ERL_NIF_TERM argv[])

ERL_NIF_TERM decoded;
unsigned char* decoded_data = enif_make_new_binary(env, totalsize, &decoded);
memset(decoded_data, 0, totalsize);
unsigned char *p = decoded_data;

for (int i = 0; i < k; i++) {
if (i == 0) {
memcpy(decoded_data, shards[i]+remainder, blocksize - remainder);
} else {
memcpy(decoded_data-remainder+(i*blocksize), shards[i], blocksize);
memcpy(p, shards[i], bytes_per_shard);
p += bytes_per_shard;
if (extra_bytes > 0) {
memcpy(p, shards[i]+bytes_per_shard, 1);
extra_bytes--;
p++;
}
}

Expand Down
26 changes: 14 additions & 12 deletions eqc/encode_decode_eqc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,29 @@
-export([prop_encode_decode_match/0]).

prop_encode_decode_match() ->
?FORALL({Players, Threshold}, gen_players_threshold(),
?FORALL(Players, gen_players(),
begin
K = 2*Threshold,
F = (Players - 1) div 3,
Threshold = F,
K = 2*F,
M = Players - K,
Bin = crypto:hash(sha256, crypto:strong_rand_bytes(128)),
Bin = crypto:strong_rand_bytes(Players*4),
{ok, Shards} = erasure:encode(K, M, Bin),
{ok, DecodedBin} = erasure:decode(K, M, Shards),
?WHENFAIL(begin
io:format("Players: ~p, Threshold: ~p, K: ~p, M: ~p~n", [Players, Threshold, K, M]),
io:format("Bin: ~p", [Bin]),
io:format("Shards: ~p", [Shards]),
io:format("Decoded: ~p", [DecodedBin])
io:format("Bin: ~p~n", [Bin]),
io:format("Shards: ~p~n", [Shards]),
io:format("Decoded: ~p~n", [DecodedBin])
end,
conjunction([
{encode_decode_equality, eqc:equals(Bin, DecodedBin)}
]))
end).

gen_players_threshold() ->
?SUCHTHAT({Players, Threshold},
?LET({X, Y},
?SUCHTHAT({A, B}, {int(), int()}, A > 0 andalso B >= 0 andalso A > B),
{X*3, X - Y}),
Players > 3*Threshold+1 andalso Threshold > 1).
gen_players() ->
%?SUCHTHAT(Players,
%?LET(X,
?SUCHTHAT(A, int(), A > 4 andalso A < 70).
%X).
%Players > 3*Threshold+1 andalso Threshold > 1).

0 comments on commit 369e514

Please sign in to comment.