zkcrypto / bellman

zk-SNARK library.
Other
1.02k stars 542 forks source link

`Boolean` XOR #54

Closed LuoZijun closed 4 years ago

LuoZijun commented 4 years ago

https://github.com/zkcrypto/bellman/blob/0f2244fdb445f054331e33b2ac7d779812cfd615/src/gadgets/boolean.rs#L474

#[derive(Debug, Clone)]
pub enum Boolean {
    Constant(bool),
    Other,
}

impl Boolean {
    pub fn not(&self) -> Self {
        match *self {
            Boolean::Constant(c) => Boolean::Constant(!c),
            _ => unreachable!()
        }
    }

    pub fn xor(&self, other: &Self) -> Self {
        match (self, other) {
            (&Boolean::Constant(false), x) | (x, &Boolean::Constant(false)) => x.clone(),
            (&Boolean::Constant(true), x) | (x, &Boolean::Constant(true)) => x.not(),
            _ => unreachable!()
        }
    }
}

fn main() {
    let a = Boolean::Constant(true);
    let b = Boolean::Constant(true);
    let c = a.xor(&b);
    // Question: the result c should be `Boolean::Constant(true)` ?
    assert_eq!(c, Boolean::Constant(false));
}

if x is Boolean::Constant(true), will return Ok(Boolean::Constant(false)). But true BitXor true should be true not false ?

Pratyush commented 4 years ago

This method implements XOR, not OR (and the | here is a pattern-matching construct, not a logical OR)

LuoZijun commented 4 years ago

@Pratyush descp is updated.

true | true --> true BitXor true.

LuoZijun commented 4 years ago

@Pratyush

You are correct, I made a mistake.