Skip to content

Commit

Permalink
Fix merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsc2 committed Sep 26, 2023
2 parents 2bcf015 + d638fec commit a09bda2
Show file tree
Hide file tree
Showing 19 changed files with 1,056 additions and 7 deletions.
29 changes: 29 additions & 0 deletions cairo_programs/cairo_keccak.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
%builtins range_check bitwise

from starkware.cairo.common.cairo_keccak.keccak import cairo_keccak, finalize_keccak
from starkware.cairo.common.uint256 import Uint256
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin
from starkware.cairo.common.alloc import alloc

func main{range_check_ptr: felt, bitwise_ptr: BitwiseBuiltin*}() {
alloc_locals;

let (keccak_ptr: felt*) = alloc();
let keccak_ptr_start = keccak_ptr;

let (inputs: felt*) = alloc();

assert inputs[0] = 8031924123371070792;
assert inputs[1] = 560229490;

let n_bytes = 16;

let (res: Uint256) = cairo_keccak{keccak_ptr=keccak_ptr}(inputs=inputs, n_bytes=n_bytes);

assert res.low = 293431514620200399776069983710520819074;
assert res.high = 317109767021952548743448767588473366791;

finalize_keccak(keccak_ptr_start=keccak_ptr_start, keccak_ptr_end=keccak_ptr);

return ();
}
31 changes: 31 additions & 0 deletions cairo_programs/keccak_add_uint256.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
%builtins output range_check bitwise

from starkware.cairo.common.keccak_utils.keccak_utils import keccak_add_uint256
from starkware.cairo.common.uint256 import Uint256
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.serialize import serialize_word

