Skip to content

Commit

Permalink
fix: curta serialization bug (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirhemo authored Sep 19, 2023
1 parent 0de0aaa commit ec1443d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 59 deletions.
66 changes: 12 additions & 54 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plonky2x/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ci = []

[dependencies]
plonky2 = { git = "https://github.com/mir-protocol/plonky2.git", default-features = false }
curta = { git = "https://github.com/succinctlabs/curta.git" }
curta = { git = "https://github.com/succinctlabs/curta.git"}
plonky2x-derive = { path = "../plonky2x-derive" }

num = { version = "0.4", default-features = false }
Expand Down
6 changes: 4 additions & 2 deletions plonky2x/src/backend/circuit/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,10 @@ where
let sha256_hint_generator_id = SHA256HintGenerator::id();
r.register_simple::<SHA256HintGenerator>(sha256_hint_generator_id);

let sha256_generator = SHA256Generator::<L::Field, L::CubicParams>::id();
r.register_simple::<SHA256Generator<L::Field, L::CubicParams>>(sha256_generator);
let sha256_generator = SHA256Generator::<L::Field, L::CubicParams, L::CurtaConfig, D>::id();
r.register_simple::<SHA256Generator<L::Field, L::CubicParams, L::CurtaConfig, D>>(
sha256_generator,
);

let simple_stark_witness_generator_id = SimpleStarkWitnessGenerator::<
SHA256AirParameters<L::Field, L::CubicParams>,
Expand Down
39 changes: 37 additions & 2 deletions plonky2x/src/frontend/hash/sha/sha256_curta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,12 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
mod tests {
use std::env;

use crate::backend::circuit::DefaultParameters;
use crate::backend::circuit::{CircuitBuild, DefaultParameters};
use crate::frontend::uint::uint32::U32Variable;
use crate::frontend::vars::Bytes32Variable;
use crate::prelude::{ByteVariable, BytesVariable, CircuitBuilder};
use crate::prelude::{
ByteVariable, BytesVariable, CircuitBuilder, GateRegistry, WitnessGeneratorRegistry,
};
use crate::utils::{bytes, bytes32};

type L = DefaultParameters;
Expand Down Expand Up @@ -346,4 +348,37 @@ mod tests {
// TODO: Add back once curta serialization is implemented.
// circuit.test_default_serializers();
}

#[test]
#[cfg_attr(feature = "ci", ignore)]
fn test_serialized_sha256_curta() {
env::set_var("RUST_LOG", "debug");
env_logger::try_init().unwrap_or_default();
dotenv::dotenv().ok();

let mut builder = CircuitBuilder::<L, D>::new();
let zero = builder.constant::<ByteVariable>(0u8);
let result = builder.curta_sha256(&[zero; 1]);
builder.watch(&result, "result");

let expected_digest =
bytes32!("0x6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d");
let expected_digest = builder.constant::<Bytes32Variable>(expected_digest);

builder.assert_is_equal(result, expected_digest);

let circuit = builder.build();
let gate_serializer = GateRegistry::<L, D>::new();
let generator_serializer = WitnessGeneratorRegistry::<L, D>::new();
let bytes = circuit
.serialize(&gate_serializer, &generator_serializer)
.unwrap();
let circuit =
CircuitBuild::<L, D>::deserialize(&bytes, &gate_serializer, &generator_serializer)
.unwrap();
let input = circuit.input();
let (proof, output) = circuit.prove(&input);
circuit.verify(&proof, &input, &output);
circuit.test_default_serializers();
}
}

0 comments on commit ec1443d

Please sign in to comment.