Skip to content
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

Adding SHA1 gadget #7

Merged
merged 4 commits into from
Nov 20, 2023
Merged

Adding SHA1 gadget #7

merged 4 commits into from
Nov 20, 2023

Conversation

avras
Copy link
Collaborator

@avras avras commented Oct 14, 2023

A second attempt at the SHA1 gadget using a simpler OR gate. See previous attempt at #6

@avras
Copy link
Collaborator Author

avras commented Oct 15, 2023

I will make changes to this gadget to use the in-built Boolean::or once lurk-lang/bellpepper#28 gets merged. We can hold this PR until then.

@avras avras marked this pull request as draft October 16, 2023 03:02
@huitseeker
Copy link
Contributor

For development purposes, you can use a dependency override to test the De Morgan OR gadget now merged in the master branch of bellpepper, before the upcoming release.

@huitseeker
Copy link
Contributor

Bellpepper 0.3.0 is released with the or gadget!

@avras
Copy link
Collaborator Author

avras commented Oct 28, 2023

Thanks @huitseeker for the heads up. It feels good to contribute to bellpepper. I will get the SHA1 PR done soon.

@avras avras marked this pull request as ready for review October 28, 2023 03:02
@avras avras changed the title Adding SHA1 gadget (with De Morgan OR gate) Adding SHA1 gadget Oct 28, 2023
Copy link

@adr1anh adr1anh left a comment

Choose a reason for hiding this comment

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

Very nice work! The code is clear and makes good use of lower-level primitives which abstracts away the risks of writing circuits. All allocations use proper constructors, and variables are always derived from previous inputs.

I left some comments suggesting the use of arrays instead of Vec for lists whose size are known at compile-time, and also suggestions for comments which could clarify the rounds. Let me know what you think.

Comment on lines 61 to 63
fn get_sha1_iv() -> Vec<UInt32> {
IV.iter().map(|&v| UInt32::constant(v)).collect()
}
Copy link

Choose a reason for hiding this comment

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

The length of the result has a size known at compile time. Have you considered returning an array instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for pointing this out. I will try changing the output to an array

.collect())
}

pub fn sha1<Scalar, CS>(mut cs: CS, input: &[Boolean]) -> Result<Vec<Boolean>, SynthesisError>
Copy link

Choose a reason for hiding this comment

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

The function could return an array instead of a Vec since the size is known at compile-time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will try changing the output type here as well

Comment on lines 121 to 126
let a1 = and_uint32(cs.namespace(|| "1st and"), &b, &c)?;
let a2 = and_uint32(cs.namespace(|| "2nd and"), &b, &d)?;
let a3 = and_uint32(cs.namespace(|| "3rd and"), &c, &d)?;

let tmp = or_uint32(cs.namespace(|| "1st or"), &a1, &a2)?;
or_uint32(cs.namespace(|| "2nd or"), &tmp, &a3)?
Copy link

Choose a reason for hiding this comment

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

The namespaces could describe the operations being performed, and a comment at the top of this block could state what the round computes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will revisit the SHA1 spec and give some descriptive labels to the namespaces. You can review and let me know if those are fine

let f = if i < 20 {
// f = (b and c) or ((not b) and d)
UInt32::sha256_ch(cs.namespace(|| "ch"), &b, &c, &d)?
} else if !(40..60).contains(&i) {
Copy link

Choose a reason for hiding this comment

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

A comment stating that this branch is for rounds 20 <= t <= 39 and 60 <= t <= 79 would make it easier to match the spec.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will add this comment

crates/sha1/src/sha1.rs Show resolved Hide resolved
@adr1anh
Copy link

adr1anh commented Nov 8, 2023

Thanks for replying to the comments, happy to review the updates!

@avras
Copy link
Collaborator Author

avras commented Nov 10, 2023

Hey @adr1anh, please check if I have addressed all your suggestions. I renamed some variables to better match the spec.

No nitpick is too small. Send them all.

Copy link

@adr1anh adr1anh left a comment

Choose a reason for hiding this comment

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

Thanks for these changes. It looks good to me.

Copy link

@adr1anh adr1anh left a comment

Choose a reason for hiding this comment

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

These changes look good to me, thanks for the work!

@huitseeker huitseeker merged commit 7d4b273 into lurk-lab:main Nov 20, 2023
3 checks passed
@avras avras deleted the sha1-simpler branch March 21, 2024 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants