Skip to content

Commit 89dfc4e

Browse files
authored
always replace SELF when copying statements (#345)
1 parent 5cdf535 commit 89dfc4e

File tree

5 files changed

+12
-14
lines changed

5 files changed

+12
-14
lines changed

src/backends/plonky2/basetypes.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
//! is enabled.
33
//! See src/middleware/basetypes.rs for more details.
44
5+
/// F is the native field we use everywhere. Currently it's Goldilocks from plonky2
6+
pub use plonky2::field::goldilocks_field::GoldilocksField as F;
57
use plonky2::{
6-
field::{extension::quadratic::QuadraticExtension, goldilocks_field::GoldilocksField},
8+
field::extension::quadratic::QuadraticExtension,
79
hash::{hash_types, poseidon::PoseidonHash},
810
plonk::{circuit_builder, circuit_data, config::GenericConfig, proof},
911
};
1012
use schemars::JsonSchema;
1113
use serde::{Deserialize, Deserializer, Serialize};
1214

13-
/// F is the native field we use everywhere. Currently it's Goldilocks from plonky2
14-
pub type F = GoldilocksField;
15-
1615
/// D defines the extension degree of the field used in the Plonky2 proofs (quadratic extension).
1716
pub const D: usize = 2;
1817

src/backends/plonky2/circuits/mainpod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use crate::{
4141
middleware::{
4242
AnchoredKey, CustomPredicate, CustomPredicateBatch, CustomPredicateRef, NativeOperation,
4343
NativePredicate, Params, PodType, PredicatePrefix, Statement, StatementArg, ToFields,
44-
Value, ValueRef, EMPTY_VALUE, F, HASH_SIZE, KEY_TYPE, SELF, VALUE_SIZE,
44+
Value, ValueRef, F, HASH_SIZE, KEY_TYPE, SELF, VALUE_SIZE,
4545
},
4646
};
4747

@@ -947,19 +947,15 @@ fn normalize_statement_circuit(
947947
statement: &StatementTarget,
948948
self_id: &ValueTarget,
949949
) -> StatementTarget {
950-
let zero_value = builder.constant_value(EMPTY_VALUE);
951950
let self_value = builder.constant_value(SELF.0.into());
952951
let args = statement
953952
.args
954953
.iter()
955954
.map(|arg| {
956955
let first = ValueTarget::from_slice(&arg.elements[..VALUE_SIZE]);
957956
let second = ValueTarget::from_slice(&arg.elements[VALUE_SIZE..]);
958-
let is_not_ak = builder.is_equal_flattenable(&zero_value, &second);
959-
let is_ak = builder.not(is_not_ak);
960957
let is_self = builder.is_equal_flattenable(&self_value, &first);
961-
let normalize = builder.and(is_ak, is_self);
962-
let first_normalized = builder.select_flattenable(params, normalize, self_id, &first);
958+
let first_normalized = builder.select_flattenable(params, is_self, self_id, &first);
963959
StatementArgTarget::new(first_normalized, second)
964960
})
965961
.collect_vec();

src/lang/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ mod tests {
3131
middleware::{
3232
hash_str, CustomPredicate, CustomPredicateBatch, CustomPredicateRef, Key,
3333
NativePredicate, Params, PodId, PodType, Predicate, RawValue, StatementTmpl,
34-
StatementTmplArg, Value, Wildcard, KEY_SIGNER, KEY_TYPE, SELF_ID_HASH,
34+
StatementTmplArg, Value, Wildcard, KEY_SIGNER, KEY_TYPE,
3535
},
3636
};
3737

@@ -125,7 +125,7 @@ mod tests {
125125
pred: Predicate::Native(NativePredicate::Equal),
126126
args: vec![
127127
sta_ak(("ConstPod", 0), "my_val"), // ?ConstPod["my_val"] -> Wildcard(0), Key("my_val")
128-
sta_lit(SELF_ID_HASH),
128+
sta_lit(RawValue::from(1)),
129129
],
130130
},
131131
StatementTmpl {

src/middleware/basetypes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub const HASH_SIZE: usize = 4;
5555
pub const VALUE_SIZE: usize = 4;
5656

5757
pub const EMPTY_VALUE: RawValue = RawValue([F::ZERO, F::ZERO, F::ZERO, F::ZERO]);
58-
pub const SELF_ID_HASH: Hash = Hash([F::ONE, F::ZERO, F::ZERO, F::ZERO]);
58+
pub const SELF_ID_HASH: Hash = Hash([F(0x5), F(0xe), F(0x1), F(0xf)]);
5959
pub const EMPTY_HASH: Hash = Hash([F::ZERO, F::ZERO, F::ZERO, F::ZERO]);
6060

6161
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]

src/middleware/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ impl Params {
766766
}
767767
}
768768

769-
/// Replace references to SELF by `self_id` in anchored keys of the statement.
769+
/// Replace references to SELF by `self_id`.
770770
pub fn normalize_statement(statement: &Statement, self_id: PodId) -> Statement {
771771
let predicate = statement.predicate();
772772
let args = statement
@@ -776,6 +776,9 @@ pub fn normalize_statement(statement: &Statement, self_id: PodId) -> Statement {
776776
StatementArg::Key(AnchoredKey { pod_id, key }) if *pod_id == SELF => {
777777
StatementArg::Key(AnchoredKey::new(self_id, key.clone()))
778778
}
779+
StatementArg::Literal(value) if value.raw.0 == SELF.0 .0 => {
780+
StatementArg::Literal(self_id.into())
781+
}
779782
_ => sa.clone(),
780783
})
781784
.collect();

0 commit comments

Comments
 (0)