-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reverted to SINGLE_FILE because of rollup issue
- Loading branch information
Showing
8 changed files
with
490 additions
and
59 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.