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

feat: add support of bls24-315 curve #104

Merged
merged 6 commits into from
Jun 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ src="banner_gnark.png">
* [`gnark` User Documentation]
* [`gnark` Issues]

## Issues
## Issues

`gnark` issues are tracked [in the GitHub issues tab][`gnark` Issues].

If you have any questions, queries or comments, [GitHub discussions] is the place to find us.

You can also get in touch directly: zkteam@consensys.net
You can also get in touch directly: zkteam@consensys.net


## `gnark` Users
Expand Down Expand Up @@ -49,6 +49,7 @@ which can be instantiated with the following curves
- [x] BLS12-381
- [x] BLS12-377
- [x] BW6-761
- [x] BLS24-315

### Example

Expand Down Expand Up @@ -95,7 +96,7 @@ Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our [code of condu

## Versioning

We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/consensys/gnark/tags).
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/consensys/gnark/tags).


## License
Expand All @@ -107,4 +108,3 @@ This project is licensed under the Apache 2 License - see the [LICENSE](LICENSE)
[`gnark` User Documentation]: https://docs.gnark.consensys.net
[GitHub discussions]: https://github.com/ConsenSys/gnark/discussions
[Proving schemes and curves]: https://docs.gnark.consensys.net

8 changes: 8 additions & 0 deletions backend/groth16/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
witness_bls12377 "github.com/consensys/gnark/internal/backend/bls12-377/witness"
backend_bls12381 "github.com/consensys/gnark/internal/backend/bls12-381/cs"
witness_bls12381 "github.com/consensys/gnark/internal/backend/bls12-381/witness"
backend_bls24315 "github.com/consensys/gnark/internal/backend/bls24-315/cs"
witness_bls24315 "github.com/consensys/gnark/internal/backend/bls24-315/witness"
backend_bn254 "github.com/consensys/gnark/internal/backend/bn254/cs"
witness_bn254 "github.com/consensys/gnark/internal/backend/bn254/witness"
backend_bw6761 "github.com/consensys/gnark/internal/backend/bw6-761/cs"
Expand Down Expand Up @@ -171,6 +173,12 @@ func IsSolved(r1cs frontend.CompiledConstraintSystem, witness frontend.Circuit)
return err
}
return _r1cs.IsSolved(w)
case *backend_bls24315.R1CS:
w := witness_bls24315.Witness{}
if err := w.FromFullAssignment(witness); err != nil {
return err
}
return _r1cs.IsSolved(w)
default:
panic("unrecognized R1CS curve type")
}
Expand Down
48 changes: 48 additions & 0 deletions backend/groth16/groth16.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ import (
"github.com/consensys/gnark/frontend"
backend_bls12377 "github.com/consensys/gnark/internal/backend/bls12-377/cs"
backend_bls12381 "github.com/consensys/gnark/internal/backend/bls12-381/cs"
backend_bls24315 "github.com/consensys/gnark/internal/backend/bls24-315/cs"
backend_bn254 "github.com/consensys/gnark/internal/backend/bn254/cs"
backend_bw6761 "github.com/consensys/gnark/internal/backend/bw6-761/cs"

witness_bls12377 "github.com/consensys/gnark/internal/backend/bls12-377/witness"
witness_bls12381 "github.com/consensys/gnark/internal/backend/bls12-381/witness"
witness_bls24315 "github.com/consensys/gnark/internal/backend/bls24-315/witness"
witness_bn254 "github.com/consensys/gnark/internal/backend/bn254/witness"
witness_bw6761 "github.com/consensys/gnark/internal/backend/bw6-761/witness"

gnarkio "github.com/consensys/gnark/io"

groth16_bls12377 "github.com/consensys/gnark/internal/backend/bls12-377/groth16"
groth16_bls12381 "github.com/consensys/gnark/internal/backend/bls12-381/groth16"
groth16_bls24315 "github.com/consensys/gnark/internal/backend/bls24-315/groth16"
groth16_bn254 "github.com/consensys/gnark/internal/backend/bn254/groth16"
groth16_bw6761 "github.com/consensys/gnark/internal/backend/bw6-761/groth16"
)
Expand Down Expand Up @@ -104,6 +107,12 @@ func Verify(proof Proof, vk VerifyingKey, publicWitness frontend.Circuit) error
return err
}
return groth16_bw6761.Verify(_proof, vk.(*groth16_bw6761.VerifyingKey), w)
case *groth16_bls24315.Proof:
w := witness_bls24315.Witness{}
if err := w.FromPublicAssignment(publicWitness); err != nil {
return err
}
return groth16_bls24315.Verify(_proof, vk.(*groth16_bls24315.VerifyingKey), w)
default:
panic("unrecognized R1CS curve type")
}
Expand Down Expand Up @@ -139,6 +148,12 @@ func ReadAndVerify(proof Proof, vk VerifyingKey, publicWitness io.Reader) error
return err
}
return groth16_bw6761.Verify(proof.(*groth16_bw6761.Proof), _vk, w)
case *groth16_bls24315.VerifyingKey:
w := witness_bls24315.Witness{}
if _, err := w.LimitReadFrom(publicWitness, vk.SizePublicWitness()); err != nil {
return err
}
return groth16_bls24315.Verify(proof.(*groth16_bls24315.Proof), _vk, w)
default:
panic("unrecognized R1CS curve type")
}
Expand Down Expand Up @@ -180,6 +195,12 @@ func Prove(r1cs frontend.CompiledConstraintSystem, pk ProvingKey, witness fronte
return nil, err
}
return groth16_bw6761.Prove(_r1cs, pk.(*groth16_bw6761.ProvingKey), w, _force)
case *backend_bls24315.R1CS:
w := witness_bls24315.Witness{}
if err := w.FromFullAssignment(witness); err != nil {
return nil, err
}
return groth16_bls24315.Prove(_r1cs, pk.(*groth16_bls24315.ProvingKey), w, _force)
default:
panic("unrecognized R1CS curve type")
}
Expand Down Expand Up @@ -222,6 +243,12 @@ func ReadAndProve(r1cs frontend.CompiledConstraintSystem, pk ProvingKey, witness
return nil, err
}
return groth16_bw6761.Prove(_r1cs, pk.(*groth16_bw6761.ProvingKey), w, _force)
case *backend_bls24315.R1CS:
w := witness_bls24315.Witness{}
if _, err := w.LimitReadFrom(witness, expectedSize); err != nil {
return nil, err
}
return groth16_bls24315.Prove(_r1cs, pk.(*groth16_bls24315.ProvingKey), w, _force)
default:
panic("unrecognized R1CS curve type")
}
Expand Down Expand Up @@ -266,6 +293,13 @@ func Setup(r1cs frontend.CompiledConstraintSystem) (ProvingKey, VerifyingKey, er
return nil, nil, err
}
return &pk, &vk, nil
case *backend_bls24315.R1CS:
var pk groth16_bls24315.ProvingKey
var vk groth16_bls24315.VerifyingKey
if err := groth16_bls24315.Setup(_r1cs, &pk, &vk); err != nil {
return nil, nil, err
}
return &pk, &vk, nil
default:
panic("unrecognized R1CS curve type")
}
Expand Down Expand Up @@ -299,6 +333,12 @@ func DummySetup(r1cs frontend.CompiledConstraintSystem) (ProvingKey, error) {
return nil, err
}
return &pk, nil
case *backend_bls24315.R1CS:
var pk groth16_bls24315.ProvingKey
if err := groth16_bls24315.DummySetup(_r1cs, &pk); err != nil {
return nil, err
}
return &pk, nil
default:
panic("unrecognized R1CS curve type")
}
Expand All @@ -317,6 +357,8 @@ func NewProvingKey(curveID ecc.ID) ProvingKey {
pk = &groth16_bls12381.ProvingKey{}
case ecc.BW6_761:
pk = &groth16_bw6761.ProvingKey{}
case ecc.BLS24_315:
pk = &groth16_bls24315.ProvingKey{}
default:
panic("not implemented")
}
Expand All @@ -336,6 +378,8 @@ func NewVerifyingKey(curveID ecc.ID) VerifyingKey {
vk = &groth16_bls12381.VerifyingKey{}
case ecc.BW6_761:
vk = &groth16_bw6761.VerifyingKey{}
case ecc.BLS24_315:
vk = &groth16_bls24315.VerifyingKey{}
default:
panic("not implemented")
}
Expand All @@ -356,6 +400,8 @@ func NewProof(curveID ecc.ID) Proof {
proof = &groth16_bls12381.Proof{}
case ecc.BW6_761:
proof = &groth16_bw6761.Proof{}
case ecc.BLS24_315:
proof = &groth16_bls24315.Proof{}
default:
panic("not implemented")
}
Expand All @@ -376,6 +422,8 @@ func NewCS(curveID ecc.ID) frontend.CompiledConstraintSystem {
r1cs = &backend_bls12381.R1CS{}
case ecc.BW6_761:
r1cs = &backend_bw6761.R1CS{}
case ecc.BLS24_315:
r1cs = &backend_bls24315.R1CS{}
default:
panic("not implemented")
}
Expand Down
8 changes: 8 additions & 0 deletions backend/plonk/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import (

backend_bls12377 "github.com/consensys/gnark/internal/backend/bls12-377/cs"
backend_bls12381 "github.com/consensys/gnark/internal/backend/bls12-381/cs"
backend_bls24315 "github.com/consensys/gnark/internal/backend/bls24-315/cs"
backend_bn254 "github.com/consensys/gnark/internal/backend/bn254/cs"
backend_bw6761 "github.com/consensys/gnark/internal/backend/bw6-761/cs"

"github.com/consensys/gnark/frontend"
witness_bls12377 "github.com/consensys/gnark/internal/backend/bls12-377/witness"
witness_bls12381 "github.com/consensys/gnark/internal/backend/bls12-381/witness"
witness_bls24315 "github.com/consensys/gnark/internal/backend/bls24-315/witness"
witness_bn254 "github.com/consensys/gnark/internal/backend/bn254/witness"
witness_bw6761 "github.com/consensys/gnark/internal/backend/bw6-761/witness"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -112,6 +114,12 @@ func IsSolved(sparseR1cs frontend.CompiledConstraintSystem, witness frontend.Cir
return err
}
return _sparseR1cs.IsSolved(w)
case *backend_bls24315.SparseR1CS:
w := witness_bls24315.Witness{}
if err := w.FromFullAssignment(witness); err != nil {
return err
}
return _sparseR1cs.IsSolved(w)
default:
panic("WIP")
}
Expand Down
41 changes: 41 additions & 0 deletions backend/plonk/plonk.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,25 @@ import (

mockcommitment_bls12377 "github.com/consensys/gnark-crypto/ecc/bls12-377/fr/polynomial/mockcommitment"
mockcommitment_bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381/fr/polynomial/mockcommitment"
mockcommitment_bls24315 "github.com/consensys/gnark-crypto/ecc/bls24-315/fr/polynomial/mockcommitment"
mockcommitment_bn254 "github.com/consensys/gnark-crypto/ecc/bn254/fr/polynomial/mockcommitment"
mockcommitment_bw6761 "github.com/consensys/gnark-crypto/ecc/bw6-761/fr/polynomial/mockcommitment"

backend_bls12377 "github.com/consensys/gnark/internal/backend/bls12-377/cs"
backend_bls12381 "github.com/consensys/gnark/internal/backend/bls12-381/cs"
backend_bls24315 "github.com/consensys/gnark/internal/backend/bls24-315/cs"
backend_bn254 "github.com/consensys/gnark/internal/backend/bn254/cs"
backend_bw6761 "github.com/consensys/gnark/internal/backend/bw6-761/cs"

plonkbls12377 "github.com/consensys/gnark/internal/backend/bls12-377/plonk"
plonkbls12381 "github.com/consensys/gnark/internal/backend/bls12-381/plonk"
plonkbls24315 "github.com/consensys/gnark/internal/backend/bls24-315/plonk"
plonkbn254 "github.com/consensys/gnark/internal/backend/bn254/plonk"
plonkbw6761 "github.com/consensys/gnark/internal/backend/bw6-761/plonk"

bls12377witness "github.com/consensys/gnark/internal/backend/bls12-377/witness"
bls12381witness "github.com/consensys/gnark/internal/backend/bls12-381/witness"
bls24315witness "github.com/consensys/gnark/internal/backend/bls24-315/witness"
bn254witness "github.com/consensys/gnark/internal/backend/bn254/witness"
bw6761witness "github.com/consensys/gnark/internal/backend/bw6-761/witness"
)
Expand Down Expand Up @@ -93,6 +97,14 @@ func Setup(sparseR1cs frontend.CompiledConstraintSystem, polynomialCommitment po
publicData := plonkbw6761.SetupRaw(_sparseR1cs, polynomialCommitment, w)
return publicData, nil

case *backend_bls24315.SparseR1CS:
w := bls24315witness.Witness{}
if err := w.FromPublicAssignment(publicWitness); err != nil {
return nil, err
}
publicData := plonkbls24315.SetupRaw(_sparseR1cs, polynomialCommitment, w)
return publicData, nil

default:
panic("unrecognized R1CS curve type")
}
Expand Down Expand Up @@ -139,6 +151,15 @@ func SetupDummyCommitment(sparseR1cs frontend.CompiledConstraintSystem, publicWi
publicData := plonkbw6761.SetupRaw(_sparseR1cs, polynomialCommitment, w)
return publicData, nil

case *backend_bls24315.SparseR1CS:
w := bls24315witness.Witness{}
if err := w.FromPublicAssignment(publicWitness); err != nil {
return nil, err
}
polynomialCommitment := &mockcommitment_bls24315.Scheme{}
publicData := plonkbls24315.SetupRaw(_sparseR1cs, polynomialCommitment, w)
return publicData, nil

default:
panic("unrecognized R1CS curve type")
}
Expand Down Expand Up @@ -197,6 +218,18 @@ func Prove(sparseR1cs frontend.CompiledConstraintSystem, publicData PublicData,
}
return proof, nil

