Skip to content

Commit

Permalink
feat: add cpu_constraint_poly
Browse files Browse the repository at this point in the history
  • Loading branch information
hduoc2003 committed Sep 12, 2024
1 parent 5f9c254 commit a0e5751
Show file tree
Hide file tree
Showing 7 changed files with 4,042 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/aptos_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ jobs:
with:
version: 4.0.0
- name: test_contract
run: (cd libs && aptos move test --coverage --dev --skip-fetch-latest-git-deps --skip-attribute-checks) & (cd verifier && aptos move test --coverage --dev --skip-fetch-latest-git-deps --skip-attribute-checks) & (cd cpu && aptos move test --coverage --dev --skip-fetch-latest-git-deps --skip-attribute-checks)
run: (cd libs && aptos move test --coverage --dev --skip-fetch-latest-git-deps --skip-attribute-checks) & (cd verifier && aptos move test --coverage --dev --skip-fetch-latest-git-deps --skip-attribute-checks) & (cd cpu && aptos move test --coverage --dev --skip-fetch-latest-git-deps --skip-attribute-checks) & (cd cpu-constraint-poly && aptos move test --coverage --dev --skip-fetch-latest-git-deps --skip-attribute-checks)
19 changes: 19 additions & 0 deletions cpu-constraint-poly/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "cpu_constraint_poly"
version = "1.0.0"
authors = []

[addresses]
cpu_constraint_poly_addr = "_"

[dev-addresses]
cpu_constraint_poly_addr = "1725fb72ba62d830193fe57a932b5071c9718a44e40891e4067a2fe0e78f4d0f"
lib_addr = "54ba5979fbdf5fa56c4f4b5147fa33068148d08f6f191c23bca865c21192dcaa"

[dependencies.AptosFramework]
git = "https://github.com/aptos-labs/aptos-core.git"
rev = "aptos-release-v1.17"
subdir = "aptos-move/framework/aptos-framework"

[dependencies]
libs = { local = "../libs" }
3,950 changes: 3,950 additions & 0 deletions cpu-constraint-poly/sources/cpu_constraint_poly.move

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions verifier/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ authors = []
verifier_addr = "_"
lib_addr = "_"
cpu_addr = "_"
cpu_constraint_poly_addr = "_"

[dev-addresses]
verifier_addr = "9ebb301117915b4ebf94bb511441b7770cab7d98b83c41f78d8e2e489cdd45c1"
lib_addr = "54ba5979fbdf5fa56c4f4b5147fa33068148d08f6f191c23bca865c21192dcaa"
cpu_addr = "1725fb72ba62d830193fe57a932b5071c9718a44e40891e4067a2fe0e78f4d0f"
cpu_constraint_poly_addr = "1725fb72ba62d830193fe57a932b5071c9718a44e40891e4067a2fe0e78f4d0e"

[dependencies.AptosFramework]
git = "https://github.com/aptos-labs/aptos-core.git"
Expand All @@ -21,5 +23,6 @@ subdir = "aptos-move/framework/aptos-framework"
[dependencies]
libs = { local = "../libs" }
cpu = { local = "../cpu" }
cpu_constraint_poly = { local = "../cpu-constraint-poly" }

