-
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.
* feat: add Pedersen builtin * fix: use pedersen handler in related tests
- Loading branch information
Showing
5 changed files
with
198 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
%builtins pedersen | ||
|
||
from starkware.cairo.common.cairo_builtins import HashBuiltin | ||
|
||
func main{pedersen_ptr: HashBuiltin*}() { | ||
assert pedersen_ptr.x = 0; | ||
assert pedersen_ptr.y = 0; | ||
assert pedersen_ptr.result = 0x49ee3eba8c1600700ee1b87eb599f16716b0b1022947733551fde4050ca6804; | ||
let pedersen_ptr = pedersen_ptr + HashBuiltin.SIZE; | ||
|
||
assert pedersen_ptr.x = 0; | ||
assert pedersen_ptr.y = 1; | ||
assert pedersen_ptr.result = 0x46c9aeb066cc2f41c7124af30514f9e607137fbac950524f5fdace5788f9d43; | ||
let pedersen_ptr = pedersen_ptr + HashBuiltin.SIZE; | ||
|
||
assert pedersen_ptr.x = 1; | ||
assert pedersen_ptr.y = 0; | ||
assert pedersen_ptr.result = 0x268a9d47dde48af4b6e2c33932ed1c13adec25555abaa837c376af4ea2f8a94; | ||
let pedersen_ptr = pedersen_ptr + HashBuiltin.SIZE; | ||
|
||
assert pedersen_ptr.x = 54; | ||
assert pedersen_ptr.y = 1249832432; | ||
assert pedersen_ptr.result = 0x20120a7d08fd21654c72a9281841406543b16d00faaca1069332053c41c07b8; | ||
let pedersen_ptr = pedersen_ptr + HashBuiltin.SIZE; | ||
|
||
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,93 @@ | ||
import { describe, expect, test } from 'bun:test'; | ||
|
||
import { UndefinedValue } from 'errors/builtins'; | ||
import { ExpectedFelt } from 'errors/virtualMachine'; | ||
|
||
import { Felt } from 'primitives/felt'; | ||
import { Relocatable } from 'primitives/relocatable'; | ||
import { Memory } from 'memory/memory'; | ||
import { pedersenHandler } from './pedersen'; | ||
|
||
type PedersenInput = { | ||
x: Felt; | ||
y: Felt; | ||
expected: Felt; | ||
}; | ||
|
||
describe('Pedersen', () => { | ||
const inputs: PedersenInput[] = [ | ||
{ | ||
x: new Felt(0n), | ||
y: new Felt(0n), | ||
expected: new Felt( | ||
0x49ee3eba8c1600700ee1b87eb599f16716b0b1022947733551fde4050ca6804n | ||
), | ||
}, | ||
{ | ||
x: new Felt(0n), | ||
y: new Felt(1n), | ||
expected: new Felt( | ||
0x46c9aeb066cc2f41c7124af30514f9e607137fbac950524f5fdace5788f9d43n | ||
), | ||
}, | ||
{ | ||
x: new Felt(1n), | ||
y: new Felt(0n), | ||
expected: new Felt( | ||
0x268a9d47dde48af4b6e2c33932ed1c13adec25555abaa837c376af4ea2f8a94n | ||
), | ||
}, | ||
{ | ||
x: new Felt(54n), | ||
y: new Felt(1249832432n), | ||
expected: new Felt( | ||
0x20120a7d08fd21654c72a9281841406543b16d00faaca1069332053c41c07b8n | ||
), | ||
}, | ||
]; | ||
|
||
test.each(inputs)( | ||
'should properly compute pedersen hash', | ||
({ x, y, expected }: PedersenInput) => { | ||
const memory = new Memory(); | ||
const { segmentId } = memory.addSegment(pedersenHandler); | ||
const addressHash = new Relocatable(segmentId, 2); | ||
|
||
memory.assertEq(new Relocatable(segmentId, 0), x); | ||
memory.assertEq(new Relocatable(segmentId, 1), y); | ||
|
||
expect(memory.get(addressHash)).toEqual(expected); | ||
} | ||
); | ||
|
||
test.each([0, 1])( | ||
'should throw UndefinedValue error when one of the two input is not constrained', | ||
(offset: number) => { | ||
const memory = new Memory(); | ||
const { segmentId } = memory.addSegment(pedersenHandler); | ||
const address = new Relocatable(segmentId, offset); | ||
const addressHash = new Relocatable(segmentId, 2); | ||
|
||
memory.assertEq(address, new Felt(0n)); | ||
|
||
expect(() => memory.get(addressHash)).toThrow( | ||
new UndefinedValue((offset + 1) % 2) | ||
); | ||
} | ||
); | ||
|
||
test('should throw ExpectedFelt error when trying to constrain an input cell to a Relocatable', () => { | ||
const memory = new Memory(); | ||
const { segmentId } = memory.addSegment(pedersenHandler); | ||
const addressHash = new Relocatable(segmentId, 2); | ||
|
||
const xAddr = new Relocatable(segmentId, 0); | ||
const yAddr = new Relocatable(segmentId, 1); | ||
const y = new Felt(1n); | ||
|
||
memory.assertEq(xAddr, xAddr); | ||
memory.assertEq(yAddr, y); | ||
|
||
expect(() => memory.get(addressHash)).toThrow(new ExpectedFelt()); | ||
}); | ||
}); |
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,38 @@ | ||
import { UndefinedValue } from 'errors/builtins'; | ||
import { BuiltinHandler } from './builtin'; | ||
import { ExpectedFelt } from 'errors/virtualMachine'; | ||
import { isFelt } from 'primitives/segmentValue'; | ||
import { pedersen } from '@scure/starknet'; | ||
import { Felt } from 'primitives/felt'; | ||
|
||
/** Pedersen Builtin - Computes Pedersen(x, y) */ | ||
export const pedersenHandler: BuiltinHandler = { | ||
get(target, prop) { | ||
if (isNaN(Number(prop))) { | ||
return Reflect.get(target, prop); | ||
} | ||
|
||
const cellsPerPedersen = 3; | ||
const inputCellsPerPedersen = 2; | ||
|
||
const offset = Number(prop); | ||
const pedersenIndex = offset % cellsPerPedersen; | ||
if (pedersenIndex < inputCellsPerPedersen) { | ||
return target[offset]; | ||
} | ||
|
||
const xOffset = offset - pedersenIndex; | ||
const xValue = target[xOffset]; | ||
if (!xValue) throw new UndefinedValue(xOffset); | ||
if (!isFelt(xValue)) throw new ExpectedFelt(); | ||
const x = xValue.toBigInt(); | ||
|
||
const yOffset = xOffset + 1; | ||
const yValue = target[yOffset]; | ||
if (!yValue) throw new UndefinedValue(yOffset); | ||
if (!isFelt(yValue)) throw new ExpectedFelt(); | ||
const y = yValue.toBigInt(); | ||
|
||
return (target[offset] = new Felt(BigInt(pedersen(x, y)))); | ||
}, | ||
}; |
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