Skip to content

Commit 6dd3870

Browse files
add qm31 operations to VM
1 parent a10d51d commit 6dd3870

File tree

8 files changed

+770
-28
lines changed

8 files changed

+770
-28
lines changed
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
from starkware.cairo.common.alloc import alloc
2+
from starkware.cairo.common.bool import FALSE, TRUE
3+
4+
func main{}() {
5+
alloc_locals;
6+
// x*y coordinates_to_packed([947980980, 1510986506, 623360030, 1260310989]),
7+
// x+y coordinates_to_packed([526650964, 1026816730, 1318309406, 1181635724]),
8+
// x coordinates_to_packed([1414213562, 1732050807, 1618033988, 1234567890]),
9+
// y coordinates_to_packed([1259921049, 1442249570, 1847759065, 2094551481]),
10+
let (local qm31_op0_coordinates) = alloc();
11+
assert qm31_op0_coordinates[0] = 1414213562;
12+
assert qm31_op0_coordinates[1] = 1732050807;
13+
assert qm31_op0_coordinates[2] = 1618033988;
14+
assert qm31_op0_coordinates[3] = 1234567890;
15+
let qm31_op0 = qm31_op0_coordinates[0] + qm31_op0_coordinates[1]*(2**36) + qm31_op0_coordinates[2]*(2**72) + qm31_op0_coordinates[3]*(2**108);
16+
17+
let (local qm31_op1_coordinates) = alloc();
18+
assert qm31_op1_coordinates[0] = 1259921049;
19+
assert qm31_op1_coordinates[1] = 1442249570;
20+
assert qm31_op1_coordinates[2] = 1847759065;
21+
assert qm31_op1_coordinates[3] = 2094551481;
22+
let qm31_op1 = qm31_op1_coordinates[0] + qm31_op1_coordinates[1]*(2**36) + qm31_op1_coordinates[2]*(2**72) + qm31_op1_coordinates[3]*(2**108);
23+
24+
let (local qm31_add_dst_coordinates) = alloc();
25+
assert qm31_add_dst_coordinates[0] = 526650964;
26+
assert qm31_add_dst_coordinates[1] = 1026816730;
27+
assert qm31_add_dst_coordinates[2] = 1318309406;
28+
assert qm31_add_dst_coordinates[3] = 1181635724;
29+
let qm31_add_dst = qm31_add_dst_coordinates[0] + qm31_add_dst_coordinates[1]*(2**36) + qm31_add_dst_coordinates[2]*(2**72) + qm31_add_dst_coordinates[3]*(2**108);
30+
31+
let (local qm31_mul_dst_coordinates) = alloc();
32+
assert qm31_mul_dst_coordinates[0] = 947980980;
33+
assert qm31_mul_dst_coordinates[1] = 1510986506;
34+
assert qm31_mul_dst_coordinates[2] = 623360030;
35+
assert qm31_mul_dst_coordinates[3] = 1260310989;
36+
let qm31_mul_dst = qm31_mul_dst_coordinates[0] + qm31_mul_dst_coordinates[1]*(2**36) + qm31_mul_dst_coordinates[2]*(2**72) + qm31_mul_dst_coordinates[3]*(2**108);
37+
38+
let runner_output_mul_dst = run_qm31_operation_get_dst(is_mul=TRUE, op0=qm31_op0, op1=qm31_op1);
39+
assert runner_output_mul_dst = qm31_mul_dst;
40+
let runner_output_add_dst = run_qm31_operation_get_dst(is_mul=FALSE, op0=qm31_op0, op1=qm31_op1);
41+
assert runner_output_add_dst = qm31_add_dst;
42+
//let runner_output_mul_dst = run_qm31_operation_get_dst(is_mul=TRUE, op0=qm31_op0, op1=qm31_op1);
43+
// assert runner_output_mul_dst = qm31_mul_dst;
44+
45+
let runner_output_mul_op1 = run_qm31_operation_get_op1(is_mul=TRUE, dst=qm31_mul_dst, op0=qm31_op0);
46+
assert runner_output_mul_op1 = qm31_op1;
47+
let runner_output_add_op1 = run_qm31_operation_get_op1(is_mul=FALSE, dst=qm31_add_dst, op0=qm31_op0);
48+
assert runner_output_add_op1 = qm31_op1;
49+
50+
let runner_output_mul_op0 = run_qm31_operation_get_op0(is_mul=TRUE, dst=qm31_mul_dst, op1=qm31_op1);
51+
assert runner_output_mul_op0 = qm31_op0;
52+
let runner_output_add_op0 = run_qm31_operation_get_op0(is_mul=FALSE, dst=qm31_add_dst, op1=qm31_op1);
53+
assert runner_output_add_op0 = qm31_op0;
54+
55+
// let runner_output_mul_op0 = run_qm31_operation_get_op0(is_mul=TRUE, dst=qm31_mul_dst, op1=qm31_op1);
56+
// //assert qm31_op1 = qm31_op0;
57+
// assert runner_output_mul_op0 = qm31_op0;
58+
// let runner_output_mul_op1 = run_qm31_operation_get_op1(is_mul=TRUE, dst=qm31_mul_dst, op0=qm31_op0);
59+
// assert runner_output_mul_op1 = qm31_op1;
60+
61+
return ();
62+
}
63+
64+
// missing_operand_index
65+
func run_qm31_operation_get_dst(
66+
is_mul: felt,
67+
op0: felt,
68+
op1: felt,
69+
) -> felt {
70+
//alloc_locals;
71+
72+
let offset0 = 2**15;
73+
let offset1 = (2**15)-4;
74+
let offset2 = (2**15)-3;
75+
76+
let flag_dst_base_fp = 0;
77+
let flag_op0_base_fp = 1;
78+
let flag_op1_imm = 0;
79+
let flag_op1_base_fp = 1;
80+
let flag_op1_base_ap = 0;
81+
let flag_res_add = 0;
82+
let flag_res_mul = is_mul; //
83+
let flag_PC_update_jump = 0;
84+
let flag_PC_update_jump_rel = 0;
85+
let flag_PC_update_jnz = 0;
86+
let flag_ap_update_add = 0;
87+
let flag_ap_update_add_1 = 0;
88+
let flag_opcode_call = 0;
89+
let flag_opcode_ret = 0;
90+
let flag_opcode_assert_eq = 1;
91+
92+
let flag_num_qm31_add = flag_dst_base_fp+flag_op0_base_fp*(2**1)+flag_op1_imm*(2**2)+flag_op1_base_fp*(2**3)+flag_op1_base_ap*(2**4)+0*(2**6)+flag_opcode_assert_eq*(2**14);
93+
let flag_num_qm31_mul = flag_dst_base_fp+flag_op0_base_fp*(2**1)+flag_op1_imm*(2**2)+flag_op1_base_fp*(2**3)+flag_op1_base_ap*(2**4)+1*(2**6)+flag_opcode_assert_eq*(2**14);
94+
let qm31_opcode_extension_num = 3;
95+
let qm31_add_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_add*(2**48) + qm31_opcode_extension_num*(2**63);
96+
let qm31_mul_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_mul*(2**48) + qm31_opcode_extension_num*(2**63);
97+
static_assert qm31_mul_instruction_num==32302772004019011584;
98+
static_assert qm31_add_instruction_num==32284757605509529600;
99+
100+
if (is_mul == TRUE) {
101+
dw 32302772004019011584;
102+
[ap - 1] = [ap -1];
103+
} else {
104+
dw 32284757605509529600;
105+
}
106+
// dw 32302772004019011584;
107+
return [ap];
108+
}
109+
110+
// offset0 = 0;
111+
// offset1 = (2**15)-4
112+
// offset2 = (2**15)-3
113+
114+
// flag_dst_base_fp = 0
115+
// flag_op0_base_fp = 1
116+
// flag_op1_imm = 0
117+
// flag_op1_base_fp = 1
118+
// flag_op1_base_ap = 0
119+
// flag_res_add = 0
120+
// flag_res_mul = is_mul #
121+
// flag_PC_update_jump = 0
122+
// flag_PC_update_jump_rel = 0
123+
// flag_PC_update_jnz = 0
124+
// flag_ap_update_add = 0
125+
// flag_ap_update_add_1 = 0
126+
// flag_opcode_call = 0
127+
// flag_opcode_ret = 0
128+
// flag_opcode_assert_eq = 1
129+
130+
// flag_num_qm31_add = flag_dst_base_fp+flag_op0_base_fp*(2**1)+flag_op1_imm*(2**2)+flag_op1_base_fp*(2**3)+0*(2**6)+flag_opcode_assert_eq*(2**14)
131+
// flag_num_qm31_mul = flag_dst_base_fp+flag_op0_base_fp*(2**1)+flag_op1_imm*(2**2)+flag_op1_base_fp*(2**3)+1*(2**6)+flag_opcode_assert_eq*(2**14)
132+
// qm31_opcode_extension_num = 3
133+
// qm31_add_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_add*(2**48) + qm31_opcode_extension_num*(2**63)
134+
// qm31_mul_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_mul*(2**48) + qm31_opcode_extension_num*(2**63)
135+
136+
func run_qm31_operation_get_op1(
137+
is_mul: felt,
138+
dst: felt,
139+
op0: felt,
140+
) -> felt {
141+
//alloc_locals;
142+
143+
let offset0 = (2**15)-4;
144+
let offset1 = (2**15)-3;
145+
let offset2 = 2**15;
146+
147+
let flag_dst_base_fp = 1;
148+
let flag_op0_base_fp = 1;
149+
let flag_op1_imm = 0;
150+
let flag_op1_base_fp = 0;
151+
let flag_op1_base_ap = 1;
152+
let flag_res_add = 0;
153+
let flag_res_mul = is_mul; //
154+
let flag_PC_update_jump = 0;
155+
let flag_PC_update_jump_rel = 0;
156+
let flag_PC_update_jnz = 0;
157+
let flag_ap_update_add = 0;
158+
let flag_ap_update_add_1 = 0;
159+
let flag_opcode_call = 0;
160+
let flag_opcode_ret = 0;
161+
let flag_opcode_assert_eq = 1;
162+
163+
let flag_num_qm31_add = flag_dst_base_fp+flag_op0_base_fp*(2**1)+flag_op1_imm*(2**2)+flag_op1_base_fp*(2**3)+flag_op1_base_ap*(2**4)+0*(2**6)+flag_opcode_assert_eq*(2**14);
164+
let flag_num_qm31_mul = flag_dst_base_fp+flag_op0_base_fp*(2**1)+flag_op1_imm*(2**2)+flag_op1_base_fp*(2**3)+flag_op1_base_ap*(2**4)+1*(2**6)+flag_opcode_assert_eq*(2**14);
165+
let qm31_opcode_extension_num = 3;
166+
let qm31_add_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_add*(2**48) + qm31_opcode_extension_num*(2**63);
167+
let qm31_mul_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_mul*(2**48) + qm31_opcode_extension_num*(2**63);
168+
static_assert qm31_mul_instruction_num==32305305291694374908;
169+
static_assert qm31_add_instruction_num==32287290893184892924;
170+
171+
if (is_mul == TRUE) {
172+
dw 32305305291694374908;
173+
[ap - 1] = [ap -1];
174+
} else {
175+
dw 32287290893184892924;
176+
}
177+
return [ap];
178+
}
179+
180+
// offset0 = (2**15)-4
181+
// offset1 = (2**15)-3
182+
// offset2 = 2**15
183+
184+
// flag_dst_base_fp = 1
185+
// flag_op0_base_fp = 1
186+
// flag_op1_imm = 0
187+
// flag_op1_base_fp = 0
188+
// flag_op1_base_ap = 1
189+
// flag_res_add = 0
190+
// flag_res_mul = is_mul //
191+
// flag_PC_update_jump = 0
192+
// flag_PC_update_jump_rel = 0
193+
// flag_PC_update_jnz = 0
194+
// flag_ap_update_add = 0
195+
// flag_ap_update_add_1 = 0
196+
// flag_opcode_call = 0
197+
// flag_opcode_ret = 0
198+
// flag_opcode_assert_eq = 1
199+
200+
// flag_num_qm31_add = flag_dst_base_fp+flag_op0_base_fp*(2**1)+flag_op1_imm*(2**2)+flag_op1_base_fp*(2**3)+flag_op1_base_ap*(2**4)+0*(2**6)+flag_opcode_assert_eq*(2**14)
201+
// flag_num_qm31_mul = flag_dst_base_fp+flag_op0_base_fp*(2**1)+flag_op1_imm*(2**2)+flag_op1_base_fp*(2**3)+flag_op1_base_ap*(2**4)+1*(2**6)+flag_opcode_assert_eq*(2**14)
202+
// qm31_opcode_extension_num = 3;
203+
// qm31_add_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_add*(2**48) + qm31_opcode_extension_num*(2**63);
204+
// qm31_mul_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_mul*(2**48) + qm31_opcode_extension_num*(2**63);
205+
206+
func run_qm31_operation_get_op0(
207+
is_mul: felt,
208+
dst: felt,
209+
op1: felt,
210+
) -> felt {
211+
//alloc_locals;
212+
213+
let offset0 = (2**15)-4;
214+
let offset1 = 2**15;
215+
let offset2 = (2**15)-3;
216+
217+
let flag_dst_base_fp = 1;
218+
let flag_op0_base_fp = 0;
219+
let flag_op1_imm = 0;
220+
let flag_op1_base_fp = 1;
221+
let flag_op1_base_ap = 0;
222+
let flag_res_add = 0;
223+
let flag_res_mul = is_mul; //
224+
let flag_PC_update_jump = 0;
225+
let flag_PC_update_jump_rel = 0;
226+
let flag_PC_update_jnz = 0;
227+
let flag_ap_update_add = 0;
228+
let flag_ap_update_add_1 = 0;
229+
let flag_opcode_call = 0;
230+
let flag_opcode_ret = 0;
231+
let flag_opcode_assert_eq = 1;
232+
233+
let flag_num_qm31_add = flag_dst_base_fp+flag_op0_base_fp*(2**1)+flag_op1_imm*(2**2)+flag_op1_base_fp*(2**3)+flag_op1_base_ap*(2**4)+0*(2**6)+flag_opcode_assert_eq*(2**14);
234+
let flag_num_qm31_mul = flag_dst_base_fp+flag_op0_base_fp*(2**1)+flag_op1_imm*(2**2)+flag_op1_base_fp*(2**3)+flag_op1_base_ap*(2**4)+1*(2**6)+flag_opcode_assert_eq*(2**14);
235+
let qm31_opcode_extension_num = 3;
236+
let qm31_add_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_add*(2**48) + qm31_opcode_extension_num*(2**63);
237+
let qm31_mul_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_mul*(2**48) + qm31_opcode_extension_num*(2**63);
238+
static_assert qm31_mul_instruction_num==32302490529042563068;
239+
static_assert qm31_add_instruction_num==32284476130533081084;
240+
241+
if (is_mul == TRUE) {
242+
dw 32302490529042563068;
243+
[ap - 1] = [ap -1];
244+
} else {
245+
dw 32284476130533081084;
246+
}
247+
return [ap];
248+
}
249+
250+
// offset0 = (2**15)-4
251+
// offset1 = 2**15
252+
// offset2 = (2**15)-3
253+
254+
// flag_dst_base_fp = 1
255+
// flag_op0_base_fp = 0
256+
// flag_op1_imm = 0
257+
// flag_op1_base_fp = 1
258+
// flag_op1_base_ap = 0
259+
// flag_res_add = 0
260+
// flag_res_mul = is_mul //
261+
// flag_PC_update_jump = 0
262+
// flag_PC_update_jump_rel = 0
263+
// flag_PC_update_jnz = 0
264+
// flag_ap_update_add = 0
265+
// flag_ap_update_add_1 = 0
266+
// flag_opcode_call = 0
267+
// flag_opcode_ret = 0
268+
// flag_opcode_assert_eq = 1
269+
270+
// flag_num_qm31_add = flag_dst_base_fp+flag_op0_base_fp*(2**1)+flag_op1_imm*(2**2)+flag_op1_base_fp*(2**3)+flag_op1_base_ap*(2**4)+0*(2**6)+flag_opcode_assert_eq*(2**14)
271+
// flag_num_qm31_mul = flag_dst_base_fp+flag_op0_base_fp*(2**1)+flag_op1_imm*(2**2)+flag_op1_base_fp*(2**3)+flag_op1_base_ap*(2**4)+1*(2**6)+flag_opcode_assert_eq*(2**14)
272+
// qm31_opcode_extension_num = 3
273+
// qm31_add_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_add*(2**48) + qm31_opcode_extension_num*(2**63)
274+
// qm31_mul_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_mul*(2**48) + qm31_opcode_extension_num*(2**63)

0 commit comments

Comments
 (0)