Skip to content

Commit

Permalink
stop being a jerk and add the context string to test_vectors.json
Browse files Browse the repository at this point in the history
  • Loading branch information
oconnor663-zoom committed Jun 29, 2020
1 parent f200567 commit 2f6f56f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions c/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
HERE = path.dirname(__file__)
TEST_VECTORS_PATH = path.join(HERE, "..", "test_vectors", "test_vectors.json")
TEST_VECTORS = json.load(open(TEST_VECTORS_PATH))
TEST_CONTEXT = "BLAKE3 2019-12-27 16:29:52 test vectors context"


def run_blake3(args, input):
Expand Down Expand Up @@ -37,6 +36,7 @@ def main():
input_len = case["input_len"]
input = make_test_input(input_len)
hex_key = hexlify(TEST_VECTORS["key"].encode())
context_string = TEST_VECTORS["context_string"]
expected_hash_xof = case["hash"]
expected_hash = expected_hash_xof[:64]
expected_keyed_hash_xof = case["keyed_hash"]
Expand Down Expand Up @@ -76,7 +76,7 @@ def main():
input_len, expected_keyed_hash_xof, line)

# Test the default derive key.
test_derive_key = run_blake3(["--derive-key", TEST_CONTEXT], input)
test_derive_key = run_blake3(["--derive-key", context_string], input)
for line in test_derive_key.splitlines():
assert expected_derive_key == line, \
"derive_key({}): {} != {}".format(
Expand All @@ -85,7 +85,7 @@ def main():
# Test the extended derive key.
xof_len = len(expected_derive_key_xof) // 2
test_derive_key_xof = run_blake3(
["--derive-key", TEST_CONTEXT, "--length",
["--derive-key", context_string, "--length",
str(xof_len)], input)
for line in test_derive_key_xof.splitlines():
assert expected_derive_key_xof == line, \
Expand Down
21 changes: 10 additions & 11 deletions test_vectors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,19 @@ pub const TEST_CASES: &[usize] = &[
100 * CHUNK_LEN, // subtrees larger than MAX_SIMD_DEGREE chunks
];

pub const TEST_KEY: &[u8; blake3::KEY_LEN] = b"whats the Elvish word for friend";
pub const TEST_CONTEXT: &str = "BLAKE3 2019-12-27 16:29:52 test vectors context";

const COMMENT: &str = r#"
Each test is an input length and three outputs, one for each of the hash,
keyed_hash, and derive_key modes. The input in each case is filled with a
251-byte-long repeating pattern: 0, 1, 2, ..., 249, 250, 0, 1, ... The key used
with keyed_hash is the 32-byte ASCII string given in the 'key' field below. For
derive_key, the test input is used as the input key, and the context string is
'BLAKE3 2019-12-27 16:29:52 test vectors context'. (As good practice for
following the security requirements of derive_key, test runners should make
that context string a hardcoded constant, and we do not provided it in
machine-readable form.) Outputs are encoded as hexadecimal. Each case is an
extended output, and implementations should also check that the first 32 bytes
match their default-length output.
repeating sequence of 251 bytes: 0, 1, 2, ..., 249, 250, 0, 1, ..., and so on.
The key used with keyed_hash is the 32-byte ASCII string "whats the Elvish word
for friend", also given in the `key` field below. The context string used with
derive_key is the ASCII string "BLAKE3 2019-12-27 16:29:52 test vectors
context", also given in the `context_string` field below. Outputs are encoded
as hexadecimal. Each case is an extended output, and implementations should
also check that the first 32 bytes match their default-length output.
"#;

// Paint the input with a repeating byte pattern. We use a cycle length of 251,
Expand All @@ -60,6 +59,7 @@ pub fn paint_test_input(buf: &mut [u8]) {
pub struct Cases {
pub _comment: String,
pub key: String,
pub context_string: String,
pub cases: Vec<Case>,
}

Expand All @@ -72,8 +72,6 @@ pub struct Case {
}

pub fn generate_json() -> String {
const TEST_KEY: &[u8; blake3::KEY_LEN] = b"whats the Elvish word for friend";

let mut cases = Vec::new();
for &input_len in TEST_CASES {
let mut input = vec![0; input_len];
Expand Down Expand Up @@ -108,6 +106,7 @@ pub fn generate_json() -> String {
let mut json = serde_json::to_string_pretty(&Cases {
_comment: COMMENT.trim().replace("\n", " "),
key: std::str::from_utf8(TEST_KEY).unwrap().to_string(),
context_string: TEST_CONTEXT.to_string(),
cases,
})
.unwrap();
Expand Down
3 changes: 2 additions & 1 deletion test_vectors/test_vectors.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"_comment": "Each test is an input length and three outputs, one for each of the hash, keyed_hash, and derive_key modes. The input in each case is filled with a 251-byte-long repeating pattern: 0, 1, 2, ..., 249, 250, 0, 1, ... The key used with keyed_hash is the 32-byte ASCII string given in the 'key' field below. For derive_key, the test input is used as the input key, and the context string is 'BLAKE3 2019-12-27 16:29:52 test vectors context'. (As good practice for following the security requirements of derive_key, test runners should make that context string a hardcoded constant, and we do not provided it in machine-readable form.) Outputs are encoded as hexadecimal. Each case is an extended output, and implementations should also check that the first 32 bytes match their default-length output.",
"_comment": "Each test is an input length and three outputs, one for each of the hash, keyed_hash, and derive_key modes. The input in each case is filled with a repeating sequence of 251 bytes: 0, 1, 2, ..., 249, 250, 0, 1, ..., and so on. The key used with keyed_hash is the 32-byte ASCII string \"whats the Elvish word for friend\", also given in the `key` field below. The context string used with derive_key is the ASCII string \"BLAKE3 2019-12-27 16:29:52 test vectors context\", also given in the `context_string` field below. Outputs are encoded as hexadecimal. Each case is an extended output, and implementations should also check that the first 32 bytes match their default-length output.",
"key": "whats the Elvish word for friend",
"context_string": "BLAKE3 2019-12-27 16:29:52 test vectors context",
"cases": [
{
"input_len": 0,
Expand Down

0 comments on commit 2f6f56f

Please sign in to comment.