Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stats code & release binary #135

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: build-expander-exec
name: build-release-binary
on:
release:
types: [released]
types: [published]

env:
RUSTFLAGS: "-Dwarnings"
Expand All @@ -14,18 +14,26 @@ jobs:
platform: [macos, 7950x3d]
include:
- platform: macos
ci_image: macos-latest
flags: ''
- platform: linux-avx2
ci_image: ubuntu-latest
flags: 'RUSTFLAGS="-C target-feature=+avx2"'
runs-on: ${{ matrix.ci_image }}
os: macos-latest
- platform: 7950x3d
os: ubuntu-latest
feature: avx2
- platform: 7950x3d
os: ubuntu-latest
feature: avx512f
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
with:
# The prefix cache key, this can be changed to start a new cache manually.
prefix-key: "mpi-v5.0.5" # update me if brew formula changes to a new version
- name: Install MPI
run: python3 ./scripts/install.py
- name: Set RUSTFLAGS for AVX
if: matrix.feature != ''
run: echo "RUSTFLAGS=$RUSTFLAGS -C target-feature=+${{ matrix.feature }}" >> $GITHUB_ENV
- name: Prepare binary
run: ${{ matrix.flags }} cargo build --release --bin expander-exec
- name: Upload release asset
Expand All @@ -37,6 +45,6 @@ jobs:
owner: context.repo.owner,
repo: context.repo.repo,
release_id: ${{ github.event.release.id }},
name: 'expander-exec-${{ matrix.platform }}',
name: 'expander-exec-${{ matrix.os }}-${{ matrix.feature }}',
data: await fs.readFile('target/release/expander-exec')
});
1 change: 1 addition & 0 deletions recursion/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func testGroth16() {

original_circuit, _ := circuit.ReadCircuit(*circuit_file, *witness_file, *mpi_size)
proof := circuit.ReadProof(*gkr_proof_file)
original_circuit.PrintStats()

verifier_circuit := VerifierCircuit{
MpiSize: *mpi_size,
Expand Down
23 changes: 23 additions & 0 deletions recursion/modules/circuit/expander_circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package circuit

import (
"ExpanderVerifierCircuit/modules/transcript"
"log"
"math/big"

"github.com/consensys/gnark/frontend"
Expand Down Expand Up @@ -87,3 +88,25 @@ func (c *Circuit) FillRndCoef(transcript *transcript.Transcript) {
c.Layers[i].FillRndCoef(transcript)
}
}

func (c *Circuit) PrintStats() {
n_mul := 0
n_add := 0
n_cst_circuit := 0
n_cst_input := 0

for i := 0; i < len(c.Layers); i++ {
n_mul += len(c.Layers[i].Mul)
n_add += len(c.Layers[i].Add)
n_cst_circuit += len(c.Layers[i].Cst)
}

n_cst_input = len(c.PublicInput[0])
n_cst_circuit -= n_cst_input

log.Println("#Layers: ", len(c.Layers))
log.Println("#Mul Gates: ", n_mul)
log.Println("#Add Gates: ", n_add)
log.Println("#Cst Circuit: ", n_cst_circuit)
log.Println("#Cst Input: ", n_cst_input)
}
14 changes: 14 additions & 0 deletions recursion/modules/transcript/transcript.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type Transcript struct {

// The state
state frontend.Variable

// helper field: counting, irrelevant to circuit
count uint
}

func NewTranscript(api frontend.API) (Transcript, error) {
Expand All @@ -26,12 +29,14 @@ func NewTranscript(api frontend.API) (Transcript, error) {
t: []frontend.Variable{},
hasher: &mimc,
state: 0,
count: 0,
}

return T, err
}

func (T *Transcript) AppendF(f frontend.Variable) {
T.count++
T.t = append(T.t, f)
}

Expand All @@ -44,6 +49,7 @@ func (T *Transcript) ChallengeF() frontend.Variable {
T.t = T.t[:0]
} else {
T.hasher.Write(T.state)
T.count++
}
T.state = T.hasher.Sum()
return T.state
Expand All @@ -60,3 +66,11 @@ func (T *Transcript) ChallengeFs(n uint) []frontend.Variable {
func (T *Transcript) GetState() frontend.Variable {
return T.state
}

func (T *Transcript) GetCount() uint {
return T.count
}

func (T *Transcript) ResetCount() {
T.count = 0
}
4 changes: 4 additions & 0 deletions recursion/modules/verifier/scratch_pad.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type ScratchPad struct {
Inv2 frontend.Variable
Deg3EvalAt [4]frontend.Variable
Deg3LagDenomsInv [4]frontend.Variable

// ====== helper field to get the statistics of the circuit =====
EqEvalsCount map[uint]uint
}

func NewScratchPad(api frontend.API, circuit *circuit.Circuit, simd_size uint, mpi_size uint) (*ScratchPad, error) {
Expand Down Expand Up @@ -64,5 +67,6 @@ func NewScratchPad(api frontend.API, circuit *circuit.Circuit, simd_size uint, m
sp.Deg3LagDenomsInv[i] = api.Inverse(denominator)
}

sp.EqEvalsCount = make(map[uint]uint)
return &sp, nil
}
14 changes: 10 additions & 4 deletions recursion/modules/verifier/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ func EqEvalsAtPrimitive(
var cur_eval_num = 1

for i := 0; i < len(r); i++ {
var eq_zero = api.Sub(1, r[i])
var eq_one = r[i]
for j := 0; j < cur_eval_num; j++ {
ret_evals[j+cur_eval_num] = api.Mul(ret_evals[j], eq_one)
ret_evals[j] = api.Mul(ret_evals[j], eq_zero)
ret_evals[j+cur_eval_num] = api.Mul(ret_evals[j], r[i])
ret_evals[j] = api.Sub(ret_evals[j], ret_evals[j+cur_eval_num])
}
cur_eval_num <<= 1
}
Expand All @@ -31,7 +29,15 @@ func EqEvalsAtEfficient(
ret_evals []frontend.Variable,
tmp_1st_half []frontend.Variable,
tmp_2nd_half []frontend.Variable,
eq_evals_count map[uint]uint,
) {
ret_len := uint(1) << len(r)
if val, ok := eq_evals_count[ret_len]; ok {
eq_evals_count[ret_len] = val + 1
} else {
eq_evals_count[ret_len] = 1
}

var first_half_bits uint = uint(len(r) >> 1)
var first_half_mask uint = (1 << first_half_bits) - 1

Expand Down
16 changes: 15 additions & 1 deletion recursion/modules/verifier/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"ExpanderVerifierCircuit/modules/circuit"
"ExpanderVerifierCircuit/modules/polycommit"
"ExpanderVerifierCircuit/modules/transcript"
"log"
"math/bits"

"github.com/consensys/gnark/frontend"
Expand Down Expand Up @@ -233,7 +234,10 @@ func GKRVerify(
}
}

// api.AssertIsEqual(api.Add(rz0[0], claimed_v0, alpha, public_input[0][0]), 1)
for size, count := range sp.EqEvalsCount {
log.Println("Eq Evals Size", size, " Count: ", count)
}

return rz0, rz1, r_simd, r_mpi, claimed_v0, claimed_v1
}

Expand Down Expand Up @@ -265,10 +269,20 @@ func Verify(
if mpi_size > 1 {
_ = transcript.ChallengeF()
}

log.Println("#Hashes for input: ", transcript.GetCount())
transcript.ResetCount()

circuit.FillRndCoef(&transcript)

log.Println("#Hashes for random gate: ", transcript.GetCount())
transcript.ResetCount()

var rx, ry, r_simd, r_mpi, claimed_v0, claimed_v1 = GKRVerify(api, circuit, public_input, claimed_v, simd_size, mpi_size, &transcript, proof)

log.Println("#Hashes for gkr challenge: ", transcript.GetCount())
transcript.ResetCount()

if len(r_simd) > 0 {
panic("Simd not supported yet.")
}
Expand Down
6 changes: 6 additions & 0 deletions recursion/modules/verifier/verifier_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func PrepareLayer(
sp.EqEvalsAtRz0,
sp.EqEvalsFirstPart,
sp.EqEvalsSecondPart,
sp.EqEvalsCount,
)

if rz1 != nil && beta != nil {
Expand All @@ -34,6 +35,7 @@ func PrepareLayer(
sp.EqEvalsAtRz1,
sp.EqEvalsFirstPart,
sp.EqEvalsSecondPart,
sp.EqEvalsCount,
)

for i := 0; i < 1<<layer.OutputLenLog; i++ {
Expand All @@ -48,6 +50,7 @@ func PrepareLayer(
sp.EqEvalsAtRSimd,
sp.EqEvalsFirstPart,
sp.EqEvalsSecondPart,
sp.EqEvalsCount,
)

EqEvalsAtEfficient(
Expand All @@ -57,6 +60,7 @@ func PrepareLayer(
sp.EqEvalsAtRMpi,
sp.EqEvalsFirstPart,
sp.EqEvalsSecondPart,
sp.EqEvalsCount,
)

sp.RSimd = &r_simd
Expand Down Expand Up @@ -155,6 +159,7 @@ func SetRx(
sp.EqEvalsAtRx,
sp.EqEvalsFirstPart,
sp.EqEvalsSecondPart,
sp.EqEvalsCount,
)
}

Expand Down Expand Up @@ -186,6 +191,7 @@ func SetRY(
sp.EqEvalsAtRy,
sp.EqEvalsFirstPart,
sp.EqEvalsSecondPart,
sp.EqEvalsCount,
)
}

Expand Down
Loading