|
| 1 | +package prover |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/consensys/gnark-crypto/ecc" |
| 10 | + "github.com/consensys/gnark/backend/groth16" |
| 11 | + "github.com/consensys/gnark/frontend" |
| 12 | + "github.com/rs/zerolog/log" |
| 13 | +) |
| 14 | + |
| 15 | +// KeyStore manages persistent storage of compiled circuit keys. |
| 16 | +type KeyStore struct { |
| 17 | + dir string |
| 18 | +} |
| 19 | + |
| 20 | +// NewKeyStore creates a key store at the given directory. |
| 21 | +func NewKeyStore(dir string) (*KeyStore, error) { |
| 22 | + if err := os.MkdirAll(dir, 0755); err != nil { |
| 23 | + return nil, fmt.Errorf("create keystore dir: %w", err) |
| 24 | + } |
| 25 | + return &KeyStore{dir: dir}, nil |
| 26 | +} |
| 27 | + |
| 28 | +// Has returns true if keys exist for the named circuit. |
| 29 | +func (ks *KeyStore) Has(name string) bool { |
| 30 | + _, err := os.Stat(ks.pkPath(name)) |
| 31 | + return err == nil |
| 32 | +} |
| 33 | + |
| 34 | +// Save writes a compiled circuit's keys to disk. |
| 35 | +func (ks *KeyStore) Save(name string, cc *CompiledCircuit) error { |
| 36 | + // Save constraint system |
| 37 | + { |
| 38 | + f, err := os.Create(ks.csPath(name)) |
| 39 | + if err != nil { |
| 40 | + return fmt.Errorf("save cs %s: %w", name, err) |
| 41 | + } |
| 42 | + if _, err := cc.CS.WriteTo(f); err != nil { |
| 43 | + f.Close() |
| 44 | + return fmt.Errorf("write cs %s: %w", name, err) |
| 45 | + } |
| 46 | + f.Close() |
| 47 | + } |
| 48 | + |
| 49 | + // Save proving key |
| 50 | + { |
| 51 | + f, err := os.Create(ks.pkPath(name)) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("save pk %s: %w", name, err) |
| 54 | + } |
| 55 | + if _, err := cc.ProvingKey.WriteTo(f); err != nil { |
| 56 | + f.Close() |
| 57 | + return fmt.Errorf("write pk %s: %w", name, err) |
| 58 | + } |
| 59 | + f.Close() |
| 60 | + } |
| 61 | + |
| 62 | + // Save verifying key |
| 63 | + { |
| 64 | + f, err := os.Create(ks.vkPath(name)) |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("save vk %s: %w", name, err) |
| 67 | + } |
| 68 | + if _, err := cc.VerifyingKey.WriteTo(f); err != nil { |
| 69 | + f.Close() |
| 70 | + return fmt.Errorf("write vk %s: %w", name, err) |
| 71 | + } |
| 72 | + f.Close() |
| 73 | + } |
| 74 | + |
| 75 | + log.Debug().Str("circuit", name).Str("dir", ks.dir).Msg("Keys saved") |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// Load reads a compiled circuit's keys from disk. |
| 80 | +func (ks *KeyStore) Load(name string) (*CompiledCircuit, error) { |
| 81 | + cs := groth16.NewCS(ecc.BN254) |
| 82 | + { |
| 83 | + f, err := os.Open(ks.csPath(name)) |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("load cs %s: %w", name, err) |
| 86 | + } |
| 87 | + if _, err := cs.ReadFrom(f); err != nil { |
| 88 | + f.Close() |
| 89 | + return nil, fmt.Errorf("read cs %s: %w", name, err) |
| 90 | + } |
| 91 | + f.Close() |
| 92 | + } |
| 93 | + |
| 94 | + pk := groth16.NewProvingKey(ecc.BN254) |
| 95 | + { |
| 96 | + f, err := os.Open(ks.pkPath(name)) |
| 97 | + if err != nil { |
| 98 | + return nil, fmt.Errorf("load pk %s: %w", name, err) |
| 99 | + } |
| 100 | + if _, err := pk.ReadFrom(f); err != nil { |
| 101 | + f.Close() |
| 102 | + return nil, fmt.Errorf("read pk %s: %w", name, err) |
| 103 | + } |
| 104 | + f.Close() |
| 105 | + } |
| 106 | + |
| 107 | + vk := groth16.NewVerifyingKey(ecc.BN254) |
| 108 | + { |
| 109 | + f, err := os.Open(ks.vkPath(name)) |
| 110 | + if err != nil { |
| 111 | + return nil, fmt.Errorf("load vk %s: %w", name, err) |
| 112 | + } |
| 113 | + if _, err := vk.ReadFrom(f); err != nil { |
| 114 | + f.Close() |
| 115 | + return nil, fmt.Errorf("read vk %s: %w", name, err) |
| 116 | + } |
| 117 | + f.Close() |
| 118 | + } |
| 119 | + |
| 120 | + return &CompiledCircuit{ |
| 121 | + Name: name, |
| 122 | + CS: cs, |
| 123 | + ProvingKey: pk, |
| 124 | + VerifyingKey: vk, |
| 125 | + Constraints: cs.GetNbConstraints(), |
| 126 | + PublicVars: cs.GetNbPublicVariables(), |
| 127 | + PrivateVars: cs.GetNbSecretVariables(), |
| 128 | + }, nil |
| 129 | +} |
| 130 | + |
| 131 | +// CompileAndSave compiles a circuit and persists the keys. |
| 132 | +// If keys already exist on disk, loads them instead (skipping compilation). |
| 133 | +func (ks *KeyStore) CompileAndSave(p *Prover, name string, circuit frontend.Circuit) (*CompiledCircuit, error) { |
| 134 | + if ks.Has(name) { |
| 135 | + log.Debug().Str("circuit", name).Msg("Loading cached keys") |
| 136 | + return ks.Load(name) |
| 137 | + } |
| 138 | + |
| 139 | + log.Debug().Str("circuit", name).Msg("Compiling circuit (no cached keys)") |
| 140 | + cc, err := p.CompileCircuit(name, circuit) |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + |
| 145 | + if err := ks.Save(name, cc); err != nil { |
| 146 | + log.Warn().Err(err).Str("circuit", name).Msg("Failed to save keys (will recompile next time)") |
| 147 | + } |
| 148 | + |
| 149 | + return cc, nil |
| 150 | +} |
| 151 | + |
| 152 | +// ExportVerifyingKey returns the raw bytes of a verifying key. |
| 153 | +func (ks *KeyStore) ExportVerifyingKey(name string) ([]byte, error) { |
| 154 | + return os.ReadFile(ks.vkPath(name)) |
| 155 | +} |
| 156 | + |
| 157 | +// ExportSolidityVerifier generates a Solidity verifier contract for a circuit. |
| 158 | +func (ks *KeyStore) ExportSolidityVerifier(name string) ([]byte, error) { |
| 159 | + vk := groth16.NewVerifyingKey(ecc.BN254) |
| 160 | + f, err := os.Open(ks.vkPath(name)) |
| 161 | + if err != nil { |
| 162 | + return nil, fmt.Errorf("load vk %s: %w", name, err) |
| 163 | + } |
| 164 | + if _, err := vk.ReadFrom(f); err != nil { |
| 165 | + f.Close() |
| 166 | + return nil, fmt.Errorf("read vk %s: %w", name, err) |
| 167 | + } |
| 168 | + f.Close() |
| 169 | + |
| 170 | + var buf bytes.Buffer |
| 171 | + if err := vk.ExportSolidity(&buf); err != nil { |
| 172 | + return nil, fmt.Errorf("export solidity verifier %s: %w", name, err) |
| 173 | + } |
| 174 | + return buf.Bytes(), nil |
| 175 | +} |
| 176 | + |
| 177 | +// Purge removes all cached keys for a circuit (forces recompilation). |
| 178 | +func (ks *KeyStore) Purge(name string) error { |
| 179 | + for _, path := range []string{ks.csPath(name), ks.pkPath(name), ks.vkPath(name)} { |
| 180 | + os.Remove(path) |
| 181 | + } |
| 182 | + log.Debug().Str("circuit", name).Msg("Keys purged") |
| 183 | + return nil |
| 184 | +} |
| 185 | + |
| 186 | +// PurgeAll removes all cached keys. |
| 187 | +func (ks *KeyStore) PurgeAll() error { |
| 188 | + entries, err := os.ReadDir(ks.dir) |
| 189 | + if err != nil { |
| 190 | + return err |
| 191 | + } |
| 192 | + for _, e := range entries { |
| 193 | + os.Remove(filepath.Join(ks.dir, e.Name())) |
| 194 | + } |
| 195 | + log.Debug().Str("dir", ks.dir).Msg("All keys purged") |
| 196 | + return nil |
| 197 | +} |
| 198 | + |
| 199 | +// RegisterWithKeyStore compiles circuits using the key store for caching. |
| 200 | +func RegisterWithKeyStore(p *Prover, ks *KeyStore, circuits map[string]frontend.Circuit) error { |
| 201 | + for name, circuit := range circuits { |
| 202 | + cc, err := ks.CompileAndSave(p, name, circuit) |
| 203 | + if err != nil { |
| 204 | + return fmt.Errorf("circuit %s: %w", name, err) |
| 205 | + } |
| 206 | + p.StoreCircuit(name, cc) |
| 207 | + } |
| 208 | + return nil |
| 209 | +} |
| 210 | + |
| 211 | +// Path helpers |
| 212 | + |
| 213 | +func (ks *KeyStore) csPath(name string) string { |
| 214 | + return filepath.Join(ks.dir, name+".cs") |
| 215 | +} |
| 216 | + |
| 217 | +func (ks *KeyStore) pkPath(name string) string { |
| 218 | + return filepath.Join(ks.dir, name+".pk") |
| 219 | +} |
| 220 | + |
| 221 | +func (ks *KeyStore) vkPath(name string) string { |
| 222 | + return filepath.Join(ks.dir, name+".vk") |
| 223 | +} |
0 commit comments