From be9013ed31c7291ee0248f276ea109e3d817e9a3 Mon Sep 17 00:00:00 2001 From: parazyd Date: Wed, 10 May 2023 13:24:02 +0200 Subject: [PATCH] zkas: Rename "contract" to "witness". --- doc/src/zkas/bincode.md | 10 ++-- example/simple.zk | 2 +- proof/arithmetic.zk | 2 +- proof/burn.zk | 2 +- proof/deploy_contract.zk | 2 +- proof/encrypt.zk | 2 +- proof/inclusion_proof.zk | 2 +- proof/lead.zk | 2 +- proof/mint.zk | 2 +- proof/opcodes.zk | 2 +- proof/tx.zk | 2 +- proof/voting.zk | 2 +- script/research/rln/signal.zk | 2 +- script/research/rln/slash.zk | 2 +- src/contract/consensus/proof/reward_v1.zk | 2 +- src/contract/dao/proof/dao-exec.zk | 2 +- src/contract/dao/proof/dao-mint.zk | 2 +- src/contract/dao/proof/dao-propose-burn.zk | 2 +- src/contract/dao/proof/dao-propose-main.zk | 2 +- src/contract/dao/proof/dao-vote-burn.zk | 2 +- src/contract/dao/proof/dao-vote-main.zk | 2 +- src/contract/money/proof/burn_v1.zk | 2 +- src/contract/money/proof/lead_burn_v1.zk | 2 +- src/contract/money/proof/lead_mint_v1.zk | 2 +- src/contract/money/proof/mint_v1.zk | 2 +- src/contract/money/proof/token_freeze_v1.zk | 2 +- src/contract/money/proof/token_mint_v1.zk | 2 +- src/zkas/compiler.rs | 4 +- src/zkas/decoder.rs | 22 ++++---- src/zkas/parser.rs | 60 ++++++++++----------- 30 files changed, 74 insertions(+), 74 deletions(-) diff --git a/doc/src/zkas/bincode.md b/doc/src/zkas/bincode.md index 0fadf5c07..c570af540 100644 --- a/doc/src/zkas/bincode.md +++ b/doc/src/zkas/bincode.md @@ -5,7 +5,7 @@ The bincode design for zkas is the compiled code in the form of a binary blob, that can be read by a program and fed into the VM. Our programs consist of four sections: `constant`, `literal`, -`contract`, and `circuit`. Our bincode represents the +`witness`, and `circuit`. Our bincode represents the same. Additionally, there is an optional section called `.debug` which can hold debug info related to the binary. @@ -27,7 +27,7 @@ CONSTANT_TYPE CONSTANT_NAME LITERAL LITERAL ... -.contract +.witness WITNESS_TYPE WITNESS_TYPE ... @@ -68,7 +68,7 @@ source code, e.g.: ``` constant "MyNamespace" { ... } -contract "MyNamespace" { ... } +witness "MyNamespace" { ... } circuit "MyNamespace" { ... } ``` @@ -87,9 +87,9 @@ that get parsed into a `u64` type inside the VM. In the future this could be extended with signed integers, and strings. -### `.contract` +### `.witness` -The `.contract` section holds the circuit witness values in the form +The `.witness` section holds the circuit witness values in the form of `WITNESS_TYPE`. Their stack index is incremented for each witness as they're kept in order like in the source file. The witnesses that are of the same type as the circuit itself (typically `Base`) diff --git a/example/simple.zk b/example/simple.zk index 1a3d593da..6a3430735 100644 --- a/example/simple.zk +++ b/example/simple.zk @@ -3,7 +3,7 @@ constant "Simple" { EcFixedPoint VALUE_COMMIT_RANDOM, } -contract "Simple" { +witness "Simple" { Base value, Scalar value_blind, } diff --git a/proof/arithmetic.zk b/proof/arithmetic.zk index e6e1cbccc..cbb0039f4 100644 --- a/proof/arithmetic.zk +++ b/proof/arithmetic.zk @@ -1,6 +1,6 @@ constant "Arith" {} -contract "Arith" { +witness "Arith" { Base a, Base b, } diff --git a/proof/burn.zk b/proof/burn.zk index 302a7ed3d..565678f50 100644 --- a/proof/burn.zk +++ b/proof/burn.zk @@ -4,7 +4,7 @@ constant "Burn" { EcFixedPointBase NULLIFIER_K, } -contract "Burn" { +witness "Burn" { Base secret, Base serial, Base value, diff --git a/proof/deploy_contract.zk b/proof/deploy_contract.zk index 7e973d7f6..be0b991b2 100644 --- a/proof/deploy_contract.zk +++ b/proof/deploy_contract.zk @@ -2,7 +2,7 @@ constant "DeployContract" { EcFixedPointBase NULLIFIER_K, } -contract "DeployContract" { +witness "DeployContract" { # Amount of bytes to store on-chain Base bytes, # Deploy key used for signing and contract reference diff --git a/proof/encrypt.zk b/proof/encrypt.zk index fc879320b..99005fac2 100644 --- a/proof/encrypt.zk +++ b/proof/encrypt.zk @@ -7,7 +7,7 @@ constant "Encrypt" {} -contract "Encrypt" { +witness "Encrypt" { # We are encrypting values to this public key EcNiPoint pubkey, diff --git a/proof/inclusion_proof.zk b/proof/inclusion_proof.zk index ccb5ecc35..62ac8728d 100644 --- a/proof/inclusion_proof.zk +++ b/proof/inclusion_proof.zk @@ -1,7 +1,7 @@ constant "InclusionProof" { } -contract "InclusionProof" { +witness "InclusionProof" { Base leaf, Uint32 leaf_pos, MerklePath path, diff --git a/proof/lead.zk b/proof/lead.zk index 5e467860b..d60d233e6 100644 --- a/proof/lead.zk +++ b/proof/lead.zk @@ -4,7 +4,7 @@ constant "Lead" { EcFixedPointBase NULLIFIER_K, } -contract "Lead" { +witness "Lead" { MerklePath c1_cm_path, Uint32 c1_cm_pos, Uint32 c1_sk_pos, diff --git a/proof/mint.zk b/proof/mint.zk index 5a839a2ea..adde2bf22 100644 --- a/proof/mint.zk +++ b/proof/mint.zk @@ -4,7 +4,7 @@ constant "Mint" { EcFixedPointBase NULLIFIER_K, } -contract "Mint" { +witness "Mint" { Base pub_x, Base pub_y, Base value, diff --git a/proof/opcodes.zk b/proof/opcodes.zk index 0b9098213..49e576450 100644 --- a/proof/opcodes.zk +++ b/proof/opcodes.zk @@ -4,7 +4,7 @@ constant "Opcodes" { EcFixedPointBase NULLIFIER_K, } -contract "Opcodes" { +witness "Opcodes" { Base value, Scalar value_blind, diff --git a/proof/tx.zk b/proof/tx.zk index 7e30893a4..ee6753852 100644 --- a/proof/tx.zk +++ b/proof/tx.zk @@ -4,7 +4,7 @@ constant "tx" { EcFixedPointBase NULLIFIER_K, } -contract "tx" { +witness "tx" { # coin (1) witnesses Base root, Base c1_root_sk, diff --git a/proof/voting.zk b/proof/voting.zk index 3bccf5b93..f93002b02 100644 --- a/proof/voting.zk +++ b/proof/voting.zk @@ -4,7 +4,7 @@ constant "Vote" { EcFixedPointBase NULLIFIER_K, } -contract "Vote" { +witness "Vote" { Base process_id_0, Base process_id_1, Base secret_key, diff --git a/script/research/rln/signal.zk b/script/research/rln/signal.zk index 144c9eb9b..42b80590c 100644 --- a/script/research/rln/signal.zk +++ b/script/research/rln/signal.zk @@ -1,6 +1,6 @@ constant "RlnSignal" {} -contract "RlnSignal" { +witness "RlnSignal" { Base secret_key, MerklePath identity_path, Uint32 identity_leaf_pos, diff --git a/script/research/rln/slash.zk b/script/research/rln/slash.zk index b59b57ee7..d7a1d249a 100644 --- a/script/research/rln/slash.zk +++ b/script/research/rln/slash.zk @@ -1,6 +1,6 @@ constant "RlnSlash" {} -contract "RlnSlash" { +witness "RlnSlash" { Base secret_key, MerklePath identity_path, Uint32 identity_leaf_pos, diff --git a/src/contract/consensus/proof/reward_v1.zk b/src/contract/consensus/proof/reward_v1.zk index 1a313c16b..29173720e 100644 --- a/src/contract/consensus/proof/reward_v1.zk +++ b/src/contract/consensus/proof/reward_v1.zk @@ -3,7 +3,7 @@ constant "Reward_V1" { EcFixedPoint VALUE_COMMIT_RANDOM, } -contract "Reward_V1" { +witness "Reward_V1" { # Burnt coin Base coin, # Burnt coin secret key diff --git a/src/contract/dao/proof/dao-exec.zk b/src/contract/dao/proof/dao-exec.zk index 925b35440..286e56924 100644 --- a/src/contract/dao/proof/dao-exec.zk +++ b/src/contract/dao/proof/dao-exec.zk @@ -3,7 +3,7 @@ constant "DaoExec" { EcFixedPoint VALUE_COMMIT_RANDOM, } -contract "DaoExec" { +witness "DaoExec" { # Proposal parameters Base proposal_dest_x, Base proposal_dest_y, diff --git a/src/contract/dao/proof/dao-mint.zk b/src/contract/dao/proof/dao-mint.zk index 6eb8ab9cb..0f851cff1 100644 --- a/src/contract/dao/proof/dao-mint.zk +++ b/src/contract/dao/proof/dao-mint.zk @@ -3,7 +3,7 @@ constant "DaoMint" { EcFixedPointBase NULLIFIER_K, } -contract "DaoMint" { +witness "DaoMint" { Base dao_proposer_limit, Base dao_quorum, Base dao_approval_ratio_quot, diff --git a/src/contract/dao/proof/dao-propose-burn.zk b/src/contract/dao/proof/dao-propose-burn.zk index e712526c5..d69faee2d 100644 --- a/src/contract/dao/proof/dao-propose-burn.zk +++ b/src/contract/dao/proof/dao-propose-burn.zk @@ -4,7 +4,7 @@ constant "DaoProposeInput" { EcFixedPointShort VALUE_COMMIT_VALUE, } -contract "DaoProposeInput" { +witness "DaoProposeInput" { Base secret, Base serial, Base spend_hook, diff --git a/src/contract/dao/proof/dao-propose-main.zk b/src/contract/dao/proof/dao-propose-main.zk index 723872804..8d914c854 100644 --- a/src/contract/dao/proof/dao-propose-main.zk +++ b/src/contract/dao/proof/dao-propose-main.zk @@ -3,7 +3,7 @@ constant "DaoProposeMain" { EcFixedPoint VALUE_COMMIT_RANDOM, } -contract "DaoProposeMain" { +witness "DaoProposeMain" { # Proposers total number of governance tokens Base total_funds, Scalar total_funds_blind, diff --git a/src/contract/dao/proof/dao-vote-burn.zk b/src/contract/dao/proof/dao-vote-burn.zk index 61e80c683..21d509cc2 100644 --- a/src/contract/dao/proof/dao-vote-burn.zk +++ b/src/contract/dao/proof/dao-vote-burn.zk @@ -4,7 +4,7 @@ constant "DaoVoteInput" { EcFixedPointShort VALUE_COMMIT_VALUE, } -contract "DaoVoteInput" { +witness "DaoVoteInput" { Base secret, Base serial, Base spend_hook, diff --git a/src/contract/dao/proof/dao-vote-main.zk b/src/contract/dao/proof/dao-vote-main.zk index 857be3d05..33e0cf257 100644 --- a/src/contract/dao/proof/dao-vote-main.zk +++ b/src/contract/dao/proof/dao-vote-main.zk @@ -3,7 +3,7 @@ constant "DaoVoteMain" { EcFixedPointShort VALUE_COMMIT_VALUE, } -contract "DaoVoteMain" { +witness "DaoVoteMain" { # Proposal parameters Base proposal_dest_x, Base proposal_dest_y, diff --git a/src/contract/money/proof/burn_v1.zk b/src/contract/money/proof/burn_v1.zk index 449a72bd1..1afda3d87 100644 --- a/src/contract/money/proof/burn_v1.zk +++ b/src/contract/money/proof/burn_v1.zk @@ -4,7 +4,7 @@ constant "Burn_V1" { EcFixedPointBase NULLIFIER_K, } -contract "Burn_V1" { +witness "Burn_V1" { # The value of this coin Base value, # The token ID diff --git a/src/contract/money/proof/lead_burn_v1.zk b/src/contract/money/proof/lead_burn_v1.zk index 011a72886..0c611c9e7 100644 --- a/src/contract/money/proof/lead_burn_v1.zk +++ b/src/contract/money/proof/lead_burn_v1.zk @@ -4,7 +4,7 @@ constant "Lead_Burn_V1" { EcFixedPointBase NULLIFIER_K, } -contract "Lead_Burn_V1" { +witness "Lead_Burn_V1" { MerklePath c1_cm_path, Uint32 c1_cm_pos, Uint32 c1_sk_pos, diff --git a/src/contract/money/proof/lead_mint_v1.zk b/src/contract/money/proof/lead_mint_v1.zk index 10831db24..25fc83d88 100644 --- a/src/contract/money/proof/lead_mint_v1.zk +++ b/src/contract/money/proof/lead_mint_v1.zk @@ -4,7 +4,7 @@ constant "Lead_Mint_V1" { EcFixedPointBase NULLIFIER_K, } -contract "Lead_Mint_V1" { +witness "Lead_Mint_V1" { Base c1_sk, Base c1_sk_root, Base c1_slot, diff --git a/src/contract/money/proof/mint_v1.zk b/src/contract/money/proof/mint_v1.zk index 42e956138..5bbaf7efe 100644 --- a/src/contract/money/proof/mint_v1.zk +++ b/src/contract/money/proof/mint_v1.zk @@ -4,7 +4,7 @@ constant "Mint_V1" { EcFixedPointBase NULLIFIER_K, } -contract "Mint_V1" { +witness "Mint_V1" { # X coordinate for public key Base pub_x, # Y coordinate for public key diff --git a/src/contract/money/proof/token_freeze_v1.zk b/src/contract/money/proof/token_freeze_v1.zk index e05502645..bfa5910fb 100644 --- a/src/contract/money/proof/token_freeze_v1.zk +++ b/src/contract/money/proof/token_freeze_v1.zk @@ -2,7 +2,7 @@ constant "TokenFreeze_V1" { EcFixedPointBase NULLIFIER_K, } -contract "TokenFreeze_V1" { +witness "TokenFreeze_V1" { # Token mint authority secret Base mint_authority, } diff --git a/src/contract/money/proof/token_mint_v1.zk b/src/contract/money/proof/token_mint_v1.zk index b1924d07d..56e9d06d6 100644 --- a/src/contract/money/proof/token_mint_v1.zk +++ b/src/contract/money/proof/token_mint_v1.zk @@ -5,7 +5,7 @@ constant "TokenMint_V1" { EcFixedPointBase NULLIFIER_K, } -contract "TokenMint_V1" { +witness "TokenMint_V1" { # Token mint authority secret Base mint_authority, # Token supply diff --git a/src/zkas/compiler.rs b/src/zkas/compiler.rs index 825b1e1f1..f131ab13d 100644 --- a/src/zkas/compiler.rs +++ b/src/zkas/compiler.rs @@ -92,9 +92,9 @@ impl Compiler { bincode.extend_from_slice(&serialize(&i.name)); } - // In the .contract section, we write all our witness types, on the stack + // In the .witness section, we write all our witness types, on the stack // they're in order of appearance. - bincode.extend_from_slice(b".contract"); + bincode.extend_from_slice(b".witness"); for i in &self.witnesses { tmp_stack.push(i.name.as_str()); bincode.push(i.typ as u8); diff --git a/src/zkas/decoder.rs b/src/zkas/decoder.rs index 0dd0eb6dc..602a030fa 100644 --- a/src/zkas/decoder.rs +++ b/src/zkas/decoder.rs @@ -46,7 +46,7 @@ impl ZkBinary { let _binary_version = &bytes[4]; - // After the binary version, we're supposed to have the contract namespace + // After the binary version, we're supposed to have the witness namespace let (namespace, _) = deserialize_partial(&bytes[5..])?; let constants_offset = match find_subslice(bytes, b".constant") { @@ -59,9 +59,9 @@ impl ZkBinary { None => return Err(ZkasErr("Could not find .literal section".to_string())), }; - let contract_offset = match find_subslice(bytes, b".contract") { + let witness_offset = match find_subslice(bytes, b".witness") { Some(v) => v, - None => return Err(ZkasErr("Could not find .contract section".to_string())), + None => return Err(ZkasErr("Could not find .witness section".to_string())), }; let circuit_offset = match find_subslice(bytes, b".circuit") { @@ -78,12 +78,12 @@ impl ZkBinary { return Err(ZkasErr(".literal section appeared before .constant".to_string())) } - if literals_offset > contract_offset { - return Err(ZkasErr(".contract section appeared before .literal".to_string())) + if literals_offset > witness_offset { + return Err(ZkasErr(".witness section appeared before .literal".to_string())) } - if contract_offset > circuit_offset { - return Err(ZkasErr(".circuit section appeared before .contract".to_string())) + if witness_offset > circuit_offset { + return Err(ZkasErr(".circuit section appeared before .witness".to_string())) } if circuit_offset > debug_offset { @@ -91,13 +91,13 @@ impl ZkBinary { } let constants_section = &bytes[constants_offset + b".constant".len()..literals_offset]; - let literals_section = &bytes[literals_offset + b".literal".len()..contract_offset]; - let contract_section = &bytes[contract_offset + b".contract".len()..circuit_offset]; + let literals_section = &bytes[literals_offset + b".literal".len()..witness_offset]; + let witness_section = &bytes[witness_offset + b".witness".len()..circuit_offset]; let circuit_section = &bytes[circuit_offset + b".circuit".len()..debug_offset]; let constants = ZkBinary::parse_constants(constants_section)?; let literals = ZkBinary::parse_literals(literals_section)?; - let witnesses = ZkBinary::parse_contract(contract_section)?; + let witnesses = ZkBinary::parse_witness(witness_section)?; let opcodes = ZkBinary::parse_circuit(circuit_section)?; // TODO: Debug info @@ -153,7 +153,7 @@ impl ZkBinary { Ok(literals) } - fn parse_contract(bytes: &[u8]) -> Result> { + fn parse_witness(bytes: &[u8]) -> Result> { let mut witnesses = vec![]; let mut iter_offset = 0; diff --git a/src/zkas/parser.rs b/src/zkas/parser.rs index 6f793a056..048140889 100644 --- a/src/zkas/parser.rs +++ b/src/zkas/parser.rs @@ -30,10 +30,10 @@ use super::{ /// zkas language builtin keywords. /// These can not be used anywhere except where they are expected. -const KEYWORDS: [&str; 3] = ["constant", "contract", "circuit"]; +const KEYWORDS: [&str; 3] = ["constant", "witness", "circuit"]; /// Forbidden namespaces -const NOPE_NS: [&str; 4] = [".constant", ".literal", ".contract", ".circuit"]; +const NOPE_NS: [&str; 4] = [".constant", ".literal", ".witness", ".circuit"]; /// Valid EcFixedPoint constant names supported by the VM. const VALID_ECFIXEDPOINT: [&str; 1] = ["VALUE_COMMIT_RANDOM"]; @@ -63,25 +63,25 @@ impl Parser { // We use these to keep state while parsing. let mut namespace = None; let (mut declaring_constant, mut declared_constant) = (false, false); - let (mut declaring_contract, mut declared_contract) = (false, false); + let (mut declaring_witness, mut declared_witness) = (false, false); let (mut declaring_circuit, mut declared_circuit) = (false, false); // The tokens gathered from each of the sections let mut constant_tokens = vec![]; - let mut contract_tokens = vec![]; + let mut witness_tokens = vec![]; let mut circuit_tokens = vec![]; // Tokens belonging to the current statement let mut circuit_stmt = vec![]; // All completed statements are pushed here let mut circuit_stmts = vec![]; - // Contains constant and contract sections + // Contains constant and witness sections let mut ast_inner = IndexMap::new(); let mut ast = IndexMap::new(); if self.tokens[0].token_type != TokenType::Symbol { self.error.abort( - "Source file does not start with a section. Expected `constant/contract/circuit`.", + "Source file does not start with a section. Expected `constant/witness/circuit`.", 0, 0, ); @@ -89,7 +89,7 @@ impl Parser { let mut iter = self.tokens.iter(); while let Some(t) = iter.next() { - // Sections "constant", "contract", and "circuit" are + // Sections "constant", "witness", and "circuit" are // the sections we must be declaring in our source code. // When we find one, we'll take all the tokens found in // the section and place them in their respective vec. @@ -97,7 +97,7 @@ impl Parser { // the sections are closed off with braces. This should // be revisited later when we decide to add other lang // functionality that also depends on using braces. - if !declaring_constant && !declaring_contract && !declaring_circuit { + if !declaring_constant && !declaring_witness && !declaring_circuit { // // We use this macro to avoid code repetition in the following // match statement for soaking up the section tokens. @@ -127,9 +127,9 @@ impl Parser { declaring_constant = true; absorb_inner_tokens!(constant_tokens); } - "contract" => { - declaring_contract = true; - absorb_inner_tokens!(contract_tokens); + "witness" => { + declaring_witness = true; + absorb_inner_tokens!(witness_tokens); } "circuit" => { declaring_circuit = true; @@ -210,19 +210,19 @@ impl Parser { declared_constant = true; } - // Parse the contract section into the AST. - if declaring_contract { - if declared_contract { - self.error.abort("Duplicate `contract` section found.", t.line, t.column); + // Parse the witness section into the AST. + if declaring_witness { + if declared_witness { + self.error.abort("Duplicate `witness` section found.", t.line, t.column); } - self.check_section_structure("contract", contract_tokens.clone()); - check_namespace!(contract_tokens); + self.check_section_structure("witness", witness_tokens.clone()); + check_namespace!(witness_tokens); let mut witnesses_map = IndexMap::new(); // This is everything between the braces: { ... } - let mut contract_inner = contract_tokens[2..contract_tokens.len() - 1].iter(); - while let Some((typ, name, comma)) = contract_inner.next_tuple() { + let mut witness_inner = witness_tokens[2..witness_tokens.len() - 1].iter(); + while let Some((typ, name, comma)) = witness_inner.next_tuple() { if comma.token_type != TokenType::Comma { self.error.abort("Separator is not a comma.", comma.line, comma.column); } @@ -231,7 +231,7 @@ impl Parser { if witnesses_map.contains_key(name.token.as_str()) { self.error.abort( &format!( - "Section `contract` already contains the token `{}`.", + "Section `witness` already contains the token `{}`.", &name.token ), name.line, @@ -242,13 +242,13 @@ impl Parser { witnesses_map.insert(name.token.clone(), (name.clone(), typ.clone())); } - if contract_inner.next().is_some() { - self.error.abort("Internal error, leftovers in 'contract' iterator", 0, 0); + if witness_inner.next().is_some() { + self.error.abort("Internal error, leftovers in 'witness' iterator", 0, 0); } - ast_inner.insert("contract".to_string(), witnesses_map); - declaring_contract = false; - declared_contract = true; + ast_inner.insert("witness".to_string(), witnesses_map); + declaring_witness = false; + declared_witness = true; } // Parse the circuit section into the AST. @@ -293,14 +293,14 @@ impl Parser { }; let witnesses = { - let c = match ast.get(&ns).unwrap().get("contract") { + let c = match ast.get(&ns).unwrap().get("witness") { Some(c) => c, None => { - self.error.abort("Missing `contract` section in .zk source.", 0, 0); + self.error.abort("Missing `witness` section in .zk source.", 0, 0); unreachable!(); } }; - self.parse_ast_contract(c) + self.parse_ast_witness(c) }; let statements = self.parse_ast_circuit(circuit_stmts); @@ -338,7 +338,7 @@ impl Parser { } match section { - "constant" | "contract" => { + "constant" | "witness" => { if tokens.len() == 3 { self.error.warn(&format!("{} section is empty.", section), 0, 0); } @@ -477,7 +477,7 @@ impl Parser { ret } - fn parse_ast_contract(&self, ast: &IndexMap) -> Vec { + fn parse_ast_witness(&self, ast: &IndexMap) -> Vec { let mut ret = vec![]; // k = name