[dev-dependencies]
100 changes: 58 additions & 42 deletions verifier/sources/cpu/layout7/stark_verifier.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module verifier_addr::stark_verifier_7 {
use std::signer::address_of;
use std::vector::{append, borrow, length, slice, borrow_mut};
use aptos_std::aptos_hash::keccak256;
use cpu_constraint_poly_addr::cpu_constraint_poly;

use cpu_addr::cpu_oods_7;
use cpu_addr::layout_specific_7::{layout_specific_init, prepare_for_oods_check, safe_div};
Expand All @@ -23,10 +24,14 @@ module verifier_addr::stark_verifier_7 {
// This line is used for generating constants DO NOT REMOVE!
// 1
const CHECKPOINT1_CFFL: u8 = 0x1;
// 100
const CHECKPOINT1_OCC: u8 = 0x64;
// 6
const CHECKPOINT1_VP: u8 = 0x6;
// 2
const CHECKPOINT2_CFFL: u8 = 0x2;
// 101
const CHECKPOINT2_OCC: u8 = 0x65;
// 7
const CHECKPOINT2_VP: u8 = 0x7;
// 3
Expand All @@ -41,6 +46,8 @@ module verifier_addr::stark_verifier_7 {
const CONSTRAINTS_DEGREE_BOUND: u64 = 0x2;
// 1
const CONTINUOUS_PAGE: u256 = 0x1;
// 30
const ECLAIMED_COMPOSITION_DOES_NOT_MATCH_TRACE: u64 = 0x1e;
// 14
const EFRI_PARAMS_DO_NOT_MATCH_TRACE_LENGTH: u64 = 0xe;
// 32
Expand Down Expand Up @@ -137,6 +144,8 @@ module verifier_addr::stark_verifier_7 {
const MM_COMPOSITION_OODS_VALUES: u64 = 0x227;
// 1178
const MM_COMPOSITION_QUERY_RESPONSES: u64 = 0x49a;
// 551
const MM_CONSTRAINT_POLY_ARGS_END: u64 = 0x227;
// 317
const MM_CONSTRAINT_POLY_ARGS_START: u64 = 0x13d;
// 1277
Expand Down Expand Up @@ -296,6 +305,9 @@ module verifier_addr::stark_verifier_7 {
move_to(signer, CfflCheckpoint {
inner: CHECKPOINT1_CFFL
});
move_to(signer, OccCheckpoint {
inner: CHECKPOINT1_OCC
});
cpu_oods_7::init_data_type(signer);
}

Expand Down Expand Up @@ -548,7 +560,7 @@ module verifier_addr::stark_verifier_7 {
proof_params: &vector<u256>,
proof: &mut vector<u256>,
public_input: &vector<u256>
): bool acquires ConstructorConfig, VpCheckpoint, CtxCache, CfflCheckpoint {
): bool acquires ConstructorConfig, VpCheckpoint, CtxCache, CfflCheckpoint, OccCheckpoint {
let signer_addr = address_of(signer);
let VpCheckpoint {
inner: checkpoint
Expand Down Expand Up @@ -652,7 +664,6 @@ module verifier_addr::stark_verifier_7 {

*checkpoint = CHECKPOINT4_VP;
};
return false
};
// emit LogGas("Send queries", gasleft());

Expand Down Expand Up @@ -972,48 +983,43 @@ module verifier_addr::stark_verifier_7 {
signer: &signer,
ctx: &mut vector<u256>,
public_input: &vector<u256>
): bool {
verify_memory_page_facts(signer, ctx, public_input);
let temp = *borrow(ctx, MM_INTERACTION_ELEMENTS);
set_el(ctx, MM_MEMORY__MULTI_COLUMN_PERM__PERM__INTERACTION_ELM, temp);
let temp = *borrow(ctx, MM_INTERACTION_ELEMENTS + 1);
set_el(ctx, MM_MEMORY__MULTI_COLUMN_PERM__HASH_INTERACTION_ELM0, temp);
let temp = *borrow(ctx, MM_INTERACTION_ELEMENTS + 2);
set_el(ctx, MM_RANGE_CHECK16__PERM__INTERACTION_ELM, temp);

let public_memory_prod = compute_public_memory_quotient(ctx, public_input);
set_el(ctx, MM_MEMORY__MULTI_COLUMN_PERM__PERM__PUBLIC_MEMORY_PROD, public_memory_prod);
prepare_for_oods_check(ctx);

// Todo
// let composition_from_trace_value;
// address
// lconstraintPoly = address(constraintPoly);
// let offset = 1 + MM_CONSTRAINT_POLY_ARGS_START;
// let size = MM_CONSTRAINT_POLY_ARGS_END - MM_CONSTRAINT_POLY_ARGS_START;
// assembly {
// // Call CpuConstraintPoly contract.
// let p = mload(0x40)
// if iszero(staticcall(not(0), lconstraintPoly, add(ctx, offset), size, p, 0x20)) {
// returndatacopy(0, 0, returndatasize())
// revert(0, returndatasize())
// }
// compositionFromTraceValue = mload(p)
// }

// let claimed_composition = fadd(
// *borrow(ctx, MM_COMPOSITION_OODS_VALUES),
// fmul(*borrow(ctx, MM_OODS_POINT), *borrow(ctx, MM_COMPOSITION_OODS_VALUES + 1))
// );

// assert!(
// composition_from_trace_value == claimed_composition,
// CLAIMED_COMPOSITION_DOES_NOT_MATCH_TRACE
// );
): bool acquires OccCheckpoint {
let OccCheckpoint {
inner: checkpoint
} = borrow_global_mut<OccCheckpoint>(address_of(signer));

if (*checkpoint == CHECKPOINT1_OCC) {
verify_memory_page_facts(signer, ctx, public_input);
let temp = *borrow(ctx, MM_INTERACTION_ELEMENTS);
set_el(ctx, MM_MEMORY__MULTI_COLUMN_PERM__PERM__INTERACTION_ELM, temp);
let temp = *borrow(ctx, MM_INTERACTION_ELEMENTS + 1);
set_el(ctx, MM_MEMORY__MULTI_COLUMN_PERM__HASH_INTERACTION_ELM0, temp);
let temp = *borrow(ctx, MM_INTERACTION_ELEMENTS + 2);
set_el(ctx, MM_RANGE_CHECK16__PERM__INTERACTION_ELM, temp);

let public_memory_prod = compute_public_memory_quotient(ctx, public_input);
set_el(ctx, MM_MEMORY__MULTI_COLUMN_PERM__PERM__PUBLIC_MEMORY_PROD, public_memory_prod);
prepare_for_oods_check(ctx);
*checkpoint = CHECKPOINT2_OCC;
return false
};

let composition_from_trace_value = cpu_constraint_poly::fallback(
slice(ctx, MM_CONSTRAINT_POLY_ARGS_START, MM_CONSTRAINT_POLY_ARGS_END)
);
let claimed_composition = fadd(
*borrow(ctx, MM_COMPOSITION_OODS_VALUES),
fmul(*borrow(ctx, MM_OODS_POINT), *borrow(ctx, MM_COMPOSITION_OODS_VALUES + 1))
);
assert!(
composition_from_trace_value == claimed_composition,
ECLAIMED_COMPOSITION_DOES_NOT_MATCH_TRACE
);
*checkpoint = CHECKPOINT1_OCC;
true
}

fun has_interaction(): bool {
inline fun has_interaction(): bool {
N_COLUMNS_IN_TRACE1 > 0
}

Expand Down Expand Up @@ -1058,6 +1064,11 @@ module verifier_addr::stark_verifier_7 {
borrow_global<CfflCheckpoint>(address_of(signer)).inner
}

#[test_only]
public fun get_occ_checkpoint(signer: &signer): u8 acquires OccCheckpoint {
borrow_global<OccCheckpoint>(address_of(signer)).inner
}

#[test_only]
public fun test_init_stark_verifier(signer: &signer, num_security_bits: u256, min_proof_of_work_bits: u256) {
move_to(signer, ConstructorConfig {
Expand All @@ -1076,7 +1087,12 @@ module verifier_addr::stark_verifier_7 {
}

// Data of the function `compute_first_fri_layer`
struct CfflCheckpoint has key {
struct CfflCheckpoint has key, drop {
inner: u8
}

// Data of the function `oods_consistency_check`
struct OccCheckpoint has key, drop {
inner: u8
}
}
1 change: 0 additions & 1 deletion verifier/sources/cpu/memory_page_fact_registry.move
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ module verifier_addr::memory_page_fact_registry {
(fact_hash, memory_hash, prod)
}

// TODO: mark as entry func
// Registers a fact based on the given values, assuming continuous addresses.
// values should be [value at startAddr, value at (startAddr + 1), ...].
public entry fun register_continuous_memorypage(
Expand Down
15 changes: 11 additions & 4 deletions verifier/sources/test/test_gps_statement_verifier.move
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ module verifier_addr::test_gps_statement_verifier {
registered_facts_,
task_meta_data_
};
use verifier_addr::stark_verifier_7::{get_cffl_checkpoint, get_vp_checkpoint};
use verifier_addr::stark_verifier_7::{get_cffl_checkpoint, get_vp_checkpoint, get_occ_checkpoint};

// This line is used for generating constants DO NOT REMOVE!
// 1
const CHECKPOINT1_CFFL: u8 = 0x1;
// 4
const CHECKPOINT1_FB: u8 = 0x4;
// 100
const CHECKPOINT1_OCC: u8 = 0x64;
// 6
const CHECKPOINT1_VP: u8 = 0x6;
// 10
Expand All @@ -34,6 +36,8 @@ module verifier_addr::test_gps_statement_verifier {
const CHECKPOINT2_CFFL: u8 = 0x2;
// 5
const CHECKPOINT2_FB: u8 = 0x5;
// 101
const CHECKPOINT2_OCC: u8 = 0x65;
// 7
const CHECKPOINT2_VP: u8 = 0x7;
// 11
Expand Down Expand Up @@ -88,15 +92,18 @@ module verifier_addr::test_gps_statement_verifier {
assert!(get_vpar_checkpoint(signer) == CHECKPOINT2_VPAR, 1);
assert!(get_vp_checkpoint(signer) == CHECKPOINT2_VP, 1);
verify_proof_and_register(signer);
// verify_proof_external::CHECKPOINT3_VP
// verify_proof_external::CHECKPOINT3_VP::oods_consistency_check::CHECKPOINT1_OCC
assert!(get_vpar_checkpoint(signer) == CHECKPOINT2_VPAR, 1);
assert!(get_vp_checkpoint(signer) == CHECKPOINT3_VP, 1);
assert!(get_occ_checkpoint(signer) == CHECKPOINT1_OCC, 1);
verify_proof_and_register(signer);
// verify_proof_external::CHECKPOINT4_VP::compute_first_fri_layer::CHECKPOINT1_CFFL
// verify_proof_external::CHECKPOINT3_VP::oods_consistency_check::CHECKPOINT2_OCC + CHECKPOINT4_VP::compute_first_fri_layer::CHECKPOINT1_CFFL
assert!(get_vpar_checkpoint(signer) == CHECKPOINT2_VPAR, 1);
assert!(get_vp_checkpoint(signer) == CHECKPOINT4_VP, 1);
assert!(get_vp_checkpoint(signer) == CHECKPOINT3_VP, 1);
assert!(get_cffl_checkpoint(signer) == CHECKPOINT1_CFFL, 1);
assert!(get_occ_checkpoint(signer) == CHECKPOINT2_OCC, 1);
verify_proof_and_register(signer);
assert!(get_occ_checkpoint(signer) == CHECKPOINT1_OCC, 1);
// verify_proof_external::CHECKPOINT4_VP::compute_first_fri_layer::CHECKPOINT2_CFFL + cpu_oods_7::fallback::CHECKPOINT1_FB
assert!(get_vpar_checkpoint(signer) == CHECKPOINT2_VPAR, 1);
assert!(get_vp_checkpoint(signer) == CHECKPOINT4_VP, 1);
Expand Down

0 comments on commit a0e5751

Please sign in to comment.