mirror of
https://github.com/vacp2p/gnark-rln.git
synced 2026-01-09 21:17:57 -05:00
chore: rln-v3 testing
This commit is contained in:
14
main.go
14
main.go
@@ -1,8 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/consensys/gnark-crypto/ecc"
|
||||
"github.com/consensys/gnark/backend/groth16"
|
||||
"github.com/consensys/gnark/frontend"
|
||||
@@ -48,6 +46,10 @@ func main() {
|
||||
X: frontend.Variable(rln.GetBn254X()),
|
||||
ExternalNullifier: frontend.Variable(rln.GetBn254ExternalNullifier()),
|
||||
IdentitySecret: frontend.Variable(rln.GetBn254IdentitySecret()),
|
||||
Epoch: frontend.Variable(240),
|
||||
EpochQuotient: frontend.Variable(2),
|
||||
RlnIdentifer: frontend.Variable(1),
|
||||
UserEpochLimit: frontend.Variable(120),
|
||||
MessageId: frontend.Variable(1),
|
||||
UserMessageLimit: frontend.Variable(100),
|
||||
PathElements: rln.GetBn254PathElements(),
|
||||
@@ -59,11 +61,7 @@ func main() {
|
||||
|
||||
witness, _ := frontend.NewWitness(assignment, ecc.BN254.ScalarField())
|
||||
|
||||
startTime := time.Now().UnixMilli()
|
||||
proof, err := groth16.Prove(cs, pk, witness)
|
||||
endTime := time.Now().UnixMilli()
|
||||
elapsed := endTime - startTime
|
||||
print("Proving time: ", elapsed, "ms.\n")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -73,11 +71,7 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
startTime = time.Now().UnixMilli()
|
||||
err = groth16.Verify(proof, vk, verifyWitness)
|
||||
endTime = time.Now().UnixMilli()
|
||||
elapsed = endTime - startTime
|
||||
print("Verification time: ", elapsed, "ms.\n")
|
||||
|
||||
if err != nil {
|
||||
print(err.Error())
|
||||
|
||||
31
rln/rln.go
31
rln/rln.go
@@ -8,9 +8,13 @@ import (
|
||||
type RlnCircuit struct {
|
||||
X frontend.Variable `gnark:"x, public"` // message hash
|
||||
ExternalNullifier frontend.Variable `gnark:"externalNullifier, public"` // external nullifier
|
||||
Epoch frontend.Variable `gnark:"epoch, secret"` // epoch
|
||||
RlnIdentifer frontend.Variable `gnark:"rlnIdentifier, public"` // rln identifier
|
||||
IdentitySecret frontend.Variable `gnark:"identitySecret,secret"` // identity secret
|
||||
MessageId frontend.Variable `gnark:"messageId,secret"` // message id
|
||||
UserMessageLimit frontend.Variable `gnark:"userMessageLimit,secret"` // user message limit
|
||||
UserEpochLimit frontend.Variable `gnark:"userEpochLimit,secret"` // user epoch limit
|
||||
EpochQuotient frontend.Variable `gnark:"epochQuotient,secret"` // epoch quotient
|
||||
PathElements [20]frontend.Variable `gnark:"pathElements,secret"` // path elements
|
||||
IdentityPathIndex [20]frontend.Variable `gnark:"identityPathIndex,secret"` // identity path index
|
||||
Y frontend.Variable `gnark:"y,public"`
|
||||
@@ -20,7 +24,10 @@ type RlnCircuit struct {
|
||||
|
||||
func (circuit RlnCircuit) Define(api frontend.API) error {
|
||||
identity_commitment := Poseidon(api, []frontend.Variable{circuit.IdentitySecret})
|
||||
rate_commitment := Poseidon(api, []frontend.Variable{identity_commitment, circuit.UserMessageLimit})
|
||||
rate_commitment := Poseidon(api, []frontend.Variable{identity_commitment, circuit.UserMessageLimit, circuit.UserEpochLimit})
|
||||
|
||||
external_nullifier := Poseidon(api, []frontend.Variable{circuit.Epoch, circuit.RlnIdentifer})
|
||||
api.AssertIsEqual(external_nullifier, circuit.ExternalNullifier)
|
||||
|
||||
levels := len(circuit.IdentityPathIndex)
|
||||
hashes := make([]frontend.Variable, levels+1)
|
||||
@@ -36,9 +43,31 @@ func (circuit RlnCircuit) Define(api frontend.API) error {
|
||||
api.AssertIsEqual(root, circuit.Root)
|
||||
|
||||
rangeChecker := rangecheck.New(api)
|
||||
// messageId can be max 16 bits, i.e 0..65535
|
||||
rangeChecker.Check(circuit.MessageId, 16)
|
||||
// messageId should be less than userMessageLimit
|
||||
api.AssertIsLessOrEqual(circuit.MessageId, circuit.UserMessageLimit)
|
||||
|
||||
rangeChecker = rangecheck.New(api)
|
||||
// epoch must be less than 2^64 (to avoid year 2038 problem)
|
||||
rangeChecker.Check(circuit.Epoch, 64)
|
||||
|
||||
rangeChecker = rangecheck.New(api)
|
||||
// userEpochLimit can be max 12 bits, i.e 0..4095
|
||||
rangeChecker.Check(circuit.UserEpochLimit, 12)
|
||||
// userEpochLimit should be less than 3600, in seconds is 1 hour
|
||||
api.AssertIsLessOrEqual(circuit.UserEpochLimit, 3600)
|
||||
|
||||
// we also need to ensure that the provided epoch is a multiple of the UserEpochLimit,
|
||||
// since UserEpochLimit defines the precision of the epoch,
|
||||
// i.e if UserEpochLimit is 1, then the epoch is unix timestamp in seconds,
|
||||
// if UserEpochLimit is 60, then the epoch is unix timestamp per 60 seconds, etc., i.e unix_epoch_timestamp % UserEpochLimit == 0
|
||||
// constraints here to ensure that the provided epoch is a multiple of the UserEpochLimit
|
||||
api.AssertIsLessOrEqual(circuit.UserEpochLimit, circuit.Epoch) // we need this since UserEpochLimit may be < 3600, and provided epoch also may be < 3600
|
||||
api.AssertIsLessOrEqual(circuit.EpochQuotient, circuit.UserEpochLimit) // we need this to ensure that no overflowing value is provided
|
||||
api.AssertIsEqual(circuit.Epoch, api.Mul(circuit.EpochQuotient, circuit.UserEpochLimit)) // we need this to ensure proper off-circuit computation of the epoch quotient
|
||||
// api.AssertIsEqual(circuit.EpochQuotient, api.Div(circuit.Epoch, circuit.UserEpochLimit)) // redundant
|
||||
|
||||
a1 := Poseidon(api, []frontend.Variable{circuit.IdentitySecret, circuit.ExternalNullifier, circuit.MessageId})
|
||||
y := api.Add(circuit.IdentitySecret, api.Mul(a1, circuit.X))
|
||||
api.AssertIsEqual(y, circuit.Y)
|
||||
|
||||
@@ -46,9 +46,30 @@ func TestRlnCircuit(t *testing.T) {
|
||||
assert.ProverSucceeded(&rlnCircuit, &RlnCircuit{
|
||||
X: frontend.Variable(GetBn254X()),
|
||||
ExternalNullifier: frontend.Variable(GetBn254ExternalNullifier()),
|
||||
Epoch: frontend.Variable(240),
|
||||
EpochQuotient: frontend.Variable(2),
|
||||
RlnIdentifer: frontend.Variable(1),
|
||||
IdentitySecret: frontend.Variable(GetBn254IdentitySecret()),
|
||||
MessageId: frontend.Variable(1),
|
||||
UserMessageLimit: frontend.Variable(100),
|
||||
UserEpochLimit: frontend.Variable(120), // i,e 120 seconds, therefore, 100 messages per 120 seconds
|
||||
PathElements: GetBn254PathElements(),
|
||||
IdentityPathIndex: identityPathIndex,
|
||||
Y: frontend.Variable(GetBn254Y()),
|
||||
Root: frontend.Variable(GetBn254Root()),
|
||||
Nullifier: frontend.Variable(GetBn254Nullifier()),
|
||||
}, test.WithCurves(ecc.BN254))
|
||||
|
||||
assert.ProverFailed(&rlnCircuit, &RlnCircuit{
|
||||
X: frontend.Variable(GetBn254X()),
|
||||
ExternalNullifier: frontend.Variable(GetBn254ExternalNullifier()),
|
||||
IdentitySecret: frontend.Variable(GetBn254IdentitySecret()),
|
||||
Epoch: frontend.Variable(2),
|
||||
EpochQuotient: frontend.Variable(2),
|
||||
RlnIdentifer: frontend.Variable(1),
|
||||
MessageId: frontend.Variable(1),
|
||||
UserMessageLimit: frontend.Variable(100),
|
||||
UserEpochLimit: frontend.Variable(3601), // i,e 5000 seconds, should fail since max is 3600 seconds
|
||||
PathElements: GetBn254PathElements(),
|
||||
IdentityPathIndex: identityPathIndex,
|
||||
Y: frontend.Variable(GetBn254Y()),
|
||||
@@ -60,8 +81,12 @@ func TestRlnCircuit(t *testing.T) {
|
||||
X: frontend.Variable(GetBls12_377X()),
|
||||
ExternalNullifier: frontend.Variable(GetBls12_377ExternalNullifier()),
|
||||
IdentitySecret: frontend.Variable(GetBls12_377IdentitySecret()),
|
||||
Epoch: frontend.Variable(240),
|
||||
EpochQuotient: frontend.Variable(2),
|
||||
RlnIdentifer: frontend.Variable(1),
|
||||
MessageId: frontend.Variable(1),
|
||||
UserMessageLimit: frontend.Variable(100),
|
||||
UserEpochLimit: frontend.Variable(120),
|
||||
PathElements: GetBls12_377PathElements(),
|
||||
IdentityPathIndex: identityPathIndex,
|
||||
Y: frontend.Variable(GetBls12_377Y()),
|
||||
|
||||
16
rln/utils.go
16
rln/utils.go
@@ -56,15 +56,15 @@ func Bls12_377FrFromStr(s string) frontend.Variable {
|
||||
}
|
||||
|
||||
func GetBn254Y() frontend.Variable {
|
||||
return Bn254FrFromStr("16401008481486069296141645075505218976370369489687327284155463920202585288271")
|
||||
return Bn254FrFromStr("13281175544510763016570133180267292915313601184867360581329388994656833676270")
|
||||
}
|
||||
|
||||
func GetBn254Nullifier() frontend.Variable {
|
||||
return Bn254FrFromStr("9102791780887227194595604713537772536258726662792598131262022534710887343694")
|
||||
return Bn254FrFromStr("3604513238300365880276490521570399006345172483994686070611902239780085852024")
|
||||
}
|
||||
|
||||
func GetBn254Root() frontend.Variable {
|
||||
return Bn254FrFromStr("8502402278351299594663821509741133196466235670407051417832304486953898514733")
|
||||
return Bn254FrFromStr("21563398249319305888836087797641818998119163671405970542413592217705449393790")
|
||||
}
|
||||
|
||||
func GetBn254X() frontend.Variable {
|
||||
@@ -72,7 +72,7 @@ func GetBn254X() frontend.Variable {
|
||||
}
|
||||
|
||||
func GetBn254ExternalNullifier() frontend.Variable {
|
||||
return Bn254FrFromStr("21074405743803627666274838159589343934394162804826017440941339048886754734203")
|
||||
return Bn254FrFromStr("21294470366718254810237647434032225509696124553282400225071837391660547166995")
|
||||
}
|
||||
|
||||
func GetBn254IdentitySecret() frontend.Variable {
|
||||
@@ -117,7 +117,7 @@ func GetBls12_377X() frontend.Variable {
|
||||
}
|
||||
|
||||
func GetBls12_377ExternalNullifier() frontend.Variable {
|
||||
return Bls12_377FrFromStr("21074405743803627666274838159589343934394162804826017440941339048886754734203")
|
||||
return Bls12_377FrFromStr("787360849358648252818691077338279789573234153791198374706693989458456548382")
|
||||
}
|
||||
|
||||
func GetBls12_377IdentitySecret() frontend.Variable {
|
||||
@@ -125,13 +125,13 @@ func GetBls12_377IdentitySecret() frontend.Variable {
|
||||
}
|
||||
|
||||
func GetBls12_377Root() frontend.Variable {
|
||||
return Bls12_377FrFromStr("8072112116574792760444558283100829445880964551173634213849140407066845417406")
|
||||
return Bls12_377FrFromStr("2043737669944833334761338450093986266164316463515674890635702452793674776443")
|
||||
}
|
||||
|
||||
func GetBls12_377Nullifier() frontend.Variable {
|
||||
return Bls12_377FrFromStr("876257700701563447380672578054931872982248733109675901894654371450836742275")
|
||||
return Bls12_377FrFromStr("953799074106070050729889849607836469056048832302941145260491557682862936362")
|
||||
}
|
||||
|
||||
func GetBls12_377Y() frontend.Variable {
|
||||
return Bls12_377FrFromStr("1363155474730118727426662020141552125273305162602216677592749750619552418376")
|
||||
return Bls12_377FrFromStr("3069712378300141780850231420039449576446957627826754397571099227803048451108")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user