diff options
author | Franklin Wei <me@fwei.tk> | 2018-06-20 21:20:04 -0400 |
---|---|---|
committer | Franklin Wei <me@fwei.tk> | 2018-06-20 21:20:04 -0400 |
commit | 6f67db25e477a94fc7160fe1052329e41e1f9da7 (patch) | |
tree | 74c00fef60d41de3506d44306b2549e4e93d9102 /crypto.c | |
parent | 9fb1f3ba699a2ff004aaf35f5fc628df7bcf6f1e (diff) | |
download | csaa-6f67db25e477a94fc7160fe1052329e41e1f9da7.zip csaa-6f67db25e477a94fc7160fe1052329e41e1f9da7.tar.gz csaa-6f67db25e477a94fc7160fe1052329e41e1f9da7.tar.bz2 csaa-6f67db25e477a94fc7160fe1052329e41e1f9da7.tar.xz |
Support creation of line-by-line IOMTs from disk files
Also minor changes to iomt_* interface.
Diffstat (limited to 'crypto.c')
-rw-r--r-- | crypto.c | 83 |
1 files changed, 80 insertions, 3 deletions
@@ -244,7 +244,7 @@ void restore_nodes(hash_t *nodes, const int *indices, const hash_t *values, int * point to an array of length mt_logleaves that contains the old node * values (whose indices are returned by bintree_ancestors()). NOTE: * this function will NOT set the corresponding IOMT leaf; use - * iomt_update_by_leafidx for that. */ + * iomt_update_leaf_full for that. */ void merkle_update(struct iomt *tree, uint64_t leafidx, hash_t newval, hash_t **old_dep) { if(old_dep) @@ -307,8 +307,8 @@ void iomt_update(struct iomt *tree, uint64_t idx, hash_t newval) merkle_update(tree, leaf - tree->mt_leaves, hash_node(leaf), NULL); } -void iomt_update_by_leafidx(struct iomt *tree, uint64_t leafidx, - uint64_t new_idx, uint64_t new_next_idx, hash_t new_val) +void iomt_update_leaf_full(struct iomt *tree, uint64_t leafidx, + uint64_t new_idx, uint64_t new_next_idx, hash_t new_val) { struct iomt_node *leaf = tree->mt_leaves + leafidx; leaf->idx = new_idx; @@ -318,6 +318,33 @@ void iomt_update_by_leafidx(struct iomt *tree, uint64_t leafidx, merkle_update(tree, leafidx, hash_node(leaf), NULL); } +void iomt_update_leaf_idx(struct iomt *tree, uint64_t leafidx, + uint64_t new_idx) +{ + struct iomt_node *leaf = tree->mt_leaves + leafidx; + leaf->idx = new_idx; + + merkle_update(tree, leafidx, hash_node(leaf), NULL); +} + +void iomt_update_leaf_nextidx(struct iomt *tree, uint64_t leafidx, + uint64_t new_next_idx) +{ + struct iomt_node *leaf = tree->mt_leaves + leafidx; + leaf->next_idx = new_next_idx; + + merkle_update(tree, leafidx, hash_node(leaf), NULL); +} + +void iomt_update_leaf_hash(struct iomt *tree, uint64_t leafidx, + hash_t new_val) +{ + struct iomt_node *leaf = tree->mt_leaves + leafidx; + leaf->val = new_val; + + merkle_update(tree, leafidx, hash_node(leaf), NULL); +} + /* Create a merkle tree with 2^logleaves leaves, each initialized to a * zero leaf (not a placeholder!) */ struct iomt *iomt_new(int logleaves) @@ -357,6 +384,56 @@ void iomt_free(struct iomt *tree) } } +/* arbitrary */ +#define FILELINES_LOGLEAVES 10 + +struct iomt *iomt_from_lines(const char *filename) +{ + struct iomt *tree = iomt_new(FILELINES_LOGLEAVES); + + FILE *f = fopen(filename, "r"); + + SHA256_CTX ctx; + SHA256_Init(&ctx); + + int c; + uint64_t line = 0; + + while(c != EOF) + { + c = fgetc(f); + + char ch = c; + + if(c != EOF) + SHA256_Update(&ctx, &ch, sizeof(ch)); + + if(ch == '\n' || c == EOF) + { + hash_t linehash; + SHA256_Final(linehash.hash, &ctx); + + /* set this leaf to loop around */ + iomt_update_leaf_full(tree, line, line + 1, 1, linehash); + + if(line > 0) + { + /* make previously inserted leaf point to this leaf */ + iomt_update_leaf_nextidx(tree, line - 1, line + 1); + } + + line++; + + /* re-initialize for next line */ + SHA256_Init(&ctx); + } + } + + fclose(f); + + return tree; +} + struct hashstring hash_format(hash_t h, int n) { struct hashstring ret; |