Add keccak services/requests/messages#3580
Conversation
commit: |
ceb850c to
c3c318f
Compare
255f897 to
312daf5
Compare
312daf5 to
2422f54
Compare
| match self { | ||
| AnyHash::Blake2b(_) => { | ||
| let mut hasher = Blake2bHasher::default(); | ||
| hasher.write_all(other.as_bytes()).unwrap(); |
There was a problem hiding this comment.
Because Hasher::digest only takes a single &[u8], so it can't hash two byte slices in sequence. We need to hash self || other without allocating.
There was a problem hiding this comment.
Ahh, sorry. Yes, I meant chain
| let mut hasher = Sha256Hasher::default(); | ||
| hasher.write_all(other.as_bytes()).unwrap(); | ||
| hasher.write_all(self.as_bytes()).unwrap(); | ||
| AnyHash::Sha256(AnyHash32(hasher.finish().into())) |
| let mut hasher = Sha512Hasher::default(); | ||
| hasher.write_all(other.as_bytes()).unwrap(); | ||
| hasher.write_all(self.as_bytes()).unwrap(); | ||
| AnyHash::Sha512(AnyHash64(hasher.finish().into())) |
| let mut hasher = Keccak256Hasher::default(); | ||
| hasher.write_all(other.as_bytes()).unwrap(); | ||
| hasher.write_all(self.as_bytes()).unwrap(); | ||
| AnyHash::Keccak256(AnyHash32(hasher.finish().into())) |
3e85391 to
8979767
Compare
| match self { | ||
| AnyHash::Blake2b(_) => { | ||
| let mut hasher = Blake2bHasher::default(); | ||
| hasher.write_all(self.as_bytes()).unwrap(); |
There was a problem hiding this comment.
Isn't it better to use chain to avoid importing std::io::Write?
8979767 to
5af33eb
Compare
| let combined: Vec<u8> = self | ||
| .as_bytes() | ||
| .iter() | ||
| .chain(other.as_bytes()) |
There was a problem hiding this comment.
I was thinking on chain from the hasher. Something like:
Blake2bHasher::default().chain(self.as_bytes()).chain(other.as_bytes()).finish();
There was a problem hiding this comment.
Ok there is a problem with chain because doing something like:
Blake2bHasher::default().chain(self.as_bytes()).chain(other.as_bytes()).finish().into(),
gives a compilation error because chain<T: SerializeContent>(self, h: &T) uses a type parameter T that is Sized by default. self.as_bytes() is &[u8], so T is inferred as [u8], which is unsized, so the compiler errors.
The fix would be to use the hasher's write API, as it currently is
There was a problem hiding this comment.
Ah yes, but then you could implement SerializeContent for AnyHash:
impl SerializeContent for AnyHash {
fn serialize_content<W: io::Write, H: HashOutput>(&self, writer: &mut W) -> io::Result<()> {
writer.write_all(self.as_bytes())?;
Ok(())
}
}
And then:
Blake2bHasher::default()
.chain(&self)
.chain(&other)
.finish()
.into(),
eaa3615 to
90efdcd
Compare
Functionality to request and relay keccak256 proofs Implemented requested changes
90efdcd to
7bb72f1
Compare
Functionality to request and relay keccak256 proofs
Pull request checklist
clippyandrustfmtwarnings.