zkas: Rename "contract" to "witness".

This commit is contained in:
parazyd
2023-05-10 13:24:02 +02:00
parent 81a2f6a0fa
commit be9013ed31
30 changed files with 74 additions and 74 deletions

View File

@@ -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`)

View File

@@ -3,7 +3,7 @@ constant "Simple" {
EcFixedPoint VALUE_COMMIT_RANDOM,
}
contract "Simple" {
witness "Simple" {
Base value,
Scalar value_blind,
}

View File

@@ -1,6 +1,6 @@
constant "Arith" {}
contract "Arith" {
witness "Arith" {
Base a,
Base b,
}

View File

@@ -4,7 +4,7 @@ constant "Burn" {
EcFixedPointBase NULLIFIER_K,
}
contract "Burn" {
witness "Burn" {
Base secret,
Base serial,
Base value,

View File

@@ -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

View File

@@ -7,7 +7,7 @@
constant "Encrypt" {}
contract "Encrypt" {
witness "Encrypt" {
# We are encrypting values to this public key
EcNiPoint pubkey,

View File

@@ -1,7 +1,7 @@
constant "InclusionProof" {
}
contract "InclusionProof" {
witness "InclusionProof" {
Base leaf,
Uint32 leaf_pos,
MerklePath path,

View File

@@ -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,

View File

@@ -4,7 +4,7 @@ constant "Mint" {
EcFixedPointBase NULLIFIER_K,
}
contract "Mint" {
witness "Mint" {
Base pub_x,
Base pub_y,
Base value,

View File

@@ -4,7 +4,7 @@ constant "Opcodes" {
EcFixedPointBase NULLIFIER_K,
}
contract "Opcodes" {
witness "Opcodes" {
Base value,
Scalar value_blind,

View File

@@ -4,7 +4,7 @@ constant "tx" {
EcFixedPointBase NULLIFIER_K,
}
contract "tx" {
witness "tx" {
# coin (1) witnesses
Base root,
Base c1_root_sk,

View File

@@ -4,7 +4,7 @@ constant "Vote" {
EcFixedPointBase NULLIFIER_K,
}
contract "Vote" {
witness "Vote" {
Base process_id_0,
Base process_id_1,
Base secret_key,

View File

@@ -1,6 +1,6 @@
constant "RlnSignal" {}
contract "RlnSignal" {
witness "RlnSignal" {
Base secret_key,
MerklePath identity_path,
Uint32 identity_leaf_pos,

View File

@@ -1,6 +1,6 @@
constant "RlnSlash" {}
contract "RlnSlash" {
witness "RlnSlash" {
Base secret_key,
MerklePath identity_path,
Uint32 identity_leaf_pos,

View File

@@ -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

View File

@@ -3,7 +3,7 @@ constant "DaoExec" {
EcFixedPoint VALUE_COMMIT_RANDOM,
}
contract "DaoExec" {
witness "DaoExec" {
# Proposal parameters
Base proposal_dest_x,
Base proposal_dest_y,

View File

@@ -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,

View File

@@ -4,7 +4,7 @@ constant "DaoProposeInput" {
EcFixedPointShort VALUE_COMMIT_VALUE,
}
contract "DaoProposeInput" {
witness "DaoProposeInput" {
Base secret,
Base serial,
Base spend_hook,

View File

@@ -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,

View File

@@ -4,7 +4,7 @@ constant "DaoVoteInput" {
EcFixedPointShort VALUE_COMMIT_VALUE,
}
contract "DaoVoteInput" {
witness "DaoVoteInput" {
Base secret,
Base serial,
Base spend_hook,

View File

@@ -3,7 +3,7 @@ constant "DaoVoteMain" {
EcFixedPointShort VALUE_COMMIT_VALUE,
}
contract "DaoVoteMain" {
witness "DaoVoteMain" {
# Proposal parameters
Base proposal_dest_x,
Base proposal_dest_y,

View File

@@ -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

View File

@@ -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,

View File

@@ -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,

View File

@@ -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

View File

@@ -2,7 +2,7 @@ constant "TokenFreeze_V1" {
EcFixedPointBase NULLIFIER_K,
}
contract "TokenFreeze_V1" {
witness "TokenFreeze_V1" {
# Token mint authority secret
Base mint_authority,
}

View File

@@ -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

View File

@@ -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);

View File

@@ -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<Vec<VarType>> {
fn parse_witness(bytes: &[u8]) -> Result<Vec<VarType>> {
let mut witnesses = vec![];
let mut iter_offset = 0;

View File

@@ -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<String, (Token, Token)>) -> Vec<Witness> {
fn parse_ast_witness(&self, ast: &IndexMap<String, (Token, Token)>) -> Vec<Witness> {
let mut ret = vec![];
// k = name