Skip to content

Commit

Permalink
doc: add JSDoc to hintSchema primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
zmalatrax committed Jul 5, 2024
1 parent daf186e commit 0a7d7e6
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/hints/hintSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@ import { z } from 'zod';
import { Register } from 'vm/instruction';
import { Felt } from 'primitives/felt';

/**
* Types that can take a ResOp
*
* Generic patterns:
* - Deref: `[register + offset]`
* - DoubleDeref: `[[register + offset1] + offset2]`
* - Immediate: `0x1000`
* - BinOp (Add): `[register1 + offset1] + [register2 + offset2]`
* - BinOp (Mul): `[register1 + offset1] * [register2 + offset2]`
*/
export enum OpType {
Deref = 'Deref',
DoubleDeref = 'DoubleDeref',
Immediate = 'Immediate',
BinOp = 'BinOp',
}

/** Zod object to parse CellRef */
export const cellRef = z.object({
register: z.enum(['AP', 'FP']).transform((reg) => {
if (reg === 'AP') return Register.Ap;
Expand All @@ -19,6 +30,7 @@ export const cellRef = z.object({
offset: z.number(),
});

/** Zod object to parse Deref */
const deref = z
.object({
Deref: cellRef,
Expand All @@ -28,6 +40,7 @@ const deref = z
cell: object.Deref,
}));

/** Zod object to parse DoubleDeref */
const doubleDeref = z
.object({
DoubleDeref: z.object({
Expand All @@ -41,17 +54,22 @@ const doubleDeref = z
offset: object.DoubleDeref.offset,
}));

/** Zod object to parse Immediate */
const immediate = z
.object({
Immediate: z.string().transform((value) => new Felt(BigInt(value))),
})
.transform((object) => ({ type: OpType.Immediate, value: object.Immediate }));

/**
* Operation that can be done by a BinOp
*/
export enum Operation {
Add = 'Add',
Mul = 'Mul',
}

/** Zod object to parse a BinOp */
const binOp = z
.object({
BinOp: z.object({
Expand All @@ -69,13 +87,48 @@ const binOp = z
b: object.BinOp.b,
}));

/** Zod object to parse a ResOp */
export const resOp = z.union([deref, doubleDeref, immediate, binOp]);

/**
* A CellRef is an object defining a register and an offset
*
* `register + offset`
*/
export type CellRef = z.infer<typeof cellRef>;

/**
* A Deref is a CellRef wrapped in a `Deref` field
*
* `[register + offset]`
*/
export type Deref = z.infer<typeof deref>;

/**
* A DoubleDeref is a Deref wrapped in a `DoubleDeref` field
* with a second offset
*
* `[[register + offset] + offset]`
*/
export type DoubleDeref = z.infer<typeof doubleDeref>;

/**
* An Immediate is an hex string representing a Felt,
* wrapped in an `Immediate` field
*
* `0x1000`
*/
export type Immediate = z.infer<typeof immediate>;

/**
* A BinOp is an addition or a multiplication
* between two values.
* - `a` is a cellRef which should points to a Felt
* - `b` is either a Deref or an Immediate, should be a Felt in both cases.
*/
export type BinOp = z.infer<typeof binOp>;

/** A ResOp is either a Deref, DoubleDeref, Immediate or BinOp */
export type ResOp = z.infer<typeof resOp>;

export enum HintName {
Expand Down

0 comments on commit 0a7d7e6

Please sign in to comment.