diff --git a/main.go b/main.go index e89d641..c8bf31d 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "time" "github.com/consensys/gnark-crypto/ecc" - "github.com/consensys/gnark-crypto/ecc/bn254/fr" "github.com/consensys/gnark/backend/groth16" "github.com/consensys/gnark/frontend" "github.com/consensys/gnark/frontend/cs/r1cs" @@ -35,39 +34,6 @@ func main() { 0, } - rawPathElements := [20]string{ - "14082964758224722211945379872337797638951236517417253447686770846170014042825", - "6628418579821163687428454604867534487917867918886059133241840211975892987309", - "12745863228198753394445659605634840709296716381893463421165313830643281758511", - "56118267389743063830320351452083247040583061493621478539311100137113963555", - "3648731943306935051357703221473866306053186513730785325303257057776816073765", - "10548621390442503192989374711060717107954536293658152583621924810330521179016", - "11741160669079729961275351458682156164905457324981803454515784688429276743441", - "17165464309215350864730477596846156251863702878546777829650812432906796008534", - "18947162586829418653666557598416458949428989734998924978331450666032720066913", - "8809427088917589399897132358419395928548406347152047718919154153577297139202", - "6261460226929242970747566981077801929281729646713842579109271945192964422300", - "13871468675790284383809887052382100311103716176061564908030808887079542722597", - "10413964486611723004584705484327518190402370933255450052832412709168190985805", - "3978387560092078849178760154060822400741873818692524912249877867958842934383", - "14014915591348694328771517896715085647041518432952027841088176673715002508448", - "17680675606519345547327984724173632294904524423937145835611954334756161077843", - "17107175244885276119916848057745382329169223109661217238296871427531065458152", - "18326186549441826262593357123467931475982067066825042001499291800252145875109", - "7043961192177345916232559778383741091053414803377017307095275172896944935996", - "2807630271073553218355393059254209097448243975722083008310815929736065268921", - } - - pathElements := [20]frontend.Variable{} - // iterate over pathElements and replace with fr.Modulus().SetString("...") for eac, 10h element - for i := 0; i < len(pathElements); i++ { - x, ret := fr.Modulus().SetString(rawPathElements[i], 10) - if !ret { - panic(ret) - } - pathElements[i] = frontend.Variable(x) - } - cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &rln.RlnCircuit{}) if err != nil { panic(err) @@ -78,47 +44,17 @@ func main() { panic(err) } - x, ret := fr.Modulus().SetString("20645213238265527935869146898028115621427162613172918400241870500502509785943", 10) - if !ret { - panic(ret) - } - - external_nullifier, ret := fr.Modulus().SetString("21074405743803627666274838159589343934394162804826017440941339048886754734203", 10) - if !ret { - panic(ret) - } - - identity_secret, ret := fr.Modulus().SetString("2301650865650889795878889082892690584512243988708213561328369865554257051708", 10) - if !ret { - panic(ret) - } - - y, ret := fr.Modulus().SetString("16401008481486069296141645075505218976370369489687327284155463920202585288271", 10) - if !ret { - panic(ret) - } - - nullifier, ret := fr.Modulus().SetString("9102791780887227194595604713537772536258726662792598131262022534710887343694", 10) - if !ret { - panic(ret) - } - - root, ret := fr.Modulus().SetString("8502402278351299594663821509741133196466235670407051417832304486953898514733", 10) - if !ret { - panic(ret) - } - assignment := &rln.RlnCircuit{ - X: frontend.Variable(x), - ExternalNullifier: frontend.Variable(external_nullifier), - IdentitySecret: frontend.Variable(identity_secret), + X: frontend.Variable(rln.GetBn254X()), + ExternalNullifier: frontend.Variable(rln.GetBn254ExternalNullifier()), + IdentitySecret: frontend.Variable(rln.GetBn254IdentitySecret()), MessageId: frontend.Variable(1), UserMessageLimit: frontend.Variable(100), - PathElements: pathElements, + PathElements: rln.GetBn254PathElements(), IdentityPathIndex: identityPathIndex, - Y: frontend.Variable(y), - Root: frontend.Variable(root), - Nullifier: frontend.Variable(nullifier), + Y: frontend.Variable(rln.GetBn254Y()), + Root: frontend.Variable(rln.GetBn254Root()), + Nullifier: frontend.Variable(rln.GetBn254Nullifier()), } witness, _ := frontend.NewWitness(assignment, ecc.BN254.ScalarField()) diff --git a/rln/rln_test.go b/rln/rln_test.go index 221dfc0..7440d19 100644 --- a/rln/rln_test.go +++ b/rln/rln_test.go @@ -13,7 +13,7 @@ import ( // TODO: get kats for other curves -func getBn254PathElements() [20]frontend.Variable { +func GetBn254PathElements() [20]frontend.Variable { rawPathElements := [20]string{ "14082964758224722211945379872337797638951236517417253447686770846170014042825", "6628418579821163687428454604867534487917867918886059133241840211975892987309", @@ -40,13 +40,13 @@ func getBn254PathElements() [20]frontend.Variable { pathElements := [20]frontend.Variable{} // iterate over pathElements and replace with fr.Modulus().SetString("...") for eac, 10h element for i := 0; i < len(pathElements); i++ { - pathElements[i] = bn254FrFromStr(rawPathElements[i]) + pathElements[i] = Bn254FrFromStr(rawPathElements[i]) } return pathElements } -func bn254FrFromStr(s string) frontend.Variable { +func Bn254FrFromStr(s string) frontend.Variable { x, ret := bn254Fr.Modulus().SetString(s, 10) if !ret { panic("failed to parse fr from string") @@ -54,7 +54,7 @@ func bn254FrFromStr(s string) frontend.Variable { return frontend.Variable(x) } -func bls12_377FrFromStr(s string) frontend.Variable { +func Bls12_377FrFromStr(s string) frontend.Variable { x, ret := bls12_377Fr.Modulus().SetString(s, 10) if !ret { panic("failed to parse fr from string") @@ -62,31 +62,31 @@ func bls12_377FrFromStr(s string) frontend.Variable { return frontend.Variable(x) } -func getBn254Y() frontend.Variable { - return bn254FrFromStr("16401008481486069296141645075505218976370369489687327284155463920202585288271") +func GetBn254Y() frontend.Variable { + return Bn254FrFromStr("16401008481486069296141645075505218976370369489687327284155463920202585288271") } -func getBn254Nullifier() frontend.Variable { - return bn254FrFromStr("9102791780887227194595604713537772536258726662792598131262022534710887343694") +func GetBn254Nullifier() frontend.Variable { + return Bn254FrFromStr("9102791780887227194595604713537772536258726662792598131262022534710887343694") } -func getBn254Root() frontend.Variable { - return bn254FrFromStr("8502402278351299594663821509741133196466235670407051417832304486953898514733") +func GetBn254Root() frontend.Variable { + return Bn254FrFromStr("8502402278351299594663821509741133196466235670407051417832304486953898514733") } -func getBn254X() frontend.Variable { - return bn254FrFromStr("20645213238265527935869146898028115621427162613172918400241870500502509785943") +func GetBn254X() frontend.Variable { + return Bn254FrFromStr("20645213238265527935869146898028115621427162613172918400241870500502509785943") } -func getBn254ExternalNullifier() frontend.Variable { - return bn254FrFromStr("21074405743803627666274838159589343934394162804826017440941339048886754734203") +func GetBn254ExternalNullifier() frontend.Variable { + return Bn254FrFromStr("21074405743803627666274838159589343934394162804826017440941339048886754734203") } -func getBn254IdentitySecret() frontend.Variable { - return bn254FrFromStr("2301650865650889795878889082892690584512243988708213561328369865554257051708") +func GetBn254IdentitySecret() frontend.Variable { + return Bn254FrFromStr("2301650865650889795878889082892690584512243988708213561328369865554257051708") } -func getBls12_377PathElements() [20]frontend.Variable { +func GetBls12_377PathElements() [20]frontend.Variable { rawPathElements := [20]string{ "14082964758224722211945379872337797638951236517417253447686770846170014042825", "6628418579821163687428454604867534487917867918886059133241840211975892987309", @@ -113,34 +113,34 @@ func getBls12_377PathElements() [20]frontend.Variable { pathElements := [20]frontend.Variable{} // iterate over pathElements and replace with fr.Modulus().SetString("...") for eac, 10h element for i := 0; i < len(pathElements); i++ { - pathElements[i] = bls12_377FrFromStr(rawPathElements[i]) + pathElements[i] = Bls12_377FrFromStr(rawPathElements[i]) } return pathElements } -func getBls12_377X() frontend.Variable { - return bls12_377FrFromStr("20645213238265527935869146898028115621427162613172918400241870500502509785943") +func GetBls12_377X() frontend.Variable { + return Bls12_377FrFromStr("20645213238265527935869146898028115621427162613172918400241870500502509785943") } -func getBls12_377ExternalNullifier() frontend.Variable { - return bls12_377FrFromStr("21074405743803627666274838159589343934394162804826017440941339048886754734203") +func GetBls12_377ExternalNullifier() frontend.Variable { + return Bls12_377FrFromStr("21074405743803627666274838159589343934394162804826017440941339048886754734203") } -func getBls12_377IdentitySecret() frontend.Variable { - return bls12_377FrFromStr("2301650865650889795878889082892690584512243988708213561328369865554257051708") +func GetBls12_377IdentitySecret() frontend.Variable { + return Bls12_377FrFromStr("2301650865650889795878889082892690584512243988708213561328369865554257051708") } -func getBls12_377Root() frontend.Variable { - return bls12_377FrFromStr("8072112116574792760444558283100829445880964551173634213849140407066845417406") +func GetBls12_377Root() frontend.Variable { + return Bls12_377FrFromStr("8072112116574792760444558283100829445880964551173634213849140407066845417406") } -func getBls12_377Nullifier() frontend.Variable { - return bls12_377FrFromStr("876257700701563447380672578054931872982248733109675901894654371450836742275") +func GetBls12_377Nullifier() frontend.Variable { + return Bls12_377FrFromStr("876257700701563447380672578054931872982248733109675901894654371450836742275") } -func getBls12_377Y() frontend.Variable { - return bls12_377FrFromStr("1363155474730118727426662020141552125273305162602216677592749750619552418376") +func GetBls12_377Y() frontend.Variable { + return Bls12_377FrFromStr("1363155474730118727426662020141552125273305162602216677592749750619552418376") } func TestRlnCircuit(t *testing.T) { @@ -175,29 +175,29 @@ func TestRlnCircuit(t *testing.T) { switch curve { case ecc.BN254: assert.ProverSucceeded(&rlnCircuit, &RlnCircuit{ - X: frontend.Variable(getBn254X()), - ExternalNullifier: frontend.Variable(getBn254ExternalNullifier()), - IdentitySecret: frontend.Variable(getBn254IdentitySecret()), + X: frontend.Variable(GetBn254X()), + ExternalNullifier: frontend.Variable(GetBn254ExternalNullifier()), + IdentitySecret: frontend.Variable(GetBn254IdentitySecret()), MessageId: frontend.Variable(1), UserMessageLimit: frontend.Variable(100), - PathElements: getBn254PathElements(), + PathElements: GetBn254PathElements(), IdentityPathIndex: identityPathIndex, - Y: frontend.Variable(getBn254Y()), - Root: frontend.Variable(getBn254Root()), - Nullifier: frontend.Variable(getBn254Nullifier()), + Y: frontend.Variable(GetBn254Y()), + Root: frontend.Variable(GetBn254Root()), + Nullifier: frontend.Variable(GetBn254Nullifier()), }, test.WithCurves(ecc.BN254)) case ecc.BLS12_377: assert.ProverSucceeded(&rlnCircuit, &RlnCircuit{ - X: frontend.Variable(getBls12_377X()), - ExternalNullifier: frontend.Variable(getBls12_377ExternalNullifier()), - IdentitySecret: frontend.Variable(getBls12_377IdentitySecret()), + X: frontend.Variable(GetBls12_377X()), + ExternalNullifier: frontend.Variable(GetBls12_377ExternalNullifier()), + IdentitySecret: frontend.Variable(GetBls12_377IdentitySecret()), MessageId: frontend.Variable(1), UserMessageLimit: frontend.Variable(100), - PathElements: getBls12_377PathElements(), + PathElements: GetBls12_377PathElements(), IdentityPathIndex: identityPathIndex, - Y: frontend.Variable(getBls12_377Y()), - Root: frontend.Variable(getBls12_377Root()), - Nullifier: frontend.Variable(getBls12_377Nullifier()), + Y: frontend.Variable(GetBls12_377Y()), + Root: frontend.Variable(GetBls12_377Root()), + Nullifier: frontend.Variable(GetBls12_377Nullifier()), }, test.WithCurves(ecc.BLS12_377)) default: continue diff --git a/rln/utils.go b/rln/utils.go new file mode 100644 index 0000000..3422e9d --- /dev/null +++ b/rln/utils.go @@ -0,0 +1,137 @@ +package rln + +import ( + bls12_377Fr "github.com/consensys/gnark-crypto/ecc/bls12-377/fr" + bn254Fr "github.com/consensys/gnark-crypto/ecc/bn254/fr" + "github.com/consensys/gnark/frontend" +) + +func GetBn254PathElements() [20]frontend.Variable { + rawPathElements := [20]string{ + "14082964758224722211945379872337797638951236517417253447686770846170014042825", + "6628418579821163687428454604867534487917867918886059133241840211975892987309", + "12745863228198753394445659605634840709296716381893463421165313830643281758511", + "56118267389743063830320351452083247040583061493621478539311100137113963555", + "3648731943306935051357703221473866306053186513730785325303257057776816073765", + "10548621390442503192989374711060717107954536293658152583621924810330521179016", + "11741160669079729961275351458682156164905457324981803454515784688429276743441", + "17165464309215350864730477596846156251863702878546777829650812432906796008534", + "18947162586829418653666557598416458949428989734998924978331450666032720066913", + "8809427088917589399897132358419395928548406347152047718919154153577297139202", + "6261460226929242970747566981077801929281729646713842579109271945192964422300", + "13871468675790284383809887052382100311103716176061564908030808887079542722597", + "10413964486611723004584705484327518190402370933255450052832412709168190985805", + "3978387560092078849178760154060822400741873818692524912249877867958842934383", + "14014915591348694328771517896715085647041518432952027841088176673715002508448", + "17680675606519345547327984724173632294904524423937145835611954334756161077843", + "17107175244885276119916848057745382329169223109661217238296871427531065458152", + "18326186549441826262593357123467931475982067066825042001499291800252145875109", + "7043961192177345916232559778383741091053414803377017307095275172896944935996", + "2807630271073553218355393059254209097448243975722083008310815929736065268921", + } + + pathElements := [20]frontend.Variable{} + // iterate over pathElements and replace with fr.Modulus().SetString("...") for eac, 10h element + for i := 0; i < len(pathElements); i++ { + pathElements[i] = Bn254FrFromStr(rawPathElements[i]) + } + + return pathElements +} + +func Bn254FrFromStr(s string) frontend.Variable { + x, ret := bn254Fr.Modulus().SetString(s, 10) + if !ret { + panic("failed to parse fr from string") + } + return frontend.Variable(x) +} + +func Bls12_377FrFromStr(s string) frontend.Variable { + x, ret := bls12_377Fr.Modulus().SetString(s, 10) + if !ret { + panic("failed to parse fr from string") + } + return frontend.Variable(x) +} + +func GetBn254Y() frontend.Variable { + return Bn254FrFromStr("16401008481486069296141645075505218976370369489687327284155463920202585288271") +} + +func GetBn254Nullifier() frontend.Variable { + return Bn254FrFromStr("9102791780887227194595604713537772536258726662792598131262022534710887343694") +} + +func GetBn254Root() frontend.Variable { + return Bn254FrFromStr("8502402278351299594663821509741133196466235670407051417832304486953898514733") +} + +func GetBn254X() frontend.Variable { + return Bn254FrFromStr("20645213238265527935869146898028115621427162613172918400241870500502509785943") +} + +func GetBn254ExternalNullifier() frontend.Variable { + return Bn254FrFromStr("21074405743803627666274838159589343934394162804826017440941339048886754734203") +} + +func GetBn254IdentitySecret() frontend.Variable { + return Bn254FrFromStr("2301650865650889795878889082892690584512243988708213561328369865554257051708") +} + +func GetBls12_377PathElements() [20]frontend.Variable { + rawPathElements := [20]string{ + "14082964758224722211945379872337797638951236517417253447686770846170014042825", + "6628418579821163687428454604867534487917867918886059133241840211975892987309", + "12745863228198753394445659605634840709296716381893463421165313830643281758511", + "56118267389743063830320351452083247040583061493621478539311100137113963555", + "3648731943306935051357703221473866306053186513730785325303257057776816073765", + "10548621390442503192989374711060717107954536293658152583621924810330521179016", + "11741160669079729961275351458682156164905457324981803454515784688429276743441", + "17165464309215350864730477596846156251863702878546777829650812432906796008534", + "18947162586829418653666557598416458949428989734998924978331450666032720066913", + "8809427088917589399897132358419395928548406347152047718919154153577297139202", + "6261460226929242970747566981077801929281729646713842579109271945192964422300", + "13871468675790284383809887052382100311103716176061564908030808887079542722597", + "10413964486611723004584705484327518190402370933255450052832412709168190985805", + "3978387560092078849178760154060822400741873818692524912249877867958842934383", + "14014915591348694328771517896715085647041518432952027841088176673715002508448", + "17680675606519345547327984724173632294904524423937145835611954334756161077843", + "17107175244885276119916848057745382329169223109661217238296871427531065458152", + "18326186549441826262593357123467931475982067066825042001499291800252145875109", + "7043961192177345916232559778383741091053414803377017307095275172896944935996", + "2807630271073553218355393059254209097448243975722083008310815929736065268921", + } + + pathElements := [20]frontend.Variable{} + // iterate over pathElements and replace with fr.Modulus().SetString("...") for eac, 10h element + for i := 0; i < len(pathElements); i++ { + pathElements[i] = Bls12_377FrFromStr(rawPathElements[i]) + } + + return pathElements +} + +func GetBls12_377X() frontend.Variable { + return Bls12_377FrFromStr("20645213238265527935869146898028115621427162613172918400241870500502509785943") +} + +func GetBls12_377ExternalNullifier() frontend.Variable { + return Bls12_377FrFromStr("21074405743803627666274838159589343934394162804826017440941339048886754734203") +} + +func GetBls12_377IdentitySecret() frontend.Variable { + return Bls12_377FrFromStr("2301650865650889795878889082892690584512243988708213561328369865554257051708") +} + +func GetBls12_377Root() frontend.Variable { + return Bls12_377FrFromStr("8072112116574792760444558283100829445880964551173634213849140407066845417406") +} + +func GetBls12_377Nullifier() frontend.Variable { + return Bls12_377FrFromStr("876257700701563447380672578054931872982248733109675901894654371450836742275") +} + +func GetBls12_377Y() frontend.Variable { + return Bls12_377FrFromStr("1363155474730118727426662020141552125273305162602216677592749750619552418376") +}