Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

[FEAT] precompile-sha256 #1032

Merged
merged 51 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
7320dc5
init table16 with generic refactoring
noel2004 Nov 3, 2023
944cf7c
update cargo
noel2004 Nov 3, 2023
215a1a2
add required cfgs
noel2004 Nov 3, 2023
55eec01
add benchmarking
noel2004 Nov 7, 2023
c1ea6b8
customable bench
noel2004 Nov 10, 2023
ab5b2fa
fmt
noel2004 Nov 10, 2023
fe5a433
config gates for sha256 circuit
noel2004 Nov 15, 2023
d0b63a6
Merge remote-tracking branch 'origin/develop' into feat/sha256
noel2004 Nov 15, 2023
70ceb47
complete sha256 circuit
noel2004 Nov 15, 2023
4795760
induce SHA256 in bus mapping
noel2004 Nov 15, 2023
152c567
better challenge spec
noel2004 Nov 16, 2023
b8379ea
integrate sha256 circuit into super
noel2004 Nov 16, 2023
bef157b
readme (wip)
noel2004 Nov 16, 2023
4adf228
complete precompile and sha256 table
noel2004 Nov 16, 2023
89505b0
clippies
noel2004 Nov 16, 2023
85732d2
fmt
noel2004 Nov 16, 2023
8d90619
better tests
noel2004 Nov 16, 2023
a51e117
fix issues and better printing for circuit layout
noel2004 Nov 17, 2023
1c14779
refine and fix issues
noel2004 Nov 17, 2023
8b125f1
fix digest in table16
noel2004 Nov 20, 2023
edef428
fix layout, clippy and fmt
noel2004 Nov 20, 2023
635beae
complete sha256 table
noel2004 Nov 20, 2023
c8a3f6d
fix rw
noel2004 Nov 20, 2023
08d3039
fix rlc on padding
noel2004 Nov 21, 2023
4af8e0e
Merge remote-tracking branch 'origin/develop' into feat/sha256
noel2004 Nov 21, 2023
5db7d69
clippy and fmt
noel2004 Nov 21, 2023
2b43069
more tests
noel2004 Nov 21, 2023
7992f8d
test added
noel2004 Nov 21, 2023
f11f6cc
sha256: update row cost per block
noel2004 Nov 21, 2023
ac0d3fb
update readme (wip)
noel2004 Nov 22, 2023
c4b1898
lookup input len
noel2004 Nov 22, 2023
41298f7
enable sha256
noel2004 Nov 23, 2023
f7740de
Merge remote-tracking branch 'origin/develop' into feat/sha256
noel2004 Nov 23, 2023
da3027c
more tests cases
noel2004 Nov 23, 2023
8fb408e
refactoring aux data
noel2004 Nov 23, 2023
abeb61a
fmt
noel2004 Nov 23, 2023
e8e2c71
fix precompile call test
noel2004 Nov 23, 2023
e600ff5
fix another callop test
noel2004 Nov 23, 2023
a748453
Merge remote-tracking branch 'origin/develop' into feat/sha256
noel2004 Nov 23, 2023
fc94e67
+ update bench to circuit-sha256,
noel2004 Nov 25, 2023
f3f497d
Merge remote-tracking branch 'origin/develop' into feat/sha256
noel2004 Nov 25, 2023
0a4c056
fix vk issue, add more test
noel2004 Nov 26, 2023
e3d1333
trivial fixs
noel2004 Nov 27, 2023
9649214
Merge remote-tracking branch 'origin/develop' into feat/sha256
noel2004 Nov 28, 2023
ff0bf52
Merge remote-tracking branch 'origin/develop' into feat/sha256
noel2004 Nov 29, 2023
88e419c
move sha256 circuit into zkevm-circuits
noel2004 Nov 30, 2023
2d9914e
purge unused sha256 dir
noel2004 Nov 30, 2023
5af254a
Merge remote-tracking branch 'origin/develop' into feat/sha256
noel2004 Nov 30, 2023
c526f09
bump halo2 version
noel2004 Nov 30, 2023
05d3747
fix blank comment
noel2004 Dec 1, 2023
9d5661c
Merge remote-tracking branch 'origin/develop' into feat/sha256
noel2004 Dec 1, 2023
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
54 changes: 29 additions & 25 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use ethers_providers::JsonRpcClient;
pub use execution::{
BigModExp, CopyBytes, CopyDataType, CopyEvent, CopyEventStepsBuilder, CopyStep, EcAddOp,
EcMulOp, EcPairingOp, EcPairingPair, ExecState, ExecStep, ExpEvent, ExpStep, NumberOrHash,
PrecompileEvent, PrecompileEvents, N_BYTES_PER_PAIR, N_PAIRING_PER_OP,
PrecompileEvent, PrecompileEvents, N_BYTES_PER_PAIR, N_PAIRING_PER_OP, SHA256,
};
use hex::decode_to_slice;

