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

Hstaykov/circuit input generators verify attestation data #287

Merged
merged 4 commits into from
Mar 19, 2024
Merged
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
6 changes: 3 additions & 3 deletions vendor/zkllvm-metacraft-circuits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ based on the [zkLLVM toolchain](https://github.com/nilfoundation/zkllvm).
The purpose of this project is to separate the compilation toolchain
from the application circuits. The tooling is added as submodule and we
have a src folder in the projects main folder where the new circuits will
be added. To compile them, use the run.sh scrypt in folder
zkllvm-metacraft-circuits/scripts passing the arguments you would pass to
zkllvm-metacraft-circuits/zkllvm-template/scripts/run.sh.
be added. In order to build and run tests, currently docker or podman can
be used. All steps (build + run tests) can be performed by executing the
script - scripts/compile_and_run_tests.sh.
8 changes: 8 additions & 0 deletions vendor/zkllvm-metacraft-circuits/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM ubuntu:22.04

RUN echo 'deb [trusted=yes] http://deb.nil.foundation/ubuntu/ all main' >>/etc/apt/sources.list
RUN apt update && apt -y upgrade
RUN apt install -y zkllvm proof-producer
RUN apt install -y vim libyaml-cpp-dev cmake clang-format libboost-all-dev

ENTRYPOINT [ "/bin/bash", "-l", "-c" ]
22 changes: 16 additions & 6 deletions vendor/zkllvm-metacraft-circuits/scripts/compile_and_run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
#!/usr/bin/env bash

echo "using nilfoundation/zkllvm-template:${ZKLLVM_VERSION:=0.0.86}"

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
echo "SCRIPT_DIR = " $SCRIPT_DIR

CURRENT_DIR=$(pwd)

echo "CURRENT_DIR = " $CURRENT_DIR

cd $SCRIPT_DIR/../docker && docker build -t zcli:latest -f Dockerfile_zcli . && docker run -v $SCRIPT_DIR/../../:/DendrETH zcli:latest
cd $SCRIPT_DIR/../docker && docker build -t zcli:latest -f Dockerfile_zcli . \
&& docker run -v $SCRIPT_DIR/../../:/DendrETH --user $(id -u ${USER}):$(id -g ${USER}) zcli:latest

if [ ! -d $SCRIPT_DIR/../src/tests/verify_attestation_data_test/finalizer-data ]
then
git clone [email protected]:metacraft-labs/finalizer-data.git $SCRIPT_DIR/../src/tests/verify_attestation_data_test/finalizer-data
fi

cd $CURRENT_DIR

bash ${SCRIPT_DIR}/run.sh --docker compile
COMMAND='cmake -G "Unix Makefiles" -B ${ZKLLVM_BUILD:-build} -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=g++ . && make -C ${ZKLLVM_BUILD:-build} template'

${SCRIPT_DIR}/docker_run.sh "$COMMAND"

bash ${SCRIPT_DIR}/docker_run.sh make test
if [ $# == 0 ]
then
bash ${SCRIPT_DIR}/docker_run.sh "make -C ${ZKLLVM_BUILD:-build} test "
else
bash ${SCRIPT_DIR}/docker_run.sh "ctest --test-dir ${ZKLLVM_BUILD:-build} -R ${@}"
fi
21 changes: 15 additions & 6 deletions vendor/zkllvm-metacraft-circuits/scripts/docker_run.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
#!/usr/bin/env bash

echo "using nilfoundation/zkllvm-template:${ZKLLVM_VERSION:=0.0.86}"

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

CURRENT_DIR=$(pwd)

cd $SCRIPT_DIR/../docker && docker build -t zkllvm:latest -f Dockerfile .

cd $CURRENT_DIR

docker run --rm -it --name zk_executable_tests \
--volume ${SCRIPT_DIR}/../zkllvm-template/build:/build \
--volume ${SCRIPT_DIR}/../zkllvm-template/:/zkllvm-template \
--volume ${SCRIPT_DIR}/../../consensus-spec-tests/:/consensus-spec-tests \
--volume ${SCRIPT_DIR}/../src/tests/verify_attestation_data_test/finalizer-data:/finalizer-data \
--volume ${SCRIPT_DIR}/../src/:/zkllvm-template/src \
--volume ${SCRIPT_DIR}/../.config:/.config/ \
--volume ${SCRIPT_DIR}/../.config:/root/.config/ \
--volume ${SCRIPT_DIR}/../.config:/opt/nil-toolchain/.config/ \
--user $(id -u ${USER}):$(id -g ${USER}) \
-w /build \
ghcr.io/nilfoundation/zkllvm-template:${ZKLLVM_VERSION} \
$@
-w /zkllvm-template \
zkllvm:latest \
"$@"
6 changes: 3 additions & 3 deletions vendor/zkllvm-metacraft-circuits/scripts/format_code.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
echo "using nilfoundation/zkllvm-template:${ZKLLVM_VERSION:=0.0.86}"
echo "Formatting source files ..."

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
CURRENT_DIR=$(pwd)
Expand All @@ -9,5 +9,5 @@ docker run --rm -it --name code_formatter \
--volume ${SCRIPT_DIR}/../src:/src \
--user $(id -u ${USER}):$(id -g ${USER}) \
-w /src \
ghcr.io/nilfoundation/zkllvm-template:${ZKLLVM_VERSION} \
/bin/bash -c "$COMMAND"
zkllvm:latest \
"$COMMAND"
39 changes: 0 additions & 39 deletions vendor/zkllvm-metacraft-circuits/scripts/run.sh

This file was deleted.

12 changes: 5 additions & 7 deletions vendor/zkllvm-metacraft-circuits/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ add_link_options(-fsanitize=address)
function(add_example example_target)
set(prefix ARG)
set(noValues "")
set(singleValues INPUT)
set(singleValues)
set(multiValues SOURCES)
cmake_parse_arguments(${prefix}
"${noValues}"
Expand Down Expand Up @@ -46,15 +46,13 @@ endfunction()

cm_find_package(Boost COMPONENTS REQUIRED filesystem)

add_custom_target(template)

find_package(LLVM REQUIRED CONFIG)
find_package(yaml-cpp REQUIRED)

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
add_custom_target(template)

# enable testing functionality
enable_testing()

add_subdirectory(tests)
add_subdirectory(circuits)
add_subdirectory(circuits)
add_subdirectory(circuit_input_generators)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
add_executable(verify_attestation_data_input_generators verify_attestation_data_input_generators.cpp)

add_dependencies(template verify_attestation_data_input_generators)

target_link_libraries (verify_attestation_data_input_generators
crypto3::algebra
crypto3::block
crypto3::codec
crypto3::containers
crypto3::hash
crypto3::kdf
crypto3::mac
marshalling::core
marshalling::crypto3_algebra
marshalling::crypto3_multiprecision
marshalling::crypto3_zk
crypto3::math
crypto3::modes
crypto3::multiprecision
crypto3::passhash
crypto3::pbkdf
crypto3::threshold
crypto3::pkpad
crypto3::pubkey
crypto3::random
crypto3::stream
crypto3::vdf
crypto3::zk

${Boost_LIBRARIES})

target_include_directories(verify_attestation_data_input_generators PRIVATE ${PROJECT_SOURCE_DIR}/src)

add_custom_command(TARGET verify_attestation_data_input_generators POST_BUILD
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/verify_attestation_data_input_generators ${CMAKE_CURRENT_BINARY_DIR}/../circuits/verify_attestation_data
DEPENDS compile_commands.json
VERBATIM ON
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#pragma once

#include <sstream>

#include "json/json.hpp"
using namespace nlohmann;

nlohmann::json pack_int_json(uint64_t val) {
nlohmann::json j;
j["int"] = val;
return j;
}

std::ostream& operator<<(std::ostream& dest, __uint128_t value) {
std::ostream::sentry s(dest);
if (s) {
char buffer[128];
char* start = std::end(buffer);
do {
--start;
*start = value % 10 + '0'; // store the rightmost digit
value /= 10; // pop the rightmost digit
} while (value != 0);
int len = std::end(buffer) - start;
if (dest.rdbuf()->sputn(start, len) != len) {
dest.setstate(std::ios_base::badbit);
}
}
return dest;
}

// Example expected output:
// {"vector": [{"field": "326522724692461750427768532537390503835"},{"field":
// "89059515727727869117346995944635890507"}]},
nlohmann::json bytes32_to_hash_type(const Bytes32& bytes) {
union Convert {
__uint128_t v[2];
Byte bytes[32];
} c;
for (size_t i = 0; i < bytes.size(); i++) {
c.bytes[bytes.size() - i - 1] = bytes[i];
}
std::stringstream val[2];
nlohmann::json field[2];
nlohmann::json result;

for (size_t i = 0; i < 2; i++) {
val[i] << c.v[i];
field[i]["field"] = val[i].str();
result["vector"].push_back(field[i]);
}

return result;
}

template<size_t N>
static_vector<Byte, N> json_to_byte_array(const nlohmann::json& j) {
static_vector<Byte, N> result;
size_t i = 0;
for (const auto& v : j["array"]) {
result[i++] = v["int"];
}
return result;
}

template<typename T>
nlohmann::json serialize(const T& val);

template<size_t N, bool F>
nlohmann::json serialize_vector(const static_vector<Byte, N, F>& bytes) {
nlohmann::json res_array;
for (size_t i = 0; i < N; i++) {
res_array["array"].push_back(pack_int_json(bytes[i]));
}
nlohmann::json result;
result["struct"].push_back(res_array);
return result;
}

nlohmann::json serialize_vector(HashType h) {
return bytes32_to_hash_type(h);
}

template<typename C, size_t S, bool B>
nlohmann::json serialize_vector(const static_vector<C, S, B>& v) {
nlohmann::json result;
result["struct"].push_back(pack_int_json((size_t)v.size()));
nlohmann::json elements;
for (size_t i = 0; i < S; i++) {
elements["array"].push_back(serialize(v[i]));
}
result["struct"].push_back(elements);
return result;
}

template<typename C1, size_t S1, bool B1, size_t S, bool B>
nlohmann::json serialize_vector(const static_vector<static_vector<C1, S1, B1>, S, B>& v) {
nlohmann::json result;
result["struct"].push_back(pack_int_json((size_t)v.size()));
nlohmann::json elements;
for (size_t i = 0; i < S; i++) {
elements["array"].push_back(serialize_vector(v[i]));
}
result["struct"].push_back(elements);
return result;
}

template<size_t N>
nlohmann::json byte_array_to_json(const static_vector<Byte, N>& bytes) {
nlohmann::json res_array;
for (size_t i = 0; i < N; i++) {
res_array["array"].push_back(pack_int_json(bytes[i]));
}
nlohmann::json result;
result["struct"].push_back(res_array);
return result;
}
Loading
Loading