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

ZIR-345: Delete unused keccak circuit; rename keccak2 to keccak #191

Merged
merged 1 commit into from
Feb 20, 2025
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
2 changes: 1 addition & 1 deletion zirgen/bootstrap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ impl Bootstrap {

fn keccak(&'static self) {
self.install_from_bazel(
"//zirgen/circuit/keccak2:bootstrap",
"//zirgen/circuit/keccak:bootstrap",
self.output_and("risc0/circuit/keccak"),
&[
Rule::copy("*.cpp", "kernels/cxx").base_suffix("-sys"),
Expand Down
1 change: 0 additions & 1 deletion zirgen/circuit/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ filegroup(
srcs = [
"//zirgen/circuit/bigint:bigint_blob",
"//zirgen/circuit/fib",
"//zirgen/circuit/keccak",
"//zirgen/circuit/predicates:recursion_zkr",
"//zirgen/circuit/recursion",
"//zirgen/circuit/rv32im/v1/edsl:rv32im",
Expand Down
105 changes: 76 additions & 29 deletions zirgen/circuit/keccak/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,48 +1,95 @@
load("@rules_pkg//pkg:zip.bzl", "pkg_zip")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the extensive changes in this diff confused me for quite a while until I realized git has simply replaced zirgen/circuit/keccak/BUILD.bazel with zirgen/circuit/keccak2/BUILD.bazel, and nothing has actually changed! Funny

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the diff is annoying. I thought about splitting it up into multiple commits, but it'll get squashed down anyway. Complete summary of my process:

  • Ran tests on unaltered repo, verified they passed
  • Deleted the /zirgen/circuit/keccak directory
  • Reran tests, noted a bazel issue with the //zirgen/circuit:circuit target. Fixed.
  • Observed the tests passed again
  • Renamed the /zirgen/circuit/keccak2 directory to keccak
  • Ran "find and replace" keccak2 -> keccak
  • Observed the tests passed again, and it's done!

load("//bazel/rules/lit:defs.bzl", "glob_lit_tests")
load("//bazel/rules/zirgen:dsl-defs.bzl", "zirgen_build")
load("//bazel/rules/zirgen:edsl-defs.bzl", "ZIRGEN_OUTS", "build_circuit")

package(
default_visibility = ["//visibility:public"],
)
package(default_visibility = ["//visibility:public"])

KECCAK_ZIR_FILES = [
"bits.zir",
"keccak.zir",
"one_hot.zir",
"sha256_for_keccak.zir",
]
filegroup(
name = "imports",
srcs = glob(["*.zir"]),
)

glob_lit_tests(
# TODO: shorten timeout once zirgen is faster
timeout = "long",
data = KECCAK_ZIR_FILES,
exclude = KECCAK_ZIR_FILES,
data = [":imports"],
size_override = {
"top.zir": "medium",
"sha2.zir": "medium",
},
test_file_exts = ["zir"],
)

KECCAK_OUTS = ZIRGEN_OUTS + [
"rust_poly_fp_0.cpp",
"rust_poly_fp_1.cpp",
"rust_poly_fp_2.cpp",
"rust_poly_fp_3.cpp",
"rust_poly_fp_4.cpp",
"eval_check_0.cu",
"eval_check_1.cu",
"eval_check_2.cu",
"eval_check_3.cu",
"eval_check_4.cu",
zirgen_build(
name = "cppinc",
out = "keccak.cpp.inc",
data = [":imports"],
opts = [
"--emit=cpp",
"--validity=false",
],
zir_file = ":top.zir",
)

SPLIT_VALIDITY = 5

SPLIT_STEP = 16

OUTS = ZIRGEN_OUTS + [
fn
for i in range(SPLIT_VALIDITY)
for fn in [
"rust_poly_fp_" + str(i) + ".cpp",
"eval_check_" + str(i) + ".cu",
]
] + [
fn
for i in range(SPLIT_STEP)
for fn in [
"steps_" + str(i) + ".cpp",
"steps_" + str(i) + ".cu",
]
] + [
"eval_check.cuh",
"steps.h",
"steps.cuh",
"layout.h.inc",
"layout.cuh.inc",
]

build_circuit(
name = "keccak",
outs = KECCAK_OUTS,
name = "codegen",
outs = OUTS,
bin = "//zirgen/Main:gen_zirgen",
data = KECCAK_ZIR_FILES,
data = [":imports"],
extra_args = [
"zirgen/circuit/keccak/keccak.zir",
"zirgen/circuit/keccak/top.zir",
"-I",
"zirgen/circuit/keccak",
"--validity-split-count=5",
"--circuit-name=keccak",
"--validity-split-count=" + str(SPLIT_VALIDITY),
"--step-split-count=" + str(SPLIT_STEP),
"--parallel-witgen",
"--protocol-info=KECCAK:v1_______"
],
)

ZKRS = [("keccak_lift_" + str(po2)) for po2 in range(14, 19)]

build_circuit(
name = "predicates",
srcs = ["predicates.cpp"],
outs = [fn for zkr in ZKRS for fn in [
zkr + ".zkr",
]],
data = ["@zirgen//zirgen/circuit/keccak:validity.ir"],
extra_args = ["--keccak-ir=$(location :validity.ir)"],
deps = ["//zirgen/circuit/predicates:lib"],
)

filegroup(
name = "bootstrap",
srcs = [
":codegen",
":predicates",
],
)
45 changes: 0 additions & 45 deletions zirgen/circuit/keccak/README.md

This file was deleted.

File renamed without changes.
75 changes: 30 additions & 45 deletions zirgen/circuit/keccak/bits.zir
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// RUN: zirgen --test %s

// Assert that a given value is a bit
#[picus_inline]
function AssertBit(val: Val) {
val * (1 - val) = 0;
}
Expand All @@ -20,66 +21,50 @@ component BitReg(val: Val) {
reg
}

// Check that valid bits are valid
test BitInRange {
AssertBit(0);
AssertBit(1);
}

// Check that 2 is not a bit
test_fails BitOutOfRange {
AssertBit(2);
}

// Assert the a given value is a 2-bit number (i.e. 0-3)
function AssertTwit(val: Val) {
val * (1 - val) * (2 - val) * (3 - val) = 0;
}

// Simple bit ops
component BitAnd(a: Val, b: Val) {
Reg(a * b)
a * b
}

component BitOr(a: Val, b: Val) {
Reg(1 - (1 - a) * (1 - b))
a + b - a * b
}

// Set a register nodeterministically, and then verify it is a twit
component NondetTwitReg(val: Val) {
reg := NondetReg(val);
AssertTwit(reg);
reg
component BitXor(a: Val, b: Val) {
a + b - 2 * a * b
}

// Set a register nodeterministically, and then verify it is a twit
component NondetFakeTwitReg(val: Val) {
reg0 := NondetBitReg(val & 1);
reg1 := NondetBitReg((val & 2) / 2);
reg1 * 2 + reg0
// Tests....

// Check that valid bits are valid
test BitInRange {
AssertBit(0);
AssertBit(1);
}

component TwitReg(val: Val) {
reg := NondetTwitReg(val);
val = reg;
reg
// Check that 2 is not a bit
test_fails BitOutOfRange {
AssertBit(2);
}

component FakeTwitReg(val: Val) {
reg := NondetFakeTwitReg(val);
val = reg;
reg
test TestAnd {
BitAnd(0, 0) = 0;
BitAnd(0, 1) = 0;
BitAnd(1, 0) = 0;
BitAnd(1, 1) = 1;
}

// Check that all valid twit values are OK
test TwitInRange{
AssertTwit(0);
AssertTwit(1);
AssertTwit(2);
AssertTwit(3);
test TestOr {
BitOr(0, 0) = 0;
BitOr(0, 1) = 1;
BitOr(1, 0) = 1;
BitOr(1, 1) = 1;
}

// Check that 4 is not a twit
test_fails TwitOutOfRange {
AssertTwit(4);
test TestXor {
BitXor(0, 0) = 0;
BitXor(0, 1) = 1;
BitXor(1, 0) = 1;
BitXor(1, 1) = 0;
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cc_library(
# "run.cpp",
"trace.cpp",
"wrap_dsl.cpp",
"//zirgen/circuit/keccak2:cppinc",
"//zirgen/circuit/keccak:cppinc",
],
hdrs = [
"preflight.h",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 RISC Zero, Inc.
// Copyright 2025 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "zirgen/circuit/keccak2/cpp/wrap_dsl.h"
#include "zirgen/circuit/keccak/cpp/wrap_dsl.h"
#include "zirgen/compiler/zkp/sha256.h"

/* The below implementation (up till the end of keccackf) is taken from:
Expand Down Expand Up @@ -84,7 +84,7 @@ static void keccakf(uint64_t s[25]) {
std::cout << "Final: " << round << ", s[0] = " << std::hex << s[0] << std::dec << "\n";
}

void ShaSingleKeccak(zirgen::Digest& digest, zirgen::keccak2::KeccakState state) {
void ShaSingleKeccak(zirgen::Digest& digest, zirgen::keccak::KeccakState state) {
std::vector<uint32_t> toHash(64);
uint32_t* viewState = (uint32_t*)&state;
for (size_t i = 0; i < 50; i++) {
Expand All @@ -95,7 +95,7 @@ void ShaSingleKeccak(zirgen::Digest& digest, zirgen::keccak2::KeccakState state)
}
}

void DoTransaction(zirgen::Digest& digest, zirgen::keccak2::KeccakState state) {
void DoTransaction(zirgen::Digest& digest, zirgen::keccak::KeccakState state) {
ShaSingleKeccak(digest, state);
std::cout << "After compressing input: " << digest << "\n";
keccakf(state.data());
Expand All @@ -105,7 +105,7 @@ void DoTransaction(zirgen::Digest& digest, zirgen::keccak2::KeccakState state) {

int main() {
// Make an example
using namespace zirgen::keccak2;
using namespace zirgen::keccak;
KeccakState state;
uint64_t pows = 987654321;
for (size_t i = 0; i < state.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 RISC Zero, Inc.
// Copyright 2025 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,15 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "zirgen/circuit/keccak2/cpp/preflight.h"
#include "zirgen/circuit/keccak2/cpp/wrap_dsl.h"
#include "zirgen/circuit/keccak/cpp/preflight.h"
#include "zirgen/circuit/keccak/cpp/wrap_dsl.h"

#include <arpa/inet.h>
#include <array>
#include <cassert>
#include <iostream>

namespace zirgen::keccak2 {
namespace zirgen::keccak {

namespace {

Expand Down Expand Up @@ -192,7 +192,7 @@ struct ControlState {
static ControlState Write() { return ControlState{3, 0, 0, 0}; }
static ControlState Keccak0(uint8_t round) { return ControlState{4, 0, 0, round}; }
static ControlState Keccak1(uint8_t round) { return ControlState{5, 0, 0, round}; }
static ControlState Keccak2(uint8_t round) { return ControlState{6, 0, 0, round}; }
static ControlState keccak(uint8_t round) { return ControlState{6, 0, 0, round}; }
static ControlState Keccak3(uint8_t round) { return ControlState{7, 0, 0, round}; }
static ControlState Keccak4(uint8_t round) { return ControlState{8, 0, 0, round}; }
static ControlState ShaIn(uint8_t block, uint8_t round) {
Expand Down Expand Up @@ -329,7 +329,7 @@ PreflightTrace preflightSegment(const std::vector<KeccakState>& inputs, size_t c
addCycle(ControlState::Keccak0(round), writeTheta(theta), kflatOffset, sflatOffset);
theta_p2_rho_pi(kstate, theta);
addCycle(ControlState::Keccak1(round), writeKeccak(kstate, false), kflatOffset, sflatOffset);
addCycle(ControlState::Keccak2(round), writeKeccak(kstate, true), kflatOffset, sflatOffset);
addCycle(ControlState::keccak(round), writeKeccak(kstate, true), kflatOffset, sflatOffset);
chi_iota(kstate, round);
addCycle(ControlState::Keccak3(round), writeKeccak(kstate, false), kflatOffset, sflatOffset);
addCycle(ControlState::Keccak4(round), writeKeccak(kstate, true), kflatOffset, sflatOffset);
Expand Down Expand Up @@ -371,4 +371,4 @@ void applyPreflight(ExecutionTrace& exec, const PreflightTrace& preflight) {
}
}

} // namespace zirgen::keccak2
} // namespace zirgen::keccak
Loading
Loading