Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.

Commit 5b4f664

Browse files
HarryRHarryR
HarryR
authored and
HarryR
committed
JS syntax cleanups, Cmake Cleanups, mpz multiply benchmark
1 parent b6f960d commit 5b4f664

8 files changed

+96
-51
lines changed

CMakeLists.txt

+19-15
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ project(ethsnarks)
44

55
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
66

7-
if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
8-
# Common compilation flags and warning configuration
9-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wfatal-errors -Wno-unused-variable")
10-
if("${MULTICORE}")
11-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
12-
endif()
13-
# Default optimizations flags (to override, use -DOPT_FLAGS=...)
14-
endif()
157

168

179
set(
@@ -60,18 +52,25 @@ option(
6052
OFF
6153
)
6254

55+
option(
56+
USE_MIXED_ADDITION
57+
"Convert each element of the key pair to affine coordinates"
58+
OFF
59+
)
60+
6361
option(
6462
BINARY_OUTPUT
6563
"Use binary output for serialisation"
6664
ON
6765
)
6866

6967
option(
70-
PERFORMANCE
71-
"Enable link-time and aggressive optimizations"
72-
OFF
68+
MONTGOMERY_OUTPUT
69+
"Serialize Fp elements as their Montgomery representations (faster but not human-readable)"
70+
ON
7371
)
7472

73+
7574
option(
7675
WITH_PROCPS
7776
"Use procps for memory profiling"
@@ -103,8 +102,6 @@ endif()
103102

104103
if("${DEBUG}" OR "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
105104
add_definitions(-DDEBUG=1)
106-
else()
107-
add_compile_options(-O3)
108105
endif()
109106

110107

@@ -143,6 +140,15 @@ else()
143140
)
144141
endif()
145142

143+
if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
144+
# Common compilation flags and warning configuration
145+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wfatal-errors -Wno-unused-variable")
146+
if("${MULTICORE}")
147+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
148+
endif()
149+
# Default optimizations flags (to override, use -DOPT_FLAGS=...)
150+
endif()
151+
146152

147153
find_path(GMP_INCLUDE_DIR NAMES gmp.h)
148154
find_library(GMP_LIBRARY gmp)
@@ -194,6 +200,4 @@ add_library(
194200

195201
target_link_libraries(ff GMP::gmp gmpxx ${PROCPS_LIBRARIES})
196202

197-
#add_subdirectory(depends)
198203
add_subdirectory(src)
199-

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ bin/miximus_genKeys: build/Makefile
4747
build/src/libmiximus.$(DLL_EXT): build/Makefile
4848
make -C build
4949

50-
cmake-debug:
50+
cmake-debug: build
5151
cd build && cmake -DCMAKE_BUILD_TYPE=Debug ..
5252

53-
cmake-release:
53+
cmake-release: build
5454
cd build && cmake -DCMAKE_BUILD_TYPE=Release ..
5555

5656
release: cmake-release all

contracts/Verifier.sol

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ library Verifier
157157
return Verify(vk, pwi.proof, pwi.input);
158158
}
159159

160+
160161
function Verify (VerifyingKey memory vk, Proof memory proof, uint256[] memory input)
161162
internal view returns (bool)
162163
{

src/test/CMakeLists.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
add_executable(benchmark_mpz_mul benchmark_mpz_mul.cpp)
2+
target_link_libraries(benchmark_mpz_mul ff)
13

4+
add_executable(benchmark_pairing benchmark_pairing.cpp)
5+
target_link_libraries(benchmark_pairing ff)
26

37
add_executable(test_vk_raw2json test_vk_raw2json.cpp)
48
target_link_libraries(test_vk_raw2json ethsnarks_common)
59

610

7-
add_executable(test_shootout test_shootout.cpp)
8-
target_link_libraries(test_shootout ff)
9-
1011
add_executable(test_load_proofkey test_load_proofkey.cpp)
1112
target_link_libraries(test_load_proofkey ethsnarks_common)
1213

src/test/benchmark_mpz_mul.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "ethsnarks.hpp"
2+
3+
using namespace libsnark;
4+
using namespace libff;
5+
6+
int main( )
7+
{
8+
bigint<alt_bn128_r_limbs> number_a;
9+
bigint<alt_bn128_r_limbs> number_b;
10+
11+
number_a.randomize();
12+
number_b.randomize();
13+
14+
mpz_t result;
15+
mpz_init(result);
16+
17+
mpz_t number_a_mpz;
18+
mpz_init(number_a_mpz);
19+
number_a.to_mpz(number_a_mpz);
20+
21+
mpz_t number_b_mpz;
22+
mpz_init(number_b_mpz);
23+
number_b.to_mpz(number_b_mpz);
24+
25+
enter_block("Multiplying with MPZ, 1 million times");
26+
27+
for( int i = 0; i < 100000000; i++ ) {
28+
mpz_mul(result, number_a_mpz, number_b_mpz);
29+
}
30+
31+
leave_block("Multiplying with MPZ, 1 million times");
32+
33+
mpz_clear(number_a_mpz);
34+
mpz_clear(number_b_mpz);
35+
mpz_clear(result);
36+
}
File renamed without changes.

test/TestMiximus.js

+34-30
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
const TestableMiximus = artifacts.require("TestableMiximus");
22

3-
const crypto = require('crypto');
3+
const crypto = require("crypto");
44

5-
const fs = require('fs');
6-
const ffi = require('ffi');
7-
const ref = require('ref');
8-
const ArrayType = require('ref-array');
9-
const BigNumber = require('bignumber.js');
5+
const fs = require("fs");
6+
const ffi = require("ffi");
7+
const ref = require("ref");
8+
const ArrayType = require("ref-array");
9+
const BigNumber = require("bignumber.js");
1010

1111
var StringArray = ArrayType(ref.types.CString);
1212

13-
var libmiximus = ffi.Library('build/src/libmiximus', {
13+
var libmiximus = ffi.Library("build/src/libmiximus", {
1414
// Retrieve depth of tree
15-
'miximus_tree_depth': [
16-
'size_t', []
15+
"miximus_tree_depth": [
16+
"size_t", []
1717
],
1818

1919
// Create a proof for the parameters
20-
'miximus_prove': [
21-
'string', [
22-
'string', // pk_file
23-
'string', // in_root
24-
'string', // in_nullifier
25-
'string', // in_exthash
26-
'string', // in_spend_preimage
27-
'string', // in_address
20+
"miximus_prove": [
21+
"string", [
22+
"string", // pk_file
23+
"string", // in_root
24+
"string", // in_nullifier
25+
"string", // in_exthash
26+
"string", // in_spend_preimage
27+
"string", // in_address
2828
StringArray, // in_path
2929
]
3030
],
3131

3232
// Verify a proof
33-
'miximus_verify': [
34-
'bool', [
35-
'string', // vk_json
36-
'string', // proof_json
33+
"miximus_verify": [
34+
"bool", [
35+
"string", // vk_json
36+
"string", // proof_json
3737
]
3838
]
3939
});
@@ -67,15 +67,14 @@ let proof_to_flat = (proof) => {
6767
};
6868

6969

70-
7170
contract("TestableMiximus", () => {
7271
describe("Deposit", () => {
7372
it("deposits then withdraws", async () => {
7473
let obj = await TestableMiximus.deployed();
7574

7675
// Parameters for deposit
77-
let spend_preimage = new BigNumber(crypto.randomBytes(30).toString('hex'), 16);
78-
let nullifier = new BigNumber(crypto.randomBytes(30).toString('hex'), 16);
76+
let spend_preimage = new BigNumber(crypto.randomBytes(30).toString("hex"), 16);
77+
let nullifier = new BigNumber(crypto.randomBytes(30).toString("hex"), 16);
7978
let leaf_hash = await obj.MakeLeafHash.call(spend_preimage, nullifier);
8079

8180

@@ -84,9 +83,12 @@ contract("TestableMiximus", () => {
8483
await obj.Deposit.sendTransaction([leaf_hash], {value: 1000000000000000000});
8584

8685

86+
// TODO: verify amount has been transferred
87+
88+
8789
// Build parameters for proving
8890
let tmp = await obj.GetPath.call(new_root_and_offset[1]);
89-
let proof_address = tmp[1].map((_) => _ ? '1' : '0').join('');
91+
let proof_address = tmp[1].map((_) => _ ? "1" : "0").join("");
9092
let proof_path = [];
9193
for( var i = 0; i < proof_address.length; i++ ) {
9294
proof_path.push( tmp[0][i].toString(10) );
@@ -109,17 +111,16 @@ contract("TestableMiximus", () => {
109111
let proof_json = libmiximus.miximus_prove(...args);
110112
assert.notStrictEqual(proof_json, null);
111113
let proof = JSON.parse(proof_json);
112-
console.log("Proof:", proof);
113114

114115

115116
// Ensure proof inputs match ours
116-
assert.strictEqual('0x' + proof_root.toString(16), proof.input[0]);
117-
assert.strictEqual('0x' + nullifier.toString(16), proof.input[1]);
118-
assert.strictEqual('0x' + proof_exthash.toString(16), proof.input[2]);
117+
assert.strictEqual("0x" + proof_root.toString(16), proof.input[0]);
118+
assert.strictEqual("0x" + nullifier.toString(16), proof.input[1]);
119+
assert.strictEqual("0x" + proof_exthash.toString(16), proof.input[2]);
119120

120121

121122
// Re-verify proof using native library
122-
let vk_json = fs.readFileSync('zksnark_element/miximus.vk.json');
123+
let vk_json = fs.readFileSync("zksnark_element/miximus.vk.json");
123124
let proof_valid_native = libmiximus.miximus_verify(vk_json, proof_json);
124125
assert.strictEqual(proof_valid_native, true);
125126
let vk = JSON.parse(vk_json);
@@ -165,6 +166,9 @@ contract("TestableMiximus", () => {
165166
// Verify nullifier exists
166167
let is_spent = await obj.IsSpent(nullifier.toString(10));
167168
assert.strictEqual(is_spent, true);
169+
170+
171+
// TODO: verify balance has been increased
168172
});
169173
});
170174
});

test/test_merkle.py

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def test_known_2pow28(self):
5858

5959
proof_a = tree.proof(0)
6060
self.assertTrue(proof_a.verify(tree.root))
61-
print(proof_a.address)
6261

6362
proof_b = tree.proof(1)
6463
self.assertTrue(proof_b.verify(tree.root))

0 commit comments

Comments
 (0)