func main{output_ptr: felt*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() {
alloc_locals;

let (inputs) = alloc();
let inputs_start = inputs;

let num = Uint256(34623634663146736, 598249824422424658356);

keccak_add_uint256{inputs=inputs_start}(num=num, bigend=0);

assert inputs[0] = 34623634663146736;
assert inputs[1] = 0;
assert inputs[2] = 7954014063719006644;
assert inputs[3] = 32;

serialize_word(inputs[0]);
serialize_word(inputs[1]);
serialize_word(inputs[2]);
serialize_word(inputs[3]);

return ();
}

107 changes: 107 additions & 0 deletions cairo_programs/keccak_integration_tests.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
%builtins range_check bitwise

from starkware.cairo.common.keccak import unsafe_keccak, unsafe_keccak_finalize, KeccakState
from starkware.cairo.common.cairo_keccak.keccak import cairo_keccak, finalize_keccak
from starkware.cairo.common.keccak_utils.keccak_utils import keccak_add_uint256
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.uint256 import Uint256
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin
from starkware.cairo.common.math import unsigned_div_rem

func fill_array(array: felt*, base: felt, step: felt, array_length: felt, iter: felt) {
if (iter == array_length) {
return ();
}
assert array[iter] = base + step * iter;
return fill_array(array, base, step, array_length, iter + 1);
}

func test_integration{range_check_ptr: felt, bitwise_ptr: BitwiseBuiltin*}(iter: felt, last: felt) {
alloc_locals;
if (iter == last) {
return ();
}

let (data_1: felt*) = alloc();
let data_len: felt = 15;
let chunk_len: felt = 5;

fill_array(data_1, iter, iter + 1, data_len, 0);

let (low_1: felt, high_1: felt) = unsafe_keccak(data_1, chunk_len);
let (low_2: felt, high_2: felt) = unsafe_keccak(data_1 + chunk_len, chunk_len);
let (low_3: felt, high_3: felt) = unsafe_keccak(data_1 + 2 * chunk_len, chunk_len);

// With the results of unsafe_keccak, create an array to pass to unsafe_keccak_finalize
// through a KeccakState
let (data_2: felt*) = alloc();
assert data_2[0] = low_1;
assert data_2[1] = high_1;
assert data_2[2] = low_2;
assert data_2[3] = high_2;
assert data_2[4] = low_3;
assert data_2[5] = high_3;

let keccak_state: KeccakState = KeccakState(start_ptr=data_2, end_ptr=data_2 + 6);
let res_1: Uint256 = unsafe_keccak_finalize(keccak_state);

let (data_3: felt*) = alloc();

// This is done to make sure that the numbers inserted in data_3
// fit in a u64
let (q, r) = unsigned_div_rem(res_1.low, 18446744073709551615);
assert data_3[0] = q;
let (q, r) = unsigned_div_rem(res_1.high, 18446744073709551615);
assert data_3[1] = q;

let (keccak_ptr: felt*) = alloc();
let keccak_ptr_start = keccak_ptr;

let res_2: Uint256 = cairo_keccak{keccak_ptr=keccak_ptr}(data_3, 16);

finalize_keccak(keccak_ptr_start=keccak_ptr_start, keccak_ptr_end=keccak_ptr);

let (inputs) = alloc();
let inputs_start = inputs;
keccak_add_uint256{inputs=inputs_start}(num=res_2, bigend=0);

// These values are hardcoded for last = 10
// Since we are dealing with hash functions and using the output of one of them
// as the input of the other, asserting only the last results of the iteration
// should be enough
if (iter == last - 1 and last == 10) {
assert res_2.low = 3896836249413878817054429671793519200;
assert res_2.high = 253424239110447628170109510737834198489;
assert inputs[0] = 16681956707691293280;
assert inputs[1] = 211247916371739620;
assert inputs[2] = 6796127878994642393;
assert inputs[3] = 13738155530201662906;
}

// These values are hardcoded for last = 100
// This should be used for benchmarking.
if (iter == last - 1 and last == 100) {
assert res_2.low = 52798800345724801884797411011515944813;
assert res_2.high = 159010026777930121161844734347918361509;
assert inputs[0] = 14656556134934286189;
assert inputs[1] = 2862228701973161639;
assert inputs[2] = 206697371206337445;
assert inputs[3] = 8619950823980503604;
}

return test_integration{range_check_ptr=range_check_ptr, bitwise_ptr=bitwise_ptr}(
iter + 1, last
);
}

func run_test{range_check_ptr: felt, bitwise_ptr: BitwiseBuiltin*}(last: felt) {
test_integration(0, last);
return ();
}

func main{range_check_ptr: felt, bitwise_ptr: BitwiseBuiltin*}() {
run_test(10);
return ();
}
4 changes: 2 additions & 2 deletions pkg/builtins/keccak.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (k *KeccakBuiltinRunner) DeduceMemoryCell(address Relocatable, mem *Memory)
for i := 0; i < 25; i++ {
output_message_u64[i] = binary.LittleEndian.Uint64(output_message_bytes[8*i : 8*i+8])
}
keccakF1600(&output_message_u64)
KeccakF1600(&output_message_u64)

// Convert back to bytes
output_message := make([]byte, 0, 200)
Expand Down Expand Up @@ -152,7 +152,7 @@ var rc = [24]uint64{

// keccakF1600 applies the Keccak permutation to a 1600b-wide
// state represented as a slice of 25 uint64s.
func keccakF1600(a *[25]uint64) {
func KeccakF1600(a *[25]uint64) {
// Implementation translated from Keccak-inplace.c
// in the keccak reference code.
var t, bc0, bc1, bc2, bc3, bc4, d0, d1, d2, d3, d4 uint64
Expand Down
98 changes: 97 additions & 1 deletion pkg/hints/ec_hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func computeSlope(vm *VirtualMachine, execScopes ExecutionScopes, idsData IdsMan

/*
Implements hint:
<<<<<<< HEAD
%{ from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_ALPHA as ALPHA %}
*/

Expand Down Expand Up @@ -257,11 +258,106 @@ func computeDoublingSlopeExternalConsts(vm VirtualMachine, execScopes ExecutionS
doublePoint_b := builtins.DoublePointB{X: point.X.Pack86(), Y: point.Y.Pack86()}

value, err := builtins.EcDoubleSlope(doublePoint_b, alpha, secpP)
execScopes.AssignOrUpdateVariable("value", value)
execScopes.AssignOrUpdateVariable("slope", value)
return nil
}

/*
%{
from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
slope = pack(ids.slope, PRIME)
x0 = pack(ids.point0.x, PRIME)
x1 = pack(ids.point1.x, PRIME)
y0 = pack(ids.point0.y, PRIME)
value = new_x = (pow(slope, 2, SECP_P) - x0 - x1) % SECP_P"
%}
*/
func fastEcAddAssignNewX(ids IdsManager, vm *VirtualMachine, execScopes *ExecutionScopes, point0Alias string, point1Alias string, secpP big.Int) error {
execScopes.AssignOrUpdateVariable("SECP_P", secpP)

point0, err := EcPointFromVarName(point0Alias, vm, ids)
point1, err := EcPointFromVarName(point1Alias, vm, ids)
if err != nil {
return err
}
slopeUnpacked, err := BigInt3FromVarName("slope", ids, vm)
if err != nil {
return err
}

slope := slopeUnpacked.Pack86()
slope = *new(big.Int).Mod(&slope, &secpP)

x0 := point0.X.Pack86()
x0 = *new(big.Int).Mod(&x0, &secpP)

x1 := point1.X.Pack86()
x1 = *new(big.Int).Mod(&x1, &secpP)

y0 := point0.Y.Pack86()
y0 = *new(big.Int).Mod(&y0, &secpP)

slopeSquared := new(big.Int).Mul(&slope, &slope)
x0PlusX1 := new(big.Int).Add(&x0, &x1)

value := *new(big.Int).Sub(slopeSquared, x0PlusX1)
value = *new(big.Int).Mod(&value, &secpP)

execScopes.AssignOrUpdateVariable("slope", slope)
execScopes.AssignOrUpdateVariable("x0", x0)
execScopes.AssignOrUpdateVariable("y0", y0)
execScopes.AssignOrUpdateVariable("value", value)
execScopes.AssignOrUpdateVariable("slope", value)
execScopes.AssignOrUpdateVariable("new_x", value)

return nil
}

/*
Implements hint:
%{ value = new_y = (slope * (x0 - new_x) - y0) % SECP_P %}
*/
func fastEcAddAssignNewY(execScopes *ExecutionScopes) error {
slope, err := execScopes.Get("slope")
if err != nil {
return err
}
slopeBigInt := slope.(big.Int)
x0, err := execScopes.Get("x0")
if err != nil {
return err
}
x0BigInt := x0.(big.Int)

newX, err := execScopes.Get("new_x")
if err != nil {
return err
}
newXBigInt := newX.(big.Int)

y0, err := execScopes.Get("y0")
if err != nil {
return err
}
y0BigInt := y0.(big.Int)

secpP, err := execScopes.Get("SECP_P")
if err != nil {
return err
}
secpBigInt := secpP.(big.Int)

x0MinusNewX := *new(big.Int).Sub(&x0BigInt, &newXBigInt)
x0MinusNewXMinusY0 := *new(big.Int).Sub(&x0MinusNewX, &y0BigInt)
valueBeforeMod := *new(big.Int).Mul(&slopeBigInt, &x0MinusNewXMinusY0)
value := *new(big.Int).Mod(&valueBeforeMod, &secpBigInt)

execScopes.AssignOrUpdateVariable("value", value)
execScopes.AssignOrUpdateVariable("new_y", value)

return nil
}
Loading

0 comments on commit a09bda2

Please sign in to comment.