Skip to content

Commit

Permalink
Apply final exponentiation only to accumulator #250 (#256)
Browse files Browse the repository at this point in the history
* Apply diff patch from issue

* Remove artifacts

* Add script to check git ignore

* Add check to CI

* Update CI, update makefile

* Fix CI

* Fix path in copy-precompiles

* Add missing cross dependency

* Add missing cargo-cross

* Update ci, makefile

* Fix clean target on makefile

* Fix pairing suggestion

* Update solc install step in CI

---------

Co-authored-by: Francisco Krause Arnim <fran@MBPdeFrancisco.fibertel.com.ar>
Co-authored-by: IAvecilla <ignacio.avecilla@lambdaclass.com>
  • Loading branch information
3 people authored Apr 18, 2024
1 parent bf19db4 commit bc5be81
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 31 deletions.
23 changes: 17 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ concurrency:
cancel-in-progress: true

jobs:
precompiles-in-git-ignore:
name: Every pre-compile artifact is in git-ignore
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3

- name: Run check
run: |
make check-git-ignore
lint:
name: Lint Tests Code
runs-on: ubuntu-latest
Expand All @@ -25,6 +35,7 @@ jobs:
tests
shared-key: "cache"


- name: Install stable toolchain
uses: dtolnay/rust-toolchain@nightly

Expand Down Expand Up @@ -72,15 +83,15 @@ jobs:
precompiles:
name: Compile node and run tests
runs-on: ubuntu-latest
needs: [check-tests]
needs: [check-tests, precompiles-in-git-ignore]
steps:
- name: Checkout sources
uses: actions/checkout@v3

- name: Add solc
run: |
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt install solc
sudo apt-get install solc
- name: Rustup toolchain install
uses: dtolnay/rust-toolchain@nightly
Expand All @@ -104,13 +115,13 @@ jobs:
with:
node-version: 18.18.0

- name: Install dependencies and build node
- name: Install dependencies and build node + precompiles
uses: awalsh128/cache-apt-pkgs-action@latest
with:
packages: build-essential cmake pkg-config libssl-dev clang
packages: build-essential make cross-gcc-dev cmake pkg-config libssl-dev clang
version: 1.0

- name: "Run tests"
- name: Start nodes + Run tests
run: |
make test-ci
Expand Down
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
era_test_node.log
submodules/era_test_node
tests/gas_reports
src/deps/contracts/EcAdd.yul.zbin
src/deps/contracts/EcAddG2.yul.zbin
src/deps/contracts/EcMul.yul.zbin
src/deps/contracts/EcMulG2.yul.zbin
src/deps/contracts/EcPairing.yul.zbin
src/deps/contracts/ModExp.yul.zbin
src/deps/contracts/P256VERIFY.yul.zbin
src/deps/contracts/secp256k1VERIFY.yul.zbin
Binary file removed .test-node-subtree/src/deps/contracts/EcAdd.yul.zbin
Binary file not shown.
Binary file not shown.
Binary file removed .test-node-subtree/src/deps/contracts/EcMul.yul.zbin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
29 changes: 16 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: clean build-node start-reth reth-and-node-ci test-ci
.PHONY: clean build-node start-reth reth-and-node-ci test-ci build-precompiles copy-precompile

