Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
8ad6ab9c
Commit
8ad6ab9c
authored
Aug 15, 2017
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
shachain: add shachain_next_index()
Signed-off-by:
Rusty Russell
<
rusty@rustcorp.com.au
>
parent
2a03b7b3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
9 deletions
+22
-9
ccan/crypto/shachain/shachain.c
ccan/crypto/shachain/shachain.c
+8
-4
ccan/crypto/shachain/shachain.h
ccan/crypto/shachain/shachain.h
+12
-4
ccan/crypto/shachain/test/run.c
ccan/crypto/shachain/test/run.c
+2
-1
No files found.
ccan/crypto/shachain/shachain.c
View file @
8ad6ab9c
...
...
@@ -66,10 +66,16 @@ void shachain_from_seed(const struct sha256 *seed, shachain_index_t index,
derive
(
0
,
index
,
seed
,
hash
);
}
shachain_index_t
shachain_next_index
(
const
struct
shachain
*
chain
)
{
return
chain
->
min_index
-
1
;
}
void
shachain_init
(
struct
shachain
*
chain
)
{
chain
->
num_valid
=
0
;
chain
->
min_index
=
0
;
/* This is 0 in the case where SHACHAIN_BITS is 64. */
chain
->
min_index
=
(
shachain_index_t
)((
UINT64_MAX
>>
(
64
-
SHACHAIN_BITS
))
+
1
);
}
bool
shachain_add_hash
(
struct
shachain
*
chain
,
...
...
@@ -78,9 +84,7 @@ bool shachain_add_hash(struct shachain *chain,
unsigned
int
i
,
pos
;
/* You have to insert them in order! */
assert
(
index
==
chain
->
min_index
-
1
||
(
index
==
(
shachain_index_t
)(
UINT64_MAX
>>
(
64
-
SHACHAIN_BITS
))
&&
chain
->
num_valid
==
0
));
assert
(
index
==
shachain_next_index
(
chain
));
pos
=
count_trailing_zeroes
(
index
);
...
...
ccan/crypto/shachain/shachain.h
View file @
8ad6ab9c
...
...
@@ -71,20 +71,28 @@ struct shachain {
*/
void
shachain_init
(
struct
shachain
*
chain
);
/**
* shachain_next_index - what's the next index I can add to the shachain?
* @chain: the chain
*
* This returns 0xFFFFFFFFFFFFFFFF (for a freshly
* initialized chain), or one less than the previously successfully
* added value.
*/
shachain_index_t
shachain_next_index
(
const
struct
shachain
*
chain
);
/**
* shachain_add_hash - record the hash for the next index.
* @chain: the chain to add to
* @index: the index of the hash
* @hash: the hash value.
*
* You can only add index 0xFFFFFFFFFFFFFFFF (for a freshly
* initialized chain), or one less than the previously successfully
* added value.
* You can only add shachain_next_index(@chain).
*
* This can fail (return false without altering @chain) if the hash
* for this index isn't consistent with previous hashes (ie. wasn't
* generated from the same seed), though it can't always detect that.
* If the hash is inconsistent yet undetected,
the next
addition will
* If the hash is inconsistent yet undetected,
a future
addition will
* fail.
*
* Example:
...
...
ccan/crypto/shachain/test/run.c
View file @
8ad6ab9c
...
...
@@ -13,7 +13,7 @@ int main(void)
uint64_t
i
,
j
;
/* This is how many tests you plan to run */
plan_tests
(
NUM_TESTS
*
3
+
NUM_TESTS
*
(
NUM_TESTS
+
1
)
-
1
);
plan_tests
(
NUM_TESTS
*
4
+
NUM_TESTS
*
(
NUM_TESTS
+
1
)
-
1
);
memset
(
&
seed
,
0
,
sizeof
(
seed
));
/* Generate a whole heap. */
...
...
@@ -34,6 +34,7 @@ int main(void)
i
--
)
{
struct
sha256
hash
;
int
expidx
=
0xFFFFFFFFFFFFFFFFULL
-
i
;
ok1
(
shachain_next_index
(
&
chain
)
==
i
);
ok1
(
shachain_add_hash
(
&
chain
,
i
,
&
expect
[
expidx
]));
for
(
j
=
i
;
j
!=
0
;
j
++
)
{
ok1
(
shachain_get_hash
(
&
chain
,
j
,
&
hash
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment