Tracks
/
Cairo
Cairo
/
Exercises
/
Cryptographer
Cryptographer

Cryptographer

Learning Exercise

Introduction

In Cairo, one of the core concepts you'll encounter is the felt252 type, also known as a field element. This type represents integers within a specific range defined by a large prime number $P$. In this exercise, you'll work exclusively with the felt252 type, which is an essential part of Cairo programming.

The felt252 type in Cairo is a scalar type used to represent integers from 0 up to $P - 1$, where $P$ is a large prime number currently set to $( 2^{251} + 17 \cdot 2^{192} + 1 )$. This type ensures that all arithmetic operations are performed modulo $P$, making it different from standard integer types you might encounter in other programming languages.

Cairo provides support for basic arithmetic operations on felt252 values:

  • Addition (+): Adds two felt252 values. If the result exceeds $P$, it's reduced modulo $P$.
  • Subtraction (-): Subtracts one felt252 value from another. If the result is negative, it's adjusted to stay within the range by adding a multiple of $P$.
  • Multiplication (*): Multiplies two felt252 values, with the result computed modulo $P$.
  • Division (/): Divides one felt252 value by another, ensuring the result satisfies the equation $( \frac{x}{y} \cdot y = x \mod P)$.

Instructions

In this exercise, you'll be assisting a cryptographer in managing and verifying a secure communication system. The system relies heavily on field elements, specifically the felt252 type in Cairo, to ensure that all arithmetic operations remain within a defined range.

1. Calculate the Sum of Two Messages

Two encrypted messages, each represented as a felt252, need to be added together to create a combined message. However, because of the encryption, any result that exceeds the maximum value $P - 1$ must be wrapped around using modulo $P$.

Implement a function that takes in two felt252 values and returns their sum, ensuring the result stays within the valid range:

add_messages(3618502788666131213697322783095070105623107215331596699973092056135007822949, 987654321)
// => 123456789

Note: The return value should be a felt252.

2. Calculate the Difference Between Two Messages

Sometimes, messages need to be subtracted to retrieve the original data. If the result of the subtraction is negative, the system automatically adjusts the result by adding $P$ to bring it back within the range of a valid felt252 value.

Implement a function that takes in two felt252 values and returns their difference, ensuring the result stays within the valid range:

subtract_messages(123456789, 987654321)
// => 3618502788666131213697322783095070105623107215331596699973092056135007822949

Note: The return value should be a felt252.

3. Calculate a Valid Product of Two Messages

To ensure the integrity of encrypted data, the cryptographic system often multiplies two messages (felt252) to ensure their product stays within a valid range. The result must be computed modulo $P$ to ensure it remains within the valid range.

Implement a function that takes in two felt252 values and returns their product, ensuring the result stays within the valid range:

multiply_messages(382927772233671078546147897004315551461400594453673204174697055647611, 9449570)
// => 123456789

Note: The return value should be a felt252.

Edit via GitHub The link opens in a new window or tab
Cairo Exercism

Ready to start Cryptographer?

Sign up to Exercism to learn and master Cairo with 25 concepts68 exercises, and real human mentoring, all for free.