# Cyclic Redundancy Checks

**URL:** <https://forum.dfinity.org/t/cyclic-redundancy-checks/676>\
**Category:** Programs & Applications\
**Created:** [May 17, 2020, 8:18pm UTC](https://forum.dfinity.org/t/cyclic-redundancy-checks/676 "2020-05-17T20:18:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![enzo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/enzo/32/268_2.png) [@enzo](https://forum.dfinity.org/u/enzo)\
**Post date:** [May 17, 2020, 8:18pm UTC](https://forum.dfinity.org/t/cyclic-redundancy-checks/676/1 "2020-05-17T20:18:19Z")

</div>

> **[enzoh/motoko-crc](https://github.com/enzoh/motoko-crc)**
>
> The Motoko CRC package. Contribute to enzoh/motoko-crc development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![enzo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/enzo/32/268_2.png) [@enzo](https://forum.dfinity.org/u/enzo)\
**Post date:** [May 17, 2020, 8:33pm UTC](https://forum.dfinity.org/t/cyclic-redundancy-checks/676/2 "2020-05-17T20:33:42Z")

</div>

This package can be useful in situations where a canister needs to get its own identifier.

```auto
import Array "mo:base/Array";
import CRC8 "../vendor/crc/src/CRC8";
import Hex "../vendor/hex/src/Hex";
import Iter "mo:base/Iter";
import Prim "mo:prim";

actor {

  public func proxy() : async Text {
    await whoami();
  };

  public shared {
    caller = caller;
  } func whoami() : async Text {
    let base = Iter.toArray<Word8>(Prim.blobOfPrincipal(caller).bytes());
    let crc8 = CRC8.crc8(base);
    Hex.encode(Array.append<Word8>(base, [crc8]));
  };
};

```

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [May 18, 2020, 12:45am UTC](https://forum.dfinity.org/t/cyclic-redundancy-checks/676/3 "2020-05-18T00:45:22Z")

</div>

You can return `caller` directly as `Principal`. When displaying the principal in text, the system will append CRC8 automatically.

```auto
public shared { caller = caller } func whoami() : async Principal { caller }

```

This works in JavaScript. dfx cannot decode Principal properly but will support it soon.

---

<div class="post-metadata">

**Author:** ![enzo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/enzo/32/268_2.png) [@enzo](https://forum.dfinity.org/u/enzo)\
**Post date:** [May 18, 2020, 1:07am UTC](https://forum.dfinity.org/t/cyclic-redundancy-checks/676/4 "2020-05-18T01:07:35Z")

</div>

> [@chenyan](#):
>
> You can return `caller` directly as `Principal` . When displaying the principal in text, the system will append CRC8 automatically.

Ah, you mean if I simply want to print it to the command-line, but I don’t want to do that. I need to pass this result to another canister that will instantiate an actor from it. Would be cool if we had `Prim.principalToText` that performs the hex encoding and tacks on the CRC.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [May 18, 2020, 2:02am UTC](https://forum.dfinity.org/t/cyclic-redundancy-checks/676/6 "2020-05-18T02:02:29Z")

</div>

Ah, that’s even simpler then.

```auto
import P "mo:base/Principal";
actor Self {
    public func greet() : async Principal {
       P.fromActor(Self)
    };
};

```

---

<div class="post-metadata">

**Author:** ![enzo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/enzo/32/268_2.png) [@enzo](https://forum.dfinity.org/u/enzo)\
**Post date:** [May 18, 2020, 3:50am UTC](https://forum.dfinity.org/t/cyclic-redundancy-checks/676/7 "2020-05-18T03:50:10Z")

</div>

Ah, yes! That is super helpful! I also want to go the other way around though.

```auto
let principal = .. : Principal;
// Don't I need to convert the principal to text here?
let counter = actor principal : actor {
  get() : async Nat;
};
count = await counter.get();

```

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [May 18, 2020, 5:45am UTC](https://forum.dfinity.org/t/cyclic-redundancy-checks/676/8 "2020-05-18T05:45:32Z")

</div>

are you building a crypto lib?

---

<div class="post-metadata">

**Author:** ![enzo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/enzo/32/268_2.png) [@enzo](https://forum.dfinity.org/u/enzo)\
**Post date:** [May 18, 2020, 5:47am UTC](https://forum.dfinity.org/t/cyclic-redundancy-checks/676/9 "2020-05-18T05:47:45Z")

</div>

No, DHT

> **[enzoh/motoko-dht](https://github.com/enzoh/motoko-dht)**
>
> The Motoko DHT Canister. Contribute to enzoh/motoko-dht development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [May 18, 2020, 6:17am UTC](https://forum.dfinity.org/t/cyclic-redundancy-checks/676/10 "2020-05-18T06:17:38Z")

</div>

very interesting, is this something you can easily build out of the box with motoko on the icp? i‘m especially thinking of the „distributed“ part of this, i‘m guessing you won‘t manually decide on which replica to store what?

---

<div class="post-metadata">

**Author:** ![enzo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/enzo/32/268_2.png) [@enzo](https://forum.dfinity.org/u/enzo)\
**Post date:** [May 18, 2020, 6:43am UTC](https://forum.dfinity.org/t/cyclic-redundancy-checks/676/11 "2020-05-18T06:43:49Z")

</div>

Yeah exactly. The implementation is pretty bare bones at the moment, but yes, you can create several instances of this canister and register them with one another. The maximum amount of memory a canister can consume is 4GB according to the WASM spec. With the DHT, you should be able to store an unbounded amount data since you can just keep adding more canisters.

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [May 18, 2020, 3:35pm UTC](https://forum.dfinity.org/t/cyclic-redundancy-checks/676/12 "2020-05-18T15:35:30Z")

</div>

is there an easy out of the box way to get the amount of memory consumed by the canister already or would need to implement some sort of counter yourself?

---

<div class="post-metadata">

**Author:** ![enzo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/enzo/32/268_2.png) [@enzo](https://forum.dfinity.org/u/enzo)\
**Post date:** [May 18, 2020, 4:16pm UTC](https://forum.dfinity.org/t/cyclic-redundancy-checks/676/13 "2020-05-18T16:16:07Z")

</div>

Great question! I know there is a WASM opcode called `current_memory` that returns the current memory size in units of pages, where the page size is 64kB, but I’m not sure if there is a Motoko function to return it. Maybe @claudio knows?

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [May 18, 2020, 10:55pm UTC](https://forum.dfinity.org/t/cyclic-redundancy-checks/676/14 "2020-05-18T22:55:30Z")

</div>

You can use `Prim.rts_memory_size()`
