|
| 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 | + |
| 7 | + let (local qm31_op0_coordinates) = alloc(); |
| 8 | + assert qm31_op0_coordinates[0] = 1414213562; |
| 9 | + assert qm31_op0_coordinates[1] = 1732050807; |
| 10 | + assert qm31_op0_coordinates[2] = 1618033988; |
| 11 | + assert qm31_op0_coordinates[3] = 1234567890; |
| 12 | + 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); |
| 13 | + |
| 14 | + let (local qm31_op1_coordinates) = alloc(); |
| 15 | + assert qm31_op1_coordinates[0] = 1259921049; |
| 16 | + assert qm31_op1_coordinates[1] = 1442249570; |
| 17 | + assert qm31_op1_coordinates[2] = 1847759065; |
| 18 | + assert qm31_op1_coordinates[3] = 2094551481; |
| 19 | + 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); |
| 20 | + |
| 21 | + let (local qm31_add_dst_coordinates) = alloc(); |
| 22 | + assert qm31_add_dst_coordinates[0] = 526650964; |
| 23 | + assert qm31_add_dst_coordinates[1] = 1026816730; |
| 24 | + assert qm31_add_dst_coordinates[2] = 1318309406; |
| 25 | + assert qm31_add_dst_coordinates[3] = 1181635724; |
| 26 | + 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); |
| 27 | + |
| 28 | + let (local qm31_mul_dst_coordinates) = alloc(); |
| 29 | + assert qm31_mul_dst_coordinates[0] = 947980980; |
| 30 | + assert qm31_mul_dst_coordinates[1] = 1510986506; |
| 31 | + assert qm31_mul_dst_coordinates[2] = 623360030; |
| 32 | + assert qm31_mul_dst_coordinates[3] = 1260310989; |
| 33 | + 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); |
| 34 | + |
| 35 | + let runner_output_mul_dst = run_qm31_operation_get_dst(is_mul=TRUE, op0=qm31_op0, op1=qm31_op1); |
| 36 | + assert runner_output_mul_dst = qm31_mul_dst; |
| 37 | + let runner_output_add_dst = run_qm31_operation_get_dst(is_mul=FALSE, op0=qm31_op0, op1=qm31_op1); |
| 38 | + assert runner_output_add_dst = qm31_add_dst; |
| 39 | + |
| 40 | + let runner_output_mul_op1 = run_qm31_operation_get_op1(is_mul=TRUE, dst=qm31_mul_dst, op0=qm31_op0); |
| 41 | + assert runner_output_mul_op1 = qm31_op1; |
| 42 | + let runner_output_add_op1 = run_qm31_operation_get_op1(is_mul=FALSE, dst=qm31_add_dst, op0=qm31_op0); |
| 43 | + assert runner_output_add_op1 = qm31_op1; |
| 44 | + |
| 45 | + let runner_output_mul_op0 = run_qm31_operation_get_op0(is_mul=TRUE, dst=qm31_mul_dst, op1=qm31_op1); |
| 46 | + assert runner_output_mul_op0 = qm31_op0; |
| 47 | + let runner_output_add_op0 = run_qm31_operation_get_op0(is_mul=FALSE, dst=qm31_add_dst, op1=qm31_op1); |
| 48 | + assert runner_output_add_op0 = qm31_op0; |
| 49 | + |
| 50 | + return (); |
| 51 | +} |
| 52 | + |
| 53 | +func run_qm31_operation_get_dst( |
| 54 | + is_mul: felt, |
| 55 | + op0: felt, |
| 56 | + op1: felt, |
| 57 | +) -> felt { |
| 58 | + let offset0 = 2**15; |
| 59 | + let offset1 = (2**15)-4; |
| 60 | + let offset2 = (2**15)-3; |
| 61 | + |
| 62 | + let flag_dst_base_fp = 0; |
| 63 | + let flag_op0_base_fp = 1; |
| 64 | + let flag_op1_imm = 0; |
| 65 | + let flag_op1_base_fp = 1; |
| 66 | + let flag_op1_base_ap = 0; |
| 67 | + let flag_res_add = 0; |
| 68 | + let flag_res_mul = is_mul; |
| 69 | + let flag_PC_update_jump = 0; |
| 70 | + let flag_PC_update_jump_rel = 0; |
| 71 | + let flag_PC_update_jnz = 0; |
| 72 | + let flag_ap_update_add = 0; |
| 73 | + let flag_ap_update_add_1 = 0; |
| 74 | + let flag_opcode_call = 0; |
| 75 | + let flag_opcode_ret = 0; |
| 76 | + let flag_opcode_assert_eq = 1; |
| 77 | +
|
| 78 | + 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); |
| 79 | + 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); |
| 80 | + let qm31_opcode_extension_num = 3; |
| 81 | + let qm31_add_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_add*(2**48) + qm31_opcode_extension_num*(2**63); |
| 82 | + let qm31_mul_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_mul*(2**48) + qm31_opcode_extension_num*(2**63); |
| 83 | + static_assert qm31_mul_instruction_num==32302772004019011584; |
| 84 | + static_assert qm31_add_instruction_num==32284757605509529600; |
| 85 | +
|
| 86 | + if (is_mul == TRUE) { |
| 87 | + dw 32302772004019011584; |
| 88 | + return [ap]; |
| 89 | + } |
| 90 | + dw 32284757605509529600; |
| 91 | + return [ap]; |
| 92 | +} |
| 93 | + |
| 94 | +func run_qm31_operation_get_op1( |
| 95 | + is_mul: felt, |
| 96 | + dst: felt, |
| 97 | + op0: felt, |
| 98 | +) -> felt { |
| 99 | + let offset0 = (2**15)-4; |
| 100 | + let offset1 = (2**15)-3; |
| 101 | + let offset2 = 2**15; |
| 102 | + |
| 103 | + let flag_dst_base_fp = 1; |
| 104 | + let flag_op0_base_fp = 1; |
| 105 | + let flag_op1_imm = 0; |
| 106 | + let flag_op1_base_fp = 0; |
| 107 | + let flag_op1_base_ap = 1; |
| 108 | + let flag_res_add = 0; |
| 109 | + let flag_res_mul = is_mul; |
| 110 | + let flag_PC_update_jump = 0; |
| 111 | + let flag_PC_update_jump_rel = 0; |
| 112 | + let flag_PC_update_jnz = 0; |
| 113 | + let flag_ap_update_add = 0; |
| 114 | + let flag_ap_update_add_1 = 0; |
| 115 | + let flag_opcode_call = 0; |
| 116 | + let flag_opcode_ret = 0; |
| 117 | + let flag_opcode_assert_eq = 1; |
| 118 | +
|
| 119 | + 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); |
| 120 | + 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); |
| 121 | + let qm31_opcode_extension_num = 3; |
| 122 | + let qm31_add_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_add*(2**48) + qm31_opcode_extension_num*(2**63); |
| 123 | + let qm31_mul_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_mul*(2**48) + qm31_opcode_extension_num*(2**63); |
| 124 | + static_assert qm31_mul_instruction_num==32305305291694374908; |
| 125 | + static_assert qm31_add_instruction_num==32287290893184892924; |
| 126 | +
|
| 127 | + if (is_mul == TRUE) { |
| 128 | + dw 32305305291694374908; |
| 129 | + return [ap]; |
| 130 | + } |
| 131 | + dw 32287290893184892924; |
| 132 | + return [ap]; |
| 133 | +} |
| 134 | + |
| 135 | +func run_qm31_operation_get_op0( |
| 136 | + is_mul: felt, |
| 137 | + dst: felt, |
| 138 | + op1: felt, |
| 139 | +) -> felt { |
| 140 | + let offset0 = (2**15)-4; |
| 141 | + let offset1 = 2**15; |
| 142 | + let offset2 = (2**15)-3; |
| 143 | + |
| 144 | + let flag_dst_base_fp = 1; |
| 145 | + let flag_op0_base_fp = 0; |
| 146 | + let flag_op1_imm = 0; |
| 147 | + let flag_op1_base_fp = 1; |
| 148 | + let flag_op1_base_ap = 0; |
| 149 | + let flag_res_add = 0; |
| 150 | + let flag_res_mul = is_mul; |
| 151 | + let flag_PC_update_jump = 0; |
| 152 | + let flag_PC_update_jump_rel = 0; |
| 153 | + let flag_PC_update_jnz = 0; |
| 154 | + let flag_ap_update_add = 0; |
| 155 | + let flag_ap_update_add_1 = 0; |
| 156 | + let flag_opcode_call = 0; |
| 157 | + let flag_opcode_ret = 0; |
| 158 | + let flag_opcode_assert_eq = 1; |
| 159 | +
|
| 160 | + 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); |
| 161 | + 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); |
| 162 | + let qm31_opcode_extension_num = 3; |
| 163 | + let qm31_add_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_add*(2**48) + qm31_opcode_extension_num*(2**63); |
| 164 | + let qm31_mul_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_mul*(2**48) + qm31_opcode_extension_num*(2**63); |
| 165 | + static_assert qm31_mul_instruction_num==32302490529042563068; |
| 166 | + static_assert qm31_add_instruction_num==32284476130533081084; |
| 167 | +
|
| 168 | + if (is_mul == TRUE) { |
| 169 | + dw 32302490529042563068; |
| 170 | + return [ap]; |
| 171 | + } |
| 172 | + dw 32284476130533081084; |
| 173 | + return [ap]; |
| 174 | +} |
0 commit comments