current_dir := ${CURDIR}
era_test_node_base_path := $(current_dir)/.test-node-subtree
Expand All @@ -10,24 +10,20 @@ era_test_node_src_files = $(shell find $(era_test_node_base_path)/src -name "*.r
precompiles_source = $(wildcard $(current_dir)/precompiles/*.yul)
precompiles_dst = $(patsubst $(current_dir)/precompiles/%, $(precompile_dst_path)/%, $(precompiles_source))

build-node: $(era_test_node) $(precompiles_dst)

run-node: build-node
run-node: build-contracts $(era_test_node)
$(era_test_node) --show-calls=all --resolve-hashes --show-gas-details=all run

run-node-light: $(era_test_node) $(precompiles_dst)
$(era_test_node) run

# test node needs contracts for include bytes directive
# source files are obtained just to recompile if there are changes, and located with a find
$(era_test_node): $(era_test_node_makefile) $(era_test_node_src_files) $(precompiles_dst)
cd $(era_test_node_base_path) && make rust-build

## precompile source is added just to avoid recompiling if they haven't changed
$(precompiles_dst): $(precompiles_source)
build-contracts: $(precompiles_source)
cp precompiles/*.yul $(precompile_dst_path) && cd $(era_test_node_base_path) && make build-contracts

build-precompiles: $(precompiles_dst)
run-node-light: $(era_test_node) $(precompiles_dst)
$(era_test_node) run

# Node Commands
update-node: era_test_node
Expand All @@ -41,15 +37,22 @@ test: start-reth
cd tests && \
cargo test ${PRECOMPILE}

reth-and-node-ci: build-node start-reth
run-node-ci: build-contracts $(era_test_node)
$(era_test_node) --show-calls=all --resolve-hashes --show-gas-details=all run > era_node.log &
sleep 1

test-ci: reth-and-node-ci
test-ci: run-node-ci start-reth
sleep 1
cd tests && cargo test

docs:
cd docs && mdbook serve --open

clean:
rm $(era_test_node_base_path)/src/deps/contracts/*.yul.zbin $(era_test_node_base_path)/etc/system-contracts/contracts/precompiles/*.yul
# for file in $(shell ls ./precompiles/*.yul | grep -o '[^/]+(?=\.yul)'); do \
# rm -f $(era_test_node_base_path)/src/deps/contracts/$${file}.yul.zbin; \
# done
cd .test-node-subtree && make clean-contracts

check-git-ignore:
cd precompiles
$(shell ./precompiles/git_ignore_check.sh)
16 changes: 4 additions & 12 deletions precompiles/EcPairing.yul
Original file line number Diff line number Diff line change
Expand Up @@ -1547,17 +1547,6 @@ object "EcPairing" {
f000, f001, f010, f011, f020, f021, f100, f101, f110, f111, f120, f121 := fp12Mul(f000, f001, f010, f011, f020, f021, f100, f101, f110, f111, f120, f121, l00, l01, l10, l11, l20, l21, l30, l31, l40, l41, l50, l51)
}

/// @notice Computes the Pairing between two points P and Q.
/// @dev Applies the Millers Loop and the final exponentiation to return the result of the pairing.
/// @params g1x, g1y The coordinates of the point P of the curve G1.
/// @params g2_x0, g2_x1 The coefficients of the X coordinate of point Q on the twisted curve G2.
/// @params g2_y0, g2_y1 The coefficients of the Y coordinate of point Q on the twisted curve G2.
/// @return f000, f001, f010, f011, f020, f021, f100, f101, f110, f111, f120, f121 The Fp12 element result of the pairing e(P,Q)
function pair(g1_x, g1_y, g2_x0, g2_x1, g2_y0, g2_y1) -> f000, f001, f010, f011, f100, f101, f110, f111, f200, f201, f210, f211 {
f000, f001, f010, f011, f100, f101, f110, f111, f200, f201, f210, f211 := millerLoop(g2_x0, g2_x1, g2_y0, g2_y1, g1_x, g1_y)
f000, f001, f010, f011, f100, f101, f110, f111, f200, f201, f210, f211 := finalExponentiation(f000, f001, f010, f011, f100, f101, f110, f111, f200, f201, f210, f211)
}

// FALLBACK

let inputSize := calldatasize()
Expand Down Expand Up @@ -1645,11 +1634,14 @@ object "EcPairing" {
continue
}

let f000, f001, f010, f011, f020, f021, f100, f101, f110, f111, f120, f121 := pair(g1_x, g1_y, g2_x0, g2_x1, g2_y0, g2_y1)

let f000, f001, f010, f011, f020, f021, f100, f101, f110, f111, f120, f121 := millerLoop(g2_x0, g2_x1, g2_y0, g2_y1, g1_x, g1_y)

r000, r001, r010, r011, r020, r021, r100, r101, r110, r111, r120, r121 := fp12Mul(r000, r001, r010, r011, r020, r021, r100, r101, r110, r111, r120, r121, f000, f001, f010, f011, f020, f021, f100, f101, f110, f111, f120, f121)
}

r000, r001, r010, r011, r020, r021, r100, r101, r110, r111, r120, r121 := finalExponentiation(r000, r001, r010, r011, r020, r021, r100, r101, r110, r111, r120, r121)

// Pair check
if and(eq(r000, MONTGOMERY_ONE()), iszero(or(r001, or(r010, r011)))) {
if iszero(or(or(r020, r021), or(r100, r101))) {
Expand Down
17 changes: 17 additions & 0 deletions precompiles/git_ignore_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
missing_pattern=0
for file in $(ls ./precompiles/*.yul); do
# Extract the base name of the file
filename=$(basename "$file")
filename="${filename%.yul}"
# Generate the pattern for .gitignore
gitignore_pattern="src/deps/contracts/${filename}.yul.zbin"
# Check if the pattern exists in .gitignore
if ! grep -qF "$gitignore_pattern" .gitignore; then
echo "File: $filename.yul.zbin should be on the gitignore file"
missing_pattern=1
fi
done
if [ $missing_pattern -eq 1 ]; then
exit 1
fi

0 comments on commit bc5be81

Please sign in to comment.