Repository navigation
Expand file tree
/
Copy pathmakefile
More file actions
219 lines (180 loc) · 7.93 KB
/
Copy pathmakefile
File metadata and controls
219 lines (180 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
CIRCOM_SRC := $(shell find circuits -type d -name 'test*' -prune -o -type f -name '*.circom' -print)
AK_FILES := $(wildcard verifier/cardano/validators/*.ak)
SHELL := /usr/bin/env bash
CIRCUITS := $(basename $(notdir $(wildcard circuits/*.circom)))
CURVES := bn254 bls12-381
CPP_OBJS := calcwit fr fr_asm templ
# Built with prep-release, those are supposed to always be included in builds
RELEASE_TARGETS := \
circuit-bls12-381-tiny_nocrypto \
circuit-bls12-381-tiny \
circuit-bls12-381-small_nocrypto \
circuit-bls12-381-small
# Build with prep-tests, those are expected to be present for cargo test.
TEST_TARGETS := \
circuit-bn254-minimal \
circuit-bn254-tiny_nocrypto \
circuit-bn254-witness_test \
circuit-bn254-blsbug1 \
circuit-bn254-blsbug2 \
circuit-bn254-blsbug3 \
circuit-bn254-only_sha \
circuit-bls12-381-tiny_nocrypto \
circuit-bls12-381-minimal \
circuit-bls12-381-blsbug1 \
circuit-bls12-381-blsbug2 \
circuit-bls12-381-blsbug3
# Build with prep-tests-slow, those are expected to be present for cargo test -F slow-tests --release
SLOW_TEST_TARGETS := \
circuit-bn254-small_nocrypto \
circuit-bls12-381-tiny
PTAU_SIZE_BN254 := 24
PTAU_SIZE_BLS12381 := 24
.PHONY: bn254 .bls12381 r1cs r1cs-bls12381 init install_w2c2
# Cryptographically relevant and expensive files we want to keep around.
# Note that these are rule names.
.PRECIOUS: %.zkey %.0001.zkey zkey/bn254/%.r1cs zkey/bls12-381/%.r1cs ptau/bn254_%.ptau ptau/bls12381_%.ptau zkey/lib/libbn254_%.a zkey/lib/libbls12-381_%.a
# Prepare the repo:
# - initialize submodules
# - Fix circom imports going into node_modules, causing duplicate import issues
# - Avoid circom symbol conflicts
init:
mkdir -p ptau
mkdir -p zkey/bn254
mkdir -p zkey/bls12-381
git submodule update --init --recursive
scripts/circom_rename.sh circuits/circom-ecdsa-p256 ecdsa__
clean:
rm -r zkey/*
###################
# for convenience #
###################
# For some reason the @# is load-bearing
prep-release: $(RELEASE_TARGETS)
@#
prep-tests: $(TEST_TARGETS)
@#
prep-tests-slow: prep-tests $(SLOW_TEST_TARGETS)
@#
# Shorthand so we don't need to repeat ourselves for generating the most common combinations
circuit-bn254-%: zkey/lib/libbn254_%.a zkey/bn254/%.0001.zkey
@#
circuit-bls12-381-%: zkey/lib/libbls12-381_%.a zkey/bls12-381/%.0001.zkey zkey/bls12-381/%.cardano.json
@#
##############
# ptau files #
##############
ptau/bn254_%.ptau:
@echo -e "\x1b[96mDownloading ptau $*\x1b[0m"
# https://github.com/privacy-ethereum/perpetualpowersoftau?tab=readme-ov-file#prepared-and-truncated-files
time curl -fSL -o $@ https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_$*.ptau
# ptau/bls12-381_%.ptau:
# @echo -e "\x1b[96mDownloading ptau $*\x1b[0m"
# # https://github.com/p0tion-tools/cardano-ppot/blob/main/README.md
# time curl -fSL -o $@ https://cardano-trusted-setup-test.s3.us-east-2.amazonaws.com/Cardano-PPOT-Results/pot23_final_$*.ptau
# I could not find a good source for a bls12-381 ptau file that is large enough, so we have to generate it (will take a long time).
ptau/bls12-381_%.ptau:
time snarkjs -v powersoftau new bls12381 $* $@
time snarkjs -v powersoftau contribute $@ $@.tmp --name="first" --entropy="$${SNARKJS_ENTROPY:-$$(uuidgen)}"
# 4 sections: tauG1, tauG2, alphaTauG1, betaTauG1
# Each section goes up to fft PTAU
time snarkjs -v powersoftau prepare phase2 $@.tmp $@ -v
rm $@.tmp
####################
# Circom + snarkjs #
####################
zkey/bn254/%.r1cs: circuits/%.circom $(CIRCOM_SRC)
mkdir -p zkey/bn254
@echo -e "\x1b[96mCompiling $*\x1b[0m"
@# circom uses a different naming scheme
time circom $< -l circuits -o zkey/bn254 --r1cs --wasm -p bn128 --sym --c
cargo clean -p eudi2web3
zkey/bls12-381/%.r1cs: circuits/%.circom $(CIRCOM_SRC)
mkdir -p zkey/bls12-381
@echo -e "\x1b[96mCompiling $*\x1b[0m"
time circom $< -l circuits -o zkey/bls12-381 --r1cs --wasm -p bls12381 --sym --c
cargo clean -p eudi2web3
zkey/bn254/%.0000.zkey: zkey/bn254/%.r1cs ptau/bn254_$(PTAU_SIZE_BN254).ptau
time NODE_OPTIONS="--max-old-space-size=8192" snarkjs groth16 setup $< ptau/bn254_$(PTAU_SIZE_BN254).ptau $@
zkey/bls12-381/%.0000.zkey: zkey/bls12-381/%.r1cs ptau/bls12-381_$(PTAU_SIZE_BLS12381).ptau
@echo -e "\x1b[96mCircuit specific setup $*\x1b[0m"
time NODE_OPTIONS="--max-old-space-size=8192" snarkjs groth16 setup $< ptau/bls12-381_$(PTAU_SIZE_BLS12381).ptau $@
%.0001.zkey: %.0000.zkey
@echo -e "\x1b[96mCircuit specific contribution 1 $*\x1b[0m"
@# Very important step for security. Can be skipped during development but without this the whole proof system is insecure.
@# See https://rekt.news/default-settings
@# See https://blog.zksecurity.xyz/posts/groth16-setup-exploit/
time NODE_OPTIONS="--max-old-space-size=8192" snarkjs zkey contribute $< $@ --entropy "$(SNARKJS_ENTROPY)"
%.zkey: %.0001.zkey
ln -sfn $(notdir $<) $@
%.vkey.json: %.zkey
snarkjs zkey export verificationkey $< $@
##########################
# Witness gen (via w2c2) #
##########################
target/w2c2 target/w2c2_includes/w2c2_base.h &:
mkdir -p target/w2c2_includes
scripts/install_w2c2.sh
# Also outputs .h
zkey/%.c zkey/%.h &: zkey/%.r1cs target/w2c2
scripts/run_w2c2.sh $< zkey/$*.c
zkey/%.o: zkey/%.c zkey/%.h target/w2c2_includes/w2c2_base.h
gcc -std=c99 -O3 -I target/w2c2_includes -c $< -o $@
zkey/lib/libbn254_%.a: zkey/bn254/%.o
mkdir -p zkey/lib
ar rcs $@ $<
zkey/lib/libbls12-381_%.a: zkey/bls12-381/%.o
mkdir -p zkey/lib
ar rcs $@ $<
##########################
# Witness gen (via C++) #
##########################
define cpp_template
# Variables:
# 1: curve
# 2: circuit
# 3: prefix (allowed in C++ identifiers)
# 4: shortcut to the _cpp folder
# $(info 1=$(1) 2=$(2) 3=$(3) 4=$(4))
$(4)/templ.cpp: templates/cpp_circuit_specific.cpp
# cp $$< $$@
sed -e "s/XXXX/$(3)g" templates/cpp_circuit_specific.cpp > $$@
# zkey/$(1)/$(2)_cpp/$(2)_edited.cpp: zkey/$(1)/$(2)_cpp/$(2).cpp templates/cpp_circuit_edit.sed
# sed -E -f templates/cpp_circuit_edit.sed $$< > $$@
$(4)/fr_edited.asm: $(4)/fr.asm
sed -E -e 's/^( *)global (.*)/\1global \2 hidden\1/g' $$< > $$@
$(4)/%.o: $(4)/%.cpp
# g++ -std=c++11 -O0 -fvisibility=hidden -fvisibility-inlines-hidden -I. -c zkey/$(1)/$(2)_cpp/$$*_wrapped.cpp -o $$@
g++ -std=c++11 -O0 -I. -c $(4)/$$*.cpp -o $$@
# g++ -std=c++11 -O3 -g1 -fno-omit-frame-pointer -I. -c $$< -o $$@
# objcopy --prefix-symbols="$(subst -,,$(1))$(subst _,,$(subst .,,$(subst -,,$(2))))_" $$@
# TODO: This should depend on $(2)_edited.o if we want custom Sha256compression.
$(4)/merged.o: $(4)/$(2).o $(foreach f,$(CPP_OBJS),$(4)/$(f).o)
g++ -r -o $$@ $$^
# objcopy --keep-global-symbols=cpp_exports.txt --keep-global-symbol=$(3)_get_size_of_witness_c --keep-global-symbol=$(3)_witness_c $$@
# objcopy --keep-global-symbol=$(subst -,,$(1))$(subst _,,$(subst .,,$(subst -,,$(2))))_get_size_of_witness_c --keep-global-symbol=$(subst -,,$(1))$(subst _,,$(subst .,,$(subst -,,$(2))))_witness_c $$@
zkey/lib/lib$(1)_$(2)_cpp.a: $(4)/merged.o
mkdir -p zkey/lib
rm -f $$@
ar rcs $$@ $$^
$(4)/%_asm.o: $(4)/%.asm
nasm -felf64 $$< -o $$@
# objcopy --prefix-symbols="$(subst -,,$(1))$(subst _,,$(subst .,,$(subst -,,$(2))))_" $$@
endef
$(foreach curve,$(CURVES),$(foreach c,$(CIRCUITS),$(eval $(call cpp_template,$(curve),$(c),$(subst -,,$(curve))$(subst _,,$(subst .,,$(subst -,,$(c)))),zkey/$(curve)/$(c)_cpp))))
###########
# CARDANO #
###########
%.sk %.vk &:
@# To make sure we don't accidentally use the addr we remove it immediately.
rm me.addr
cardano-cli address key-gen --verification-key-file $*.vk --signing-key-file $*.sk
%.addr: %.vk
cardano-cli conway address build --testnet-magic 2 --payment-verification-key-file $< > $@
verifier/cardano/plutus.json: $(AK_FILES)
(cd verifier/cardano && aiken build)
# Examples: zkey/bls12-381/minimal.eudi2web3.cardano.script
# Warning: Not useful for bn254
%.cardano.json: %.vkey.json verifier/cardano/plutus.json
STEM=$*; aiken blueprint apply -i verifier/cardano/plutus.json -o $@ -m eudi2web3_demo \
"$$(cargo run --bin vkey2cardano -- $<)"