Skip to content

Commit 93cf966

Browse files
committed
[WIP] EC_MUL_INNER hint
1 parent 1cdb017 commit 93cf966

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

cairo_programs/ec_mul_inner.cairo

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
%builtins range_check
2+
3+
from starkware.cairo.common.cairo_secp.ec import (
4+
EcPoint,
5+
ec_mul_inner,
6+
)
7+
from starkware.cairo.common.cairo_secp.bigint import BigInt3
8+
9+
func main{range_check_ptr: felt}() {
10+
// ec_mul_inner
11+
let (pow2, res) = ec_mul_inner(
12+
EcPoint(
13+
BigInt3(65162296, 359657, 04862662171381), BigInt3(-5166641367474701, -63029418, 793)
14+
),
15+
123,
16+
298,
17+
);
18+
assert pow2 = EcPoint(
19+
BigInt3(30016796425722798916160189, 75045389156830800234717485, 13862403786096360935413684),
20+
BigInt3(43820690643633544357415586, 29808113745001228006676979, 15112469502208690731782390),
21+
);
22+
return ();
23+
}

pkg/hints/ec_hint.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,20 @@ func ecNegateEmbeddedSecpP(virtual_machine vm.VirtualMachine, exec_scopes types.
114114
secp_p.Sub(secp_p, big.NewInt(19))
115115
return ecNegate(virtual_machine, exec_scopes, ids_data, *secp_p)
116116
}
117+
118+
/*
119+
Implements hint:
120+
%{ memory[ap] = (ids.scalar % PRIME) % 2 %}
121+
*/
122+
func ecMulInner(virtualMachine *vm.VirtualMachine, ids hint_utils.IdsManager) error {
123+
scalar, err := ids.GetFelt("scalar", virtualMachine)
124+
125+
if err != nil {
126+
return err
127+
}
128+
129+
result := scalar.And(lambdaworks.FeltOne())
130+
virtualMachine.Segments.Memory.Insert(virtualMachine.RunContext.Ap, memory.NewMaybeRelocatableFelt(result))
131+
132+
return nil
133+
}

pkg/hints/ec_hint_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,81 @@ func TestRunEcEmbeddedSecpOk(t *testing.T) {
128128
}
129129

130130
}
131+
132+
func TestEcMulInnerSuccessEven(t *testing.T) {
133+
vm := NewVirtualMachine()
134+
vm.Segments.AddSegment()
135+
vm.Segments.AddSegment()
136+
137+
scalar := NewMaybeRelocatableFelt(FeltFromUint64(89712))
138+
idsManager := SetupIdsForTest(
139+
map[string][]*MaybeRelocatable{
140+
"scalar": {scalar},
141+
},
142+
vm,
143+
)
144+
145+
hintProcessor := CairoVmHintProcessor{}
146+
147+
hintData := any(HintData{
148+
Ids: idsManager,
149+
Code: EC_MUL_INNER,
150+
})
151+
152+
vm.RunContext.Ap = NewRelocatable(1, 1)
153+
154+
err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil)
155+
result, err := vm.Segments.Memory.Get(NewRelocatable(1, 1))
156+
157+
if err != nil {
158+
t.Errorf("EC_MUL_INNER hint failed with error %s", err)
159+
}
160+
161+
resultFelt, ok := result.GetFelt()
162+
if !ok {
163+
t.Errorf("EC_MUL_INNER hint expected Felt value as result")
164+
}
165+
166+
if !resultFelt.IsZero() {
167+
t.Errorf("EC_MUL_INNER should have returned 0 but got %v", resultFelt)
168+
}
169+
}
170+
171+
func TestEcMulInnerSuccessOdd(t *testing.T) {
172+
vm := NewVirtualMachine()
173+
vm.Segments.AddSegment()
174+
vm.Segments.AddSegment()
175+
176+
scalar := NewMaybeRelocatableFelt(FeltFromUint64(89711))
177+
idsManager := SetupIdsForTest(
178+
map[string][]*MaybeRelocatable{
179+
"scalar": {scalar},
180+
},
181+
vm,
182+
)
183+
184+
hintProcessor := CairoVmHintProcessor{}
185+
186+
hintData := any(HintData{
187+
Ids: idsManager,
188+
Code: EC_MUL_INNER,
189+
})
190+
191+
vm.RunContext.Ap = NewRelocatable(1, 1)
192+
193+
err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil)
194+
result, err := vm.Segments.Memory.Get(NewRelocatable(1, 1))
195+
196+
if err != nil {
197+
t.Errorf("EC_MUL_INNER hint failed with error %s", err)
198+
}
199+
200+
resultFelt, ok := result.GetFelt()
201+
if !ok {
202+
t.Errorf("EC_MUL_INNER hint expected Felt value as result")
203+
}
204+
205+
if resultFelt.Cmp(FeltOne()) != 0 {
206+
t.Errorf("EC_MUL_INNER should have returned 1 but got %v", resultFelt)
207+
}
208+
}

pkg/hints/hint_codes/ec_op_hints.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ package hint_codes
22

33
const EC_NEGATE = "from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack\n\ny = pack(ids.point.y, PRIME) % SECP_P\n# The modulo operation in python always returns a nonnegative number.\nvalue = (-y) % SECP_P"
44
const EC_NEGATE_EMBEDDED_SECP = "from starkware.cairo.common.cairo_secp.secp_utils import pack\nSECP_P = 2**255-19\n\ny = pack(ids.point.y, PRIME) % SECP_P\n# The modulo operation in python always returns a nonnegative number.\nvalue = (-y) % SECP_P"
5+
const EC_MUL_INNER = "memory[ap] = (ids.scalar % PRIME) % 2"

pkg/hints/hint_processor.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any,
138138
return splitInt(data.Ids, vm)
139139
case SPLIT_INT_ASSERT_RANGE:
140140
return splitIntAssertRange(data.Ids, vm)
141+
case EC_MUL_INNER:
142+
return ecMulInner(vm, data.Ids)
141143
default:
142144
return errors.Errorf("Unknown Hint: %s", data.Code)
143145
}

pkg/vm/cairo_run/cairo_run_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,7 @@ func TestSplitIntHint(t *testing.T) {
197197
t.Errorf("Program execution failed with error: %s", err)
198198
}
199199
}
200+
201+
func TestEcMulInner(t *testing.T) {
202+
testProgram("ec_mul_inner", t)
203+
}

0 commit comments

Comments
 (0)