-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/diff-testing
- Loading branch information
Showing
13 changed files
with
200 additions
and
1 deletion.
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
7 changes: 7 additions & 0 deletions
7
cairo_programs/cairo_0/bad_programs/bad_range_check96_builtin.cairo
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,7 @@ | ||
%builtins range_check96 | ||
|
||
func main{range_check96_ptr: felt*}() { | ||
assert [range_check96_ptr] = 2 ** 96; | ||
let range_check96_ptr = range_check96_ptr + 1; | ||
return (); | ||
} |
7 changes: 7 additions & 0 deletions
7
cairo_programs/cairo_0/bad_programs/bad_range_check_builtin.cairo
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,7 @@ | ||
%builtins range_check | ||
|
||
func main{range_check_ptr: felt*}() { | ||
assert [range_check_ptr] = -1; | ||
let range_check_ptr = range_check_ptr + 1; | ||
return (); | ||
} |
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
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
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
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,7 @@ | ||
%builtins range_check96 | ||
|
||
func main{range_check96_ptr: felt*}() { | ||
assert [range_check96_ptr] = 2 ** 96 - 1; | ||
let range_check96_ptr = range_check96_ptr + 1; | ||
return (); | ||
} |
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,7 @@ | ||
%builtins range_check | ||
|
||
func main{range_check_ptr: felt*}() { | ||
assert [range_check_ptr] = 2 ** 128 - 1; | ||
let range_check_ptr = range_check_ptr + 1; | ||
return (); | ||
} |
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
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,64 @@ | ||
import { describe, expect, test } from 'bun:test'; | ||
|
||
import { Relocatable } from 'primitives/relocatable'; | ||
import { Memory } from 'memory/memory'; | ||
import { rangeCheckHandler } from './rangeCheck'; | ||
import { Felt } from 'primitives/felt'; | ||
|
||
describe('range check', () => { | ||
test.each([new Felt(0n), new Felt((1n << 128n) - 1n)])( | ||
'should properly assert values inferior to 2 ** 128 in range check segment', | ||
(value) => { | ||
const memory = new Memory(); | ||
const { segmentId } = memory.addSegment(rangeCheckHandler(128n)); | ||
|
||
const offset = 0; | ||
const address = new Relocatable(segmentId, offset); | ||
|
||
expect(() => memory.assertEq(address, value)).not.toThrow(); | ||
expect(memory.segments[segmentId][offset]).toEqual(value); | ||
} | ||
); | ||
|
||
test.each([new Felt(0n), new Felt((1n << 96n) - 1n)])( | ||
'should properly assert values inferior to 2 ** 96 in range check96 segment', | ||
(value) => { | ||
const memory = new Memory(); | ||
const { segmentId } = memory.addSegment(rangeCheckHandler(96n)); | ||
|
||
const offset = 0; | ||
const address = new Relocatable(segmentId, offset); | ||
|
||
expect(() => memory.assertEq(address, value)).not.toThrow(); | ||
expect(memory.segments[segmentId][offset]).toEqual(value); | ||
} | ||
); | ||
|
||
test.each([new Felt(1n << 128n), new Felt(-1n)])( | ||
'should throw when trying to assert values equal or superior to 2 ** 128 in range check segment', | ||
(value) => { | ||
const memory = new Memory(); | ||
const { segmentId } = memory.addSegment(rangeCheckHandler(128n)); | ||
|
||
const offset = 0; | ||
const address = new Relocatable(segmentId, offset); | ||
|
||
expect(() => memory.assertEq(address, value)).toThrow(); | ||
expect(memory.segments[segmentId][offset]).toBeUndefined(); | ||
} | ||
); | ||
|
||
test.each([new Felt(1n << 96n), new Felt(-1n)])( | ||
'should throw when trying to assert values equal or superior to 2 ** 96 in range check96 segment', | ||
(value) => { | ||
const memory = new Memory(); | ||
const { segmentId } = memory.addSegment(rangeCheckHandler(96n)); | ||
|
||
const offset = 0; | ||
const address = new Relocatable(segmentId, offset); | ||
|
||
expect(() => memory.assertEq(address, value)).toThrow(); | ||
expect(memory.segments[segmentId][offset]).toBeUndefined(); | ||
} | ||
); | ||
}); |
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,20 @@ | ||
import { ExpectedOffset, RangeCheckOutOfBounds } from 'errors/builtins'; | ||
import { BuiltinHandler } from './builtin'; | ||
import { isFelt } from 'primitives/segmentValue'; | ||
import { ExpectedFelt } from 'errors/virtualMachine'; | ||
|
||
export const rangeCheckHandler = (boundExponent: bigint): BuiltinHandler => { | ||
return { | ||
set(target, prop, newValue): boolean { | ||
if (isNaN(Number(prop))) throw new ExpectedOffset(); | ||
|
||
const offset = Number(prop); | ||
if (!isFelt(newValue)) throw new ExpectedFelt(); | ||
if (newValue.toBigInt() >> boundExponent !== 0n) | ||
throw new RangeCheckOutOfBounds(); | ||
|
||
target[offset] = newValue; | ||
return true; | ||
}, | ||
}; | ||
}; |
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
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