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

impl Absorb for String #138

Merged
merged 5 commits into from
Feb 10, 2024
Merged

impl Absorb for String #138

merged 5 commits into from
Feb 10, 2024

Conversation

mmagician
Copy link
Member

Description

Needed for deriving Absorb for LabeledCommitment in poly-commit.

closes: #XXXX


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against correct branch (main)
  • Linked to Github issue with discussion and accepted design OR have an explanation in the PR that describes this work.
  • Wrote unit tests
  • Updated relevant documentation in the code
  • Added a relevant changelog entry to the Pending section in CHANGELOG.md
  • Re-reviewed Files changed in the Github PR explorer

@mmagician mmagician requested a review from a team as a code owner February 6, 2024 15:45
@mmagician mmagician requested review from z-tech, Pratyush and weikengchen and removed request for a team February 6, 2024 15:45
@@ -227,6 +228,16 @@ impl Absorb for isize {
}
}

impl Absorb for String {
fn to_sponge_bytes(&self, dest: &mut Vec<u8>) {
dest.extend_from_slice(self.as_bytes())
Copy link
Member

Choose a reason for hiding this comment

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

Presumably this absorbs the length too?

Copy link
Member Author

Choose a reason for hiding this comment

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

I can also implement AbsorbWithLength for String and leave this as is, wdyt?
Then it's just the question of whether the caller knows about the trait AbsorbWithLength, which arguably is not so obvious since most types only implement pure Absorb.

Copy link
Member

Choose a reason for hiding this comment

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

I do think we should absorb the length by default, because I don't want it to be the case that absorb two strings is the same as the absorbing the concatenation of the strings

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added the length by default to to_sponge_bytes.

I left to_sponge_field_elements as-is. It already prevents the scenario you mentioned: each substring gets mapped to a new field element, whereas a concat string gets mapped to a single element, so the resulting vectors even have different lengths.

@Pratyush
Copy link
Member

Pratyush commented Feb 9, 2024

Also, I don't think we should derive Absorb for LabelledCommitment. Really the label only exists in the code for debugging purposes; the commitment should already be a unique representative of its polynomial. Absorbing the string would only add inefficiency.

@mmagician
Copy link
Member Author

Also, I don't think we should derive Absorb for LabelledCommitment. Really the label only exists in the code for debugging purposes; the commitment should already be a unique representative of its polynomial. Absorbing the string would only add inefficiency.

Sure, makes sense. We can impl Absorb for String regardless.

Copy link
Member

@Pratyush Pratyush left a comment

Choose a reason for hiding this comment

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

Looks good modulo the nit about the length!

@mmagician mmagician requested a review from Pratyush February 9, 2024 17:23
@Pratyush Pratyush added this pull request to the merge queue Feb 10, 2024
Merged via the queue into arkworks-rs:main with commit b93e005 Feb 10, 2024
5 checks passed
@burdges
Copy link

burdges commented Feb 10, 2024

If one wants String then one typically wants str instead (or perhaps in addition).

As an aside, it's bizarre LabeledCommitment::label returns &String instead of &str.

I think folks have typically chosen &[u8]instead of the utf8String/str` for labels, but actually the unicode lookalikes are not a problem here, so probably not a real concern.

@mmagician mmagician deleted the absorb-string branch February 10, 2024 21:08
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