From 53004bda7183b4f204cbbfe8ba7d7435510a1d3e Mon Sep 17 00:00:00 2001 From: mmagician Date: Tue, 6 Feb 2024 16:43:57 +0100 Subject: [PATCH 1/4] impl Absorb for String --- src/sponge/absorb.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/sponge/absorb.rs b/src/sponge/absorb.rs index 3827d428..d3dbe8d2 100644 --- a/src/sponge/absorb.rs +++ b/src/sponge/absorb.rs @@ -227,6 +227,16 @@ impl Absorb for isize { } } +impl Absorb for String { + fn to_sponge_bytes(&self, dest: &mut Vec) { + dest.extend_from_slice(self.as_bytes()) + } + + fn to_sponge_field_elements(&self, dest: &mut Vec) { + self.as_bytes().to_sponge_field_elements(dest) + } +} + impl Absorb for TEAffine

where P::BaseField: ToConstraintField<::BasePrimeField>, From 4b5922084e252737eb99424188c2e78e9532158a Mon Sep 17 00:00:00 2001 From: mmagician Date: Tue, 6 Feb 2024 16:56:07 +0100 Subject: [PATCH 2/4] add String from ark_std --- src/sponge/absorb.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sponge/absorb.rs b/src/sponge/absorb.rs index d3dbe8d2..2d28757e 100644 --- a/src/sponge/absorb.rs +++ b/src/sponge/absorb.rs @@ -7,6 +7,7 @@ use ark_ec::{ use ark_ff::models::{Fp, FpConfig}; use ark_ff::{BigInteger, Field, PrimeField, ToConstraintField}; use ark_serialize::CanonicalSerialize; +use ark_std::string::String; use ark_std::vec::Vec; /// An interface for objects that can be absorbed by a `CryptographicSponge`. From 7b4e10be29df1d230470f7bd50d66c8107167be5 Mon Sep 17 00:00:00 2001 From: mmagician Date: Fri, 9 Feb 2024 18:16:15 +0100 Subject: [PATCH 3/4] absorb length when converting String to sponge bytes --- src/sponge/absorb.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sponge/absorb.rs b/src/sponge/absorb.rs index 2d28757e..e5a657d1 100644 --- a/src/sponge/absorb.rs +++ b/src/sponge/absorb.rs @@ -230,6 +230,7 @@ impl Absorb for isize { impl Absorb for String { fn to_sponge_bytes(&self, dest: &mut Vec) { + self.len().to_sponge_bytes(dest); dest.extend_from_slice(self.as_bytes()) } From 9ddd53cba6e834d6ba6b3b4af43b01b33e1b139f Mon Sep 17 00:00:00 2001 From: mmagician Date: Fri, 9 Feb 2024 18:16:49 +0100 Subject: [PATCH 4/4] add test to ensure that absorbing two strings is different from concat --- src/sponge/absorb.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/sponge/absorb.rs b/src/sponge/absorb.rs index e5a657d1..6d9c67bf 100644 --- a/src/sponge/absorb.rs +++ b/src/sponge/absorb.rs @@ -382,6 +382,7 @@ macro_rules! collect_sponge_field_elements { #[cfg(test)] mod tests { + use super::*; use crate::sponge::field_cast; use crate::sponge::test::Fr; use ark_std::{test_rng, vec::Vec, UniformRand}; @@ -394,4 +395,31 @@ mod tests { field_cast::<_, Fr>(&expected, &mut actual).unwrap(); assert_eq!(actual, expected); } + + #[test] + fn test_string_absort() { + // absorbing two strings should not be the same as absorbing the concatenated string + let s1 = "hello".to_string(); + let s2 = "world".to_string(); + + let mut dest1 = Vec::new(); + s1.to_sponge_bytes(&mut dest1); + s2.to_sponge_bytes(&mut dest1); + + let mut dest2 = Vec::new(); + let s3 = "helloworld".to_string(); + s3.to_sponge_bytes(&mut dest2); + + assert_ne!(dest1, dest2); + + // Same for converting to sponge field elements + let mut dest1: Vec = Vec::new(); + s1.to_sponge_field_elements(&mut dest1); + s2.to_sponge_field_elements(&mut dest1); + + let mut dest2 = Vec::new(); + s3.to_sponge_field_elements(&mut dest2); + + assert_ne!(dest1, dest2); + } }