-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef1aa46
commit bd6140f
Showing
7 changed files
with
446 additions
and
27 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
263 changes: 263 additions & 0 deletions
263
cairo_programs/stwo_exclusive_programs/qm31_opcodes_test.cairo
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,263 @@ | ||
from starkware.cairo.common.bool import FALSE, TRUE | ||
|
||
func main{}() { | ||
let qm31_op0_coordinates_a = 1414213562; | ||
let qm31_op0_coordinates_b = 1732050807; | ||
let qm31_op0_coordinates_c = 1618033988; | ||
let qm31_op0_coordinates_d = 1234567890; | ||
let qm31_op0 = qm31_op0_coordinates_a + qm31_op0_coordinates_b*(2**36) + qm31_op0_coordinates_c*(2**72) + qm31_op0_coordinates_d*(2**108); | ||
|
||
let qm31_op1_coordinates_a = 1259921049; | ||
let qm31_op1_coordinates_b = 1442249570; | ||
let qm31_op1_coordinates_c = 1847759065; | ||
let qm31_op1_coordinates_d = 2094551481; | ||
let qm31_op1 = qm31_op1_coordinates_a + qm31_op1_coordinates_b*(2**36) + qm31_op1_coordinates_c*(2**72) + qm31_op1_coordinates_d*(2**108); | ||
static_assert qm31_op1==679720817185961464190715473544778505313945; | ||
|
||
|
||
let qm31_add_dst_coordinates_a = 526650964; | ||
let qm31_add_dst_coordinates_b = 1026816730; | ||
let qm31_add_dst_coordinates_c = 1318309406; | ||
let qm31_add_dst_coordinates_d = 1181635724; | ||
let qm31_add_dst = qm31_add_dst_coordinates_a + qm31_add_dst_coordinates_b*(2**36) + qm31_add_dst_coordinates_c*(2**72) + qm31_add_dst_coordinates_d*(2**108); | ||
|
||
let qm31_mul_dst_coordinates_a = 947980980; | ||
let qm31_mul_dst_coordinates_b = 1510986506; | ||
let qm31_mul_dst_coordinates_c = 623360030; | ||
let qm31_mul_dst_coordinates_d = 1260310989; | ||
let qm31_mul_dst = qm31_mul_dst_coordinates_a + qm31_mul_dst_coordinates_b*(2**36) + qm31_mul_dst_coordinates_c*(2**72) + qm31_mul_dst_coordinates_d*(2**108); | ||
|
||
let runner_output_mul_dst = run_qm31_operation_get_dst(is_mul=TRUE, op0=qm31_op0, op1=qm31_op1); | ||
assert runner_output_mul_dst = qm31_mul_dst; | ||
let runner_output_add_dst = run_qm31_operation_get_dst(is_mul=FALSE, op0=qm31_op0, op1=qm31_op1); | ||
assert runner_output_add_dst = qm31_add_dst; | ||
|
||
let runner_output_mul_op1 = run_qm31_operation_get_op1(is_mul=TRUE, dst=qm31_mul_dst, op0=qm31_op0); | ||
assert runner_output_mul_op1 = qm31_op1; | ||
let runner_output_add_op1 = run_qm31_operation_get_op1(is_mul=FALSE, dst=qm31_add_dst, op0=qm31_op0); | ||
assert runner_output_add_op1 = qm31_op1; | ||
|
||
let runner_output_mul_op0 = run_qm31_operation_get_op0(is_mul=TRUE, dst=qm31_mul_dst, op1=qm31_op1); | ||
assert runner_output_mul_op0 = qm31_op0; | ||
let runner_output_add_op0 = run_qm31_operation_get_op0(is_mul=FALSE, dst=qm31_add_dst, op1=qm31_op1); | ||
assert runner_output_add_op0 = qm31_op0; | ||
|
||
let runner_output_mul_dst = run_qm31_operation_imm_op1_get_dst(is_mul=TRUE, op0=qm31_op0); | ||
assert runner_output_mul_dst = qm31_mul_dst; | ||
let runner_output_add_dst = run_qm31_operation_imm_op1_get_dst(is_mul=FALSE, op0=qm31_op0); | ||
assert runner_output_add_dst = qm31_add_dst; | ||
|
||
let runner_output_mul_op0 = run_qm31_operation_imm_op1_get_op0(is_mul=TRUE, dst=qm31_mul_dst); | ||
assert runner_output_mul_op0 = qm31_op0; | ||
let runner_output_add_op0 = run_qm31_operation_imm_op1_get_op0(is_mul=FALSE, dst=qm31_add_dst); | ||
assert runner_output_add_op0 = qm31_op0; | ||
|
||
return (); | ||
} | ||
|
||
func run_qm31_operation_get_dst( | ||
is_mul: felt, | ||
op0: felt, | ||
op1: felt, | ||
) -> felt { | ||
let offset0 = 2**15; | ||
let offset1 = (2**15)-4; | ||
let offset2 = (2**15)-3; | ||
let flag_dst_base_fp = 0; | ||
let flag_op0_base_fp = 1; | ||
let flag_op1_imm = 0; | ||
let flag_op1_base_fp = 1; | ||
let flag_op1_base_ap = 0; | ||
let flag_res_add = 1-is_mul; | ||
let flag_res_mul = is_mul; | ||
let flag_PC_update_jump = 0; | ||
let flag_PC_update_jump_rel = 0; | ||
let flag_PC_update_jnz = 0; | ||
let flag_ap_update_add = 0; | ||
let flag_ap_update_add_1 = 0; | ||
let flag_opcode_call = 0; | ||
let flag_opcode_ret = 0; | ||
let flag_opcode_assert_eq = 1; | ||
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)+1*(2**5)+0*(2**6)+flag_opcode_assert_eq*(2**14); | ||
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)+0*(2**5)+1*(2**6)+flag_opcode_assert_eq*(2**14); | ||
let qm31_opcode_extension_num = 3; | ||
let qm31_add_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_add*(2**48) + qm31_opcode_extension_num*(2**63); | ||
let qm31_mul_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_mul*(2**48) + qm31_opcode_extension_num*(2**63); | ||
static_assert qm31_mul_instruction_num==32302772004019011584; | ||
static_assert qm31_add_instruction_num==32293764804764270592; | ||
if (is_mul == TRUE) { | ||
dw 32302772004019011584; | ||
return [ap]; | ||
} | ||
dw 32293764804764270592; | ||
return [ap]; | ||
} | ||
|
||
func run_qm31_operation_get_op1( | ||
is_mul: felt, | ||
dst: felt, | ||
op0: felt, | ||
) -> felt { | ||
let offset0 = (2**15)-4; | ||
let offset1 = (2**15)-3; | ||
let offset2 = 2**15; | ||
let flag_dst_base_fp = 1; | ||
let flag_op0_base_fp = 1; | ||
let flag_op1_imm = 0; | ||
let flag_op1_base_fp = 0; | ||
let flag_op1_base_ap = 1; | ||
let flag_res_add = 1-is_mul; | ||
let flag_res_mul = is_mul; | ||
let flag_PC_update_jump = 0; | ||
let flag_PC_update_jump_rel = 0; | ||
let flag_PC_update_jnz = 0; | ||
let flag_ap_update_add = 0; | ||
let flag_ap_update_add_1 = 0; | ||
let flag_opcode_call = 0; | ||
let flag_opcode_ret = 0; | ||
let flag_opcode_assert_eq = 1; | ||
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)+1*(2**5)+0*(2**6)+flag_opcode_assert_eq*(2**14); | ||
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)+0*(2**5)+1*(2**6)+flag_opcode_assert_eq*(2**14); | ||
let qm31_opcode_extension_num = 3; | ||
let qm31_add_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_add*(2**48) + qm31_opcode_extension_num*(2**63); | ||
let qm31_mul_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_mul*(2**48) + qm31_opcode_extension_num*(2**63); | ||
static_assert qm31_mul_instruction_num==32305305291694374908; | ||
static_assert qm31_add_instruction_num==32296298092439633916; | ||
if (is_mul == TRUE) { | ||
dw 32305305291694374908; | ||
return [ap]; | ||
} | ||
dw 32296298092439633916; | ||
return [ap]; | ||
} | ||
|
||
func run_qm31_operation_get_op0( | ||
is_mul: felt, | ||
dst: felt, | ||
op1: felt, | ||
) -> felt { | ||
let offset0 = (2**15)-4; | ||
let offset1 = 2**15; | ||
let offset2 = (2**15)-3; | ||
let flag_dst_base_fp = 1; | ||
let flag_op0_base_fp = 0; | ||
let flag_op1_imm = 0; | ||
let flag_op1_base_fp = 1; | ||
let flag_op1_base_ap = 0; | ||
let flag_res_add = 1-is_mul; | ||
let flag_res_mul = is_mul; | ||
let flag_PC_update_jump = 0; | ||
let flag_PC_update_jump_rel = 0; | ||
let flag_PC_update_jnz = 0; | ||
let flag_ap_update_add = 0; | ||
let flag_ap_update_add_1 = 0; | ||
let flag_opcode_call = 0; | ||
let flag_opcode_ret = 0; | ||
let flag_opcode_assert_eq = 1; | ||
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)+1*(2**5)+0*(2**6)+flag_opcode_assert_eq*(2**14); | ||
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)+0*(2**5)+1*(2**6)+flag_opcode_assert_eq*(2**14); | ||
let qm31_opcode_extension_num = 3; | ||
let qm31_add_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_add*(2**48) + qm31_opcode_extension_num*(2**63); | ||
let qm31_mul_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_mul*(2**48) + qm31_opcode_extension_num*(2**63); | ||
static_assert qm31_mul_instruction_num==32302490529042563068; | ||
static_assert qm31_add_instruction_num==32293483329787822076; | ||
if (is_mul == TRUE) { | ||
dw 32302490529042563068; | ||
return [ap]; | ||
} | ||
dw 32293483329787822076; | ||
return [ap]; | ||
} | ||
|
||
func run_qm31_operation_imm_op1_get_dst( | ||
is_mul: felt, | ||
op0: felt, | ||
) -> felt { | ||
let offset0 = 2**15; | ||
let offset1 = (2**15)-3; | ||
let offset2 = (2**15)+1; | ||
let flag_dst_base_fp = 0; | ||
let flag_op0_base_fp = 1; | ||
let flag_op1_imm = 1; | ||
let flag_op1_base_fp = 0; | ||
let flag_op1_base_ap = 0; | ||
let flag_res_add = 1-is_mul; | ||
let flag_res_mul = is_mul; | ||
let flag_PC_update_jump = 0; | ||
let flag_PC_update_jump_rel = 0; | ||
let flag_PC_update_jnz = 0; | ||
let flag_ap_update_add = 0; | ||
let flag_ap_update_add_1 = 0; | ||
let flag_opcode_call = 0; | ||
let flag_opcode_ret = 0; | ||
let flag_opcode_assert_eq = 1; | ||
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)+1*(2**5)+0*(2**6)+flag_opcode_assert_eq*(2**14); | ||
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)+0*(2**5)+1*(2**6)+flag_opcode_assert_eq*(2**14); | ||
let qm31_opcode_extension_num = 3; | ||
let qm31_add_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_add*(2**48) + qm31_opcode_extension_num*(2**63); | ||
let qm31_mul_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_mul*(2**48) + qm31_opcode_extension_num*(2**63); | ||
static_assert qm31_mul_instruction_num==32301646121292103680; | ||
static_assert qm31_add_instruction_num==32292638922037362688; | ||
if (is_mul == TRUE) { | ||
dw 32301646121292103680; | ||
dw 679720817185961464190715473544778505313945; | ||
return [ap]; | ||
} | ||
dw 32292638922037362688; | ||
dw 679720817185961464190715473544778505313945; | ||
return [ap]; | ||
} | ||
|
||
func run_qm31_operation_imm_op1_get_op0( | ||
is_mul: felt, | ||
dst: felt, | ||
) -> felt { | ||
let offset0 = (2**15)-3; | ||
let offset1 = 2**15; | ||
let offset2 = (2**15)+1; | ||
let flag_dst_base_fp = 1; | ||
let flag_op0_base_fp = 0; | ||
let flag_op1_imm = 1; | ||
let flag_op1_base_fp = 0; | ||
let flag_op1_base_ap = 0; | ||
let flag_res_add = 1-is_mul; | ||
let flag_res_mul = is_mul; | ||
let flag_PC_update_jump = 0; | ||
let flag_PC_update_jump_rel = 0; | ||
let flag_PC_update_jnz = 0; | ||
let flag_ap_update_add = 0; | ||
let flag_ap_update_add_1 = 0; | ||
let flag_opcode_call = 0; | ||
let flag_opcode_ret = 0; | ||
let flag_opcode_assert_eq = 1; | ||
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)+1*(2**5)+0*(2**6)+flag_opcode_assert_eq*(2**14); | ||
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)+0*(2**5)+1*(2**6)+flag_opcode_assert_eq*(2**14); | ||
let qm31_opcode_extension_num = 3; | ||
let qm31_add_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_add*(2**48) + qm31_opcode_extension_num*(2**63); | ||
let qm31_mul_instruction_num = offset0 + offset1*(2**16) + offset2*(2**32) + flag_num_qm31_mul*(2**48) + qm31_opcode_extension_num*(2**63); | ||
static_assert qm31_mul_instruction_num==32301364646315589629; | ||
static_assert qm31_add_instruction_num==32292357447060848637; | ||
if (is_mul == TRUE) { | ||
dw 32301364646315589629; | ||
dw 679720817185961464190715473544778505313945; | ||
return [ap]; | ||
} | ||
dw 32292357447060848637; | ||
dw 679720817185961464190715473544778505313945; | ||
return [ap]; | ||
} |
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
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
Oops, something went wrong.