case *backend_bls24315.SparseR1CS:
_publicData := publicData.(*plonkbls24315.PublicRaw)
w := bls24315witness.Witness{}
if err := w.FromFullAssignment(fullWitness); err != nil {
return nil, err
}
proof, err := plonkbls24315.ProveRaw(_sparseR1cs, _publicData, w)
if err != nil {
return proof, err
}
return proof, nil

default:
panic("unrecognized R1CS curve type")
}
Expand Down Expand Up @@ -239,6 +272,14 @@ func Verify(proof Proof, publicData PublicData, publicWitness frontend.Circuit)
}
return plonkbw6761.VerifyRaw(_proof, _publicData, w)

case *plonkbls24315.ProofRaw:
_publicData := publicData.(*plonkbls24315.PublicRaw)
w := bls24315witness.Witness{}
if err := w.FromPublicAssignment(publicWitness); err != nil {
return err
}
return plonkbls24315.VerifyRaw(_proof, _publicData, w)

default:
panic("unrecognized proof type")
}
Expand Down
13 changes: 13 additions & 0 deletions backend/witness/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/consensys/gnark-crypto/ecc"
witness_bls12377 "github.com/consensys/gnark/internal/backend/bls12-377/witness"
witness_bls12381 "github.com/consensys/gnark/internal/backend/bls12-381/witness"
witness_bls24315 "github.com/consensys/gnark/internal/backend/bls24-315/witness"
witness_bn254 "github.com/consensys/gnark/internal/backend/bn254/witness"
witness_bw6761 "github.com/consensys/gnark/internal/backend/bw6-761/witness"