Expand Down
25 changes: 25 additions & 0 deletions bus-mapping/src/circuit_input_builder/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,20 @@ impl PrecompileEvents {
.cloned()
.collect()
}
/// Get all SHA256 events.
pub fn get_sha256_events(&self) -> Vec<SHA256> {
self.events
.iter()
.filter_map(|e| {
if let PrecompileEvent::SHA256(op) = e {
Some(op)
} else {
None
}
})
.cloned()
.collect()
}
}

/// I/O from a precompiled contract call.
Expand All @@ -933,6 +947,8 @@ pub enum PrecompileEvent {
EcPairing(Box<EcPairingOp>),
/// Represents the I/O from Modexp call.
ModExp(BigModExp),
/// Represents the I/O from SHA256 call.
SHA256(SHA256),
}

impl Default for PrecompileEvent {
Expand Down Expand Up @@ -1379,3 +1395,12 @@ impl Default for BigModExp {
}
}
}

/// Event representating an SHA256 hash in precompile sha256.
#[derive(Clone, Debug, Default)]
pub struct SHA256 {
/// input bytes
pub input: Vec<u8>,
/// digest
pub digest: [u8; 32],
}
3 changes: 0 additions & 3 deletions bus-mapping/src/evm/opcodes/callop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,9 +753,6 @@ pub mod tests {
address: Word::from(0x2),
stack_value: vec![(
Word::from(0x20),
#[cfg(feature = "scroll")]
Word::zero(),
#[cfg(not(feature = "scroll"))]
word!("a8100ae6aa1940d0b663bb31cd466142ebbdbd5187131b92d93818987832eb89"),
)],
..Default::default()
Expand Down
21 changes: 20 additions & 1 deletion bus-mapping/src/evm/opcodes/precompiles/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use eth_types::{GethExecStep, ToWord, Word};

use crate::{
circuit_input_builder::{Call, CircuitInputStateRef, ExecState, ExecStep},
circuit_input_builder::{
Call, CircuitInputStateRef, ExecState, ExecStep, PrecompileEvent, SHA256,
},
operation::CallContextField,
precompile::{PrecompileAuxData, PrecompileCalls},
Error,
Expand Down Expand Up @@ -50,6 +52,23 @@ pub fn gen_associated_ops(
return_bytes: return_bytes.to_vec(),
}),
),
PrecompileCalls::Sha256 => (
if output_bytes.is_empty() {
None
} else {
Some(PrecompileEvent::SHA256(SHA256 {
input: input_bytes.to_vec(),
digest: output_bytes
.try_into()
.expect("output bytes must be 32 bytes"),
}))
},
Some(PrecompileAuxData::SHA256 {
input_bytes: input_bytes.to_vec(),
output_bytes: output_bytes.to_vec(),
return_bytes: return_bytes.to_vec(),
}),
),
_ => {
log::warn!("precompile {:?} unsupported in circuits", precompile);
(
Expand Down
15 changes: 12 additions & 3 deletions bus-mapping/src/precompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ pub(crate) fn execute_precompiled(
// Revm behavior is different from scroll evm,
// so we need to override the behavior of invalid input
match PrecompileCalls::from(address.0[19]) {
PrecompileCalls::Blake2F
| PrecompileCalls::Sha256
| PrecompileCalls::Ripemd160 => (vec![], gas, false, false),
PrecompileCalls::Blake2F | PrecompileCalls::Ripemd160 => {
(vec![], gas, false, false)
}
PrecompileCalls::Bn128Pairing => {
if input.len() > N_PAIRING_PER_OP * N_BYTES_PER_PAIR {
(vec![], gas, false, false)
Expand Down Expand Up @@ -455,6 +455,15 @@ pub enum PrecompileAuxData {
/// bytes returned back to the caller from the identity call.
return_bytes: Vec<u8>,
},
/// SHA256
SHA256 {
/// input bytes to the sha256 call.
input_bytes: Vec<u8>,
/// output bytes from the sha256 call.
output_bytes: Vec<u8>,
/// bytes returned back to the caller from the sha256 call.
return_bytes: Vec<u8>,
},
/// Ecrecover.
Ecrecover(EcrecoverAuxData),
/// Modexp.
Expand Down
6 changes: 3 additions & 3 deletions geth-utils/l2geth/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/imdario/mergo v0.3.16
github.com/scroll-tech/go-ethereum v1.10.14-0.20230919024151-fa0be69a3fb9
github.com/scroll-tech/go-ethereum v1.10.14-0.20231123003536-35313dc92055
)

require (
Expand Down Expand Up @@ -36,7 +36,7 @@ require (
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/sys v0.13.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
)
8 changes: 8 additions & 0 deletions geth-utils/l2geth/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ github.com/scroll-tech/go-ethereum v1.10.14-0.20230901060443-e1eebd17067c h1:GuA
github.com/scroll-tech/go-ethereum v1.10.14-0.20230901060443-e1eebd17067c/go.mod h1:DiN3p2inoXOxGffxSswDKqWjQ7bU+Mp0c9v0XQXKmaA=
github.com/scroll-tech/go-ethereum v1.10.14-0.20230919024151-fa0be69a3fb9 h1:QiqH+ZGNNzMcKy21VGX6XYg81DXE+/9j1Ik7owm13hs=
github.com/scroll-tech/go-ethereum v1.10.14-0.20230919024151-fa0be69a3fb9/go.mod h1:DiN3p2inoXOxGffxSswDKqWjQ7bU+Mp0c9v0XQXKmaA=
github.com/scroll-tech/go-ethereum v1.10.14-0.20231123003536-35313dc92055 h1:7dGW0GxAu5y+8SlXc4LtuoWXxbV5de3vDjN2qz2oO+k=
github.com/scroll-tech/go-ethereum v1.10.14-0.20231123003536-35313dc92055/go.mod h1:4HrFcoStbViFVy/9l/rvKl1XmizVAaPdgqI8v0U8hOc=
github.com/scroll-tech/zktrie v0.6.0 h1:xLrMAO31Yo2BiPg1jtYKzcjpEFnXy8acbB7iIsyshPs=
github.com/scroll-tech/zktrie v0.6.0/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk=
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
Expand Down Expand Up @@ -165,6 +167,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand All @@ -175,6 +179,7 @@ golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -204,12 +209,15 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ misc-precompiled-circuit = { package = "misc-precompiled-circuit", git = "https:

halo2-base = { git = "https://github.com/scroll-tech/halo2-lib", tag = "v0.1.5", default-features=false, features=["halo2-pse","display"] }
halo2-ecc = { git = "https://github.com/scroll-tech/halo2-lib", tag = "v0.1.5", default-features=false, features=["halo2-pse","display"] }
halo2_gadgets = { git = "https://github.com/scroll-tech/halo2.git", branch = "develop", features = ["unstable"] }

num-bigint.workspace = true
subtle.workspace = true
Expand Down
Loading