Skip to content

Commit

Permalink
Reverted to SINGLE_FILE because of rollup issue
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzc0re committed Jul 3, 2023
1 parent a40ad76 commit e6d6149
Show file tree
Hide file tree
Showing 8 changed files with 490 additions and 59 deletions.
104 changes: 49 additions & 55 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@deliberative/crypto",
"description": "Libsodium and Shamir secret sharing wasm module for nodejs and the browser.",
"version": "0.9.1",
"version": "0.9.2",
"repository": {
"type": "git",
"url": "https://github.com/deliberative/crypto.git"
Expand Down
3 changes: 0 additions & 3 deletions rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import json from "@rollup/plugin-json";
import resolve from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
import typescript from "@rollup/plugin-typescript";
import { wasm } from "@rollup/plugin-wasm";
import url from "@rollup/plugin-url";
// import { terser } from "rollup-plugin-terser";
import analyzer from "rollup-plugin-analyzer";
Expand Down Expand Up @@ -32,8 +31,6 @@ const plugins = [

url(),

wasm(),

json({
compact: true,
preferConst: true,
Expand Down
1 change: 1 addition & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const withJS = ` \
-s MODULARIZE=1 \
-s MAIN_MODULE=2 \
-s POLYFILL=0 \
-s SINGLE_FILE=1 \
`;

const memory = `\
Expand Down
143 changes: 143 additions & 0 deletions src/c/commitment/generate_commit_details.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright (C) 2023 Deliberative Technologies P.C.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>

#include "../utils/utils.h"

#include "../../../libsodium/src/libsodium/include/sodium/crypto_hash_sha512.h"

/**
* In order to generate commit details, the receiver decides how many random
* keypairs need to exist between the owning keypair of the previous commit and
* the next one.
*/
__attribute__((used)) int
generate_commit_details(
const unsigned int LEAVES_LEN,
const uint8_t leaves_hashed[LEAVES_LEN][crypto_hash_sha512_BYTES],
const uint8_t element_hash[crypto_hash_sha512_BYTES],
uint8_t proof[LEAVES_LEN][crypto_hash_sha512_BYTES + 1])
{
size_t i, j, k;

int32_t index[1];
items_indexes_in_array(
LEAVES_LEN, 1, leaves_hashed,
(uint8_t(*)[crypto_hash_sha512_BYTES]) & element_hash[0], index);
if (index[0] == -1) return -1;

unsigned int element_of_interest = index[0];

uint8_t(*hashes)[crypto_hash_sha512_BYTES]
= malloc(sizeof(uint8_t[LEAVES_LEN][crypto_hash_sha512_BYTES]));
memcpy(&hashes[0][0], &leaves_hashed[0][0],
LEAVES_LEN * crypto_hash_sha512_BYTES);

uint8_t *concat_hashes = malloc(2 * crypto_hash_sha512_BYTES);

unsigned int leaves = LEAVES_LEN;
int res;
bool odd_leaves;
bool element_of_interest_found = false;

// Counts the index of proof artifacts.
k = 0;

while (leaves > 0)
{
// Check if number of leaves is odd or even.
odd_leaves = leaves % 2 != 0;

// For every two leaves.
for (i = 0, j = 0; i < leaves; i += 2, j++)
{
// If we are at the last position to the right of a tree with odd
// number of leaves.
if (odd_leaves && i + 1 == leaves)
{
memcpy(&concat_hashes[0], &hashes[i][0], crypto_hash_sha512_BYTES);
// Concat leaf hash with itself.
memcpy(&concat_hashes[crypto_hash_sha512_BYTES], &hashes[i][0],
crypto_hash_sha512_BYTES);

if (i == element_of_interest)
{
// Copy required hash of interest in the proof.
memcpy(&proof[k][0], &hashes[i][0], crypto_hash_sha512_BYTES);
// We do not care if left(0) or right(1) since hash of itself
proof[k][crypto_hash_sha512_BYTES] = 0;

k++;
element_of_interest = j;
element_of_interest_found = true;
}
}
else
{
memcpy(&concat_hashes[0], &hashes[i][0], crypto_hash_sha512_BYTES);
// In any other case concat leaf hash with the one on its right.
memcpy(&concat_hashes[crypto_hash_sha512_BYTES], &hashes[i + 1][0],
crypto_hash_sha512_BYTES);

if (i == element_of_interest || i + 1 == element_of_interest)
{
if (i == element_of_interest)
{
memcpy(&proof[k][0], &hashes[i + 1][0], crypto_hash_sha512_BYTES);
// Proof artifact needs to go to the right when concatenated with
// element.
proof[k][crypto_hash_sha512_BYTES] = 1;
}
else if (i + 1 == element_of_interest)
{
memcpy(&proof[k][0], &hashes[i][0], crypto_hash_sha512_BYTES);
// Proof artifact needs to go to the left when concatenated with
// element.
proof[k][crypto_hash_sha512_BYTES] = 0;
}

k++;
element_of_interest = j;
element_of_interest_found = true;
}
}

res = crypto_hash_sha512(hashes[j], concat_hashes,
2 * crypto_hash_sha512_BYTES);
if (res != 0)
{
free(hashes);
free(concat_hashes);

return -2;
}
}

if (leaves == 1) break;

leaves = ceil((double)leaves / 2);

element_of_interest_found = false;
}

free(hashes);
free(concat_hashes);

return k * (crypto_hash_sha512_BYTES + 1);
}
Loading

0 comments on commit e6d6149

Please sign in to comment.