-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(avm): tagged memory #4213
Merged
Merged
feat(avm): tagged memory #4213
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
64d0fdb
WIP prototyping memory types interfaces
fcarreiro 7ff4eb6
WIP tagged memory
fcarreiro 2af730c
fix tests
fcarreiro dbc296f
format
fcarreiro f4f3945
more fixing and formatting
fcarreiro 4d5ba0a
format
fcarreiro 051eec1
remove comment
fcarreiro b35ced1
Merge branch 'master' into fc/01-24-tagged-memory
Maddiaa0 bbc10d4
fix: compilation error
Maddiaa0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
yarn-project/acir-simulator/src/avm/avm_memory_types.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { FieldValue, Uint8, Uint32 } from './avm_memory_types.js'; | ||
|
||
describe('Uint8', () => { | ||
it('Unsigned 8 max value', () => { | ||
expect(new Uint8(255).toBigInt()).toEqual(255n); | ||
}); | ||
|
||
it('Unsigned 8 bit add', () => { | ||
expect(new Uint8(50).add(new Uint8(20))).toEqual(new Uint8(70)); | ||
}); | ||
|
||
it('Unsigned 8 bit add wraps', () => { | ||
expect(new Uint8(200).add(new Uint8(100))).toEqual(new Uint8(44)); | ||
}); | ||
}); | ||
|
||
describe('Uint32', () => { | ||
it('ands', () => { | ||
expect(new Uint32(0b11111110010011100100n).and(new Uint32(0b11100100111001001111n))).toEqual( | ||
new Uint32(0b11100100010001000100n), | ||
); | ||
}); | ||
}); | ||
|
||
describe('FieldValue', () => { | ||
it('xxx', () => { | ||
expect(new FieldValue(27).add(new FieldValue(48))).toEqual(new FieldValue(75)); | ||
}); | ||
}); |
292 changes: 292 additions & 0 deletions
292
yarn-project/acir-simulator/src/avm/avm_memory_types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,292 @@ | ||
import { Fr } from '@aztec/foundation/fields'; | ||
|
||
import { strict as assert } from 'assert'; | ||
|
||
export interface FieldValueType { | ||
add(rhs: FieldValueType): FieldValueType; | ||
sub(rhs: FieldValueType): FieldValueType; | ||
mul(rhs: FieldValueType): FieldValueType; | ||
div(rhs: FieldValueType): FieldValueType; | ||
|
||
// Use sparingly. | ||
toBigInt(): bigint; | ||
} | ||
|
||
export interface IntegralValueType extends FieldValueType { | ||
shl(rhs: IntegralValueType): IntegralValueType; | ||
shr(rhs: IntegralValueType): IntegralValueType; | ||
and(rhs: IntegralValueType): IntegralValueType; | ||
or(rhs: IntegralValueType): IntegralValueType; | ||
xor(rhs: IntegralValueType): IntegralValueType; | ||
not(): IntegralValueType; | ||
} | ||
|
||
// TODO: optimize calculation of mod, etc. Can only 1 per class? | ||
// TODO: optimize return new UnsignedInteger. | ||
abstract class UnsignedInteger implements IntegralValueType { | ||
private readonly bitmask: bigint; | ||
private readonly mod: bigint; | ||
|
||
protected constructor(private n: bigint, private bits: bigint) { | ||
assert(bits > 0); | ||
// x % 2^n == x & (2^n - 1) | ||
this.mod = 1n << bits; | ||
fcarreiro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.bitmask = this.mod - 1n; | ||
assert(n < this.mod); | ||
} | ||
|
||
// TODO: comment | ||
protected abstract build(n: bigint): UnsignedInteger; | ||
|
||
public add(rhs: UnsignedInteger): UnsignedInteger { | ||
assert(this.bits == rhs.bits); | ||
return this.build((this.n + rhs.n) & this.bitmask); | ||
} | ||
|
||
public sub(rhs: UnsignedInteger): UnsignedInteger { | ||
assert(this.bits == rhs.bits); | ||
const res: bigint = this.n - rhs.n; | ||
return this.build(res >= 0 ? res : res + this.mod); | ||
} | ||
|
||
public mul(rhs: UnsignedInteger): UnsignedInteger { | ||
assert(this.bits == rhs.bits); | ||
return this.build((this.n * rhs.n) & this.bitmask); | ||
} | ||
|
||
public div(rhs: UnsignedInteger): UnsignedInteger { | ||
assert(this.bits == rhs.bits); | ||
return this.build(this.n / rhs.n); | ||
} | ||
|
||
// No sign extension. | ||
public shr(rhs: UnsignedInteger): UnsignedInteger { | ||
assert(this.bits == rhs.bits); | ||
// Note that this.n is > 0 by class invariant. | ||
return this.build(this.n >> rhs.n); | ||
} | ||
|
||
public shl(rhs: UnsignedInteger): UnsignedInteger { | ||
assert(this.bits == rhs.bits); | ||
return this.build((this.n << rhs.n) & this.bitmask); | ||
} | ||
|
||
public and(rhs: UnsignedInteger): UnsignedInteger { | ||
assert(this.bits == rhs.bits); | ||
return this.build(this.n & rhs.n); | ||
} | ||
|
||
public or(rhs: UnsignedInteger): UnsignedInteger { | ||
assert(this.bits == rhs.bits); | ||
return this.build(this.n | rhs.n); | ||
} | ||
|
||
public xor(rhs: UnsignedInteger): UnsignedInteger { | ||
assert(this.bits == rhs.bits); | ||
return this.build(this.n ^ rhs.n); | ||
} | ||
|
||
public not(): UnsignedInteger { | ||
return this.build(~this.n & this.bitmask); | ||
} | ||
|
||
public toBigInt(): bigint { | ||
return this.n; | ||
} | ||
|
||
public equals(rhs: UnsignedInteger) { | ||
return this.bits == rhs.bits && this.toBigInt() == rhs.toBigInt(); | ||
} | ||
} | ||
|
||
export class Uint8 extends UnsignedInteger { | ||
constructor(n: number | bigint) { | ||
super(BigInt(n), 8n); | ||
} | ||
|
||
// TODO: should be private | ||
build(n: bigint): Uint8 { | ||
return new Uint8(n); | ||
} | ||
} | ||
|
||
export class Uint16 extends UnsignedInteger { | ||
constructor(n: number | bigint) { | ||
super(BigInt(n), 16n); | ||
} | ||
|
||
// TODO: should be private | ||
build(n: bigint): Uint16 { | ||
return new Uint16(n); | ||
} | ||
} | ||
|
||
export class Uint32 extends UnsignedInteger { | ||
constructor(n: number | bigint) { | ||
super(BigInt(n), 32n); | ||
} | ||
|
||
// TODO: should be private | ||
build(n: bigint): Uint32 { | ||
return new Uint32(n); | ||
} | ||
} | ||
|
||
export class Uint64 extends UnsignedInteger { | ||
constructor(n: number | bigint) { | ||
super(BigInt(n), 64n); | ||
} | ||
|
||
// TODO: should be private | ||
build(n: bigint): Uint64 { | ||
return new Uint64(n); | ||
} | ||
} | ||
|
||
export class Uint128 extends UnsignedInteger { | ||
constructor(n: number | bigint) { | ||
super(BigInt(n), 128n); | ||
} | ||
|
||
// TODO: should be private | ||
build(n: bigint): Uint128 { | ||
return new Uint128(n); | ||
} | ||
} | ||
|
||
export class FieldValue implements FieldValueType { | ||
private readonly rep: Fr; | ||
|
||
constructor(v: number | bigint | Fr) { | ||
this.rep = new Fr(v); | ||
} | ||
|
||
public add(rhs: FieldValue): FieldValue { | ||
return new FieldValue(this.rep.add(rhs.rep)); | ||
} | ||
|
||
public sub(rhs: FieldValue): FieldValue { | ||
return new FieldValue(this.rep.sub(rhs.rep)); | ||
} | ||
|
||
public mul(rhs: FieldValue): FieldValue { | ||
return new FieldValue(this.rep.mul(rhs.rep)); | ||
} | ||
|
||
public div(rhs: FieldValue): FieldValue { | ||
return new FieldValue(this.rep.div(rhs.rep)); | ||
} | ||
|
||
public toBigInt(): bigint { | ||
return this.rep.toBigInt(); | ||
} | ||
} | ||
|
||
export enum TypeTag { | ||
UNINITIALIZED, | ||
UINT8, | ||
UINT16, | ||
UINT32, | ||
UINT64, | ||
UINT128, | ||
FIELD, | ||
INVALID, | ||
} | ||
|
||
export class TaggedMemory { | ||
private _mem: FieldValueType[]; | ||
|
||
constructor() { | ||
this._mem = []; | ||
} | ||
|
||
public get(offset: number): FieldValueType { | ||
return this.getAs<FieldValueType>(offset); | ||
} | ||
|
||
public getAs<T>(offset: number): T { | ||
assert(offset < 2n ** 32n); | ||
// TODO: non-existing case. | ||
const e = this._mem[offset]; | ||
return <T>e; | ||
} | ||
|
||
public getSlice(offset: number, size: number): FieldValueType[] { | ||
assert(offset < 2n ** 32n); | ||
// TODO: non-existing case. | ||
return this._mem.slice(offset, offset + size); | ||
} | ||
|
||
public getSliceTags(offset: number, size: number): TypeTag[] { | ||
assert(offset < 2n ** 32n); | ||
// TODO: non-existing case. | ||
return this._mem.slice(offset, offset + size).map(TaggedMemory.getTag); | ||
} | ||
|
||
public set(offset: number, v: FieldValueType) { | ||
assert(offset < 2n ** 32n); | ||
this._mem[offset] = v; | ||
} | ||
|
||
public setSlice(offset: number, vs: FieldValueType[]) { | ||
assert(offset < 2n ** 32n); | ||
this._mem.splice(offset, vs.length, ...vs); | ||
} | ||
|
||
public getTag(offset: number): TypeTag { | ||
return TaggedMemory.getTag(this._mem[offset]); | ||
} | ||
|
||
public tagsMatchStrict(offsetA: number, offsetB: number): boolean { | ||
return this.getTag(offsetA) == this.getTag(offsetB); | ||
} | ||
|
||
public tagsMatch(offsetA: number, offsetB: number): boolean { | ||
return ( | ||
this.tagsMatchStrict(offsetA, offsetB) || | ||
this.getTag(offsetA) == TypeTag.UNINITIALIZED || | ||
this.getTag(offsetB) == TypeTag.UNINITIALIZED | ||
); | ||
} | ||
|
||
public static getTag(v: FieldValueType | undefined): TypeTag { | ||
let tag = TypeTag.INVALID; | ||
|
||
if (v === undefined) { | ||
tag = TypeTag.UNINITIALIZED; | ||
} else if (v instanceof FieldValue) { | ||
tag = TypeTag.FIELD; | ||
} else if (v instanceof Uint8) { | ||
tag = TypeTag.UINT8; | ||
} else if (v instanceof Uint16) { | ||
tag = TypeTag.UINT16; | ||
} else if (v instanceof Uint32) { | ||
tag = TypeTag.UINT32; | ||
} else if (v instanceof Uint64) { | ||
tag = TypeTag.UINT64; | ||
} else if (v instanceof Uint128) { | ||
tag = TypeTag.UINT128; | ||
} | ||
|
||
return tag; | ||
} | ||
|
||
// Truncates! | ||
public static integralFromTag(v: bigint, tag: TypeTag): IntegralValueType { | ||
switch (tag) { | ||
case TypeTag.UINT8: | ||
return new Uint8(v & ((1n << 8n) - 1n)); | ||
case TypeTag.UINT16: | ||
return new Uint16(v & ((1n << 16n) - 1n)); | ||
case TypeTag.UINT32: | ||
return new Uint32(v & ((1n << 32n) - 1n)); | ||
case TypeTag.UINT64: | ||
return new Uint64(v & ((1n << 64n) - 1n)); | ||
case TypeTag.UINT128: | ||
return new Uint128(v & ((1n << 128n) - 1n)); | ||
default: | ||
// TODO: better message | ||
throw new Error('wrong tag for integral type'); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume more tests are coming for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes (but not in this PR! lmk if you feel strongly). I'm thinking if it makes sense to test everything here (for the types) AND in the arithmetic+bitwise opcodes, or if there's a less verbose way. (note that there will be massive duplication).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good i can wait until later!