From 20a0873dcbfe4a862ad53a9c137030689a521a04 Mon Sep 17 00:00:00 2001
From: David Banks <47112877+dbanks12@users.noreply.github.com>
Date: Thu, 4 May 2023 10:46:42 -0400
Subject: [PATCH] feat: CI to test aztec circuits with current commit of bberg
 (#418)

* More generators for aztec3.

* update js vk (because we now use UP for merkle hashing)

* Helpers for ECDSA in A3 (#364)

* Add `stdlib_keccak` in cmake.

Correct an assertion in `to_byte_array` in bigfield.

* Add `random_element` to affine element.

* negate y conditionally.

* Change pedersen hash c_bind to use `pedersen_hash::lookup`.

* c_binds and other ECDSA related fixes (#407)

* Add v to stdlib ecdsa.

* create an engine if its empty.

* Add ecdsa c_bind.

* print v as a uint32.

* Add secp256k1 cbind.

add c_bind.hpp

Change hpp to h.

remove hpp.

* Add ecdsa in cmakelists.

remove stdlib_ecdsa from build.

* hack: (aztec3) introduce barretenberg crypto generator parameters hack (#408)

* hack: introduce BARRETENBERG_CRYPTO_GENERATOR_PARAMETERS_HACK

* doc: concise

* chore: align BARRETENBERG_CRYPTO_GENERATOR_PARAMETERS_HACK usage

* CI to test aztec circuits with current commit of bberg

* build manifest

* chore: align BARRETENBERG_CRYPTO_GENERATOR_PARAMETERS_HACK usage (#411)

* try other branch of aztec packages

* ci rename script

* Update join_split test

* bump aztec version and merge in aztec3-temporary fixes

* aztec commit switched to branch

* bump aztec commit and document

* typo README.md

* Update README.md

---------

Co-authored-by: Suyash Bagad <suyashnbagad1997@gmail.com>
Co-authored-by: Suyash Bagad <suyash@aztecprotocol.com>
Co-authored-by: ludamad <adam.domurad@gmail.com>
Co-authored-by: ludamad <domuradical@gmail.com>
---
 .circleci/cond_spot_run_build                 |  2 +-
 .circleci/cond_spot_run_test_script           | 13 ++++
 .circleci/config.yml                          | 69 ++++++++++++++++++-
 README.md                                     |  6 ++
 build_manifest.json                           | 16 +++--
 cpp/.aztec-packages-commit                    |  1 +
 ...e.circuits-wasm-linux-clang-builder-runner |  8 +++
 ...circuits-x86_64-linux-clang-builder-runner | 18 +++++
 cpp/scripts/run_aztec_circuits_tests          | 52 ++++++++++++++
 9 files changed, 178 insertions(+), 7 deletions(-)
 create mode 100755 .circleci/cond_spot_run_test_script
 create mode 100644 cpp/.aztec-packages-commit
 create mode 100644 cpp/dockerfiles/Dockerfile.circuits-wasm-linux-clang-builder-runner
 create mode 100644 cpp/dockerfiles/Dockerfile.circuits-x86_64-linux-clang-builder-runner
 create mode 100755 cpp/scripts/run_aztec_circuits_tests

diff --git a/.circleci/cond_spot_run_build b/.circleci/cond_spot_run_build
index d4fa4723ea7..6aa18b7ffe3 100755
--- a/.circleci/cond_spot_run_build
+++ b/.circleci/cond_spot_run_build
@@ -13,7 +13,7 @@ LAST_SUCCESSFUL_COMMIT=$(last_successful_commit $REPOSITORY)
 echo "Last successful commit: $LAST_SUCCESSFUL_COMMIT"
 
 if check_rebuild "$LAST_SUCCESSFUL_COMMIT" $REPOSITORY; then
-  spot_run_script $SPEC ../.circleci/remote_build/remote_build $REPOSITORY $@
+  spot_run_script $SPEC $BUILD_SYSTEM_PATH/remote_build/remote_build $REPOSITORY $@
 else
   echo "No rebuild necessary. Retagging..."
   STAGES=$(cat $DOCKERFILE | sed -n -e 's/^FROM .* AS \(.*\)/\1/p')
diff --git a/.circleci/cond_spot_run_test_script b/.circleci/cond_spot_run_test_script
new file mode 100755
index 00000000000..044777b673d
--- /dev/null
+++ b/.circleci/cond_spot_run_test_script
@@ -0,0 +1,13 @@
+#!/bin/bash
+set -e
+REPOSITORY=$1
+SCRIPT_PATH=$2
+shift
+shift
+
+cd $(query_manifest projectDir $REPOSITORY)
+
+mkdir -p /tmp/test-logs
+
+set -o pipefail
+cond_spot_run_script $REPOSITORY $JOB_NAME 32 $SCRIPT_PATH $@ | tee "/tmp/test-logs/$JOB_NAME.log"
diff --git a/.circleci/config.yml b/.circleci/config.yml
index c4618ddccac..d6264413523 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -82,7 +82,16 @@ benchmark_add_keys: &benchmark_add_keys
 setup_env: &setup_env
   run:
     name: "Setup environment"
-    command: cd .circleci && ./setup_env "$CIRCLE_SHA1" "$CIRCLE_TAG" "$CIRCLE_JOB" "$CIRCLE_REPOSITORY_URL" "$CIRCLE_BRANCH"
+    command: |
+      cd .circleci && ./setup_env "$CIRCLE_SHA1" "$CIRCLE_TAG" "$CIRCLE_JOB" "$CIRCLE_REPOSITORY_URL" "$CIRCLE_BRANCH"
+
+setup_aztec_commit: &setup_aztec_commit
+  run:
+    name: "Setup environment for Aztec Integration Testing"
+    command: |
+      # Load the aztec commit into env for use in integration tests
+      echo "export AZTEC_COMMIT=$(cat cpp/.aztec-packages-commit 2>/dev/null || echo master)" >> "$BASH_ENV"
+      source "$BASH_ENV"
 
 # This step is used to save logs from various barretenberg test to the workspace so that they can be used later to parse benchmark values out of them
 save_logs: &save_logs
@@ -235,6 +244,56 @@ jobs:
           name: "Test"
           command: store_test_benchmark_logs barretenberg-x86_64-linux-clang-assert
 
+  ###################################
+  # Aztec integration tests
+  circuits-wasm-linux-clang-builder-runner:
+    docker:
+      - image: aztecprotocol/alpine-build-image
+    resource_class: small
+    steps:
+      - *checkout
+      - *setup_env
+      - run:
+          name: "Build"
+          command: cond_spot_run_build barretenberg-circuits-wasm-linux-clang-builder-runner 64
+
+  circuits-x86_64-linux-clang-builder-runner:
+    docker:
+      - image: aztecprotocol/alpine-build-image
+    resource_class: small
+    steps:
+      - *checkout
+      - *setup_env
+      - run:
+          name: "Build"
+          command: cond_spot_run_build barretenberg-circuits-x86_64-linux-clang-builder-runner 64
+
+  circuits-wasm-tests:
+    docker:
+      - image: aztecprotocol/alpine-build-image
+    resource_class: small
+    steps:
+      - *checkout
+      - *setup_env
+      - *setup_aztec_commit
+      - run:
+          name: "Build"
+          command: cond_spot_run_test_script barretenberg-circuits-wasm-linux-clang-builder-runner ./scripts/run_aztec_circuits_tests "$AZTEC_COMMIT" 1 wasm scripts/a3-tests -*.skip*:*.circuit*
+
+  circuits-x86_64-tests:
+    docker:
+      - image: aztecprotocol/alpine-build-image
+    resource_class: small
+    steps:
+      - *checkout
+      - *setup_env
+      - *setup_aztec_commit
+      - run:
+          name: "Build"
+          command: cond_spot_run_test_script barretenberg-circuits-x86_64-linux-clang-builder-runner ./scripts/run_aztec_circuits_tests "$AZTEC_COMMIT" 1 x86_64 scripts/a3-tests -*.skip*
+  # End Aztec integration tests
+  ###################################
+
 # Repeatable config for defining the workflow below.
 bb_test: &bb_test
   requires:
@@ -266,3 +325,11 @@ workflows:
             branches:
               only:
                 - master
+      - circuits-wasm-linux-clang-builder-runner
+      - circuits-x86_64-linux-clang-builder-runner
+      - circuits-wasm-tests:
+          requires:
+            - circuits-wasm-linux-clang-builder-runner
+      - circuits-x86_64-tests:
+          requires:
+            - circuits-x86_64-linux-clang-builder-runner
\ No newline at end of file
diff --git a/README.md b/README.md
index ef207aa92b8..eba775746a7 100644
--- a/README.md
+++ b/README.md
@@ -199,3 +199,9 @@ Alternatively you can build separate test binaries, e.g. honk_tests or numeric_t
 ### VS Code configuration
 
 A default configuration for VS Code is provided by the file [`barretenberg.code-workspace`](barretenberg.code-workspace). These settings can be overridden by placing configuration files in `.vscode/`.
+
+### Integration tests with Aztec Circuits
+
+CI will automatically run integration tests against Aztec's circuits which live [here](https://github.com/AztecProtocol/aztec-packages/tree/master/circuits). To change which Aztec branch or commit for CI to test against, modify [`.aztec-packages-commit`](./cpp/.aztec-packages-commit).
+
+When working on a PR, you may want to point this file to a adifferent Aztec branch or commit, but then it should probably be pointed back to master before merging.
diff --git a/build_manifest.json b/build_manifest.json
index a44071b71ee..364f8fd2724 100644
--- a/build_manifest.json
+++ b/build_manifest.json
@@ -23,10 +23,16 @@
     "rebuildPatterns": ["^cpp/"],
     "dependencies": []
   },
-  "barretenberg.js": {
-    "buildDir": "js",
-    "dockerfile": "js/Dockerfile",
-    "rebuildPatterns": ["^js/"],
-    "dependencies": ["barretenberg-wasm-linux-clang"]
+  "barretenberg-circuits-x86_64-linux-clang-builder-runner": {
+    "buildDir": "cpp",
+    "dockerfile": "dockerfiles/Dockerfile.circuits-x86_64-linux-clang-builder-runner",
+    "rebuildPatterns": ["^cpp/"],
+    "dependencies": []
+  },
+  "barretenberg-circuits-wasm-linux-clang-builder-runner": {
+    "buildDir": "cpp",
+    "dockerfile": "dockerfiles/Dockerfile.circuits-wasm-linux-clang-builder-runner",
+    "rebuildPatterns": ["^cpp/"],
+    "dependencies": []
   }
 }
diff --git a/cpp/.aztec-packages-commit b/cpp/.aztec-packages-commit
new file mode 100644
index 00000000000..1f7391f92b6
--- /dev/null
+++ b/cpp/.aztec-packages-commit
@@ -0,0 +1 @@
+master
diff --git a/cpp/dockerfiles/Dockerfile.circuits-wasm-linux-clang-builder-runner b/cpp/dockerfiles/Dockerfile.circuits-wasm-linux-clang-builder-runner
new file mode 100644
index 00000000000..0b634d1ad86
--- /dev/null
+++ b/cpp/dockerfiles/Dockerfile.circuits-wasm-linux-clang-builder-runner
@@ -0,0 +1,8 @@
+FROM ubuntu:kinetic
+RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y bash build-essential git libssl-dev cmake ninja-build curl binaryen xz-utils curl
+
+RUN curl https://wasmtime.dev/install.sh -sSf | bash /dev/stdin --version v3.0.1
+WORKDIR /usr/src/barretenberg/cpp/src
+RUN curl -s -L https://github.com/CraneStation/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-linux.tar.gz | tar zxfv -
+WORKDIR /usr/src/barretenberg/cpp
+COPY . .
\ No newline at end of file
diff --git a/cpp/dockerfiles/Dockerfile.circuits-x86_64-linux-clang-builder-runner b/cpp/dockerfiles/Dockerfile.circuits-x86_64-linux-clang-builder-runner
new file mode 100644
index 00000000000..7ac0ea01a07
--- /dev/null
+++ b/cpp/dockerfiles/Dockerfile.circuits-x86_64-linux-clang-builder-runner
@@ -0,0 +1,18 @@
+FROM alpine:3.17
+RUN apk update \
+    && apk upgrade \
+    && apk add --no-cache \
+        bash \
+        build-base \
+        clang15 \
+        openmp \
+        openmp-dev \
+        cmake \
+        ninja \
+        git \
+        curl \
+        perl
+
+WORKDIR /usr/src/barretenberg/cpp
+
+COPY . .
\ No newline at end of file
diff --git a/cpp/scripts/run_aztec_circuits_tests b/cpp/scripts/run_aztec_circuits_tests
new file mode 100755
index 00000000000..0c3358300cd
--- /dev/null
+++ b/cpp/scripts/run_aztec_circuits_tests
@@ -0,0 +1,52 @@
+#!/bin/bash
+set -e
+
+# To be called from CI for testing with docker and AWS.
+# Can't be called locally unless AWS credentials are set up.
+#
+# Call from config.yml
+# Example:
+#   command: cond_spot_run_script circuits-wasm-linux-clang-assert 1 wasm scripts/a3-tests -*.skip*:*.circuit*
+
+AZTEC_COMMIT=$1 # Aztec commit/branch to checkout (MANDATORY)
+NUM_TRANSCRIPTS=$2 # integer (MANDATORY)
+ARCH=$3 # x86_64 or wasm (MUST BE LOWERCASE) (MANDATORY) used in aztec's circuits `run_tests_local`
+# TESTS=$4 (MANDATORY) used in aztec's circuits `run_tests_local` (test files rel to circuits/cpp)
+# GTEST_FILTER=$5 (optional) used in aztec's circuits `run_tests_local`
+# *** See `run_tests_local` for the args forwarded to that script
+shift # arg1 (aztec commit) and arg2 (num transcripts) are not forwarded
+shift # to aztec's circuits `run_tests_local`
+
+$(aws ecr get-login --region us-east-2 --no-include-email) 2> /dev/null
+
+IMAGE_URI=278380418400.dkr.ecr.us-east-2.amazonaws.com/barretenberg-circuits-${ARCH}-linux-clang-builder-runner:cache-$COMMIT_HASH
+docker pull $IMAGE_URI
+
+if [ "$ARCH" != "wasm" ]; then
+  # x86_64 / anything other than wasm
+  PRESET=default
+  CONFIGURE_OPTS="-DCMAKE_BUILD_TYPE=RelWithAssert -DCI=ON"
+  BUILD_DIR=build
+else
+  PRESET=wasm
+  BUILD_DIR=build-wasm
+fi
+
+echo "*** Running Aztec circuits tests on commit: $AZTEC_COMMIT"
+# run tests in docker
+RUN_ARGS="$@" # helper var necessary for some reason to pass all args to docker run
+docker run --rm -t $IMAGE_URI /bin/sh -c "\
+  set -e; \
+  cd /usr/src/; \
+  git clone https://github.com/AztecProtocol/aztec3-packages.git; \
+  cd /usr/src/aztec3-packages/circuits/cpp; \
+  git checkout $AZTEC_COMMIT; \
+  rm -rf /usr/src/aztec3-packages/circuits/cpp/barretenberg;
+  mv /usr/src/barretenberg .; \
+  cmake --preset $PRESET $CONFIGURE_OPTS; \
+  cmake --build --preset $PRESET; \
+  cd /usr/src/aztec3-packages/circuits/cpp/barretenberg/cpp/srs_db; \
+  ./download_ignition.sh $NUM_TRANSCRIPTS; \
+  cd /usr/src/aztec3-packages/circuits/cpp; \
+  export PATH=\$PATH:~/.wasmtime/bin/; \
+  ./scripts/run_tests_local $RUN_ARGS;"