Expand Down Expand Up @@ -79,6 +80,12 @@ func WriteFullTo(w io.Writer, curveID ecc.ID, witness frontend.Circuit) (int64,
return 0, err
}
return _witness.WriteTo(w)
case ecc.BLS24_315:
_witness := &witness_bls24315.Witness{}
if err := _witness.FromFullAssignment(witness); err != nil {
return 0, err
}
return _witness.WriteTo(w)
default:
panic("not implemented")
}
Expand Down Expand Up @@ -111,6 +118,12 @@ func WritePublicTo(w io.Writer, curveID ecc.ID, publicWitness frontend.Circuit)
return 0, err
}
return _witness.WriteTo(w)
case ecc.BLS24_315:
_witness := &witness_bls24315.Witness{}
if err := _witness.FromPublicAssignment(publicWitness); err != nil {
return 0, err
}
return _witness.WriteTo(w)
default:
panic("not implemented")
}
Expand Down
1 change: 1 addition & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// - BLS12_377
// - BLS12_381
// - BW6_761
// - BLS24_315
//
// User documentation
// https://docs.gnark.consensys.net
Expand Down
3 changes: 3 additions & 0 deletions frontend/cs_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/consensys/gnark-crypto/ecc"
frbls12377 "github.com/consensys/gnark-crypto/ecc/bls12-377/fr"
frbls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
frbls24315 "github.com/consensys/gnark-crypto/ecc/bls24-315/fr"
frbn254 "github.com/consensys/gnark-crypto/ecc/bn254/fr"
frbw6761 "github.com/consensys/gnark-crypto/ecc/bw6-761/fr"
)
Expand Down Expand Up @@ -271,6 +272,8 @@ func (cs *ConstraintSystem) IsZero(a Variable, id ecc.ID) Variable {
expo.Set(frbls12377.Modulus())
case ecc.BW6_761:
expo.Set(frbw6761.Modulus())
case ecc.BLS24_315:
expo.Set(frbls24315.Modulus())
default:
panic("not implemented")
}
Expand Down
3 changes: 3 additions & 0 deletions frontend/cs_to_r1cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

bls12377r1cs "github.com/consensys/gnark/internal/backend/bls12-377/cs"
bls12381r1cs "github.com/consensys/gnark/internal/backend/bls12-381/cs"
bls24315r1cs "github.com/consensys/gnark/internal/backend/bls24-315/cs"
bn254r1cs "github.com/consensys/gnark/internal/backend/bn254/cs"
bw6761r1cs "github.com/consensys/gnark/internal/backend/bw6-761/cs"
)
Expand Down Expand Up @@ -122,6 +123,8 @@ func (cs *ConstraintSystem) toR1CS(curveID ecc.ID) (CompiledConstraintSystem, er
return bn254r1cs.NewR1CS(res, cs.coeffs), nil
case ecc.BW6_761:
return bw6761r1cs.NewR1CS(res, cs.coeffs), nil
case ecc.BLS24_315:
return bls24315r1cs.NewR1CS(res, cs.coeffs), nil
case ecc.UNKNOWN:
return &res, nil
default:
Expand Down