diff --git a/Cargo.lock b/Cargo.lock index af4a687..e9ac61e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2051,7 +2051,7 @@ dependencies = [ [[package]] name = "eip4844" version = "0.0.1" -source = "git+https://github.com/InfinityVM/InfinityVM.git?branch=maanav/sp1-porting-improvements#fe14d90bc64697e07098131174a74e16c31f0e83" +source = "git+https://github.com/InfinityVM/InfinityVM.git?branch=dylan/sp1-porting#fe14d90bc64697e07098131174a74e16c31f0e83" dependencies = [ "alloy", "c-kzg", @@ -2973,7 +2973,7 @@ checksum = "540654e97a3f4470a492cd30ff187bc95d89557a903a2bbf112e2fae98104ef2" [[package]] name = "ivm-abi" version = "0.0.1" -source = "git+https://github.com/InfinityVM/InfinityVM.git?branch=maanav/sp1-porting-improvements#fe14d90bc64697e07098131174a74e16c31f0e83" +source = "git+https://github.com/InfinityVM/InfinityVM.git?branch=dylan/sp1-porting#fe14d90bc64697e07098131174a74e16c31f0e83" dependencies = [ "alloy", ] @@ -2981,7 +2981,7 @@ dependencies = [ [[package]] name = "ivm-proto" version = "0.0.1" -source = "git+https://github.com/InfinityVM/InfinityVM.git?branch=maanav/sp1-porting-improvements#fe14d90bc64697e07098131174a74e16c31f0e83" +source = "git+https://github.com/InfinityVM/InfinityVM.git?branch=dylan/sp1-porting#fe14d90bc64697e07098131174a74e16c31f0e83" dependencies = [ "borsh", "prost", @@ -2993,7 +2993,7 @@ dependencies = [ [[package]] name = "ivm-sp1-utils" version = "0.1.0" -source = "git+https://github.com/InfinityVM/InfinityVM.git?branch=maanav/sp1-porting-improvements#fe14d90bc64697e07098131174a74e16c31f0e83" +source = "git+https://github.com/InfinityVM/InfinityVM.git?branch=dylan/sp1-porting#fe14d90bc64697e07098131174a74e16c31f0e83" dependencies = [ "bincode", "cargo_metadata", @@ -3004,7 +3004,7 @@ dependencies = [ [[package]] name = "ivm-zkvm" version = "0.0.1" -source = "git+https://github.com/InfinityVM/InfinityVM.git?branch=maanav/sp1-porting-improvements#fe14d90bc64697e07098131174a74e16c31f0e83" +source = "git+https://github.com/InfinityVM/InfinityVM.git?branch=dylan/sp1-porting#fe14d90bc64697e07098131174a74e16c31f0e83" dependencies = [ "alloy", "anyhow", @@ -3015,7 +3015,7 @@ dependencies = [ [[package]] name = "ivm-zkvm-executor" version = "0.0.1" -source = "git+https://github.com/InfinityVM/InfinityVM.git?branch=maanav/sp1-porting-improvements#fe14d90bc64697e07098131174a74e16c31f0e83" +source = "git+https://github.com/InfinityVM/InfinityVM.git?branch=dylan/sp1-porting#fe14d90bc64697e07098131174a74e16c31f0e83" dependencies = [ "alloy", "eip4844", diff --git a/Cargo.toml b/Cargo.toml index 16d9d54..9bc9336 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -101,12 +101,12 @@ alloy-sol-types = { version = "0.7", default-features = false } clap = { version = "4", features = ["derive"] } -ivm-abi = { git = "https://github.com/InfinityVM/InfinityVM.git", branch = "maanav/sp1-porting-improvements" } +ivm-abi = { git = "https://github.com/InfinityVM/InfinityVM.git", branch = "dylan/sp1-porting" } # TODO: Update these to main after merging SP1 feature branch -ivm-proto = { git = "https://github.com/InfinityVM/InfinityVM.git", branch = "maanav/sp1-porting-improvements" } -ivm-sp1-utils = { git = "https://github.com/InfinityVM/InfinityVM.git", branch = "maanav/sp1-porting-improvements" } -ivm-zkvm = { git = "https://github.com/InfinityVM/InfinityVM.git", branch = "maanav/sp1-porting-improvements" } -ivm-zkvm-executor = { git = "https://github.com/InfinityVM/InfinityVM.git", branch = "maanav/sp1-porting-improvements" } +ivm-proto = { git = "https://github.com/InfinityVM/InfinityVM.git", branch = "dylan/sp1-porting" } +ivm-sp1-utils = { git = "https://github.com/InfinityVM/InfinityVM.git", branch = "dylan/sp1-porting" } +ivm-zkvm = { git = "https://github.com/InfinityVM/InfinityVM.git", branch = "dylan/sp1-porting" } +ivm-zkvm-executor = { git = "https://github.com/InfinityVM/InfinityVM.git", branch = "dylan/sp1-porting" } k256 = { version = "0.13", default-features = false } diff --git a/contracts/script/Deployer.s.sol b/contracts/script/Deployer.s.sol index dff57fd..c8846e9 100644 --- a/contracts/script/Deployer.s.sol +++ b/contracts/script/Deployer.s.sol @@ -46,8 +46,7 @@ contract Deployer is Script, Utils { // Set ELF paths jobManager.setElfPath( - bytes32(0x11f33be858c41ce554eea7d643911c8e5c4de4f775da47bf100317333d2ef425), - "target/sp1/square-root/square-root" + hex"11f33be858c41ce554eea7d643911c8e5c4de4f775da47bf100317333d2ef425", "target/sp1/square-root/square-root" ); vm.stopBroadcast(); diff --git a/contracts/src/ProgramID.sol b/contracts/src/ProgramID.sol index 81ac573..d2365b7 100644 --- a/contracts/src/ProgramID.sol +++ b/contracts/src/ProgramID.sol @@ -1,5 +1,5 @@ pragma solidity ^0.8.28; library ProgramID { - bytes32 public constant SQUARE_ROOT_ID = bytes32(0x11f33be858c41ce554eea7d643911c8e5c4de4f775da47bf100317333d2ef425); + bytes public constant SQUARE_ROOT_ID = hex"11f33be858c41ce554eea7d643911c8e5c4de4f775da47bf100317333d2ef425"; } diff --git a/contracts/src/coprocessor/Consumer.sol b/contracts/src/coprocessor/Consumer.sol index b115b35..d6e7333 100644 --- a/contracts/src/coprocessor/Consumer.sol +++ b/contracts/src/coprocessor/Consumer.sol @@ -59,7 +59,7 @@ abstract contract Consumer { } function requestJob( - bytes32 programID, + bytes memory programID, bytes memory onchainInput, uint64 maxCycles ) internal virtual returns (bytes32) { diff --git a/contracts/src/coprocessor/IJobManager.sol b/contracts/src/coprocessor/IJobManager.sol index 16bb6b3..56fadf1 100644 --- a/contracts/src/coprocessor/IJobManager.sol +++ b/contracts/src/coprocessor/IJobManager.sol @@ -8,13 +8,13 @@ uint8 constant JOB_STATE_COMPLETED = 3; interface IJobManager { // EVENTS - event JobCreated(bytes32 indexed jobID, uint64 indexed nonce, address indexed consumer, uint64 maxCycles, bytes32 programID, bytes onchainInput); + event JobCreated(bytes32 indexed jobID, uint64 indexed nonce, address indexed consumer, uint64 maxCycles, bytes programID, bytes onchainInput); event JobCancelled(bytes32 indexed jobID); event JobCompleted(bytes32 indexed jobID, bytes result); // STRUCTS struct JobMetadata { - bytes32 programID; + bytes programID; uint64 maxCycles; address consumer; uint8 status; @@ -24,7 +24,7 @@ interface IJobManager { bytes32 jobID; bytes32 onchainInputHash; uint64 maxCycles; - bytes32 programID; + bytes programID; bytes result; } @@ -33,7 +33,7 @@ interface IJobManager { bytes32 onchainInputHash; bytes32 offchainInputHash; uint64 maxCycles; - bytes32 programID; + bytes programID; bytes result; bytes32[] versionedBlobHashes; } @@ -42,13 +42,13 @@ interface IJobManager { uint64 nonce; uint64 maxCycles; address consumer; - bytes32 programID; + bytes programID; bytes onchainInput; bytes32 offchainInputHash; } // FUNCTIONS - function createJob(uint64 nonce, bytes32 programID, bytes calldata onchainInput, uint64 maxCycles) external returns (bytes32 jobID); + function createJob(uint64 nonce, bytes calldata programID, bytes calldata onchainInput, uint64 maxCycles) external returns (bytes32 jobID); function getJobMetadata(bytes32 jobID) external view returns (JobMetadata memory); function cancelJob(bytes32 jobID) external; function submitResult(bytes calldata resultWithMetadata, bytes calldata signature) external; diff --git a/contracts/src/coprocessor/JobManager.sol b/contracts/src/coprocessor/JobManager.sol index 866e65b..2142447 100644 --- a/contracts/src/coprocessor/JobManager.sol +++ b/contracts/src/coprocessor/JobManager.sol @@ -41,7 +41,7 @@ contract JobManager is // Mapping from job ID --> versioned blob hashes mapping(bytes32 => bytes32[]) public jobIDToBlobhashes; // Mapping from program ID (verification key) --> ELF path - mapping(bytes32 => string) public programIDToElfPath; + mapping(bytes32 => string) public programIDHashToElfPath; // storage gap for upgradeability uint256[50] private __GAP; @@ -68,8 +68,8 @@ contract JobManager is return jobIDToMetadata[jobID]; } - function getElfPath(bytes32 programID) public view returns (string memory) { - return programIDToElfPath[programID]; + function getElfPath(bytes calldata programID) public view returns (string memory) { + return programIDHashToElfPath[keccak256(programID)]; } function setRelayer(address _relayer) external onlyOwner { @@ -80,11 +80,11 @@ contract JobManager is coprocessorOperator = _coprocessorOperator; } - function setElfPath(bytes32 programID, string calldata elfPath) external onlyOwner { - programIDToElfPath[programID] = elfPath; + function setElfPath(bytes calldata programID, string calldata elfPath) external onlyOwner { + programIDHashToElfPath[keccak256(programID)] = elfPath; } - function createJob(uint64 nonce, bytes32 programID, bytes calldata onchainInput, uint64 maxCycles) external override returns (bytes32) { + function createJob(uint64 nonce, bytes calldata programID, bytes calldata onchainInput, uint64 maxCycles) external override returns (bytes32) { address consumer = msg.sender; bytes32 jobID = keccak256(abi.encodePacked(nonce, consumer)); _createJob(nonce, jobID, programID, maxCycles, consumer, onchainInput); @@ -98,7 +98,7 @@ contract JobManager is return jobID; } - function _createJob(uint64 nonce, bytes32 jobID, bytes32 programID, uint64 maxCycles, address consumer, bytes memory onchainInput) internal { + function _createJob(uint64 nonce, bytes32 jobID, bytes memory programID, uint64 maxCycles, address consumer, bytes memory onchainInput) internal { require(jobIDToMetadata[jobID].status == 0, "JobManager.createJob: job already exists with this nonce and consumer"); jobIDToMetadata[jobID] = JobMetadata(programID, maxCycles, consumer, JOB_STATE_PENDING); Consumer(consumer).setInputsForJob(jobID, onchainInput, keccak256("")); @@ -190,7 +190,7 @@ contract JobManager is bytes32 jobID, uint64 maxCycles, bytes32 onchainInputHash, - bytes32 programID, + bytes memory programID, bytes memory result ) internal { JobMetadata memory job = jobIDToMetadata[jobID]; @@ -201,7 +201,7 @@ contract JobManager is "JobManager.submitResult: onchain input signed by coprocessor doesn't match onchain input submitted with job"); // This is to prevent coprocessor from using a different program ID to produce a malicious result - require(job.programID == programID, + require(keccak256(job.programID) == keccak256(programID), "JobManager.submitResult: program ID signed by coprocessor doesn't match program ID submitted with job"); require(job.maxCycles == maxCycles, "JobManager.submitResult: max cycles signed by coprocessor doesn't match max cycles submitted with job"); @@ -215,7 +215,7 @@ contract JobManager is Consumer(job.consumer).receiveResult(jobID, result); } - function executeOnchainJob(string memory elfPath, bytes32 programID, bytes memory onchainInput, bytes32 jobID, uint64 maxCycles) internal returns (bytes memory, bytes memory) { + function executeOnchainJob(string memory elfPath, bytes memory programID, bytes memory onchainInput, bytes32 jobID, uint64 maxCycles) internal returns (bytes memory, bytes memory) { string[] memory imageRunnerInput = new string[](13); uint256 i = 0; imageRunnerInput[i++] = "cargo"; diff --git a/contracts/test/SquareRootConsumer.t.sol b/contracts/test/SquareRootConsumer.t.sol index 7b05b6c..17ba86b 100644 --- a/contracts/test/SquareRootConsumer.t.sol +++ b/contracts/test/SquareRootConsumer.t.sol @@ -28,7 +28,7 @@ contract SquareRootConsumerTest is Test, Deployer { bytes32 jobID = consumer.requestSquareRoot(9); JobManager.JobMetadata memory jobMetadata = jobManager.getJobMetadata(jobID); - assertEq(jobMetadata.programID, ProgramID.SQUARE_ROOT_ID); + assertEq(keccak256(jobMetadata.programID), keccak256(ProgramID.SQUARE_ROOT_ID)); // Job status is COMPLETED since createJob in JobManager calls // submitResult in this Foundry template @@ -57,7 +57,7 @@ contract SquareRootConsumerTest is Test, Deployer { ); JobManager.JobMetadata memory jobMetadata = jobManager.getJobMetadata(DEFAULT_JOB_ID); - assertEq(jobMetadata.programID, ProgramID.SQUARE_ROOT_ID); + assertEq(keccak256(jobMetadata.programID), keccak256(ProgramID.SQUARE_ROOT_ID)); // Job status is COMPLETED since createJob in JobManager calls // submitResult in this Foundry template diff --git a/zkvm-utils/src/main.rs b/zkvm-utils/src/main.rs index 892faed..4cedb83 100644 --- a/zkvm-utils/src/main.rs +++ b/zkvm-utils/src/main.rs @@ -130,12 +130,17 @@ async fn execute_onchain_job_ffi( max_cycles: u64, zkvm_executor: &ZkvmExecutorService>, ) -> Result<()> { + // println!("NARULA program id hex is {:?}", hex::encode(program_id.clone())); let elf = std::fs::read(elf_path).unwrap(); let (result_with_metadata, zkvm_operator_signature) = zkvm_executor .execute_onchain_job(job_id, max_cycles, program_id, onchain_input, elf, VmType::Sp1) .await .unwrap(); + // abi decode + // let result = ResultWithMetadata::abi_decode(&result_with_metadata, false).unwrap(); + // println!("NARULA program ID is {:?}", hex::encode(result.program_id.clone())); + let calldata = abi_encode_result_with_signature_calldata(result_with_metadata, zkvm_operator_signature); let output = hex::encode(calldata); diff --git a/zkvm-utils/src/sol.rs b/zkvm-utils/src/sol.rs index 7f39e0d..831217d 100644 --- a/zkvm-utils/src/sol.rs +++ b/zkvm-utils/src/sol.rs @@ -100,7 +100,7 @@ pub fn generate_program_id_sol(programs: &[ProgramMetadata]) -> Result> .map(|program| { let name = program.name.to_uppercase().replace('-', "_"); let program_id = program.program_id_hex.clone(); - format!("bytes32 public constant {name}_ID = bytes32(0x{program_id});") + format!("bytes public constant {name}_ID = hex\"{program_id}\";") }) .collect(); @@ -118,7 +118,7 @@ pub fn generate_deploy_script(programs: &[ProgramMetadata]) -> Result> { .iter() .map(|program| { format!( - "jobManager.setElfPath(bytes32(0x{}), \"{}\");", + "jobManager.setElfPath(hex\"{}\", \"{}\");", program.program_id_hex, program.elf_path ) })