Skip to content

Commit

Permalink
feat: add function verify_proof_and_register
Browse files Browse the repository at this point in the history
  • Loading branch information
hduoc2003 committed Aug 29, 2024
1 parent 4059291 commit 4d27178
Show file tree
Hide file tree
Showing 12 changed files with 4,125 additions and 21 deletions.
2 changes: 1 addition & 1 deletion contracts/navori
Submodule navori updated 44 files
+1 −1 .github/workflows/aptos_test.yaml
+3 −0 README.md
+19 −0 cpu/Move.toml
+19 −0 cpu/sources/cairo_bootloader_program.move
+893 −0 cpu/sources/layout7/cpu_oods.move
+336 −0 cpu/sources/layout7/layout_specific.move
+32 −0 cpu/sources/layout7/memory_access_utils.move
+50 −0 cpu/sources/layout7/poseidon_poseidon_full_round_key_0_column.move
+37 −0 cpu/sources/layout7/poseidon_poseidon_full_round_key_1_column.move
+37 −0 cpu/sources/layout7/poseidon_poseidon_full_round_key_2_column.move
+172 −0 cpu/sources/layout7/poseidon_poseidon_partial_round_key_0_column.move
+94 −0 cpu/sources/layout7/poseidon_poseidon_partial_round_key_1_column.move
+40 −0 cpu/sources/layout7/public_memory_offsets.move
+1,289 −0 cpu/sources/periodic_columns/pedersen_hash_points_x_column.move
+1,289 −0 cpu/sources/periodic_columns/pedersen_hash_points_y_column.move
+2,566 −0 cpu/sources/tests/cpu_oods_7_test_data.move
+47 −14 libs/sources/bytes.move
+17 −13 libs/sources/prime_field_element_0.move
+25 −0 libs/sources/vector.move
+5 −2 verifier/Move.toml
+748 −0 verifier/scripts/script.move
+10 −4 verifier/sources/components/fact_registry.move
+27 −0 verifier/sources/cpu/cairo_verifier_contract.move
+14 −0 verifier/sources/cpu/layout7/cpu_verifier.move
+143 −0 verifier/sources/cpu/layout7/fri.move
+238 −0 verifier/sources/cpu/layout7/fri_statement_verifier.move
+1,173 −0 verifier/sources/cpu/layout7/stark_verifier.move
+162 −198 verifier/sources/cpu/memory_page_fact_registry.move
+3 −3 verifier/sources/fri_layer.move
+4 −4 verifier/sources/fri_statement_contract.move
+330 −0 verifier/sources/gps/gps_output_parser.move
+746 −0 verifier/sources/gps/gps_statement_verifier.move
+62 −0 verifier/sources/horner_evaluator.move
+2 −2 verifier/sources/merkle_statement_contract.move
+35 −0 verifier/sources/merkle_statement_verifier.move
+4 −4 verifier/sources/merkle_verifier.move
+37 −0 verifier/sources/prng.move
+751 −0 verifier/sources/test/constructor.move
+1,737 −0 verifier/sources/test/fri_statement_verifier_7_test_data.move
+4 −0 verifier/sources/test/gps_output_parser_test.move
+3,593 −0 verifier/sources/test/gps_statement_verifier_test_data.move
+2 −1 verifier/sources/test/test_verify_fri_statement.move
+2 −1 verifier/sources/test/test_verify_merkle_statement.move
+249 −0 verifier/sources/verifier_channel.move
325 changes: 325 additions & 0 deletions data

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions dcm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
with open("data", "r") as r:
for num in r.readlines():
print(f"\"{num[0:-1]}\",")
2 changes: 2 additions & 0 deletions src/contracts_caller/gps/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod types;
pub mod verify_proof_and_register;
1 change: 1 addition & 0 deletions src/contracts_caller/gps/types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod verify_proof_and_register;
58 changes: 58 additions & 0 deletions src/contracts_caller/gps/types/verify_proof_and_register.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::str::FromStr;

