Line data Source code
1 : /**
2 : * The MIT License (MIT)
3 : *
4 : * Copyright (c) 2021 RSK Labs Ltd
5 : *
6 : * Permission is hereby granted, free of charge, to any person obtaining a copy
7 : * of this software and associated documentation files (the "Software"), to
8 : * deal in the Software without restriction, including without limitation the
9 : * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 : * sell copies of the Software, and to permit persons to whom the Software is
11 : * furnished to do so, subject to the following conditions:
12 : *
13 : * The above copyright notice and this permission notice shall be included in
14 : * all copies or substantial portions of the Software.
15 : *
16 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 : * IN THE SOFTWARE.
23 : */
24 :
25 : #include "bc.h"
26 : #include "bc_hash.h"
27 :
28 : /*
29 : * Compute a double sha256 and a final reversal.
30 : *
31 : * @arg[in] ctx sha256 context
32 : * @arg[in] data pointer tobytes to double hash and reverse
33 : * @arg[in] len length of data to hash in bytes
34 : * @arg[out] hash 32-byte buffer where hash will be stored
35 : */
36 295 : void double_sha256_rev(sha256_ctx_t* ctx,
37 : uint8_t* data,
38 : size_t len,
39 : uint8_t* hash) {
40 295 : SHA256_INIT(ctx);
41 295 : SHA256_UPDATE(ctx, data, len);
42 295 : SHA256_FINAL(ctx, hash);
43 295 : SHA256_INIT(ctx);
44 295 : SHA256_UPDATE(ctx, hash, HASH_SIZE);
45 295 : SHA256_FINAL(ctx, hash);
46 5015 : REV_HASH(hash);
47 295 : }
48 :
49 : /*
50 : * Perform one step of the Merkle proof validation. The result
51 : * of the hash step will be stored in `left`, thus clobbering
52 : * `left`'s input value.
53 : *
54 : * @arg[in] ctx s ha256 context
55 : * @arg[in/out] left pointer to left hash, result will be stored here
56 : * @arg[in] right pointer to right hash
57 : */
58 79 : void fold_left(sha256_ctx_t* ctx, uint8_t* left, uint8_t* right) {
59 1343 : REV_HASH(left);
60 1343 : REV_HASH(right);
61 79 : SHA256_INIT(ctx);
62 79 : SHA256_UPDATE(ctx, left, HASH_SIZE);
63 79 : SHA256_UPDATE(ctx, right, HASH_SIZE);
64 79 : SHA256_FINAL(ctx, left);
65 79 : SHA256_INIT(ctx);
66 79 : SHA256_UPDATE(ctx, left, HASH_SIZE);
67 79 : SHA256_FINAL(ctx, left);
68 1343 : REV_HASH(left);
69 79 : }
|