Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/pierre #185

Merged
merged 16 commits into from
Jan 11, 2024
Merged
48 changes: 46 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions assembler/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,16 @@ fn split_ola_asm_pieces(
| OlaOpcode::TSTORE
| OlaOpcode::SCCALL
| OlaOpcode::SLOAD
| OlaOpcode::SSTORE => {
| OlaOpcode::SSTORE
| OlaOpcode::SIGCHECK => {
if ops.len() != 2 {
return Err(format!("invalid operand size: {}", asm_line));
}
if opcode == OlaOpcode::MOV || opcode == OlaOpcode::NOT || opcode == OlaOpcode::MLOAD {
if opcode == OlaOpcode::MOV
|| opcode == OlaOpcode::NOT
|| opcode == OlaOpcode::MLOAD
|| opcode == OlaOpcode::SIGCHECK
{
let dst = ops.get(0).unwrap();
let op1 = ops.get(1).unwrap();
Ok((opcode, None, Some(op1.clone()), Some(dst.clone())))
Expand Down
10 changes: 10 additions & 0 deletions assembler/src/test_data_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ mod tests {
);
}

#[test]
fn generate_hash() {
generate_from_file("hash_asm.json".to_string(), "hash.json".to_string());
}

#[test]
fn generate_ecdsa() {
generate_from_file("ecdsa_asm.json".to_string(), "ecdsa.json".to_string());
}

fn generate_from_file(input_file_name: String, output_file_name: String) {
let _ = fs::create_dir_all("test_data/bin/sccall");
let input_path = format!("test_data/asm/{}", input_file_name);
Expand Down
97 changes: 97 additions & 0 deletions assembler/test_data/asm/ecdsa_asm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"program": "heap_malloc:\n.LBL17_0:\n mov r3 18446744060824649731\n mload r0 [r3]\n add r2 r0 r1\n mov r1 18446744060824649731\n mstore [r1] r2\n ret\nvector_new:\n.LBL18_0:\n mov r4 18446744060824649731\n mload r0 [r4]\n add r2 r1 1\n add r3 r0 r2\n mov r2 18446744060824649731\n mstore [r2] r3\n mstore [r0] r1\n ret\nsplit_field:\n.LBL19_0:\n mov r6 r1\n mov r1 r6\n.PROPHET19_0:\n mov r0 psp\n mload r0 [r0]\n mov r7 r0\n range r7\n mov r1 r6\n.PROPHET19_1:\n mov r0 psp\n mload r0 [r0]\n mov r1 r0\n range r1\n mul r4 r7 4294967296\n add r5 r4 r1\n eq r4 r6 r5\n assert r4\n mstore [r2] r7\n mstore [r3] r1\n ret\nmemcpy:\n.LBL20_0:\n add r9 r9 1\n mov r4 0\n mstore [r9,-1] r4\n jmp .LBL20_1\n.LBL20_1:\n mload r4 [r9,-1]\n gte r5 r3 r4\n neq r6 r4 r3\n and r5 r5 r6\n cjmp r5 .LBL20_2\n jmp .LBL20_3\n.LBL20_2:\n mload r6 [r1,r4]\n mstore [r2,r4] r6\n add r5 r4 1\n mstore [r9,-1] r5\n jmp .LBL20_1\n.LBL20_3:\n add r9 r9 -1\n ret\nmemcmp_eq:\n.LBL21_0:\n add r9 r9 1\n mov r4 0\n mstore [r9,-1] r4\n jmp .LBL21_1\n.LBL21_1:\n mload r4 [r9,-1]\n gte r5 r3 r4\n neq r6 r4 r3\n and r5 r5 r6\n cjmp r5 .LBL21_2\n mov r0 1\n jmp .LBL21_3\n.LBL21_2:\n mload r6 [r1,r4]\n mload r7 [r2,r4]\n add r5 r4 1\n mstore [r9,-1] r5\n eq r4 r6 r7\n cjmp r4 .LBL21_1\n mov r0 0\n jmp .LBL21_3\n.LBL21_3:\n add r9 r9 -1\n ret\nmemcmp_ne:\n.LBL22_0:\n add r9 r9 1\n mov r4 0\n mstore [r9,-1] r4\n jmp .LBL22_1\n.LBL22_1:\n mload r4 [r9,-1]\n gte r5 r3 r4\n neq r6 r4 r3\n and r5 r5 r6\n cjmp r5 .LBL22_2\n mov r0 0\n jmp .LBL22_3\n.LBL22_2:\n mload r6 [r1,r4]\n mload r7 [r2,r4]\n add r5 r4 1\n mstore [r9,-1] r5\n eq r4 r6 r7\n cjmp r4 .LBL22_1\n mov r0 1\n jmp .LBL22_3\n.LBL22_3:\n add r9 r9 -1\n ret\nmemcmp_ugt:\n.LBL23_0:\n add r9 r9 1\n mov r4 0\n mstore [r9,-1] r4\n jmp .LBL23_1\n.LBL23_1:\n mload r4 [r9,-1]\n gte r5 r3 r4\n neq r6 r4 r3\n and r5 r5 r6\n cjmp r5 .LBL23_2\n mov r0 0\n jmp .LBL23_3\n.LBL23_2:\n mload r6 [r1,r4]\n mload r7 [r2,r4]\n add r5 r4 1\n mstore [r9,-1] r5\n gte r4 r7 r6\n cjmp r4 .LBL23_1\n mov r0 1\n jmp .LBL23_3\n.LBL23_3:\n add r9 r9 -1\n ret\nmemcmp_uge:\n.LBL24_0:\n add r9 r9 1\n mov r4 0\n mstore [r9,-1] r4\n jmp .LBL24_1\n.LBL24_1:\n mload r4 [r9,-1]\n gte r5 r3 r4\n neq r6 r4 r3\n and r5 r5 r6\n cjmp r5 .LBL24_2\n mov r0 1\n jmp .LBL24_3\n.LBL24_2:\n mload r6 [r1,r4]\n mload r7 [r2,r4]\n add r5 r4 1\n mstore [r9,-1] r5\n gte r4 r6 r7\n cjmp r4 .LBL24_1\n mov r0 0\n jmp .LBL24_3\n.LBL24_3:\n add r9 r9 -1\n ret\nmemcmp_ult:\n.LBL25_0:\n add r9 r9 1\n mov r4 0\n mstore [r9,-1] r4\n jmp .LBL25_1\n.LBL25_1:\n mload r4 [r9,-1]\n gte r5 r3 r4\n neq r6 r4 r3\n and r5 r5 r6\n cjmp r5 .LBL25_2\n mov r0 0\n jmp .LBL25_3\n.LBL25_2:\n mload r6 [r1,r4]\n mload r7 [r2,r4]\n add r5 r4 1\n mstore [r9,-1] r5\n gte r4 r6 r7\n cjmp r4 .LBL25_1\n mov r0 1\n jmp .LBL25_3\n.LBL25_3:\n add r9 r9 -1\n ret\nmemcmp_ule:\n.LBL26_0:\n add r9 r9 1\n mov r4 0\n mstore [r9,-1] r4\n jmp .LBL26_1\n.LBL26_1:\n mload r4 [r9,-1]\n gte r5 r3 r4\n neq r6 r4 r3\n and r5 r5 r6\n cjmp r5 .LBL26_2\n mov r0 1\n jmp .LBL26_3\n.LBL26_2:\n mload r6 [r1,r4]\n mload r7 [r2,r4]\n add r5 r4 1\n mstore [r9,-1] r5\n gte r4 r7 r6\n cjmp r4 .LBL26_1\n mov r0 0\n jmp .LBL26_3\n.LBL26_3:\n add r9 r9 -1\n ret\nfield_memcmp_ugt:\n.LBL27_0:\n add r9 r9 20\n mstore [r9,-2] r9\n mstore [r9,-8] r1\n mov r1 r2\n mstore [r9,-13] r1\n mov r1 r3\n mstore [r9,-10] r1\n mov r1 0\n mstore [r9,-7] r1\n jmp .LBL27_1\n.LBL27_1:\n mload r1 [r9,-7]\n mstore [r9,-12] r1\n mload r1 [r9,-10]\n mload r2 [r9,-12]\n gte r1 r1 r2\n mload r2 [r9,-10]\n mload r3 [r9,-12]\n neq r2 r3 r2\n and r1 r1 r2\n cjmp r1 .LBL27_2\n mov r0 0\n jmp .LBL27_4\n.LBL27_2:\n mload r1 [r9,-8]\n mload r2 [r9,-12]\n mload r1 [r1,r2]\n mload r2 [r9,-12]\n mload r3 [r9,-13]\n mload r2 [r3,r2]\n mstore [r9,-9] r2\n add r3 r9 -5\n add r2 r9 -6\n call split_field\n mload r1 [r9,-6]\n mstore [r9,-20] r1\n mload r1 [r9,-5]\n mstore [r9,-17] r1\n add r3 r9 -3\n add r2 r9 -4\n mload r1 [r9,-9]\n call split_field\n mload r1 [r9,-4]\n mload r2 [r9,-3]\n mload r3 [r9,-12]\n add r3 r3 1\n mstore [r9,-11] r3\n mload r3 [r9,-11]\n mstore [r9,-7] r3\n mload r3 [r9,-20]\n gte r1 r1 r3\n cjmp r1 .LBL27_3\n mov r0 1\n jmp .LBL27_4\n.LBL27_3:\n mload r1 [r9,-17]\n gte r1 r2 r1\n cjmp r1 .LBL27_1\n mov r0 1\n jmp .LBL27_4\n.LBL27_4:\n add r9 r9 -20\n ret\nfield_memcmp_uge:\n.LBL28_0:\n add r9 r9 20\n mstore [r9,-2] r9\n mstore [r9,-8] r1\n mov r1 r2\n mstore [r9,-13] r1\n mov r1 r3\n mstore [r9,-10] r1\n mov r1 0\n mstore [r9,-7] r1\n jmp .LBL28_1\n.LBL28_1:\n mload r1 [r9,-7]\n mstore [r9,-12] r1\n mload r1 [r9,-10]\n mload r2 [r9,-12]\n gte r1 r1 r2\n mload r2 [r9,-10]\n mload r3 [r9,-12]\n neq r2 r3 r2\n and r1 r1 r2\n cjmp r1 .LBL28_2\n mov r0 1\n jmp .LBL28_4\n.LBL28_2:\n mload r1 [r9,-8]\n mload r2 [r9,-12]\n mload r1 [r1,r2]\n mload r2 [r9,-12]\n mload r3 [r9,-13]\n mload r2 [r3,r2]\n mstore [r9,-9] r2\n add r3 r9 -5\n add r2 r9 -6\n call split_field\n mload r1 [r9,-6]\n mstore [r9,-20] r1\n mload r1 [r9,-5]\n mstore [r9,-17] r1\n add r3 r9 -3\n add r2 r9 -4\n mload r1 [r9,-9]\n call split_field\n mload r1 [r9,-4]\n mload r2 [r9,-3]\n mload r3 [r9,-12]\n add r3 r3 1\n mstore [r9,-11] r3\n mload r3 [r9,-11]\n mstore [r9,-7] r3\n mload r3 [r9,-20]\n gte r1 r3 r1\n cjmp r1 .LBL28_3\n mov r0 0\n jmp .LBL28_4\n.LBL28_3:\n mload r1 [r9,-17]\n gte r1 r1 r2\n cjmp r1 .LBL28_1\n mov r0 0\n jmp .LBL28_4\n.LBL28_4:\n add r9 r9 -20\n ret\nfield_memcmp_ule:\n.LBL29_0:\n add r9 r9 20\n mstore [r9,-2] r9\n mstore [r9,-8] r1\n mov r1 r2\n mstore [r9,-13] r1\n mov r1 r3\n mstore [r9,-10] r1\n mov r1 0\n mstore [r9,-7] r1\n jmp .LBL29_1\n.LBL29_1:\n mload r1 [r9,-7]\n mstore [r9,-12] r1\n mload r1 [r9,-10]\n mload r2 [r9,-12]\n gte r1 r1 r2\n mload r2 [r9,-10]\n mload r3 [r9,-12]\n neq r2 r3 r2\n and r1 r1 r2\n cjmp r1 .LBL29_2\n mov r0 1\n jmp .LBL29_4\n.LBL29_2:\n mload r1 [r9,-8]\n mload r2 [r9,-12]\n mload r1 [r1,r2]\n mload r2 [r9,-12]\n mload r3 [r9,-13]\n mload r2 [r3,r2]\n mstore [r9,-9] r2\n add r3 r9 -5\n add r2 r9 -6\n call split_field\n mload r1 [r9,-6]\n mstore [r9,-20] r1\n mload r1 [r9,-5]\n mstore [r9,-17] r1\n add r3 r9 -3\n add r2 r9 -4\n mload r1 [r9,-9]\n call split_field\n mload r1 [r9,-4]\n mload r2 [r9,-3]\n mload r3 [r9,-12]\n add r3 r3 1\n mstore [r9,-11] r3\n mload r3 [r9,-11]\n mstore [r9,-7] r3\n mload r3 [r9,-20]\n gte r1 r1 r3\n cjmp r1 .LBL29_3\n mov r0 0\n jmp .LBL29_4\n.LBL29_3:\n mload r1 [r9,-17]\n gte r1 r2 r1\n cjmp r1 .LBL29_1\n mov r0 0\n jmp .LBL29_4\n.LBL29_4:\n add r9 r9 -20\n ret\nfield_memcmp_ult:\n.LBL30_0:\n add r9 r9 20\n mstore [r9,-2] r9\n mstore [r9,-8] r1\n mov r1 r2\n mstore [r9,-13] r1\n mov r1 r3\n mstore [r9,-10] r1\n mov r1 0\n mstore [r9,-7] r1\n jmp .LBL30_1\n.LBL30_1:\n mload r1 [r9,-7]\n mstore [r9,-12] r1\n mload r1 [r9,-10]\n mload r2 [r9,-12]\n gte r1 r1 r2\n mload r2 [r9,-10]\n mload r3 [r9,-12]\n neq r2 r3 r2\n and r1 r1 r2\n cjmp r1 .LBL30_2\n mov r0 0\n jmp .LBL30_4\n.LBL30_2:\n mload r1 [r9,-8]\n mload r2 [r9,-12]\n mload r1 [r1,r2]\n mload r2 [r9,-12]\n mload r3 [r9,-13]\n mload r2 [r3,r2]\n mstore [r9,-9] r2\n add r3 r9 -5\n add r2 r9 -6\n call split_field\n mload r1 [r9,-6]\n mstore [r9,-20] r1\n mload r1 [r9,-5]\n mstore [r9,-17] r1\n add r3 r9 -3\n add r2 r9 -4\n mload r1 [r9,-9]\n call split_field\n mload r1 [r9,-4]\n mload r2 [r9,-3]\n mload r3 [r9,-12]\n add r3 r3 1\n mstore [r9,-11] r3\n mload r3 [r9,-11]\n mstore [r9,-7] r3\n mload r3 [r9,-20]\n gte r1 r3 r1\n cjmp r1 .LBL30_3\n mov r0 1\n jmp .LBL30_4\n.LBL30_3:\n mload r1 [r9,-17]\n gte r1 r1 r2\n cjmp r1 .LBL30_1\n mov r0 1\n jmp .LBL30_4\n.LBL30_4:\n add r9 r9 -20\n ret\nu32_div_mod:\n.LBL31_0:\n add r9 r9 5\n mstore [r9,-3] r1\n mov r1 r2\n mstore [r9,-4] r1\n mload r1 [r9,-4]\n mov r2 r1\n mload r1 [r9,-3]\n.PROPHET31_0:\n mov r0 psp\n mload r0 [r0]\n mov r1 r0\n mstore [r9,-5] r1\n mload r1 [r9,-5]\n range r1\n mload r1 [r9,-5]\n add r5 r1 1\n not r7 r5\n add r7 r7 1\n mload r1 [r9,-4]\n add r6 r1 r7\n range r6\n mload r1 [r9,-4]\n mov r2 r1\n mload r1 [r9,-3]\n.PROPHET31_1:\n mov r0 psp\n mload r0 [r0]\n mov r1 r0\n range r3\n mload r2 [r9,-4]\n mul r2 r1 r2\n mstore [r9,-1] r2\n mload r2 [r9,-1]\n mload r5 [r9,-5]\n add r2 r2 r5\n mstore [r9,-2] r2\n mload r2 [r9,-2]\n mload r5 [r9,-3]\n eq r2 r2 r5\n assert r2\n mstore [r3] r1\n mload r1 [r9,-5]\n mstore [r4] r1\n add r9 r9 -5\n ret\nu32_power:\n.LBL32_0:\n mov r0 1\n mov r3 0\n jmp .LBL32_1\n.LBL32_1:\n add r5 r3 1\n mul r4 r0 r1\n gte r3 r2 r5\n cjmp r3 .LBL32_1\n mov r0 r4\n mov r3 r5\n jmp .LBL32_2\n.LBL32_2:\n range r0\n ret\ncheck_ecdsa:\n.LBL33_0:\n add r9 r9 12\n mstore [r9,-2] r9\n mov r5 r1\n mstore [r9,-3] r5\n mov r5 r2\n mstore [r9,-4] r5\n mov r5 r3\n mstore [r9,-5] r5\n mov r1 20\n call heap_malloc\n mov r5 r0\n mstore [r9,-8] r5\n mov r3 4\n mload r2 [r9,-8]\n mload r1 [r9,-3]\n call memcpy\n mov r3 8\n mload r5 [r9,-8]\n add r5 r5 4\n mstore [r9,-9] r5\n mload r2 [r9,-9]\n mload r5 [r9,-4]\n add r1 r5 1\n call memcpy\n mov r3 8\n mload r5 [r9,-9]\n add r2 r5 8\n mload r5 [r9,-5]\n add r1 r5 1\n call memcpy\n mload r5 [r9,-8]\n sigcheck r0 r5\n add r9 r9 -12\n ret\ncheck_ecdsa_test:\n.LBL34_0:\n add r9 r9 5\n mstore [r9,-2] r9\n mov r1 4\n call heap_malloc\n mov r5 r0\n mov r6 8100099710329060113\n mstore [r5] r6\n mov r6 1691964059337354474\n mstore [r5,+1] r6\n mov r6 2619454573477891356\n mstore [r5,+2] r6\n mov r6 14789418598207764286\n mstore [r5,+3] r6\n mstore [r9,-4] r5\n mov r1 8\n call vector_new\n mov r5 r0\n mstore [r9,-5] r5\n mload r5 [r9,-5]\n add r5 r5 1\n mov r6 16082862629786954522\n mstore [r5] r6\n mov r6 18230050229933857031\n mstore [r5,+1] r6\n mov r6 5147452894439374020\n mstore [r5,+2] r6\n mov r6 7123102033214096931\n mstore [r5,+3] r6\n mov r6 18146431795413334852\n mstore [r5,+4] r6\n mov r6 14267346997562947978\n mstore [r5,+5] r6\n mov r6 16850228878206657517\n mstore [r5,+6] r6\n mov r6 11850760313150838435\n mstore [r5,+7] r6\n mov r1 8\n call vector_new\n mov r3 r0\n add r5 r3 1\n mov r6 3136689052219066478\n mstore [r5] r6\n mov r6 13835160098228565381\n mstore [r5,+1] r6\n mov r6 16653245190936664457\n mstore [r5,+2] r6\n mov r6 8159748729834307751\n mstore [r5,+3] r6\n mov r6 9003827909761961299\n mstore [r5,+4] r6\n mov r6 15903809184042520377\n mstore [r5,+5] r6\n mov r6 8474226046933642703\n mstore [r5,+6] r6\n mov r6 14159585317199163842\n mstore [r5,+7] r6\n mload r1 [r9,-4]\n mload r2 [r9,-5]\n call check_ecdsa\n mov r5 r0\n mstore [r9,-3] r5\n mload r5 [r9,-3]\n assert r5\n add r9 r9 -5\n ret\nfunction_dispatch:\n.LBL35_0:\n add r9 r9 2\n mstore [r9,-2] r9\n mov r2 r3\n eq r8 r1 370402988\n cjmp r8 .LBL35_2\n jmp .LBL35_1\n.LBL35_1:\n ret\n.LBL35_2:\n call check_ecdsa_test\n mov r1 1\n call heap_malloc\n mov r1 r0\n mov r2 0\n mstore [r1] r2\n tstore r1 1\n add r9 r9 -2\n ret\nmain:\n.LBL36_0:\n add r9 r9 8\n mstore [r9,-2] r9\n mov r1 13\n call heap_malloc\n mov r5 r0\n mov r6 1\n tload r5 r6 13\n mload r5 [r5]\n mstore [r9,-4] r5\n mov r1 14\n call heap_malloc\n mov r5 r0\n mov r6 1\n tload r5 r6 14\n mload r5 [r5]\n mstore [r9,-6] r5\n mload r5 [r9,-6]\n add r5 r5 14\n mstore [r9,-3] r5\n mload r1 [r9,-3]\n call heap_malloc\n mov r3 r0\n mov r5 1\n mload r6 [r9,-3]\n tload r3 r5 r6\n mload r2 [r9,-6]\n mload r1 [r9,-4]\n call function_dispatch\n add r9 r9 -8\n end\n",
"prophets": [
{
"label": ".PROPHET19_0",
"code": "%{\n function split_hi(felt in) -> felt {\n return in / 4294967296;\n }\n entry() {\n cid.out = split_hi(cid.in);\n }\n%}",
"inputs": [
{
"name": "cid.in",
"length": 1,
"is_ref": false,
"is_input_output": false
}
],
"outputs": [
{
"name": "cid.out",
"length": 1,
"is_ref": false,
"is_input_output": false
}
]
},
{
"label": ".PROPHET19_1",
"code": "%{\n function split_lo(felt in) -> felt {\n return in % 4294967296;\n }\n entry() {\n cid.out = split_lo(cid.in);\n }\n%}",
"inputs": [
{
"name": "cid.in",
"length": 1,
"is_ref": false,
"is_input_output": false
}
],
"outputs": [
{
"name": "cid.out",
"length": 1,
"is_ref": false,
"is_input_output": false
}
]
},
{
"label": ".PROPHET31_0",
"code": "%{\n function mod(felt x, felt y) -> felt {\n return x % y;\n }\n entry() {\n cid.r = mod(cid.x, cid.y);\n }\n%}",
"inputs": [
{
"name": "cid.x",
"length": 1,
"is_ref": false,
"is_input_output": false
},
{
"name": "cid.y",
"length": 1,
"is_ref": false,
"is_input_output": false
}
],
"outputs": [
{
"name": "cid.r",
"length": 1,
"is_ref": false,
"is_input_output": false
}
]
},
{
"label": ".PROPHET31_1",
"code": "%{\n function div(felt x, felt y) -> felt {\n return x / y;\n }\n entry() {\n cid.q = div(cid.x, cid.y);\n }\n%}",
"inputs": [
{
"name": "cid.x",
"length": 1,
"is_ref": false,
"is_input_output": false
},
{
"name": "cid.y",
"length": 1,
"is_ref": false,
"is_input_output": false
}
],
"outputs": [
{
"name": "cid.q",
"length": 1,
"is_ref": false,
"is_input_output": false
}
]
}
]
}
Loading
Loading