# Candid, to\_candid, motoko assumptions

**URL:** <https://forum.dfinity.org/t/candid-to-candid-motoko-assumptions/18778>\
**Category:** Developers\
**Tags:** candid\
**Created:** [March 2, 2023, 1:06pm UTC](https://forum.dfinity.org/t/candid-to-candid-motoko-assumptions/18778 "2023-03-02T13:06:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [March 2, 2023, 1:06pm UTC](https://forum.dfinity.org/t/candid-to-candid-motoko-assumptions/18778/1 "2023-03-02T13:06:10Z")

</div>

Just a couple of assumptions/questions here that I’d like to have verified:

1. the bytes resulting from to\_candid are stable enough so that a hash will be consistent?
2. How expensive is the to\_candid method? If I’m trying to take a hash of a highly variant type with nesting variants, do you think it would be cheaper to write my own has or just hash the to\_candid output?

@kentosugama @ggreif @claudio

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [March 2, 2023, 2:10pm UTC](https://forum.dfinity.org/t/candid-to-candid-motoko-assumptions/18778/2 "2023-03-02T14:10:59Z")

</div>

Just a couple of assumptions/questions here that I’d like to have verified:

> 1. the bytes resulting from to\_candid are stable enough so that a hash will be consistent?

Absolutely not. Don’t use the blob for hashing or even equality testing.

> 1. How expensive is the to\_candid method? If I’m trying to take a hash of a highly variant type with nesting variants, do you think it would be cheaper to write my own has or just hash the to\_candid output?

It doesn’t matter how expensive it is, you need to write your own. The candid encoding is not a function but a relation. There are many valid candid encoding for the same source value and your motoko code will just return one of them. A different compilation of the same code could well produce different candid blobs for the same Motoko value. In principle, difference calls to\_candid on the same value could also produce different results in the same code, but that won’t happen at the moment, I expect.

I think I warn about this in the docs…

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [March 2, 2023, 2:15pm UTC](https://forum.dfinity.org/t/candid-to-candid-motoko-assumptions/18778/3 "2023-03-02T14:15:02Z")

</div>

The main reason the blob isn’t a function of the value is that the encoder is free to choose the representation of the type table included in the blob and different ordering of type table entries or different ways of representing the same recursive type (e.g. different number of unfoldings) can lead to totally different blobs that still encode the same value.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [March 2, 2023, 3:47pm UTC](https://forum.dfinity.org/t/candid-to-candid-motoko-assumptions/18778/4 "2023-03-02T15:47:25Z")

</div>

> [@claudio](#):
>
> Absolutely not. Don’t use the blob for hashing or even equality testing.

I’m glad I asked! 😂

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [March 2, 2023, 9:23pm UTC](https://forum.dfinity.org/t/candid-to-candid-motoko-assumptions/18778/5 "2023-03-02T21:23:48Z")

</div>

[Candid Serialization](https://internetcomputer.org/docs/current/motoko/main/language-manual#candid-serialization) actually warns about this at the end of that section.
