diff --git a/rln/rln_test.go b/rln/rln_test.go index 19e6376..221dfc0 100644 --- a/rln/rln_test.go +++ b/rln/rln_test.go @@ -3,44 +3,205 @@ package rln import ( "testing" + "github.com/consensys/gnark-crypto/ecc" + 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" + "github.com/consensys/gnark/test" ) // TODO: get kats for other curves +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") +} + func TestRlnCircuit(t *testing.T) { assert := test.NewAssert(t) var rlnCircuit RlnCircuit + identityPathIndex := [20]frontend.Variable{ + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + } - var identityPathIndex [20]frontend.Variable - for i := 0; i < 20; i++ { - var direction frontend.Variable - if i%2 == 0 { - direction = frontend.Variable(1) - } else { - direction = frontend.Variable(0) + curves := ecc.Implemented() + for _, curve := range curves { + switch curve { + case ecc.BN254: + assert.ProverSucceeded(&rlnCircuit, &RlnCircuit{ + X: frontend.Variable(getBn254X()), + ExternalNullifier: frontend.Variable(getBn254ExternalNullifier()), + IdentitySecret: frontend.Variable(getBn254IdentitySecret()), + MessageId: frontend.Variable(1), + UserMessageLimit: frontend.Variable(100), + PathElements: getBn254PathElements(), + IdentityPathIndex: identityPathIndex, + 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()), + MessageId: frontend.Variable(1), + UserMessageLimit: frontend.Variable(100), + PathElements: getBls12_377PathElements(), + IdentityPathIndex: identityPathIndex, + Y: frontend.Variable(getBls12_377Y()), + Root: frontend.Variable(getBls12_377Root()), + Nullifier: frontend.Variable(getBls12_377Nullifier()), + }, test.WithCurves(ecc.BLS12_377)) + default: + continue } - identityPathIndex[i] = direction } - var pathElements [20]frontend.Variable - for i := 0; i < 20; i++ { - pathElements[i] = frontend.Variable(10) - } - - assert.ProverSucceeded(&rlnCircuit, &RlnCircuit{ - X: frontend.Variable(10), - ExternalNullifier: frontend.Variable(10), - IdentitySecret: frontend.Variable(10), - MessageId: frontend.Variable(10), - UserMessageLimit: frontend.Variable(20), - PathElements: pathElements, - IdentityPathIndex: identityPathIndex, - Y: frontend.Variable(0), - Root: frontend.Variable(0), - Nullifier: frontend.Variable(0), - }) - }