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

feat(avm)!: store public bytecode compressed in artifact #11193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions avm-transpiler/Cargo.lock

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

1 change: 1 addition & 0 deletions avm-transpiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ env_logger = "0.11"
log = "0.4"
serde_json = "1.0"
serde = { version = "1.0.136", features = ["derive"] }
flate2 = "1.0.35"
10 changes: 9 additions & 1 deletion avm-transpiler/src/transpile_contract.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::io::Read;

use acvm::FieldElement;
use base64::Engine;
use log::info;
Expand Down Expand Up @@ -111,14 +113,20 @@ impl From<CompiledAcirContractArtifact> for TranspiledContractArtifact {
&brillig_pcs_to_avm_pcs,
);

// Gzip AVM bytecode. We compress so that the artifact is smaller, but it will be uncompressed before deployment.
let mut compressed_avm_bytecode = Vec::new();
let mut encoder =
flate2::read::GzEncoder::new(&avm_bytecode[..], flate2::Compression::best());
let _ = encoder.read_to_end(&mut compressed_avm_bytecode);

// Push modified function entry to ABI
functions.push(AvmOrAcirContractFunctionArtifact::Avm(
AvmContractFunctionArtifact {
name: function.name,
is_unconstrained: function.is_unconstrained,
custom_attributes: function.custom_attributes,
abi: function.abi,
bytecode: base64::prelude::BASE64_STANDARD.encode(avm_bytecode),
bytecode: base64::prelude::BASE64_STANDARD.encode(compressed_avm_bytecode),
debug_symbols: ProgramDebugInfo { debug_infos },
brillig_names: function.brillig_names,
},
Expand Down
16 changes: 15 additions & 1 deletion yarn-project/types/src/abi/contract_artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
} from '@aztec/foundation/abi';
import { jsonParseWithSchema, jsonStringify } from '@aztec/foundation/json-rpc';

import { gunzipSync } from 'zlib';

import {
AZTEC_INITIALIZER_ATTRIBUTE,
AZTEC_INTERNAL_ATTRIBUTE,
Expand Down Expand Up @@ -279,7 +281,7 @@ function getNoteTypes(input: NoirCompiledContract) {
*/
function generateContractArtifact(contract: NoirCompiledContract, aztecNrVersion?: string): ContractArtifact {
try {
return ContractArtifactSchema.parse({
const artifact = ContractArtifactSchema.parse({
name: contract.name,
functions: contract.functions.map(f => generateFunctionArtifact(f, contract)),
outputs: contract.outputs,
Expand All @@ -288,6 +290,18 @@ function generateContractArtifact(contract: NoirCompiledContract, aztecNrVersion
fileMap: contract.file_map,
...(aztecNrVersion ? { aztecNrVersion } : {}),
});

// Uncompress the bytecode if the function is public.
// The bytecode is compressed _at storage_ but should be handled in uncompressed form.
// In particular, it gets deployed uncompressed.
for (const fn of artifact.functions) {
if (fn.functionType === FunctionType.PUBLIC) {
const bytecodeBuffer = Buffer.from(fn.bytecode);
fn.bytecode = gunzipSync(bytecodeBuffer);
}
}

return artifact;
} catch (err) {
throw new Error(`Could not generate contract artifact for ${contract.name}: ${err}`);
}
Expand Down
Loading