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

Recognise that CircuitBuilder::namespace can't fail #494

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5176ab2
Recognise `create_witin` can't fail
matthiasgoergens Oct 28, 2024
b5b8ff0
Recognise that `namespace` can't fail
matthiasgoergens Oct 28, 2024
7821ae0
WIP
matthiasgoergens Oct 28, 2024
700b4f8
Merge branch 'matthias/remove-wrapper-2' into matthias/name-space-2
matthiasgoergens Oct 28, 2024
c233633
The other namespace can't fail either
matthiasgoergens Oct 28, 2024
4b68882
Merge remote-tracking branch 'origin/master' into matthias/name-space
matthiasgoergens Oct 29, 2024
c89cf30
Merge remote-tracking branch 'origin/master' into matthias/name-space-2
matthiasgoergens Oct 29, 2024
31f3e8f
Merge remote-tracking branch 'origin/matthias/name-space' into matthi…
matthiasgoergens Oct 29, 2024
9def3cb
Merge remote-tracking branch 'origin/master' into matthias/name-space
matthiasgoergens Oct 29, 2024
ea80904
Merge branch 'matthias/name-space' into matthias/name-space-2
matthiasgoergens Oct 29, 2024
df70ce8
Merge remote-tracking branch 'origin/master' into matthias/name-space-2
matthiasgoergens Nov 4, 2024
58c1cda
Merge remote-tracking branch 'origin/master' into matthias/name-space-2
matthiasgoergens Nov 5, 2024
27376a1
Simpler
matthiasgoergens Nov 5, 2024
1a39515
Revert "Simpler"
matthiasgoergens Nov 5, 2024
4cb5177
Merge branch 'master' into matthias/name-space-2
matthiasgoergens Nov 5, 2024
5f9eeb6
Merge remote-tracking branch 'origin/master' into matthias/name-space-2
matthiasgoergens Nov 25, 2024
3a91761
Merge remote-tracking branch 'origin/master' into matthias/name-space-2
matthiasgoergens Nov 25, 2024
da17644
Merge remote-tracking branch 'origin/matthias/name-space-2' into matt…
matthiasgoergens Nov 25, 2024
04a5c3b
Simpler
matthiasgoergens Nov 25, 2024
2b0bd1e
Merge remote-tracking branch 'origin/master' into matthias/name-space-2
matthiasgoergens Nov 25, 2024
9cff960
Merge remote-tracking branch 'origin/master' into matthias/name-space-2
matthiasgoergens Dec 10, 2024
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
4 changes: 2 additions & 2 deletions ceno_zkvm/src/chip_handler/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ impl<'a, E: ExtensionField> CircuitBuilder<'a, E> {
pub fn namespace<NR: Into<String>, N: FnOnce() -> NR, T>(
&mut self,
name_fn: N,
cb: impl FnOnce(&mut CircuitBuilder<E>) -> Result<T, ZKVMError>,
) -> Result<T, ZKVMError> {
cb: impl FnOnce(&mut CircuitBuilder<E>) -> T,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@lispc Here's a simple realisation: the namespace function as written actually works for any type T, not just for Result.

Realising that, lets us remove plenty of unnecessary OK(..) and .unwrap() later down.

It's useful to do this, because Result types should be used when we actually have something that can go wrong and should be handled; instead of being dealt with via .unwrap() thoughout. (If all the 'error handling' you ever gonna do is via .unwrap(), you might as well panic!() instead of creating an Err in the first place, at least that gives more useful stack traces.)

) -> T {
self.cs.namespace(name_fn, |cs| {
let mut inner_circuit_builder =
CircuitBuilder::new_with_params(cs, self.params.clone());
Expand Down
45 changes: 7 additions & 38 deletions ceno_zkvm/src/instructions/riscv/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,7 @@ mod test {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(
|| "add",
|cb| {
let config = AddInstruction::construct_circuit(cb);
Ok(config)
},
)
.unwrap()
.namespace(|| "add", AddInstruction::construct_circuit)
.unwrap();

let insn_code = encode_rv32(InsnKind::ADD, 2, 3, 4, 0);
Expand Down Expand Up @@ -228,14 +221,7 @@ mod test {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(
|| "add",
|cb| {
let config = AddInstruction::construct_circuit(cb);
Ok(config)
},
)
.unwrap()
.namespace(|| "add", AddInstruction::construct_circuit)
.unwrap();

let insn_code = encode_rv32(InsnKind::ADD, 2, 3, 4, 0);
Expand Down Expand Up @@ -272,14 +258,7 @@ mod test {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(
|| "sub",
|cb| {
let config = SubInstruction::construct_circuit(cb);
Ok(config)
},
)
.unwrap()
.namespace(|| "sub", SubInstruction::construct_circuit)
.unwrap();

let insn_code = encode_rv32(InsnKind::SUB, 2, 3, 4, 0);
Expand Down Expand Up @@ -316,14 +295,7 @@ mod test {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(
|| "sub",
|cb| {
let config = SubInstruction::construct_circuit(cb);
Ok(config)
},
)
.unwrap()
.namespace(|| "sub", SubInstruction::construct_circuit)
.unwrap();

let insn_code = encode_rv32(InsnKind::SUB, 2, 3, 4, 0);
Expand Down Expand Up @@ -360,8 +332,7 @@ mod test {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(|| "mul", |cb| Ok(MulInstruction::construct_circuit(cb)))
.unwrap()
.namespace(|| "mul", MulInstruction::construct_circuit)
.unwrap();

// values assignment
Expand Down Expand Up @@ -396,8 +367,7 @@ mod test {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(|| "mul", |cb| Ok(MulInstruction::construct_circuit(cb)))
.unwrap()
.namespace(|| "mul", MulInstruction::construct_circuit)
.unwrap();

// values assignment
Expand Down Expand Up @@ -431,8 +401,7 @@ mod test {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(|| "mul", |cb| Ok(MulInstruction::construct_circuit(cb)))
.unwrap()
.namespace(|| "mul", MulInstruction::construct_circuit)
.unwrap();

let a = Value::<'_, u32>::new_unchecked(u32::MAX);
Expand Down
12 changes: 2 additions & 10 deletions ceno_zkvm/src/instructions/riscv/arith_imm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,8 @@ mod test {
let config = cb
.namespace(
|| "addi",
|cb| {
let config = AddiInstruction::<GoldilocksExt2>::construct_circuit(cb);
Ok(config)
},
AddiInstruction::<GoldilocksExt2>::construct_circuit,
)
.unwrap()
.unwrap();

let insn_code = encode_rv32(InsnKind::ADDI, 2, 0, 4, imm_i(3));
Expand Down Expand Up @@ -135,12 +131,8 @@ mod test {
let config = cb
.namespace(
|| "addi",
|cb| {
let config = AddiInstruction::<GoldilocksExt2>::construct_circuit(cb);
Ok(config)
},
AddiInstruction::<GoldilocksExt2>::construct_circuit,
)
.unwrap()
.unwrap();

let insn_code = encode_rv32(InsnKind::ADDI, 2, 0, 4, imm_i(-3));
Expand Down
18 changes: 2 additions & 16 deletions ceno_zkvm/src/instructions/riscv/branch/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@ fn impl_opcode_beq(equal: bool) {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(
|| "beq",
|cb| {
let config = BeqInstruction::construct_circuit(cb);
Ok(config)
},
)
.unwrap()
.namespace(|| "beq", BeqInstruction::construct_circuit)
.unwrap();

let insn_code = encode_rv32(InsnKind::BEQ, 2, 3, 0, imm_b(8));
Expand Down Expand Up @@ -60,14 +53,7 @@ fn impl_opcode_bne(equal: bool) {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(
|| "bne",
|cb| {
let config = BneInstruction::construct_circuit(cb);
Ok(config)
},
)
.unwrap()
.namespace(|| "bne", BneInstruction::construct_circuit)
.unwrap();

let insn_code = encode_rv32(InsnKind::BNE, 2, 3, 0, imm_b(8));
Expand Down
3 changes: 1 addition & 2 deletions ceno_zkvm/src/instructions/riscv/divu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,8 @@ mod test {
let config = cb
.namespace(
|| format!("divu_({name})"),
|cb| Ok(DivUInstruction::construct_circuit(cb)),
DivUInstruction::construct_circuit,
)
.unwrap()
.unwrap();

let outcome = if divisor == 0 {
Expand Down
27 changes: 3 additions & 24 deletions ceno_zkvm/src/instructions/riscv/dummy/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@ fn test_dummy_ecall() {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(
|| "ecall_dummy",
|cb| {
let config = EcallDummy::construct_circuit(cb);
Ok(config)
},
)
.unwrap()
.namespace(|| "ecall_dummy", EcallDummy::construct_circuit)
.unwrap();

let step = StepRecord::new_ecall_any(4, MOCK_PC_START);
Expand All @@ -42,14 +35,7 @@ fn test_dummy_r() {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(
|| "add_dummy",
|cb| {
let config = AddDummy::construct_circuit(cb);
Ok(config)
},
)
.unwrap()
.namespace(|| "add_dummy", AddDummy::construct_circuit)
.unwrap();

let insn_code = encode_rv32(InsnKind::ADD, 2, 3, 4, 0);
Expand All @@ -74,14 +60,7 @@ fn test_dummy_b() {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(
|| "beq_dummy",
|cb| {
let config = BeqDummy::construct_circuit(cb);
Ok(config)
},
)
.unwrap()
.namespace(|| "beq_dummy", BeqDummy::construct_circuit)
.unwrap();

let insn_code = encode_rv32(InsnKind::BEQ, 2, 3, 0, 8);
Expand Down
24 changes: 4 additions & 20 deletions ceno_zkvm/src/instructions/riscv/jump/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ fn test_opcode_jal() {
let config = cb
.namespace(
|| "jal",
|cb| {
let config = JalInstruction::<GoldilocksExt2>::construct_circuit(cb);
Ok(config)
},
JalInstruction::<GoldilocksExt2>::construct_circuit,
)
.unwrap()
.unwrap();

let pc_offset: i32 = -8i32;
Expand Down Expand Up @@ -53,12 +49,8 @@ fn test_opcode_jalr() {
let config = cb
.namespace(
|| "jalr",
|cb| {
let config = JalrInstruction::<GoldilocksExt2>::construct_circuit(cb);
Ok(config)
},
JalrInstruction::<GoldilocksExt2>::construct_circuit,
)
.unwrap()
.unwrap();

let imm = -15i32;
Expand Down Expand Up @@ -90,12 +82,8 @@ fn test_opcode_lui() {
let config = cb
.namespace(
|| "lui",
|cb| {
let config = LuiInstruction::<GoldilocksExt2>::construct_circuit(cb);
Ok(config)
},
LuiInstruction::<GoldilocksExt2>::construct_circuit,
)
.unwrap()
.unwrap();

let imm_value = imm_u(0x90005);
Expand Down Expand Up @@ -123,12 +111,8 @@ fn test_opcode_auipc() {
let config = cb
.namespace(
|| "auipc",
|cb| {
let config = AuipcInstruction::<GoldilocksExt2>::construct_circuit(cb);
Ok(config)
},
AuipcInstruction::<GoldilocksExt2>::construct_circuit,
)
.unwrap()
.unwrap();

let imm_value = imm_u(0x90005);
Expand Down
27 changes: 3 additions & 24 deletions ceno_zkvm/src/instructions/riscv/logic/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ fn test_opcode_and() {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(
|| "and",
|cb| {
let config = AndInstruction::construct_circuit(cb);
Ok(config)
},
)
.unwrap()
.namespace(|| "and", AndInstruction::construct_circuit)
.unwrap();

let insn_code = encode_rv32(InsnKind::AND, 2, 3, 4, 0);
Expand Down Expand Up @@ -58,14 +51,7 @@ fn test_opcode_or() {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(
|| "or",
|cb| {
let config = OrInstruction::construct_circuit(cb);
Ok(config)
},
)
.unwrap()
.namespace(|| "or", OrInstruction::construct_circuit)
.unwrap();

let insn_code = encode_rv32(InsnKind::OR, 2, 3, 4, 0);
Expand Down Expand Up @@ -98,14 +84,7 @@ fn test_opcode_xor() {
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(
|| "xor",
|cb| {
let config = XorInstruction::construct_circuit(cb);
Ok(config)
},
)
.unwrap()
.namespace(|| "xor", XorInstruction::construct_circuit)
.unwrap();

let insn_code = encode_rv32(InsnKind::XOR, 2, 3, 4, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,8 @@ mod test {
let config = cb
.namespace(
|| format!("{prefix}_({name})"),
|cb| {
let config = LogicInstruction::<GoldilocksExt2, I>::construct_circuit(cb);
Ok(config)
},
LogicInstruction::<GoldilocksExt2, I>::construct_circuit,
)
.unwrap()
.unwrap();

let insn_code = encode_rv32(I::INST_KIND, 2, 0, 4, imm);
Expand Down
18 changes: 2 additions & 16 deletions ceno_zkvm/src/instructions/riscv/memory/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,7 @@ fn impl_opcode_store<E: ExtensionField + Hash, I: RIVInstruction, Inst: Instruct
let mut cs = ConstraintSystem::<E>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(
|| Inst::name(),
|cb| {
let config = Inst::construct_circuit(cb);
Ok(config)
},
)
.unwrap()
.namespace(|| Inst::name(), Inst::construct_circuit)
.unwrap();

let insn_code = encode_rv32(I::INST_KIND, 2, 3, 0, imm);
Expand Down Expand Up @@ -126,14 +119,7 @@ fn impl_opcode_load<E: ExtensionField + Hash, I: RIVInstruction, Inst: Instructi
let mut cs = ConstraintSystem::<E>::new(|| "riscv");
let mut cb = CircuitBuilder::new(&mut cs);
let config = cb
.namespace(
|| Inst::name(),
|cb| {
let config = Inst::construct_circuit(cb);
Ok(config)
},
)
.unwrap()
.namespace(|| Inst::name(), Inst::construct_circuit)
.unwrap();

let insn_code = encode_rv32(I::INST_KIND, 2, 3, 0, imm);
Expand Down
Loading
Loading