8000 kernel: utilities: add crc32b implementation by bradjc · Pull Request #3825 · tock/tock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

kernel: utilities: add crc32b implementation #3825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed

kernel: utilities: add crc32b implementation #3825

wants to merge 3 commits into from

Conversation

bradjc
Copy link
Contributor
@bradjc bradjc commented Jan 29, 2024

Pull Request Overview

Alternative to #3808.

This is helpful for, for one example, computing ShortIDs from process names.

The Compress trait has one function:

fn to_short_id(&self, process: &dyn Process, credentials: &TbfFooterV2Credentials) -> ShortID;

I want to add a function with the signature fn (&'static str) -> u32) so I can implement Compress and assign ShortIDs based on process names.

This complements #3818, which is an app checker that looks like:

pub struct AppCheckerNames<'a, F: Fn(&'static str) -> u32> {
    hasher: &'a F,
    ...
}

For my purposes (and for most simple use cases where apps are identified by their name), it's not important what the hash function is, as long as it is reasonably ok at generating different short IDs for different strings.

Now, I've been convinced to change the signature of the function in this PR to fn (&[u8]) -> u32. That makes this a little more general. For my use case then I will create a small wrapper function in the board main.rs, or do something like &|s| crc32b(s.as_bytes()).

The other reason I want any simple function like fn (&'static str) -> u32) is it makes actually using AppID in board files fairly ergonomic. So If I have a capsule and I want to give some resource to a specific app, I can do something like:

my_capsule::new(
  // Identify the app that is allowed to use this capsule
  ShortID::Fixed(NonZeroU32::new(kernel::utilities::helpers::crc("circle")).unwrap())
  // Other resources the capsule needs
  i2c, ...
)

I guess now I have to do:

fn my_hash(s: &'static str) -> u32 {
  kernel::utilities::helpers::crc(s.as_bytes())
}

my_capsule::new(
  // Identify the app that is allowed to use this capsule
  ShortID::Fixed(NonZeroU32::new(my_hash("circle")).unwrap())
  // Other resources the capsule needs
  i2c, ...
)

but you get the point.

What makes this nice is that it's easy for someone to know the ShortID for an app since we already use process names. Changing which app gets a resource is just updating the human readable string.

If we want to make AppID usable, having ways to generate ShortIDs that is intuitive is very helpful.

Testing Strategy

Comparing to online calculator.

TODO or Help Wanted

n/a

Documentation Updated

  • Updated the relevant files in /docs, or no updates are required.

Formatting

  • Ran make prepush.

This is helpful for, for one example, computing ShortIDs from process
names.
///
/// Implementation based on https://lxp32.github.io/docs/a-simple-example-crc32-calculation/.
/// Online calculator here: https://md5calc.com/hash/crc32b
pub fn crc32b_str(s: &'static str) -> u32 {
Copy link
Member
@lschuermann lschuermann Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nicer to have this accept a byte slice instead. This way we can use core::str::as_bytes to calculate a CRC over a string, but also support other non-unicode byte sequences. Shouldn't need a lot of changes, just iterating over the bytes and removing the cast.

@alistair23
Copy link
Contributor

Why not use the existing CRC implementation?

@bradjc
Copy link
Contributor Author
bradjc commented Jan 30, 2024

Why not use the existing CRC implementation?

I think adding a dependency of tickv for this use case is pretty heavyweight. Also the kernel won't have tickv as a dependency and I need to use this in the kernel. I don't actually need CRC, I just need something of fn(str) -> u32 that I can get merged into Tock.

@alistair23
Copy link
Contributor

You could copy the same implementation though. Keep all our CRCs the same

@bradjc
Copy link
Contributor Author
bradjc commented Jan 30, 2024

You could copy the same implementation though. Keep all our CRCs the same

I need something so I can do something like:

ShortID::Fixed(NonZeroU32::new(kernel::utilities::helpers::crc("circle")).unwrap())

Any extra machinery would just complicate that code for I don't think much benefit.

Use case: https://github.com/tock/tock/pull/3817/files#diff-98d9b86eaf1d01d892fb1c96c448e2799cf55fa442b5ff12aa4b5d85c3d16de5R506

@alistair23
Copy link
Contributor

I don't follow. You said in the PR description that this is helpful for computing shortIDs. Why do we need to use CRC-32B? Could we use CRC-32 to try to keep all of our CRCs the same?

Maybe it's worth saying why we need this exact implementation in the PR description? It's not really clear

@bradjc
Copy link
Contributor Author
bradjc commented Jan 31, 2024

I updated the PR description. I guess I liken this to something like addition. Sure, there are libraries that enable adding any two numbers together. But for simple cases the + operator is sufficient and makes the code easier to read and reuse.

@bradjc bradjc mentioned this pull request Feb 1, 2024
2 tasks
@bradjc
Copy link
Contributor Author
bradjc commented Feb 1, 2024

Version that matches tickv in #3829

@lschuermann
Copy link
Member

I'm not really invested into which particular CRC polynomial we ship here, but is the intention to include this PR, #3829, or both? And if both are included, which one will we use for which purpose? Will TicKV ever use the one shipped in the kernel, and if not, what's the point of including it?

@bradjc
Copy link
Contributor Author
bradjc commented Feb 1, 2024

I don't think anyone wants to add crc32b. But the PR description is still helpful.

@bradjc bradjc closed this Feb 1, 2024
@bradjc bradjc deleted the crc32b branch February 16, 2024 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0