use aptos_sdk::move_types::u256::U256;
use serde::Deserialize;

#[derive(Debug)]
pub struct VerifyProofAndRegisterData {
pub proof_params: Vec<U256>,
pub proof: Vec<U256>,
pub task_metadata: Vec<U256>,
pub cairo_aux_input: Vec<U256>,
pub cairo_verifier_id: U256,
pub pre_registered_facts: Vec<U256>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VerifyProofAndRegisterDataJson {
pub proof_params: Vec<String>,
pub proof: Vec<String>,
pub task_metadata: Vec<String>,
pub cairo_aux_input: Vec<String>,
pub cairo_verifier_id: String,
pub pre_registered_facts: Vec<String>,
}

impl From<VerifyProofAndRegisterDataJson> for VerifyProofAndRegisterData {
fn from(value: VerifyProofAndRegisterDataJson) -> Self {
VerifyProofAndRegisterData {
proof_params: value
.proof_params
.iter()
.map(|x| U256::from_str(x).unwrap())
.collect(),
proof: value
.proof
.iter()
.map(|x| U256::from_str(x).unwrap())
.collect(),
task_metadata: value
.task_metadata
.iter()
.map(|x| U256::from_str(x).unwrap())
.collect(),
cairo_aux_input: value
.cairo_aux_input
.iter()
.map(|x| U256::from_str(x).unwrap())
.collect(),
cairo_verifier_id: U256::from_str(value.cairo_verifier_id.as_str()).unwrap(),
pre_registered_facts: value
.pre_registered_facts
.iter()
.map(|x| U256::from_str(x).unwrap())
.collect(),
}
}
}
97 changes: 97 additions & 0 deletions src/contracts_caller/gps/verify_proof_and_register.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use std::fs;
use std::str::FromStr;

use aptos_sdk::move_types::value::MoveValue;
use aptos_sdk::rest_client::aptos_api_types::MoveType;
use log::debug;

use crate::config::AppConfig;
use crate::contracts_caller::gps::types::verify_proof_and_register::{
VerifyProofAndRegisterData, VerifyProofAndRegisterDataJson,
};
use crate::contracts_caller::transaction_helper::{get_event_from_transaction, send_tx};

pub async fn verify_proof_and_register(
config: &AppConfig,
data: &VerifyProofAndRegisterData,
) -> anyhow::Result<()> {
let module_name = "gps_statement_verifier";
debug!("pre_registered_facts");
send_tx(
config,
"fact_registry",
"register_facts",
&vec![MoveValue::Vector(
data.pre_registered_facts
.iter()
.map(|v| MoveValue::U256(*v))
.collect(),
)],
)
.await?;
debug!("prepush_task_metadata");
send_tx(
config,
module_name,
"prepush_task_metadata",
&vec![MoveValue::Vector(
data.task_metadata
.iter()
.map(|v| MoveValue::U256(*v))
.collect(),
)],
)
.await?;
debug!("prepush_data_to_verify_proof_and_register");
send_tx(
config,
module_name,
"prepush_data_to_verify_proof_and_register",
&vec![
MoveValue::Vector(
data.proof_params
.iter()
.map(|v| MoveValue::U256(*v))
.collect(),
),
MoveValue::Vector(data.proof.iter().map(|v| MoveValue::U256(*v)).collect()),
MoveValue::Vector(
data.cairo_aux_input
.iter()
.map(|v| MoveValue::U256(*v))
.collect(),
),
MoveValue::U256(data.cairo_verifier_id),
],
)
.await?;
let mut time = 0;
loop {
time += 1;
debug!("verify_proof_and_register {}", time);
let tx = send_tx(config, module_name, "verify_proof_and_register", &vec![]).await?;
let event = get_event_from_transaction(
&tx,
MoveType::from_str(&format!(
"{}::{}::VparFinished",
config.module_address, module_name
))?,
);
if event.is_ok() {
break;
}
}
Ok(())
}

// #[cfg(test)]
pub fn sample_data(test_num: isize) -> anyhow::Result<VerifyProofAndRegisterData> {
let data = serde_json::from_str::<VerifyProofAndRegisterDataJson>(
fs::read_to_string(format!(
"src/data_samples/gps/verify_proof_and_register_{}.json",
test_num
))?
.as_str(),
)?;
Ok(VerifyProofAndRegisterData::from(data))
}
1 change: 1 addition & 0 deletions src/contracts_caller/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod memory_page_fact_registry;
pub mod gps;
pub mod transaction_helper;
pub mod types;
pub mod verify_fri;
Expand Down
42 changes: 38 additions & 4 deletions src/contracts_caller/transaction_helper.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
use std::str::FromStr;
use std::time::SystemTime;

use crate::config::AppConfig;
use crate::error::CoreError;
use anyhow::Error;
use aptos_sdk::move_types::identifier::Identifier;
use aptos_sdk::move_types::language_storage::ModuleId;
use aptos_sdk::move_types::u256::U256;
use aptos_sdk::move_types::value::{serialize_values, MoveValue};
use aptos_sdk::rest_client::aptos_api_types::{Event, MoveType};
use aptos_sdk::rest_client::Transaction;
use aptos_sdk::transaction_builder::TransactionBuilder;
use aptos_sdk::types::chain_id::ChainId;
use aptos_sdk::types::transaction::{SignedTransaction, TransactionPayload};
use aptos_sdk::types::transaction::{EntryFunction, SignedTransaction, TransactionPayload};
use aptos_sdk::types::LocalAccount;
use log::info;
use rand_core::OsRng;

use crate::error::CoreError;

#[inline]
pub fn str_to_u256(s: &str) -> Result<U256, CoreError> {
U256::from_str(s).map_err(|e| e.into())
Expand Down Expand Up @@ -40,7 +44,7 @@ pub fn build_transaction(
)
.sender(sender.address())
.sequence_number(i)
.max_gas_amount(30000)
.max_gas_amount(200000)
.gas_unit_price(100)
.build();
sender.sign_transaction(tx)
Expand Down Expand Up @@ -90,3 +94,33 @@ pub fn get_event_from_transaction(
};
event.ok_or(Error::new(CoreError::NotFound))
}

pub async fn send_tx(
config: &AppConfig,
module_name: &str,
fn_name: &str,
args: &Vec<MoveValue>,
) -> anyhow::Result<Transaction> {
let payload = TransactionPayload::EntryFunction(EntryFunction::new(
ModuleId::new(config.module_address, Identifier::new(module_name)?),
Identifier::new(fn_name)?,
vec![],
serialize_values(args),
));
let tx = build_transaction(payload, &config.account, config.chain_id);
let transaction = config.client.submit_and_wait(&tx).await?.into_inner();
let transaction_info = transaction.transaction_info()?;
info!(
"Finished running the function {} {}; gas used: {}",
fn_name,
transaction_info.hash.to_string(),
transaction_info.gas_used
);
if !transaction_info.success {
Err(Error::from(CoreError::TransactionNotSucceed(
transaction_info.hash.to_string(),
)))
} else {
Ok(transaction)
}
}
3 changes: 2 additions & 1 deletion src/data_samples/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Aptos Transaction Verification

This project verifies transaction data from the Ethereum mainnet. It follows a specific flow to verify both Merkle and FRI transactions, and then registers a continuous page batch and verifies proof and register.
This project verifies transaction data from the Ethereum mainnet. It follows a specific flow to verify both Merkle and
FRI transactions, and then registers a continuous page batch and verifies proof and register.

## Transaction Verification Flow

Expand Down
Loading

0 comments on commit 4d27178

Please sign in to comment.