# RAVEN hashes

> How RAVEN turns a SEG into a behavior hash, what changes the hash and what doesn't, and how to use it for caching, provenance and migrations.

Source: https://atracode.com/docs/raven

A RAVEN hash names what a program does. RAVEN writes a program's [Semantic Execution Graph](/docs/semantic-execution-graph) out in one canonical byte form and hashes those bytes with SHA-256. The result is a 64-character hex string, printed as `raven:<hash>`.

Two programs share a RAVEN hash only when they compute the same thing, whatever language they started in. A change in behavior, even to the rounding of one value, gives a new hash.

## How the hash is made

There are two steps:

1. **Canonical serialization.** The SEG is written out in a single, fixed byte form. Anything that doesn't affect behavior, such as source variable names, formatting, comments and the order in which independent nodes happened to be produced, has no place in that form. Two graphs that compute the same thing serialize to the same bytes.
2. **SHA-256.** The bytes are hashed with SHA-256.

Because the hash comes from the graph and the graph comes from behavior, the hash depends only on behavior. You can recompute it yourself with `atracode hash` and compare it with any hash you've been given.

## What changes the hash

| Change | Hash |
| --- | --- |
| Renaming variables or functions | Same |
| Reformatting, adding or removing comments | Same |
| Compiler version, optimization flags, target platform | Same |
| Rewriting the program in another language with the same behavior | Same |
| Changing an integer width or overflow behavior (for example wrapping to checked) | Different |
| Changing rounding, even for one value | Different |
| Reading a new input, or fixing an input as a constant | Different |
| Adding or removing a memory write | Different |

The short rule: build settings don't count, and behavior does.

## Example: four files, two behaviors

Here are four versions of the compound interest program from the [quickstart](/docs/quickstart), compiled with Atracode:

| File | Language | RAVEN hash |
| --- | --- | --- |
| `INTEREST.cbl` | COBOL | `raven:13803fe6908d64b3301b931688e6244c99d9b80f15c781f5de7581abb7c3d2af` |
| `interest.py` | Python | `raven:13803fe6908d64b3301b931688e6244c99d9b80f15c781f5de7581abb7c3d2af` |
| `interest.rs` | Rust | `raven:13803fe6908d64b3301b931688e6244c99d9b80f15c781f5de7581abb7c3d2af` |
| `interest.rs` | Rust, rounds half to even | `raven:9c41e07b52aa0f6e3d19c8b4407f2e15a6d0b93e7c58124f0e6ab3d9f1c27e85` |

The first three round half away from zero each year, COBOL with `ROUNDED`, Python and Rust by adding 5,000 before dividing by 10,000. They lower to the same graph and share a hash.

The fourth file rounds half to even instead. For most balances the result is identical, but on an exact half cent it differs, so it is a different computation and gets a different hash. The hash catches a difference that a handful of hand-picked test cases could easily miss.

> **Note:** The hashes in this example are illustrative.

## Uses

### Caching

The hash is a content address for behavior. If you have already compiled, tested or run something with a given RAVEN hash, any program with that hash will behave identically, so you can reuse the result. Team plans share caches across a workspace.

### Provenance and audit

A RAVEN hash names exactly what was verified. Record it when you release, and anyone can later recompute it from the source and check it matches. `atracode audit <dir>` compares built artifacts against the verified hashes of their source. A backdoored compiler or an altered build produces a program whose hash no longer matches the verified graph, so tampering shows up.

### Migration proof

When you rewrite code, in the same language or a different one, hash the old version and the new one. If the hashes match, the rewrite kept its behavior. If they don't, `atracode verify-equivalence INTEREST.cbl interest.rs` shows an input where the two diverge.

This is how a COBOL to Rust migration can be checked function by function, instead of relying on a test suite to cover every case.

## Next steps

- [Verification](/docs/verification): how each compile is checked before it gets a hash
- [Semantic Execution Graph](/docs/semantic-execution-graph): the graph RAVEN serializes
- [Attestation](/docs/attestation): how Atra OS nodes report what they are running
