Home

Awesome

Noir SHA-1

Noir License: MIT Nargo Test 🌌

This library contains an implementation of the SHA-1 hashing algorithm.

While SHA-1 is no longer considered secure for general use, it is still useful on resource-constrained devices for certain use cases.

One example is in smart cards where a random challenge is generated by a verifier and the smart card digitally signs a payload that contains a SHA-1 hash based on the random challenge. This is still reasonably secure because the time window available for brute-forcing a SHA-1 hash is extremely limited.

Security Notice

SHA-1 is deprecated for most security-sensitive applications but retains utility in specific, time-bound scenarios.

Usage

In your Nargo.toml file, add the following dependency:

[dependencies]
sha1 = { tag = "v0.0.5", git = "https://github.com/michaelelliot/noir-sha1", directory = "crates/noir-sha1" }

Then use it in your Noir project like this:

use dep::sha1::sha1;

fn main(input: [u8; 128], input_len: u16, hash: pub [u8; 20]) {
    // Generate SHA-1 hash digest of input
    let compare_hash = sha1(input, input_len);
    assert(hash == compare_hash);
}

NOTE: The input parameter must be a u8 byte array with a length that's a multiple of 64, such as 64, 128, 192, or 256 etc. (currently up to a maximum of 256). The rest of the byte array can be zero-padded (0x00) as shown in the example below, with the input_len parameter specifying the number of initial bytes from the input to be used for calculating the digest.

Here's an example unit test for the main entrypoint above:

#[test]
fn test_main() {
    // Hal Finney was a cypherpunk pioneer
    let test_msg: [u8; 64] = [
        0x48, 0x61, 0x6c, 0x20, 0x46, 0x69, 0x6e, 0x6e,
        0x65, 0x79, 0x20, 0x77, 0x61, 0x73, 0x20, 0x61,
        0x20, 0x63, 0x79, 0x70, 0x68, 0x65, 0x72, 0x70,
        0x75, 0x6e, 0x6b, 0x20, 0x70, 0x69, 0x6f, 0x6e,
        0x65, 0x65, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
    let test_hash: [u8; 20] =  [
        0x3a, 0x93, 0x96, 0x11, 0xee, 0x6e, 0x4d, 0xfb, 0xe2, 0x0e,
        0xc2, 0xcb, 0xf2, 0xa3, 0x55, 0x52, 0xbc, 0x47, 0x03, 0x4a];
    main(test_msg, 35, test_hash);
}

Example

Noir example source code: ./example/src/main.nr

Gates

ACIR Opcodes: 23501. Backend Circuit Size: 81495.

License

MIT License

Copyright (c) 2023 Michael Elliot

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.