@ -3,10 +3,10 @@ import { createClient } from '@remixproject/plugin-webview'
import EventManager from 'events'
import EventManager from 'events'
import pathModule from 'path'
import pathModule from 'path'
import { compiler_list } from 'circom_wasm'
import { compiler_list } from 'circom_wasm'
import compilerV216 from 'circom_wasm/v2.1.6'
import * as compilerV216 from 'circom_wasm/v2.1.6'
import compilerV215 from 'circom_wasm/v2.1.5'
import * as compilerV215 from 'circom_wasm/v2.1.5'
import { extractNameFromKey , extractParentFromKey } from '@remix-ui/helper'
import { extractNameFromKey , extractParentFromKey } from '@remix-ui/helper'
import { CompilationConfig , CompilerReport , ResolverOutput } from '../types'
import { CompilationConfig , CompilerReport , PrimeValue , ResolverOutput } from '../types'
export class CircomPluginClient extends PluginClient {
export class CircomPluginClient extends PluginClient {
public internalEvents : EventManager
public internalEvents : EventManager
@ -17,6 +17,7 @@ export class CircomPluginClient extends PluginClient {
private lastCompiledCircuitPath : string = ''
private lastCompiledCircuitPath : string = ''
private lastParsedFiles : Record < string , string > = { }
private lastParsedFiles : Record < string , string > = { }
private lastCompiledFile : string = ''
private lastCompiledFile : string = ''
private compiler : typeof compilerV215 | typeof compilerV216 = compilerV216
constructor ( ) {
constructor ( ) {
super ( )
super ( )
@ -34,14 +35,26 @@ export class CircomPluginClient extends PluginClient {
this . internalEvents . emit ( 'circom_activated' )
this . internalEvents . emit ( 'circom_activated' )
}
}
set compilerVersion ( version : string ) {
if ( ! compiler_list . versions . includes ( version ) ) throw new Error ( "Unsupported compiler version" )
this . _compilationConfig . version = version
if ( version === '2.1.5' ) this . compiler = compilerV215
else if ( version === '2.1.6' ) this . compiler = compilerV216
}
set compilerPrime ( prime : PrimeValue ) {
if ( ( prime !== "bn128" ) && ( prime !== "bls12381" ) && ( prime !== "goldilocks" ) && ( this . _compilationConfig . version === '2.1.5' ) ) throw new Error ( 'Invalid prime value' )
if ( ( prime !== "bn128" ) && ( prime !== "bls12381" ) && ( prime !== "goldilocks" ) && ( prime !== "grumpkin" ) && ( prime !== "pallas" ) && ( prime !== "vesta" ) && ( this . _compilationConfig . version === '2.1.6' ) ) throw new Error ( 'Invalid prime value' )
this . _compilationConfig . prime = prime
}
async parse ( path : string , fileContent? : string ) : Promise < [ CompilerReport [ ] , Record < string , string > ] > {
async parse ( path : string , fileContent? : string ) : Promise < [ CompilerReport [ ] , Record < string , string > ] > {
if ( ! fileContent ) {
if ( ! fileContent ) {
// @ts-ignore
// @ts-ignore
fileContent = await this . call ( 'fileManager' , 'readFile' , path )
fileContent = await this . call ( 'fileManager' , 'readFile' , path )
}
}
this . lastParsedFiles = await this . resolveDependencies ( path , fileContent , { [ path ] : { content : fileContent , parent : null } } )
this . lastParsedFiles = await this . resolveDependencies ( path , fileContent , { [ path ] : { content : fileContent , parent : null } } )
const compiler = this . _compilationConfig . version === '2.1.5' ? compilerV215 : compilerV216
const parsedOutput = this . compiler . parse ( path , this . lastParsedFiles )
const parsedOutput = compiler . parse ( path , this . lastParsedFiles )
try {
try {
const result : CompilerReport [ ] = JSON . parse ( parsedOutput . report ( ) )
const result : CompilerReport [ ] = JSON . parse ( parsedOutput . report ( ) )
@ -125,13 +138,10 @@ export class CircomPluginClient extends PluginClient {
if ( compilationConfig ) {
if ( compilationConfig ) {
const { prime , version } = compilationConfig
const { prime , version } = compilationConfig
if ( ( prime !== "bn128" ) && ( prime !== "bls12381" ) && ( prime !== "goldilocks" ) && ( prime !== "grumpkin" ) && ( prime !== "pallas" ) && ( prime !== "vesta" ) ) throw new Error ( 'Invalid prime value' )
this . compilerVersion = version
if ( ! compiler_list . versions . includes ( version ) ) throw new Error ( "Unsupported compiler version" )
this . compilerPrime = prime
this . _compilationConfig . prime = prime
this . _compilationConfig . version = version
}
}
const compiler = this . _compilationConfig . version === '2.1.5' ? compilerV215 : compilerV216
const circuitApi = this . compiler . compile ( path , this . lastParsedFiles , { prime : this._compilationConfig.prime } )
const circuitApi = compiler . compile ( path , this . lastParsedFiles , { prime : this._compilationConfig.prime } )
const circuitProgram = circuitApi . program ( )
const circuitProgram = circuitApi . program ( )
if ( circuitProgram . length < 1 ) {
if ( circuitProgram . length < 1 ) {
@ -176,13 +186,10 @@ export class CircomPluginClient extends PluginClient {
if ( compilationConfig ) {
if ( compilationConfig ) {
const { prime , version } = compilationConfig
const { prime , version } = compilationConfig
if ( ( prime !== "bn128" ) && ( prime !== "bls12381" ) && ( prime !== "goldilocks" ) && ( prime !== "grumpkin" ) && ( prime !== "pallas" ) && ( prime !== "vesta" ) ) throw new Error ( 'Invalid prime value' )
this . compilerVersion = version
if ( ! compiler_list . versions . includes ( version ) ) throw new Error ( "Unsupported compiler version" )
this . compilerPrime = prime
this . _compilationConfig . prime = prime
this . _compilationConfig . version = version
}
}
const compiler = this . _compilationConfig . version === '2.1.5' ? compilerV215 : compilerV216
const r1csApi = this . compiler . generate_r1cs ( path , this . lastParsedFiles , { prime : this._compilationConfig.prime } )
const r1csApi = compiler . generate_r1cs ( path , this . lastParsedFiles , { prime : this._compilationConfig.prime } )
const r1csProgram = r1csApi . program ( )
const r1csProgram = r1csApi . program ( )
if ( r1csProgram . length < 1 ) {
if ( r1csProgram . length < 1 ) {
@ -207,8 +214,7 @@ export class CircomPluginClient extends PluginClient {
// @ts-ignore
// @ts-ignore
const buffer : any = await this . call ( 'fileManager' , 'readFile' , wasmPath , { encoding : null } )
const buffer : any = await this . call ( 'fileManager' , 'readFile' , wasmPath , { encoding : null } )
const dataRead = new Uint8Array ( buffer )
const dataRead = new Uint8Array ( buffer )
const compiler = this . _compilationConfig . version === '2.1.5' ? compilerV215 : compilerV216
const witness = await this . compiler . generate_witness ( dataRead , input )
const witness = await compiler . generate_witness ( dataRead , input )
// @ts-ignore
// @ts-ignore
await this . call ( 'fileManager' , 'writeFile' , wasmPath . replace ( '.wasm' , '.wtn' ) , witness , true )
await this . call ( 'fileManager' , 'writeFile' , wasmPath . replace ( '.wasm' , '.wtn' ) , witness , true )
this . internalEvents . emit ( 'circuit_computing_witness_done' )
this . internalEvents . emit ( 'circuit_computing_witness_done' )