Skip to content

Commit

Permalink
use bytes for programid
Browse files Browse the repository at this point in the history
  • Loading branch information
MaanavKhaitan committed Nov 21, 2024
1 parent 2c9ebbc commit 950a1f9
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 35 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
3 changes: 1 addition & 2 deletions contracts/script/Deployer.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/ProgramID.sol
Original file line number Diff line number Diff line change
@@ -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";
}
2 changes: 1 addition & 1 deletion contracts/src/coprocessor/Consumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ abstract contract Consumer {
}

function requestJob(
bytes32 programID,
bytes memory programID,
bytes memory onchainInput,
uint64 maxCycles
) internal virtual returns (bytes32) {
Expand Down
12 changes: 6 additions & 6 deletions contracts/src/coprocessor/IJobManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +24,7 @@ interface IJobManager {
bytes32 jobID;
bytes32 onchainInputHash;
uint64 maxCycles;
bytes32 programID;
bytes programID;
bytes result;
}

Expand All @@ -33,7 +33,7 @@ interface IJobManager {
bytes32 onchainInputHash;
bytes32 offchainInputHash;
uint64 maxCycles;
bytes32 programID;
bytes programID;
bytes result;
bytes32[] versionedBlobHashes;
}
Expand All @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions contracts/src/coprocessor/JobManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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(""));
Expand Down Expand Up @@ -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];
Expand All @@ -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");
Expand All @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions contracts/test/SquareRootConsumer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions zkvm-utils/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,17 @@ async fn execute_onchain_job_ffi(
max_cycles: u64,
zkvm_executor: &ZkvmExecutorService<LocalSigner<SigningKey>>,
) -> 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);
Expand Down
4 changes: 2 additions & 2 deletions zkvm-utils/src/sol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn generate_program_id_sol(programs: &[ProgramMetadata]) -> Result<Vec<u8>>
.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();

Expand All @@ -118,7 +118,7 @@ pub fn generate_deploy_script(programs: &[ProgramMetadata]) -> Result<Vec<u8>> {
.iter()
.map(|program| {
format!(
"jobManager.setElfPath(bytes32(0x{}), \"{}\");",
"jobManager.setElfPath(hex\"{}\", \"{}\");",
program.program_id_hex, program.elf_path
)
})
Expand Down

0 comments on commit 950a1f9

Please sign in to comment.