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

Test plonk backend serialization #59

Open
wants to merge 127 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
127 commits
Select commit Hold shift + click to select a range
5361a93
Add function that builds an R1CS with a RawR1CS
ilitteri Feb 27, 2023
92c6196
Add function that fills the witnesses (private & public)
ilitteri Feb 27, 2023
215823c
Add imports
ilitteri Feb 27, 2023
63b3b6a
Update main (for testing)
ilitteri Feb 27, 2023
1fd9fdf
Remove print
ilitteri Feb 27, 2023
4dab5a0
Merge branch 'main' of github.com:lambdaclass/noir_backend_using_gnar…
ilitteri Feb 28, 2023
a1d1019
Add test functions
ilitteri Feb 28, 2023
2707733
Add hex dependency
ilitteri Feb 28, 2023
249a945
Update naming
ilitteri Feb 28, 2023
96b0029
Add `rand` as a dev-dependencie
ilitteri Feb 28, 2023
9497392
Fix parameter naming
ilitteri Feb 28, 2023
4aa40a2
Fix export name
ilitteri Feb 28, 2023
ce39435
Implement `TestFeltsSerialization`
ilitteri Feb 28, 2023
2704c9f
Implement `TestU64Serialization`
ilitteri Feb 28, 2023
ec22dfb
Expose structs and modules (for testing)
ilitteri Feb 28, 2023
e646012
Add tests for felt, felt vector and u64 serialization
ilitteri Feb 28, 2023
5f98d33
cargo fmt
ilitteri Feb 28, 2023
bf6ca3c
Merge branch 'main' of github.com:lambdaclass/noir_backend_using_gnar…
ilitteri Feb 28, 2023
879d13b
Fix imports
ilitteri Mar 1, 2023
c8c58c2
Fix comment
ilitteri Mar 1, 2023
081eb7a
Update felts serialization
ilitteri Mar 1, 2023
a0eada0
Update deps
ilitteri Mar 1, 2023
d232a64
Update Makefile
ilitteri Mar 1, 2023
47deb57
Implement `AddTerm` with JSON deserialization
ilitteri Mar 1, 2023
e35226e
Implement `MulTerm` with JSON deserialization
ilitteri Mar 1, 2023
cb25e3c
Implement `RawGate` with JSON deserialization
ilitteri Mar 1, 2023
86731d9
Implement `RawR1CS` with JSON deserialization
ilitteri Mar 1, 2023
7092853
Add `helpers` module
ilitteri Mar 1, 2023
7ed009f
Cleanup main function
ilitteri Mar 1, 2023
c32ae92
Move struct to a module
ilitteri Mar 1, 2023
2dc0f82
`PublicInputs` is no longer a BTreeSet
ilitteri Mar 1, 2023
e344e09
Merge branch 'main' of github.com:lambdaclass/noir_backend_using_gnar…
ilitteri Mar 1, 2023
2ab5d84
Implement `AddTerm` with JSON deserialization
ilitteri Mar 1, 2023
9f593ca
Implement `MulTerm` with JSON deserialization
ilitteri Mar 1, 2023
40e3a2a
Implement `RawGate` with JSON deserialization
ilitteri Mar 1, 2023
1518107
Implement `RawR1CS` with JSON deserialization
ilitteri Mar 1, 2023
4c4cde2
Add `helpers` module
ilitteri Mar 1, 2023
515d222
Cleanup main function
ilitteri Mar 1, 2023
e7c73ab
Update deps
ilitteri Mar 1, 2023
9160888
Move struct to a module
ilitteri Mar 1, 2023
3d1402a
`PublicInputs` is no longer a BTreeSet
ilitteri Mar 1, 2023
28fd066
Update function naming
ilitteri Mar 1, 2023
370a827
Improve testing using randomness
ilitteri Mar 1, 2023
675dff8
Finished testing
ilitteri Mar 1, 2023
1303328
Update Makefile
ilitteri Mar 1, 2023
2c6962c
Enabled testing for Go
ilitteri Mar 1, 2023
81110fb
Fix Makefile
ilitteri Mar 1, 2023
57448a9
Fix Makefile
ilitteri Mar 1, 2023
8722a8b
Remove old serialize terms functions
ilitteri Mar 1, 2023
5f8d92c
cargo fmt
ilitteri Mar 1, 2023
b1aa320
Add serialization integration tests
ilitteri Mar 1, 2023
a3ae1e4
Expose `acir_to_r1cs` structs
ilitteri Mar 1, 2023
3b55e07
Implement `MulTerm` and `AddTerm` structs
ilitteri Mar 1, 2023
f1d72d3
Update serialization
ilitteri Mar 1, 2023
9f9ab83
`public_inputs` is no longer a `BTreeSet`
ilitteri Mar 1, 2023
53523e9
Implement Go-side integration test functions
ilitteri Mar 1, 2023
8ab5414
cargo fmt
ilitteri Mar 1, 2023
b09c0da
Fix tests
ilitteri Mar 1, 2023
d21668c
cargo clippy
ilitteri Mar 1, 2023
c52cb88
Implement `Display` and `Copy`
ilitteri Mar 1, 2023
6857a1c
Add missing integration tests
ilitteri Mar 1, 2023
8b77618
Update `RawR1CS` fields types
ilitteri Mar 1, 2023
a40df29
Change `write!` with `writeln!` & `Display` with `Debug`
ilitteri Mar 1, 2023
ec076b5
cargo fmt
ilitteri Mar 1, 2023
1079e78
Merge branch 'go_circuit_serialization' of github.com:lambdaclass/noi…
ilitteri Mar 1, 2023
f254ce9
Merge branch 'main' of github.com:lambdaclass/noir_backend_using_gnar…
ilitteri Mar 1, 2023
76efdd9
Merge branch 'go_circuit_serialization' of github.com:lambdaclass/noi…
ilitteri Mar 1, 2023
65a2991
Fix imports
ilitteri Mar 1, 2023
52b86f5
cargo fmt
ilitteri Mar 1, 2023
8903c46
Update `extern` function names
ilitteri Mar 1, 2023
17f9645
Merge branch 'main' of github.com:lambdaclass/noir_backend_using_gnar…
ilitteri Mar 2, 2023
4a3f8be
Fix R1CS building
ilitteri Mar 2, 2023
d88c932
Update `acvm`
ilitteri Mar 2, 2023
8b61967
Automatically build go backend when compiling
ilitteri Mar 2, 2023
df6eb17
Fix `preprocess`
ilitteri Mar 2, 2023
31b8786
cargo fmt
ilitteri Mar 2, 2023
2976f05
Encode proving and verifying keys and proof
ilitteri Mar 2, 2023
78f1e6e
Add opcode resolution when preprocessing
ilitteri Mar 2, 2023
7ec68e4
Fix example raw R1CS
ilitteri Mar 2, 2023
ec0e876
Add examples
ilitteri Mar 3, 2023
2b35045
Update
ilitteri Mar 3, 2023
7ef8ccf
Fix public variables counting when building witnesses
ilitteri Mar 3, 2023
368d699
Cleanup prints
ilitteri Mar 3, 2023
4f369f1
Fix proving & verifying key serialization
ilitteri Mar 3, 2023
12f13e3
Add constraint for multiplication terms
ilitteri Mar 3, 2023
f7efa84
Cleanup prints
ilitteri Mar 3, 2023
a665713
Update values flattening function
ilitteri Mar 3, 2023
642ba6c
Update prover initial values
ilitteri Mar 3, 2023
664fbfb
Fix proving & verifying keys and proof serialization
ilitteri Mar 3, 2023
42d983d
Fix dependencies
ilitteri Mar 4, 2023
4c6b371
Improve naming
ilitteri Mar 4, 2023
c2ff2f5
Add product to terms
ilitteri Mar 4, 2023
05bf26f
Use `0` as an index for the `ONE_WIRE`
ilitteri Mar 4, 2023
dfd8f1f
Update `NbPublicVariables` sent to `witness.Fill`
ilitteri Mar 4, 2023
2cb4dc3
Replace old example with a simpler one
ilitteri Mar 4, 2023
8a0e9b1
Fix errors
ilitteri Mar 4, 2023
7b03e06
Add target for installing `nargo`
ilitteri Mar 4, 2023
e4015b0
Cleanup simple circuit example
ilitteri Mar 4, 2023
736ab9c
Fix `preprocess` random values sampling
ilitteri Mar 4, 2023
cb9a3ec
Add an integration test
ilitteri Mar 5, 2023
5e1c18a
Rename `acvm_interop` -> `backend`
ilitteri Mar 6, 2023
6ca8204
Deserialization for felts (#50)
ilitteri Mar 6, 2023
05fd0af
Abstract groth16 backend structures
ilitteri Mar 8, 2023
669c584
Abstract plonk backend structures
ilitteri Mar 8, 2023
479bc9b
Add general backend structures and helpers
ilitteri Mar 8, 2023
b2542c2
Refactor old structure
ilitteri Mar 8, 2023
5ec2b78
Refactor plonk structs using interfaces
matias-gonz Mar 8, 2023
1c4021a
Arithmetic opcode (#52)
matias-gonz Mar 8, 2023
af76c56
Directive opcode (#53)
matias-gonz Mar 8, 2023
5dbf68c
Acir unmarshal (#54)
matias-gonz Mar 8, 2023
9dc2287
Update Makefile
ilitteri Mar 8, 2023
cbdade7
Refactor backend package (#56)
ilitteri Mar 8, 2023
8192777
Add ACIR JSON example
ilitteri Mar 8, 2023
827a189
Implement buildSparseR1CS function
ilitteri Mar 8, 2023
10e557f
Import plonk_backend
ilitteri Mar 8, 2023
af74672
Fix field visibility
ilitteri Mar 8, 2023
11c0724
Plonk backend wrapper (#55)
matias-gonz Mar 9, 2023
0ce03a0
Fix `buildSparseR1CS`
ilitteri Mar 9, 2023
4133a27
Merge branch 'plonk_backend' of github.com:lambdaclass/noir_backend_u…
ilitteri Mar 9, 2023
455d19a
Fix Arithmetic unmarshal (#57)
matias-gonz Mar 9, 2023
f07a171
Remove unused print
matias-gonz Mar 9, 2023
9b8f5a8
Fix mul term condition
ilitteri Mar 9, 2023
0349e58
Add IntegrationTestCircuitSerialization
matias-gonz Mar 9, 2023
7f269f0
Add test_acir_serialization
matias-gonz Mar 9, 2023
a4492d7
Update test_acir_serialization
matias-gonz Mar 9, 2023
55f1145
Fix linter
matias-gonz Mar 9, 2023
6c5e34a
Merge branch 'plonk_backend' of github.com:lambdaclass/noir_backend_u…
matias-gonz Mar 9, 2023
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@

# These are backup files generated by rustfmt
**/*.rs.bk

# nargo (integration tests)
tests/test_programs/*/target
tests/test_programs/*/proofs
25 changes: 17 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
acvm = { git = "https://github.com/noir-lang/acvm" }
# acvm = { git = "https://github.com/noir-lang/acvm" }
acvm = "0.5"

ark-ff = "0.4.0"
ark-bls12-381 = "0.4.0"
Expand All @@ -19,6 +20,7 @@ cfg-if = "1"
hex = "0.4"
hex-literal = "0.3.4"
thiserror = "1.0"
rand = "0.8"

[profile.test]
opt-level = 3
Expand All @@ -30,5 +32,6 @@ debug-assertions = true
[features]
default = ["bn254", "groth16"]
groth16 = []
plonk = []
bn254 = ["acvm/bn254"]
bls12_381 = ["acvm/bls12_381"]
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ build-go:
# Temporary solution for testing the only tests we have. We should test recurively.
test-go:
$ cd ${FFI_LIB_PATH}; \
go test -run '' gnark_backend_ffi/structs
go test -run '' gnark_backend_ffi/backend/groth16; \
go test -run '' gnark_backend_ffi/backend/plonk

build: build-go
$ RUSTFLAGS="-L${FFI_LIB_PATH}" cargo build

test: build-go
$ RUSTFLAGS="-L${FFI_LIB_PATH}" cargo test
$ RUSTFLAGS="-L${FFI_LIB_PATH}" cargo test ${TEST} -- --nocapture

clippy:
$ cargo clippy --all-targets -- -D warnings

nargo:
$ cargo install --force --locked --git https://github.com/lambdaclass/noir --branch fork nargo

20 changes: 17 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
#[allow(clippy::all)]
fn build_go_backend() {
std::process::Command::new("make")
.arg("-C")
.arg(
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.to_str()
.unwrap(),
)
.arg("build-go")
.spawn()
.unwrap();
}

fn main() {
build_go_backend();

let path = "gnark_backend_ffi";
let lib = "gnark_backend";

println!("cargo:rustc-link-search=native={}", path);
println!("cargo:rustc-link-lib=static={}", lib);
println!("cargo:rustc-link-search=native={path}");
println!("cargo:rustc-link-lib=static={lib}");
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package structs
package backend

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package structs
package backend

import (
"encoding/json"
Expand Down
36 changes: 36 additions & 0 deletions gnark_backend_ffi/backend/groth16/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package groth16

import (
"encoding/json"
"log"
)

func UncheckedDeserializeRawGate(rawGate string) RawGate {
var r RawGate
err := json.Unmarshal([]byte(rawGate), &r)
if err != nil {
log.Fatal(err)
}

return r
}

func UncheckedDeserializeRawGates(rawGates string) []RawGate {
var r []RawGate
err := json.Unmarshal([]byte(rawGates), &r)
if err != nil {
log.Fatal(err)
}

return r
}

func UncheckedDeserializeRawR1CS(rawR1CS string) RawR1CS {
var r RawR1CS
err := json.Unmarshal([]byte(rawR1CS), &r)
if err != nil {
log.Fatal(err)
}

return r
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package structs
package groth16

import (
"encoding/json"
"gnark_backend_ffi/backend"
"log"

fr_bn254 "github.com/consensys/gnark-crypto/ecc/bn254/fr"
)

type RawGate struct {
MulTerms []MulTerm
AddTerms []AddTerm
MulTerms []backend.MulTerm
AddTerms []backend.AddTerm
ConstantTerm fr_bn254.Element
}

Expand All @@ -21,8 +22,8 @@ func (g *RawGate) UnmarshalJSON(data []byte) error {
return err
}

var mulTerms []MulTerm
var addTerms []AddTerm
var mulTerms []backend.MulTerm
var addTerms []backend.AddTerm
var constantTerm fr_bn254.Element

// Deserialize mul terms.
Expand Down Expand Up @@ -61,7 +62,7 @@ func (g *RawGate) UnmarshalJSON(data []byte) error {

// Deserialize constant term.
if encodedConstantTerm, ok := rawGateMap["constant_term"].(string); ok {
constantTerm = DeserializeFelt(encodedConstantTerm)
constantTerm = backend.DeserializeFelt(encodedConstantTerm)
} else {
log.Fatal("Error: coefficient is not a felt.")
return &json.UnmarshalTypeError{}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package structs
package groth16

import (
"encoding/json"
"fmt"
"gnark_backend_ffi/backend"
"log"
"math/rand"
"testing"
Expand All @@ -13,13 +14,13 @@ import (
// TODO: Test error cases.

func TestRawGateTermUnmarshalJSON(t *testing.T) {
encodedCoefficient, _ := SampleEncodedFelt()
encodedCoefficient, _ := backend.SampleEncodedFelt()
multiplicand := rand.Uint32()
multiplier := rand.Uint32()
sum := rand.Uint32()
mulTerms := fmt.Sprintf(`[{"coefficient":"%s","multiplicand":%d,"multiplier":%d},{"coefficient":"%s","multiplicand":%d,"multiplier":%d}]`, encodedCoefficient, multiplicand, multiplier, encodedCoefficient, multiplicand, multiplier)
addTerms := fmt.Sprintf(`[{"coefficient":"%s","sum":%d},{"coefficient":"%s","sum":%d}]`, encodedCoefficient, sum, encodedCoefficient, sum)
encodedConstantTerm, nonEncodedConstantTerm := SampleEncodedFelt()
encodedConstantTerm, nonEncodedConstantTerm := backend.SampleEncodedFelt()
rawGate := fmt.Sprintf(`{"mul_terms":%s,"add_terms":%s,"constant_term":"%s"}`, mulTerms, addTerms, encodedConstantTerm)

var r RawGate
Expand All @@ -29,19 +30,19 @@ func TestRawGateTermUnmarshalJSON(t *testing.T) {
}

assert.NoError(t, err)
assert.Equal(t, UncheckedDeserializeMulTerms(mulTerms), r.MulTerms)
assert.Equal(t, UncheckedDeserializeAddTerms(addTerms), r.AddTerms)
assert.Equal(t, backend.UncheckedDeserializeMulTerms(mulTerms), r.MulTerms)
assert.Equal(t, backend.UncheckedDeserializeAddTerms(addTerms), r.AddTerms)
assert.Equal(t, nonEncodedConstantTerm, r.ConstantTerm)
}

func TestRawGatesTermUnmarshalJSON(t *testing.T) {
encodedCoefficient, _ := SampleEncodedFelt()
encodedCoefficient, _ := backend.SampleEncodedFelt()
multiplicand := rand.Uint32()
multiplier := rand.Uint32()
sum := rand.Uint32()
mulTerms := fmt.Sprintf(`[{"coefficient":"%s","multiplicand":%d,"multiplier":%d},{"coefficient":"%s","multiplicand":%d,"multiplier":%d}]`, encodedCoefficient, multiplicand, multiplier, encodedCoefficient, multiplicand, multiplier)
addTerms := fmt.Sprintf(`[{"coefficient":"%s","sum":%d},{"coefficient":"%s","sum":%d}]`, encodedCoefficient, sum, encodedCoefficient, sum)
encodedConstantTerm, nonEncodedConstantTerm := SampleEncodedFelt()
encodedConstantTerm, nonEncodedConstantTerm := backend.SampleEncodedFelt()
rawGate := fmt.Sprintf(`{"mul_terms":%s,"add_terms":%s,"constant_term":"%s"}`, mulTerms, addTerms, encodedConstantTerm)
rawGates := fmt.Sprintf(`[%s,%s]`, rawGate, rawGate)

Expand All @@ -53,8 +54,8 @@ func TestRawGatesTermUnmarshalJSON(t *testing.T) {

assert.NoError(t, err)
for _, rawGate := range r {
assert.Equal(t, UncheckedDeserializeMulTerms(mulTerms), rawGate.MulTerms)
assert.Equal(t, UncheckedDeserializeAddTerms(addTerms), rawGate.AddTerms)
assert.Equal(t, backend.UncheckedDeserializeMulTerms(mulTerms), rawGate.MulTerms)
assert.Equal(t, backend.UncheckedDeserializeAddTerms(addTerms), rawGate.AddTerms)
assert.Equal(t, nonEncodedConstantTerm, rawGate.ConstantTerm)
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package structs
package groth16

import (
"encoding/json"
"fmt"
"gnark_backend_ffi/backend"
"log"

fr_bn254 "github.com/consensys/gnark-crypto/ecc/bn254/fr"
)

type RawR1CS struct {
Gates []RawGate
PublicInputs Witnesses
PublicInputs backend.Witnesses
Values fr_bn254.Vector
NumVariables uint64
NumConstraints uint64
Expand All @@ -25,7 +25,7 @@ func (r *RawR1CS) UnmarshalJSON(data []byte) error {
}

var gates []RawGate
var publicInputs Witnesses
var publicInputs backend.Witnesses
var values fr_bn254.Vector
var numVariables uint64
var numConstraints uint64
Expand Down Expand Up @@ -65,7 +65,7 @@ func (r *RawR1CS) UnmarshalJSON(data []byte) error {

// Deserialize values.
if encodedValues, ok := rawR1CSMap["values"].(string); ok {
values = DeserializeFelts(encodedValues)
values = backend.DeserializeFelts(encodedValues)
} else {
log.Fatal("Error: couldn't deserialize values.")
return &json.UnmarshalTypeError{}
Expand All @@ -80,7 +80,6 @@ func (r *RawR1CS) UnmarshalJSON(data []byte) error {
}

// Deserialize num_constraints.
fmt.Println("", rawR1CSMap["num_constraints"])
if numConstraintsValue, ok := rawR1CSMap["num_constraints"].(float64); ok {
numConstraints = uint64(numConstraintsValue)
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package structs
package groth16

import (
"encoding/json"
"fmt"
"gnark_backend_ffi/backend"
"math/rand"
"testing"

Expand All @@ -12,17 +13,17 @@ import (
// TODO: Test error cases.

func TestRawR1CSTermUnmarshalJSON(t *testing.T) {
encodedCoefficient, _ := SampleEncodedFelt()
encodedCoefficient, _ := backend.SampleEncodedFelt()
multiplicand := rand.Uint32()
multiplier := rand.Uint32()
sum := rand.Uint32()
mulTerms := fmt.Sprintf(`[{"coefficient":"%s","multiplicand":%d,"multiplier":%d},{"coefficient":"%s","multiplicand":%d,"multiplier":%d}]`, encodedCoefficient, multiplicand, multiplier, encodedCoefficient, multiplicand, multiplier)
addTerms := fmt.Sprintf(`[{"coefficient":"%s","sum":%d},{"coefficient":"%s","sum":%d}]`, encodedCoefficient, sum, encodedCoefficient, sum)
encodedConstantTerm, _ := SampleEncodedFelt()
encodedConstantTerm, _ := backend.SampleEncodedFelt()
rawGate := fmt.Sprintf(`{"mul_terms":%s,"add_terms":%s,"constant_term":"%s"}`, mulTerms, addTerms, encodedConstantTerm)
rawGates := fmt.Sprintf(`[%s,%s]`, rawGate, rawGate)
publicInputs := fmt.Sprintf("[%d,%d,%d]", multiplicand, multiplier, sum)
encodedValues, nonEncodedValues := SampleEncodedFelts()
encodedValues, nonEncodedValues := backend.SampleEncodedFelts()
numVariables := uint64(10)
numConstraints := uint64(10)
rawR1CS := fmt.Sprintf(`{"gates":%s,"public_inputs":%s,"values":"%s","num_variables":%d,"num_constraints":%d}`, rawGates, publicInputs, encodedValues, numVariables, numConstraints)
Expand All @@ -32,7 +33,7 @@ func TestRawR1CSTermUnmarshalJSON(t *testing.T) {

assert.NoError(t, err)
assert.Equal(t, UncheckedDeserializeRawGates(rawGates), r.Gates)
assert.Equal(t, Witnesses{multiplicand, multiplier, sum}, r.PublicInputs)
assert.Equal(t, backend.Witnesses{multiplicand, multiplier, sum}, r.PublicInputs)
assert.Equal(t, nonEncodedValues, r.Values)
assert.Equal(t, numConstraints, r.NumConstraints)
assert.Equal(t, numVariables, r.NumVariables)
Expand Down
Loading